diff --git a/dataset/fileUtil.py b/dataset/fileUtil.py deleted file mode 100644 index 967712f..0000000 --- a/dataset/fileUtil.py +++ /dev/null @@ -1,129 +0,0 @@ -import os -import re -filePath='D:\\nlp\\codeCom\\datagit\\' -savePath='D:\\nlp\\codeCom\\datagit\\' -isExists=os.path.exists(savePath) -if not isExists: - os.makedirs(savePath) -# p0=re.compile(u"\\[.*?]", re.M) -# p1 = re.compile(r"\([^()]*\)", re.M) -# p2=re.compile('[\'\"](.*?)[\'\"]', re.M) -# rep2=" QUOTMARK " -# p3='(' -# rep3=" PARELEFT " -# p4=')' -# rep4=" PARERIGHT " -# p5='.' -# rep5=" DOT " -# p6=',' -# rep6=" COMMA " -# p7='=' -# rep7=' EQUAL ' -# p8=':' -# rep8=' COLON ' - -# temp1 = re.sub(p2, rep2, re.sub(p0, "[]", re.sub(p1, "()", str, count=0, flags=0), -# count=0, flags=0), count=0, flags=0) -# temp2 = temp1.replace(p3, rep3).replace(p4, rep4).replace(p5, rep5).replace(p6, rep6).replace(p7, rep7)\ -# .replace(p8,rep8).replace('\n', ' DOM\n') -# print(temp2) - -# #测试代码 -# with open(filePath+'0.txt','r',encoding="utf-8") as file: -# str = file.read() -# temp1 = re.sub(p2, rep2, re.sub(p0, "[]", re.sub(p1, "()", str, count=0, flags=0), -# count=0, flags=0), count=0, flags=0) -# temp2=temp1.replace(p4, rep4).replace(p5,rep5).replace(p6,rep6).replace(p7,rep7).replace('\n',' DOM\n') -# print(temp2) - -def merge_data(filePath,saveFile): - mergeStr = "" - for filename in os.listdir(filePath): - with open(filePath + filename, 'r', encoding="utf-8") as file: - str = file.read() - mergeStr = mergeStr + str - with open(saveFile, 'w', encoding="utf-8") as f: - f.write(mergeStr) - return - -def onlyASCII(data,model='string',outputName='result.txt'): - if model=='file': - if not (isinstance(data, str) and '.txt' in data): - return "请输入.txt文件或字符串或列表" - with open(data, 'r', encoding="utf-8") as f: - str = f.read().encode("utf-8").decode("ascii", 'ignore') - with open(outputName, 'w', encoding="utf-8") as f: - f.write(str) - return - elif model=='string': - data_str = data.encode("utf-8").decode("ascii", 'ignore') - return data_str - elif model=='list': - data_list=[] - for item in data: - data_str = item.encode("utf-8").decode("ascii", 'ignore') - data_list.extend(data_str) - return data_list - else: - return "请输入.txt文件或字符串或列表" - -def onlyASCII_generator(data_list): - for data in data_list: - data_str=data.encode("utf-8").decode("ascii", 'ignore') - yield data_str - -def deleteComment(data,model='file'): - p0=re.compile(u"#.*", re.M) - data=re.sub(p0, "",data) - p1=re.compile(u"\\'''.*?'''", re.S) - data=re.sub(p1, "",data) - print(data) - -def deletePrint(data): - p0=re.compile(u"print\(.*?\)", re.M) - data=re.sub(p0, "",data) - print(data) - -def mult2one(data): - p0=re.compile(u"\\\n", re.M) - data=re.sub(p0, "",data) - print(data) - -def deleteParement(data): - p0=re.compile(u"\((.*)=(.*)?\)", re.S) - string=re.search(p0,data).group() - print(string) - str_list=string.split(',') - result='' - for itrm in str_list: - p1 = re.compile(u"=(.*)", re.S) - data = re.sub(p1, "= ", itrm) - result=result+data+',' - result=result[:-1]+' )' - print(result) - - - - -def matchIdentation(data): - p0=re.compile(u"\(.*?\)", re.M) - data=re.sub(p0, " IDT ",data) - print(data) - - - - # data - # str.replace(p2, " quotMark ") - # temp1 = re.sub(p2, " quotMark ", re.sub(p0, "[]", re.sub(p1, "()", str, count=0, flags=0), - # count=0, flags=0), count=0, flags=0) - # temp2 = re.sub(p6, rep6, - # re.sub(p5, rep5, re.sub(p4, rep4, re.sub(p3, rep3, temp1, count=0, flags=0), - # count=0, flags=0), count=0, flags=0), count=0, flags=0) - # temp3 = re.sub(p7, rep7, temp2, count=0, flags=0) - # print(re.sub('\n', ' DOM\n',temp3, count=0, flags=0)) - # print(str.replace('\n',' DOM\n')) - -if __name__ == '__main__': - pass - str1="def onlyASCII(data,model='string',outputName='result.txt'):" - deleteParement(str1) diff --git a/dataset/merge.txt b/dataset/merge.txt new file mode 100644 index 0000000..f5d1743 --- /dev/null +++ b/dataset/merge.txt @@ -0,0 +1,101787 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import sys +sys.path.insert(0, '..') +import tensorflow as tf +from util import batch_norm_layer +from mnist_gan_layers import feed_forward_layer, transposed_conv_layer, conv_layer +from mnist_gan_helper import MNIST_GAN +import numpy as np +np.random.seed(0) +from matplotlib import pyplot as plt + + +class GAN: + IDT """.. :py:class::``GAN`` + + IDT This class encapsulates both generator and discriminator. + + IDT Attributes + IDT ---------- + IDT input : tf.placeholder + IDT IDT IDT IDT Placeholder of shape (batch_size, 50) of uniform random values from [-1,1] + IDT is_training : tf.placeholder + IDT IDT IDT IDT IDT Bool placeholder for correct batch norm during inference + IDT gen_output : tf.Tensor + IDT IDT IDT IDT IDT Output of the generator portion + IDT dis_output : tf.Tensor + IDT IDT IDT IDT IDT Output of the discriminator portion + IDT loss_dis IDT : Cross-entropy loss of the discriminator + IDT loss_gen IDT : Cross-entropy loss of the generator + IDT input_reals : tf.placeholder + IDT IDT IDT IDT IDT Placeholder for feeding batch_size real input images + IDT """ + + IDT def __init__(self, batch_size= , learning_rate= ) IDT IDT with tf.variable_scope('generator') as scope_gen: + IDT IDT IDT self.input = , shape= , 50)) ) IDT IDT IDT self.is_training = , shape= ) IDT IDT IDT expanded_input = , 64 * 4 * 4, self.is_training, normalize= , activation_function= ) IDT IDT IDT with tf.variable_scope('layer1'): + IDT IDT IDT IDT layer_1 = tf.reshape(expanded_input, (-1, 4, 4, 64)) + IDT IDT IDT with tf.variable_scope('layer2'): + IDT IDT IDT IDT layer_2 = , (5, 5, 32, 64), (batch_size, 7, 7, 32), (1, 2, 2, 1), self.is_training, normalize= , activation_function= ) IDT IDT IDT with tf.variable_scope('layer3'): + IDT IDT IDT IDT layer_3 = , (5, 5, 16, 32), (batch_size, 14, 14, 16), (1, 2, 2, 1), self.is_training, normalize= , activation_function= ) IDT IDT IDT with tf.variable_scope('layer4'): + IDT IDT IDT IDT layer_4 = , (5, 5, 1, 16), (batch_size, 28, 28, 1), (1, 2, 2, 1), self.is_training, normalize= , activation_function= ) IDT IDT IDT self.gen_output = layer_4 + IDT IDT IDT variables_gen = , scope= ) IDT IDT with tf.variable_scope('discriminator') as scope_dis: + IDT IDT IDT self.input_reals = , shape= , 28, 28, 1)) ) IDT IDT IDT inputs = tf.concat([self.gen_output, self.input_reals], 0) + IDT IDT IDT labels = tf.concat([tf.zeros((batch_size, 1), tf.float32), tf.ones((batch_size, 1), tf.float32)], 0) + IDT IDT IDT act_fn = tf.nn.leaky_relu + IDT IDT IDT with tf.variable_scope('layer1'): + IDT IDT IDT IDT conv1 = , 8, 5, 2, activation_function= , normalize= ) IDT IDT IDT with tf.variable_scope('layer2'): + IDT IDT IDT IDT conv2 = , 16, 5, 2, activation_function= , normalize= ) IDT IDT IDT with tf.variable_scope('layer3'): + IDT IDT IDT IDT conv3 = , 32, 5, 2, activation_function= , normalize= ) IDT IDT IDT with tf.variable_scope('layer4'): + IDT IDT IDT IDT conv3_reshaped = , shape= , 4 * 4 * 32)) ) IDT IDT IDT IDT self.dis_output = , 1, self.is_training, normalize= , activation_function= ) IDT IDT IDT variables_dis = , scope= ) IDT IDT IDT entropy_dis = , logits= ) IDT IDT IDT self.loss_dis = tf.reduce_mean(entropy_dis) + IDT IDT IDT entropy_gen = , 1), tf.float32), logits= ) IDT IDT IDT self.loss_gen = tf.reduce_mean(entropy_gen) + IDT IDT IDT self.train_step_gen = , beta1= , var_list= ) IDT IDT IDT self.train_step_dis = , beta1= , var_list= ) + IDT def train_step(self, session, vector, real_images, is_training): + IDT IDT + IDT IDT fetches = [self.loss_dis, self.loss_gen, self.train_step_dis, self.train_step_gen] + IDT IDT feeds = {self.input: vector, self.input_reals: real_images, self.is_training: is_training} + IDT IDT loss_dis, loss_gen, _, _ = , feed_dict= ) IDT IDT return loss_dis, loss_gen + + IDT def generate_images(self, session, vectors): + IDT IDT fetches = self.gen_output + IDT IDT feeds = {self.input: vectors, self.is_training: False} + IDT IDT return session.run(fetches, feed_dict= ) + +def plot_images(imgs): + IDT n, h, w, c = imgs.shape + IDT cols = int(np.sqrt(n)) + IDT rows = int(n / cols) + 1 + IDT fig, axarr = , cols, figsize= , 20)) ) IDT for index in range(n): + IDT IDT row = index // cols + IDT IDT col = index % cols + IDT IDT ax = axarr[row][col] + IDT IDT ax.imshow(imgs[(index), :, :, (0)], cmap= ) IDT for index in range(n, rows * cols): + IDT IDT row = index // cols + IDT IDT col = index % cols + IDT IDT fig.delaxes(axarr[row][col]) + IDT plt.show() + + +def main(): + IDT mnist_helper = MNIST_GAN('data') + IDT epochs = 5 + IDT batch_size = 64 + IDT gan = GAN(batch_size) + IDT losses_dis = [] + IDT losses_gen = [] + IDT with tf.Session() as session: + IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT for epoch in range(epochs): + IDT IDT IDT }/{epochs}') + IDT IDT IDT for batch in mnist_helper.get_batch(batch_size): + IDT IDT IDT IDT z = , 1, size= , 50)) ) IDT IDT IDT IDT loss_dis, loss_gen = gan.train_step(session, z, batch, True) + IDT IDT IDT IDT losses_dis.append(loss_dis) + IDT IDT IDT IDT losses_gen.append(loss_gen) + IDT IDT imgs = , np.random.uniform(-1, 1, size= , 50))) ) IDT IDT plot_images(imgs) + IDT fig = plt.figure() + IDT ax = fig.add_subplot(111) + IDT ax.set_title('Entropy of GAN') + IDT ax.set_xlabel('Training step') + IDT ax.set_ylabel('Sigmoid cross-entropy') + IDT ax.plot(losses_dis, label= ) IDT ax.plot(losses_gen, label= ) IDT ax.legend() + IDT plt.show() + + +if __name__ == '__main__': + IDT main() +import sys +import os +SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) +sys.path.append(os.path.join(SCRIPT_PATH, 'models/tutorials/image/cifar10/')) +import cifar10 +import cifar10_input +import tensorflow as tf +import numpy as np +import time +import math +EPOCHES = 3000 +BATCH_SIZE = 128 +DATA_DIR = '/tmp/cifar10_data/cifar-10-batches-bin' + + +def variable_with_weight_loss(shape, stddev, wl): + IDT variable = , stddev= ) IDT if wl is not None: + IDT IDT weight_loss = tf.multiply(tf.nn.l2_loss(variable), wl) + IDT IDT tf.add_to_collection('losses', weight_loss) + IDT return variable + + +def loss(logits, labels): + IDT labels = tf.cast(labels, tf.int32) + IDT cross_entropy = , labels= , name= ) IDT cross_entropy_mean = , name= ) IDT tf.add_to_collection('losses', cross_entropy_mean) + IDT return tf.add_n(tf.get_collection('losses'), name= ) + +if __name__ == '__main__': + IDT cifar10.maybe_download_and_extract() + IDT images_train, labels_train = , batch_size= ) IDT images_test, labels_test = , data_dir= , batch_size= ) IDT images_placeholder = tf.placeholder(tf.float32, [BATCH_SIZE, 24, 24, 3]) + IDT labels_placeholder = tf.placeholder(tf.int32, [BATCH_SIZE]) + IDT with tf.variable_scope('conv_1'): + IDT IDT weight_1 = , 5, 3, 64], stddev= , wl= ) IDT IDT kernel_1 = , weight_1, strides= , 1, 1, 1], padding= ) IDT IDT bias_1 = tf.Variable(tf.zeros([64])) + IDT IDT conv_1 = tf.nn.relu(kernel_1 + bias_1) + IDT IDT pool_1 = , ksize= , 3, 3, 1], strides= , 2, 2, 1], padding= ) IDT IDT norm_1 = , 4, bias= , alpha= , beta= ) IDT with tf.variable_scope('conv_2'): + IDT IDT weight_2 = , 5, 64, 64], stddev= , wl= ) IDT IDT kernel_2 = , weight_2, strides= , 1, 1, 1], padding= ) IDT IDT bias_2 = , shape= ) IDT IDT conv_2 = tf.nn.relu(kernel_2 + bias_2) + IDT IDT norm_2 = , 4, bias= , alpha= , beta= ) IDT IDT pool_2 = , ksize= , 3, 3, 1], strides= , 2, 2, 1], padding= ) IDT with tf.variable_scope('fc_1'): + IDT IDT reshape = tf.reshape(pool_2, [BATCH_SIZE, -1]) + IDT IDT dim = reshape.get_shape()[1].value + IDT IDT weight_3 = , 384], stddev= , wl= ) IDT IDT bias_3 = , shape= ) IDT IDT fc_1 = tf.nn.relu(tf.matmul(reshape, weight_3) + bias_3) + IDT with tf.variable_scope('fc_2'): + IDT IDT weight_4 = , 192], stddev= , wl= ) IDT IDT bias_3 = , shape= ) IDT IDT fc_2 = tf.nn.relu(tf.matmul(fc_1, weight_4) + bias_3) + IDT with tf.variable_scope('fc_3'): + IDT IDT weight_5 = , 10], stddev= , wl= ) IDT IDT bias_5 = , shape= ) IDT IDT logits = tf.matmul(fc_2, weight_5) + bias_5 + IDT with tf.variable_scope('loss'): + IDT IDT loss = loss(logits, labels_placeholder) + IDT with tf.variable_scope('trainer'): + IDT IDT trainer = tf.train.AdamOptimizer(0.001).minimize(loss) + IDT session = tf.InteractiveSession() + IDT tf.global_variables_initializer().run() + IDT tf.train.start_queue_runners() + IDT for epoch in range(EPOCHES): + IDT IDT start_time = time.time() + IDT IDT image_batch, label_batch = session.run([images_train, labels_train]) + IDT IDT _, loss_value = , loss], feed_dict= , labels_placeholder: label_batch}) ) IDT IDT duration = time.time() - start_time + IDT IDT if epoch % 10 == 0: + IDT IDT IDT examples_per_sec = BATCH_SIZE / duration + IDT IDT IDT seconds_per_batch = float(duration) + IDT IDT IDT format_str = 'step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)' + IDT IDT IDT ) + IDT num_examples = 10000 + IDT num_iter = int(math.ceil(num_examples / BATCH_SIZE)) + IDT top_k = tf.nn.in_top_k(logits, labels_placeholder, 1) + IDT total_sample_count = num_iter * BATCH_SIZE + IDT true_count = 0 + IDT while num_iter > 0: + IDT IDT image_batch, label_batch = session.run([images_test, labels_test]) + IDT IDT predictions = , feed_dict= , labels_placeholder: label_batch}) ) IDT IDT true_count += np.sum(predictions) + IDT IDT num_iter -= 1 + IDT precision = true_count / total_sample_count + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +get_ipython().magic('matplotlib inline') +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT logits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (1-layer net)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT logits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_bacthes + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT drop1 = tf.nn.dropout(lay1_train, 0.5) + IDT logits = tf.matmul(drop1, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_batches = 3 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_batches + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_labels], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT logits = tf.matmul(lay2_train, weights3) + biases3 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) + IDT learning_rate = , global_step, 1000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_hidden_nodes3], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + IDT weights4 = , num_labels], stddev= ) IDT biases4 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + IDT logits = tf.matmul(lay3_train, weights4) + biases4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT learning_rate = , global_step, 4000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 18001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 512 +num_hidden_nodes3 = 256 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_hidden_nodes3], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + IDT weights4 = , num_labels], stddev= ) IDT biases4 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT drop1 = tf.nn.dropout(lay1_train, 0.5) + IDT lay2_train = tf.nn.relu(tf.matmul(drop1, weights2) + biases2) + IDT drop2 = tf.nn.dropout(lay2_train, 0.5) + IDT lay3_train = tf.nn.relu(tf.matmul(drop2, weights3) + biases3) + IDT drop3 = tf.nn.dropout(lay3_train, 0.5) + IDT logits = tf.matmul(drop3, weights4) + biases4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT learning_rate = , global_step, 5000, 0.8, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 20001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import tensorflow as tf +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.optimization = ) + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.sigma = , name= ) IDT IDT self.mu = , name= ) IDT IDT self.upsilon = , name= ) IDT IDT self.beta = , name= ) IDT IDT self.one = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT self.normalized_update_op = [self.upsilon.assign((self.one - self.beta) * self.upsilon + self.beta * tf.reduce_mean(tf.square(self.output))), self.mu.assign((self.one - self.beta) * self.mu + self.beta * tf.reduce_mean(self.output)), self.sigma.assign(self.upsilon - self.mu * self.mu)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT self.nomalized_q_function = tf.sqrt(self.sigma) * self.q_function + self.mu + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + IDT IDT self.optimizer = ) IDT IDT norm_gradients = , var_list= , self.b0]) ) IDT IDT norm_gradients = [(tf.reciprocal(self.sigma) * g, v) for g, v in norm_gradients] + IDT IDT gradients = , var_list= , self.b1]) ) IDT IDT gradients.extend(norm_gradients) + IDT IDT self.apply_gradients = self.optimizer.apply_gradients(gradients) + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def update_normalized_parms(self, session, outputs): + IDT IDT session.run(self.normalized_update_op, feed_dict= ) + IDT def norm_train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT self.update_normalized_parms(session, outputs) + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def get_norm_parms(self, session): + IDT IDT return session.run([self.beta, self.upsilon, self.mu, self.sigma]) +import tensorflow as tf +import numpy as np +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT self.rewards = , shape= ,), name= ) IDT IDT self.indices = , shape= , 2), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + IDT IDT self.select_prob = tf.gather_nd(self.prob, self.indices) + IDT IDT self.result = tf.multiply(self.select_prob, self.rewards) + IDT IDT self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + IDT IDT self.pg_optimization = ) IDT IDT self.optimization = ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.saved_path = './saved_model' + IDT IDT self.count = 0 + + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def policy_train(self, session, inputs, outputs, rewards): + IDT IDT form, pos, deprel = inputs + IDT IDT indices = [] + IDT IDT for aid in range(len(outputs)): + IDT IDT IDT indices.append([aid, outputs[aid]]) + IDT IDT _, cost, indices, prob, res = , self.pg_loss, self.indices, self.prob, self.result], feed_dict= , self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) ) IDT IDT self.count += 1 + IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + IDT def variable_output(self, session): + IDT IDT form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + IDT IDT return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + IDT def save(self, session): + IDT IDT self.saved_path = self.saver.save(session, './saved_model') + + IDT def restore(self, session): + IDT IDT self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + IDT IDT session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + IDT IDT session.run(self.update_op) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class GRUAttnCell(tf.nn.rnn_cell.GRUCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(GRUAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(gru_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(tf.nn.rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, gru_state + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(LSTMAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, lstm_state + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT IDT -vocab_dim: dimension of the embeddings + IDT """ + + IDT def __init__(self, size, vocab_dim, name): + IDT IDT self.size = size + IDT IDT self.vocab_dim = vocab_dim + IDT IDT self.name = name + + IDT def encode(self, inputs, masks, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(self.name): + IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(self.size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT elif model_type == 'gru': + IDT IDT IDT IDT cell = GRUAttnCell(self.size, attention_inputs) + IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT cell = LSTMAttnCell(self.size, attention_inputs) + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT outputs, final_state = , inputs, sequence_length= , dtype= , initial_state= ) IDT IDT return outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, output_size, name): + IDT IDT self.output_size = output_size + IDT IDT self.name = name + + IDT def decode(self, knowledge_rep, masks, dropout, state_size, model_type= ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT + IDT + IDT + IDT + + IDT + IDT + IDT + IDT + IDT IDT with tf.variable_scope(self.name): + IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(state_size) + IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(state_size) + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT knowledge_rep = tf.squeeze(knowledge_rep, -1) + IDT IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer1'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer2'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer3'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer4'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, self.output_size, True, 1.0) + IDT IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer1'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer2'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer3'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer4'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, self.output_size, True, 1.0) + IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT a = tf.constant(-1e+30) + IDT IDT b = tf.constant(1.0) + IDT IDT add_mask = a * (b - bool_masks) + IDT IDT start_probs = start_probs + add_mask + IDT IDT end_probs = end_probs + add_mask + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, encoder, decoder, pretrained_embeddings, max_ctx_len, max_q_len, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param encoder: tuple of 2 encoders that you constructed in train.py + IDT IDT :param decoder: a decoder that you constructed in train.py + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.question_encoder, self.context_encoder = encoder + IDT IDT self.decoder = decoder + IDT IDT self.max_ctx_len = max_ctx_len + IDT IDT self.max_q_len = max_q_len + IDT IDT self.embed_size = encoder[0].vocab_dim + IDT IDT self.flags = flags + IDT IDT self.context_placeholder = , shape= , self.max_ctx_len), name= ) IDT IDT self.question_placeholder = , shape= , self.max_q_len), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def mixer(self, final_q_state, ctx_states, model_type= ) IDT IDT if model_type == 'gru': + IDT IDT IDT pass + IDT IDT elif model_type == 'lstm': + IDT IDT IDT final_q_state = final_q_state[-1] + IDT IDT else: + IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT with vs.variable_scope('mixer'): + IDT IDT IDT ht = tf.nn.rnn_cell._linear(final_q_state, self.flags.state_size, True, 1.0) + IDT IDT IDT ht = , axis= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT ctx_state_rep = ctx_states * scores + IDT IDT return ctx_state_rep + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT question_states, final_question_state = , self.mask_q_placeholder, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT feed_states = batch_matmul(ctx_states, tf.expand_dims(final_question_state, 2)) + IDT IDT self.start_probs, self.end_probs = , self.mask_ctx_placeholder, self.dropout_placeholder, self.flags.state_size, model_type= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT self.question_embeddings = tf.reshape(question_embeddings, [-1, self.max_q_len, self.embed_size]) + IDT IDT IDT context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT self.context_embeddings = tf.reshape(context_embeddings, [-1, self.max_ctx_len, self.embed_size]) + + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = np.array(map(np.array, context_batch)) + IDT IDT input_feed[self.question_placeholder] = np.array(map(np.array, question_batch)) + IDT IDT input_feed[self.mask_ctx_placeholder] = np.array(map(np.array, mask_ctx_batch)) + IDT IDT input_feed[self.mask_q_placeholder] = np.array(mask_q_batch) + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def answer(self, session, data): + IDT IDT data = np.array(data).T + IDT IDT yp, yp2 = self.decode(session, *data) + IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch) + + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset) + IDT IDT else: + IDT IDT IDT sampled = dataset[np.random.choice(dataset.shape[0], sample)] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT sampled = sampled.T + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][sampled[2][i][0]:sampled[2][i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT em.append(exact_match_score(pred_words, actual_words)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), None, sample)) + IDT IDT return f1, em + + IDT def run_epoch(self, sess, train_set, val_set, context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT + IDT IDT if self.flags.debug == 0: + IDT IDT IDT prog_val = ) IDT IDT IDT for i, batch in enumerate(minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT IDT IDT + IDT IDT IDT train_f1, train_em = , train_set, context= , sample= , log= , eval_set= ) IDT IDT IDT val_f1, val_em = , val_set, context= , sample= , log= , eval_set= ) + IDT def train(self, session, saver, dataset, contexts, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT train_dataset, val_dataset = dataset + IDT IDT train_mask = [None, None] + IDT IDT val_mask = [None, None] + IDT IDT train_dataset[0], train_mask[0] = self.pad(train_dataset[0], self.max_ctx_len) + IDT IDT train_dataset[1], train_mask[1] = self.pad(train_dataset[1], self.max_q_len) + IDT IDT val_dataset[0], val_mask[0] = self.pad(val_dataset[0], self.max_ctx_len) + IDT IDT val_dataset[1], val_mask[1] = self.pad(val_dataset[1], self.max_q_len) + IDT IDT for i in range(1, len(train_dataset[0])): + IDT IDT IDT assert len(train_dataset[0][i]) = ) IDT IDT IDT assert len(train_dataset[1][i]) = ) IDT IDT for i in range(1, len(val_dataset[0])): + IDT IDT IDT assert len(val_dataset[0][i]) = ) IDT IDT IDT assert len(val_dataset[1][i]) = ) IDT IDT + IDT IDT train_dataset.extend(train_mask) + IDT IDT val_dataset.extend(val_mask) + IDT IDT train_dataset = np.array(train_dataset).T + IDT IDT val_dataset = np.array(val_dataset).T + IDT IDT train_context = contexts[0] + IDT IDT val_context = contexts[1] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = train_dataset[:self.flags.batch_size] + IDT IDT IDT val_dataset = val_dataset[:self.flags.batch_size] + IDT IDT IDT num_epochs = 20 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT saver.save(session, train_dir) + IDT IDT self.evaluate_answer(session, train_dataset, train_context, sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session, val_dataset, val_context, sample= , log= , eval_set= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import pandas as pd +import numpy as np +import pickle +import matplotlib.pyplot as plt +from scipy import stats +import tensorflow as tf +import seaborn as sns +from pylab import rcParams +from sklearn import metrics +from sklearn.model_selection import train_test_split +import tensorflow as tf +import pandas as pd +import numpy as np +from tensorflow.contrib import rnn +from sklearn.model_selection import train_test_split +from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score +import os +vocab_size = 10 + + +def buil_model(hidden_units= , num_classes= , batch_size= , num_layers= , learning_rate= ) IDT x = , [batch_size, None], name= ) IDT y = , [batch_size, None], name= ) IDT seq_lens = tf.placeholder(tf.int32, [batch_size]) + IDT embeddings = tf.get_variable('embedding_matrix', [num_classes, hidden_units]) + IDT rnn_inputs = tf.nn.embedding_lookup(embeddings, x) + IDT lstm_cell = , state_is_tuple= ) IDT cells = , state_is_tuple= ) IDT init_state = cells.zero_state(batch_size, tf.float32) + IDT rnn_outputs, final_state = , rnn_inputs, initial_state= , sequence_length= ) IDT with tf.variable_scope('softmax'): + IDT IDT W = tf.get_variable('W', [hidden_units, num_classes]) + IDT IDT b = , [num_classes], initializer= ) IDT outputs = tf.reshape(rnn_outputs, [-1, hidden_units]) + IDT logits = tf.matmul(outputs, W) + b + IDT error = , labels= , [-1]))) ) IDT optimizer = tf.train.AdamOptimizer(learning_rate).minimize(error) + IDT correct_predictions = tf.equal(tf.argmax(logits, 1), y) + IDT accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32)) + IDT return dict(x= , y= , init_state= , final_state= , error= , accuracy= , optimizer= , sequence_lengths= ) + +def run_epoch(session, model, data): + IDT err = 0.0 + IDT acc = 0.0 + IDT iters = 0 + IDT training_state = None + IDT for _sequence_length, _input, _target in data: + IDT IDT feed_dict = , dtype= , model['y']: np.array([_target], dtype= , model['sequence_lengths']: np.array(_sequence_length, dtype= ) IDT IDT if training_state is not None: + IDT IDT IDT feed_dict[model['init_state']] = training_state + IDT IDT err, training_state, acc, _ = session.run([model['error'], model['final_state'], model['accuracy'], model['optimizer']], feed_dict) + IDT IDT iters += _sequence_length[0] + IDT return err / iters, acc / iters + + +def train_model(train_data, epochs= ) IDT WORK_DIR = 'tmp_data' + IDT with tf.Graph().as_default(), tf.Session() as session: + IDT IDT initializer = tf.random_uniform_initializer(-0.1, 0.1) + IDT IDT with tf.variable_scope('model', reuse= , initializer= ) IDT IDT IDT model = buil_model() + IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT saver = tf.train.Saver(tf.global_variables()) + IDT IDT for i in range(epochs): + IDT IDT IDT error, accuracy = run_epoch(session, model, train_data) + IDT IDT IDT ) + IDT IDT IDT ckp_path = os.path.join(WORK_DIR, 'model.ckpt') + IDT IDT IDT saver.save(session, ckp_path, global_step= ) + +train_data = [([5.8], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6])] +train_model(train_data) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt + + +def course1(): + IDT x = np.random.rand(100).astype(np.float32) + IDT y = x * 0.1 + 0.3 + IDT W = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) + IDT b = tf.Variable(tf.zeros([1])) + IDT _y = W * x + b + IDT loss = tf.reduce_mean(tf.square(_y - y)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5) + IDT train = optimizer.minimize(loss) + IDT init = tf.initialize_all_variables() + IDT sess = tf.Session() + IDT sess.run(init) + IDT for step in range(201): + IDT IDT sess.run(train) + IDT IDT if step % 20 == 0: + IDT IDT IDT , sess.run(b))) + + +def course2(): + IDT m1 = tf.constant([[3, 4]]) + IDT m2 = tf.constant([[2], [2]]) + IDT product = tf.matmul(m1, m2) + IDT with tf.Session() as sess: + IDT IDT result = sess.run(product) + IDT IDT + IDT IDT sess.close() + + +def course3(): + IDT state = , name= ) IDT one = tf.constant(1) + IDT new_value = tf.add(state, one) + IDT update = tf.assign(state, new_value) + IDT init = tf.initialize_all_variables() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT for i in range(10): + IDT IDT IDT sess.run(update) + IDT IDT IDT ) + IDT IDT sess.close() + + +def course4(): + IDT input1 = tf.placeholder(tf.float32) + IDT input2 = tf.placeholder(tf.float32) + IDT output = tf.multiply(input1, input2) + IDT with tf.Session() as sess: + IDT IDT ) + IDT IDT sess.close() + + +def add_layer(input, in_size, out_size, activation_function= , namespace= ) IDT with tf.name_scope(namespace): + IDT IDT W = , out_size]), name= ) IDT IDT tf.summary.histogram('{}::{}'.format(namespace, 'Weights'), W) + IDT IDT b = , out_size]) + 0.1, name= ) IDT IDT tf.summary.histogram('{}::{}'.format(namespace, 'Bias'), b) + IDT IDT with tf.name_scope('Wx_plus_b'): + IDT IDT IDT Wx_plus_b = tf.matmul(input, W) + b + IDT IDT if activation_function is None: + IDT IDT IDT outputs = Wx_plus_b + IDT IDT else: + IDT IDT IDT outputs = activation_function(Wx_plus_b) + IDT IDT tf.summary.histogram('{}::{}'.format(namespace, 'Output'), outputs) + IDT return outputs + + +def course5(): + IDT x = np.linspace(-1, 1, 300)[:, (np.newaxis)] + IDT noise = np.random.normal(0, 0.1, x.shape) + IDT y = np.square(x) - 0.8 + noise + IDT xi = tf.placeholder(tf.float32, [None, 1]) + IDT yi = tf.placeholder(tf.float32, [None, 1]) + IDT l1 = , 1, 10, activation_function= ) IDT prediction = , 10, 1, activation_function= ) IDT loss = tf.reduce_mean(tf.reduce_sum(tf.square(yi - prediction), 1)) + IDT train = tf.train.GradientDescentOptimizer(0.4).minimize(loss) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT fig = plt.figure() + IDT IDT ax = fig.add_subplot(1, 1, 1) + IDT IDT ax.scatter(x, y) + IDT IDT plt.ion() + IDT IDT feed_input = {xi: x, yi: y} + IDT IDT for i in range(1000): + IDT IDT IDT sess.run(train, feed_dict= ) IDT IDT IDT if i % 50 == 0: + IDT IDT IDT IDT try: + IDT IDT IDT IDT IDT ax.lines.remove(lines[0]) + IDT IDT IDT IDT except Exception: + IDT IDT IDT IDT IDT pass + IDT IDT IDT IDT plt_predict_y = , feed_dict= ) IDT IDT IDT IDT lines = , plt_predict_y, 'r-', lw= ) IDT IDT IDT IDT plt.pause(0.1) + IDT IDT sess.close() + + +def course6(): + IDT x = np.linspace(-1, 1, 300)[:, (np.newaxis)] + IDT noise = np.random.normal(0, 0.1, x.shape) + IDT y = np.square(x) - 0.8 + noise + IDT with tf.name_scope('inputs'): + IDT IDT xi = , [None, 1], name= ) IDT IDT yi = , [None, 1], name= ) IDT layer = , 1, 100, activation_function= , namespace= ) IDT layer_1 = , 100, 10, activation_function= , namespace= ) IDT prediction = , 10, 1, activation_function= , namespace= ) IDT with tf.name_scope('loss'): + IDT IDT loss = tf.reduce_mean(tf.reduce_sum(tf.square(yi - prediction), 1)) + IDT IDT tf.summary.scalar('loss', loss) + IDT with tf.name_scope('train'): + IDT IDT train = tf.train.GradientDescentOptimizer(0.4).minimize(loss) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT merged = tf.summary.merge_all() + IDT IDT writer = tf.summary.FileWriter('log', sess.graph) + IDT IDT sess.run(init) + IDT IDT feed_input = {xi: x, yi: y} + IDT IDT for i in range(1000): + IDT IDT IDT sess.run(train, feed_dict= ) IDT IDT IDT if i % 50 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT result = , feed_dict= ) IDT IDT IDT IDT writer.add_summary(result, i) + IDT IDT sess.close() + + +if __name__ == '__main__': + IDT course6() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import sys +import numpy as np +import tensorflow as tf +from sklearn import cross_validation +from sklearn.cross_validation import KFold +from sklearn import metrics + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +class TextCNN(object): + + IDT def __init__(self, train_dataset, train_labels, valid_dataset, valid_labels, embeddings, vocabulary, l2_reg_lambda, num_steps, batch_size, num_filters, filter_sizes_1, filter_sizes_2, filter_sizes_3, dropout_keep_prob, lexical, shuffling): + IDT IDT vocab_size = len(vocabulary) + IDT IDT sequence_length = train_dataset.shape[1] + IDT IDT train_size = train_dataset.shape[0] + IDT IDT num_classes = 2 + IDT IDT filter_sizes = [filter_sizes_1, filter_sizes_2, filter_sizes_3] + IDT IDT num_filters_total = num_filters * len(filter_sizes) + IDT IDT embedding_size = embeddings.shape[1] + IDT IDT embeddings_number = embeddings.shape[0] + IDT IDT graph = tf.Graph() + IDT IDT with graph.as_default(): + IDT IDT IDT tf.set_random_seed(10) + IDT IDT IDT input_x = , shape= , sequence_length]) ) IDT IDT IDT input_y = , shape= , num_classes]) ) IDT IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT IDT tf_argmax_dataset = tf.constant(valid_dataset) + IDT IDT IDT reg_coef = tf.placeholder(tf.float32) + IDT IDT IDT l2_loss = tf.constant(0.0) + IDT IDT IDT weights_conv = , embedding_size, 1, num_filters], stddev= , seed= , filter_size in enumerate(filter_sizes) ) IDT IDT IDT biases_conv = , shape= ) IDT IDT IDT weight_output = , num_classes], stddev= , seed= ) IDT IDT IDT bias_output = , shape= ) IDT IDT IDT embeddings_const = , shape= , embedding_size]) ) IDT IDT IDT embeddings_tuned = tf.Variable(embeddings_const) + IDT IDT IDT embedded_chars = tf.nn.embedding_lookup(embeddings_tuned, input_x) + IDT IDT IDT embedded_chars_expanded = tf.expand_dims(embedded_chars, -1) + IDT IDT IDT embedded_chars_valid = tf.nn.embedding_lookup(embeddings_tuned, tf_valid_dataset) + IDT IDT IDT embedded_chars_expanded_valid = tf.expand_dims(embedded_chars_valid, -1) + IDT IDT IDT embedded_chars_argmax = tf.nn.embedding_lookup(embeddings_tuned, tf_argmax_dataset) + IDT IDT IDT embedded_chars_expanded_argmax = tf.expand_dims(embedded_chars_argmax, -1) + + IDT IDT IDT def model(data, dropout_prob): + IDT IDT IDT IDT pooled_outputs = [] + IDT IDT IDT IDT for i, filter_size in enumerate(filter_sizes): + IDT IDT IDT IDT IDT conv = , weights_conv[i], strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + IDT IDT IDT IDT IDT pooled = , ksize= , sequence_length - filter_size + 1, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT pooled_outputs.append(pooled) + IDT IDT IDT IDT h_pool = tf.concat(3, pooled_outputs) + IDT IDT IDT IDT h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + IDT IDT IDT IDT h_drop = tf.nn.dropout(h_pool_flat, dropout_prob) + IDT IDT IDT IDT scores = tf.nn.xw_plus_b(h_drop, weight_output, bias_output) + IDT IDT IDT IDT return scores + + IDT IDT IDT def model_argmax(data, dropout_prob): + IDT IDT IDT IDT argmaxs = [] + IDT IDT IDT IDT maximums = [] + IDT IDT IDT IDT pooled_outputs = [] + IDT IDT IDT IDT for i, filter_size in enumerate(filter_sizes): + IDT IDT IDT IDT IDT conv = , weights_conv[i], strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + IDT IDT IDT IDT IDT maximum = tf.reduce_max(h, tf.to_int32(1)) + IDT IDT IDT IDT IDT maximums.append(maximum) + IDT IDT IDT IDT IDT argmax = tf.argmax(h, tf.to_int32(1)) + IDT IDT IDT IDT IDT argmaxs.append(argmax) + IDT IDT IDT IDT return argmaxs, maximums + IDT IDT IDT scores = model(embedded_chars_expanded, dropout_keep_prob) + IDT IDT IDT train_prediction = tf.nn.softmax(scores) + IDT IDT IDT losses = tf.nn.softmax_cross_entropy_with_logits(scores, tf.cast(input_y, tf.float32)) + IDT IDT IDT for i in range(len(weights_conv)): + IDT IDT IDT IDT l2_loss += tf.nn.l2_loss(weights_conv[i]) + IDT IDT IDT l2_loss += tf.nn.l2_loss(weight_output) + IDT IDT IDT loss = tf.reduce_mean(losses) + reg_coef * l2_loss + IDT IDT IDT global_step = , trainable= ) IDT IDT IDT optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss) + IDT IDT IDT argmaxs, maximums = model_argmax(embedded_chars_expanded_argmax, 1.0) + IDT IDT IDT maximum1 = maximums[0] + IDT IDT IDT maximum2 = maximums[1] + IDT IDT IDT maximum3 = maximums[2] + IDT IDT IDT argmax1 = argmaxs[0] + IDT IDT IDT argmax2 = argmaxs[1] + IDT IDT IDT argmax3 = argmaxs[2] + IDT IDT IDT valid_prediction = tf.nn.softmax(model(embedded_chars_expanded_valid, 1.0)) + IDT IDT with tf.Session(graph= ) IDT IDT IDT session.run(tf.initialize_all_variables(), feed_dict= ) IDT IDT IDT + IDT IDT IDT for step in range(num_steps): + IDT IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size] + IDT IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size] + IDT IDT IDT IDT feed_dict = {input_x: batch_data, input_y: batch_labels, reg_coef: l2_reg_lambda, embeddings_const: embeddings} + IDT IDT IDT IDT _, l, predictions, embeddings = session.run([optimizer, loss, train_prediction, embeddings_tuned], feed_dict) + IDT IDT IDT IDT if not step % 100: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT ) + IDT IDT IDT IDT IDT + IDT IDT IDT maximum1 = , feed_dict= ) IDT IDT IDT maximum1 = np.asarray(maximum1) + IDT IDT IDT maximum2 = , feed_dict= ) IDT IDT IDT maximum2 = np.asarray(maximum2) + IDT IDT IDT maximum3 = , feed_dict= ) IDT IDT IDT maximum3 = np.asarray(maximum3) + IDT IDT IDT argmax1 = , feed_dict= ) IDT IDT IDT argmax1 = np.asarray(argmax1) + IDT IDT IDT argmax2 = , feed_dict= ) IDT IDT IDT argmax2 = np.asarray(argmax2) + IDT IDT IDT argmax3 = , feed_dict= ) IDT IDT IDT argmax3 = np.asarray(argmax3) + IDT IDT IDT np.save('argmax_filter_sizes_1_tuned.npy', argmax1) + IDT IDT IDT np.save('argmax_filter_sizes_2_tuned.npy', argmax2) + IDT IDT IDT np.save('argmax_filter_sizes_3_tuned.npy', argmax3) + IDT IDT IDT np.save('maximum_filter_sizes_1_tuned.npy', maximum1) + IDT IDT IDT np.save('maximum_filter_sizes_2_tuned.npy', maximum2) + IDT IDT IDT np.save('maximum_filter_sizes_3_tuned.npy', maximum3) + IDT IDT IDT self.valid_predictions = , feed_dict= ) IDT IDT IDT self.valid_predictions = np.asarray(self.valid_predictions).reshape(valid_labels.shape) + IDT IDT IDT predictions_label = np.argmax(self.valid_predictions, 1) + IDT IDT IDT labels = ['neg', 'pos'] + IDT IDT IDT self.prediction_labels_char = [labels[i] for i in predictions_label] + IDT IDT IDT self.prediction_labels_char = np.asarray(self.prediction_labels_char) + IDT IDT IDT np.save('gold_labels_tuned.npy', predictions_label) + IDT IDT IDT self.valid_accuracy = accuracy(self.valid_predictions, np.asarray(valid_labels)) + IDT IDT IDT self.embeddings_final = embeddings +import numpy as np +import tensorflow as tf +import neural_network as nn + + +class Generator(nn.Generator): + IDT """ + IDT Example OpenAI-Gym Generator architecture. + IDT """ + + IDT def __init__(self, sess): + IDT IDT """ + IDT IDT Args + IDT IDT ---- + IDT IDT IDT sess : the tensorflow session to be used + IDT IDT """ + IDT IDT self.sess_ = sess + IDT IDT with tf.variable_scope('gen'): + IDT IDT IDT self.input_state_ = , shape= , 16], name= ) IDT IDT IDT self.input_seed_ = , shape= , 1], name= ) IDT IDT IDT self.concat = , self.input_seed_], 1, name= ) IDT IDT IDT self.hidden = , 8, activation= , name= ) IDT IDT IDT self.output_ = , 4, name= ) IDT IDT self.sess.run(tf.global_variables_initializer()) + + IDT @property + IDT def input_state(self): + IDT IDT """ + IDT IDT The input state of shape [None, 4] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input state's placeholder tensor + IDT IDT """ + IDT IDT return self.input_state_ + + IDT @property + IDT def output(self): + IDT IDT """ + IDT IDT The outputted action distribution of shape [None, 2] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A tensor: the output tensor + IDT IDT """ + IDT IDT return self.output_ + + IDT @property + IDT def sess(self): + IDT IDT """ + IDT IDT The session used to create the graph + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A session: the graph's session + IDT IDT """ + IDT IDT return self.sess_ + + IDT @property + IDT def input_seed(self): + IDT IDT """ + IDT IDT The input random seed + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder: the input seed's placeholder tensor + IDT IDT """ + IDT IDT return self.input_seed_ + + IDT @property + IDT def trainable_variables(self): + IDT IDT """ + IDT IDT A list of the trainable variables in our generator + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A list of tensors: the trainable variables in this graph + IDT IDT """ + IDT IDT return tf.trainable_variables('gen') + + +class Discriminator(nn.Discriminator): + IDT """ + IDT Example OpenAI-Gym Discriminator Architecture + IDT """ + + IDT def __init__(self, sess): + IDT IDT """ + IDT IDT Args + IDT IDT ---- + IDT IDT IDT sess : the tensorflow session to be used + IDT IDT """ + IDT IDT self.sess_ = sess + IDT IDT with tf.variable_scope('dis'): + IDT IDT IDT self.input_state_ = , shape= , 16], name= ) IDT IDT IDT self.input_reward_ = , shape= , name= ) IDT IDT IDT self.input_action_ = , shape= , 1], name= ) IDT IDT IDT self.input_reward_exp = , axis= , name= ) IDT IDT IDT self.concat = , self.input_reward_exp, self.input_action_], axis= , name= ) IDT IDT IDT self.hidden = , 8, activation= , name= ) IDT IDT IDT self.output_ = , 1, activation= , name= ) IDT IDT self.sess.run(tf.global_variables_initializer()) + + IDT @property + IDT def input_state(self): + IDT IDT """ + IDT IDT The input state of shape [None, 4] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input state's placeholder tensor + IDT IDT """ + IDT IDT return self.input_state_ + + IDT @property + IDT def input_action(self): + IDT IDT """ + IDT IDT The input action of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input action's placeholder tensor + IDT IDT """ + IDT IDT return self.input_action_ + + IDT @property + IDT def output(self): + IDT IDT """ + IDT IDT The probability output of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A tensor: the output's tensor + IDT IDT """ + IDT IDT return self.output_ + + IDT @property + IDT def sess(self): + IDT IDT """ + IDT IDT The session used to create a graph + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A session: the graph's session + IDT IDT """ + IDT IDT return self.sess_ + + IDT @property + IDT def input_reward(self): + IDT IDT """ + IDT IDT The input reward + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input reward's tensor + IDT IDT """ + IDT IDT return self.input_reward_ + + IDT @property + IDT def trainable_variables(self): + IDT IDT """ + IDT IDT A list of the trainable variables in our generator + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A list of tensors: the trainable variables in this graph + IDT IDT """ + IDT IDT return tf.trainable_variables('dis') + + +class Discriminator_copy(nn.Discriminator_copy): + IDT """ + IDT Example OpenAI-Gym Discriminator Copying method + IDT """ + + IDT def __init__(self, dis, new_rew_input): + IDT IDT """ + IDT IDT Initializes a discriminator_copy object + + IDT IDT Args + IDT IDT ---- + IDT IDT IDT dis (Discriminator) : The discriminator to copy + IDT IDT IDT new_rew_input (tf.placeholder) : a new reward input. + IDT IDT """ + IDT IDT self.sess_ = dis.sess + IDT IDT with tf.variable_scope('dis', reuse= ) IDT IDT IDT self.input_state_ = , shape= , 16], name= ) IDT IDT IDT self.input_reward_ = new_rew_input + IDT IDT IDT self.input_action_ = , shape= , 1], name= ) IDT IDT IDT self.input_reward_exp = , axis= , name= ) IDT IDT IDT self.concat = , self.input_reward_exp, self.input_action_], axis= , name= ) IDT IDT IDT self.hidden_ker = tf.get_variable('hidden/kernel') + IDT IDT IDT self.hidden_bias = tf.get_variable('hidden/bias') + IDT IDT IDT self.output_ker = tf.get_variable('output/kernel') + IDT IDT IDT self.output_bias = tf.get_variable('output/bias') + IDT IDT self.hidden = tf.matmul(self.concat, self.hidden_ker) + self.hidden_bias + IDT IDT self.output_ = tf.sigmoid(tf.matmul(self.hidden, self.output_ker) + self.output_bias) + + IDT @property + IDT def input_state(self): + IDT IDT """ + IDT IDT The input state of shape [None, 4] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input state's placeholder tensor + IDT IDT """ + IDT IDT return self.input_state_ + + IDT @property + IDT def input_action(self): + IDT IDT """ + IDT IDT The input action of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input action's placeholder tensor + IDT IDT """ + IDT IDT return self.input_action_ + + IDT @property + IDT def output(self): + IDT IDT """ + IDT IDT The probability output of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A tensor: the output's tensor + IDT IDT """ + IDT IDT return self.output_ + + IDT @property + IDT def sess(self): + IDT IDT """ + IDT IDT The session used to create a graph + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A session: the graph's session + IDT IDT """ + IDT IDT return self.sess_ + + IDT @property + IDT def input_reward(self): + IDT IDT """ + IDT IDT The input reward + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input reward's tensor + IDT IDT """ + IDT IDT return self.input_reward_ + + IDT @property + IDT def trainable_variables(self): + IDT IDT """ + IDT IDT A list of the trainable variables in our generator + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A list of tensors: the trainable variables in this graph + IDT IDT """ + IDT IDT return tf.trainable_variables('dis') +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT data_d = , keep_prob= ) IDT output1 = tf.matmul(data_d, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT hidden_1 = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +tf . + + +Z = n +- `tf.Graph()` +- `tf.Variable()` +- `tf.placeholder()` +- `tf.nn.sigmoid_cross_entropy_with_logits()` +- `tf.reduce_sum()` +- `tf.nn.l2_loss()` +- `tf.sigmoid()` +- `tf.multiply()` +- `tf.concat()` tensor. +- `tf.gather()` +- `tf.square()` +- `tf.square()` +- `tf.constant()` +- `tf.transpose()` +- `tf.sparse_to_dense()` +- `tf.matmul()` +- `tf.nn.dropout()` +- `tf.nn.moments()` +- `tf.random_uniform()` +- `tf.random_normal()` +- `tf.train.FtrlOptimizer()` +- `tf.train.GradientDescentOptimizer()` +- `tf.tanh()` +- `tf.nn.relu()` +- `tf.train.AdagradOptimizer()` +- `tf.train.AdamOptimizer()` +- `tf.name_scope()` + + + +""" +import tensorflow as tf +sess = tf.Session() +""" + +- placeholder, +- feed_dict session . + +placeholder + + run . +""" +x = tf.placeholder(tf.float32) +y = tf.placeholder(tf.float32) +z = x + y + 1 +z_result = , feed_dict= , y: 3.2}) ) +x_constant = tf.constant([[1, 2, 3], [5, 98, 20]]) +r_s_1 = , axis= )r_s_2 = , axis= )r_1_result = sess.run(r_s_1) +r_2_result = sess.run(r_s_2) + + +v_1 = tf.constant(2) +v_2 = tf.constant(3) +mul_12 = tf.multiply(v_1, v_2) +mul_12_result = sess.run(mul_12) + +t_1 = [[1, 4, 3], [2, 5, 6]] +t_2 = [[8, 9, 7], [10, 9, 26]] +concat_1 = tf.concat([t_1, t_2], 0) +concat_2 = tf.concat([t_1, t_2], 1) +) +) +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +import numpy as np +try: + IDT xrange +except NameError: + IDT xrange = range +mnist = , one_hot= ) + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +x = tf.placeholder(tf.float32, [None, 784]) +y_ = tf.placeholder(tf.float32, [None, 10]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder(tf.float32) +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +loss = , reduction_indices= )train = tf.train.AdamOptimizer(0.0001).minimize(loss) +correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +session = tf.Session() +session.run(tf.initialize_all_variables()) +for i in xrange(1000): + IDT batch = mnist.train.next_batch(50) + IDT if i % 100 == 0: + IDT IDT train_accuracy = , y_: batch[1], keep_prob: 1.0}, session= ) IDT IDT ) + IDT session.run(train, feed_dict= , y_: batch[1], keep_prob: 0.5}) )final_accuracy = , y_: mnist.test.labels, keep_prob: 1.0}, session= ) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import tensorflow.contrib.rnn as rnn +from math import sqrt +learning_rate = 0.01 +gaussian_noise = 0.2 +dropout_prob = 0.7 +regularize_beta = 1e-07 +word_vocab_size = 100004 +subword_vocab_size = 100004 +word_embedding_dim = 64 +subword_embedding_dim = 64 +input_subword_dim = 100 +input_dim = 100 +hidden_dim = 150 +hidden_2_dim = 100 +output_types = ['tag', 'word_freq'] +n_output = dict() +n_output['tag'] = 15 +n_output['word_freq'] = 14 +n_saved_models = 2 +x = dict() +x['words'] = , [None, None], name= )x['subwords'] = , [None, None], name= )x['subwords_len'] = , [None], name= )label = dict() +label['tag'] = , [None], name= )label['word_freq'] = , [None], name= )word_embedding = , word_embedding_dim]), trainable= , name= )subword_embedding = , subword_embedding_dim]), trainable= , name= ) + +def nn_run_subword_processing(subwords): + IDT with tf.variable_scope('subword_lstm'): + IDT IDT mapped_subwords = tf.nn.embedding_lookup(subword_embedding, subwords) + IDT IDT lstm_fw_cell = rnn.BasicLSTMCell(input_subword_dim) + IDT IDT lstm_bw_cell = rnn.BasicLSTMCell(input_subword_dim) + IDT IDT lstm_outputs, _ = , lstm_bw_cell, mapped_subwords, dtype= ) IDT IDT fw_outputs, bw_outputs = lstm_outputs + IDT IDT return tf.concat([fw_outputs[:, (-1), :], bw_outputs[:, (-1), :]], 1) + + +def nn_run_input_layer(input_dict): + IDT mapped_words = tf.nn.embedding_lookup(word_embedding, input_dict['words']) + IDT subword_vec = nn_run_subword_processing(input_dict['subwords']) + IDT expanded_subword_vec = tf.expand_dims(subword_vec, 0) + IDT word_subword = , expanded_subword_vec], axis= ) IDT with tf.variable_scope('word_lstm'): + IDT IDT lstm_fw_cell = rnn.BasicLSTMCell(input_dim) + IDT IDT lstm_bw_cell = rnn.BasicLSTMCell(input_dim) + IDT IDT lstm_outputs, _ = , lstm_bw_cell, word_subword, dtype= ) IDT concat_data = , lstm_outputs[1]], axis= ) IDT input_data = tf.reshape(concat_data, [-1, input_dim * 2]) + IDT input_noise = , input_dim * 2], stddev= , name= ) IDT return tf.add(input_data, input_noise) + + +def nn_run_hidden_layer(input_vec): + IDT hidden_weight = , hidden_dim], stddev= , name= ) IDT hidden_bias = , name= ) IDT hidden_2_weight = , hidden_2_dim], stddev= , name= ) IDT hidden_2_bias = , name= ) IDT hidden_1_output = tf.nn.tanh(tf.add(tf.matmul(input_vec, hidden_weight), hidden_bias)) + IDT hidden_2_output = tf.nn.relu(tf.add(tf.matmul(hidden_1_output, hidden_2_weight), hidden_2_bias)) + IDT return hidden_2_output + + +def nn_classify(hidden_vec): + IDT out_weights = dict() + IDT out_biases = dict() + IDT outputs = dict() + IDT for output_type in output_types: + IDT IDT out_weights[output_type] = , n_output[output_type]], stddev= , name= ) IDT IDT out_biases[output_type] = , name= ) IDT IDT outputs[output_type] = tf.matmul(hidden_vec, out_weights[output_type]) + out_biases[output_type] + IDT return outputs + + +def nn_calculate_loss(predicted_outputs): + IDT all_loss = 0.0 + IDT for output_type in output_types: + IDT IDT y = , n_output[output_type], on_value= , off_value= , axis= ) IDT IDT dropped_output = tf.nn.dropout(predicted_outputs[output_type], dropout_prob) + IDT IDT ce_loss = , logits= ) IDT IDT all_loss += ce_loss + IDT all_weights = [tensor for tensor in tf.global_variables() if 'weights' in tensor.name] + IDT l2_score = regularize_beta * sum([tf.nn.l2_loss(tensor) for tensor in all_weights]) + IDT all_loss += l2_score + IDT return all_loss + + +processed_input = nn_run_input_layer(x) +h = nn_run_hidden_layer(processed_input) +outputs = nn_classify(h) +loss = nn_calculate_loss(outputs) +optimize = )init = tf.global_variables_initializer() +saver = ) + +class TaggerModel: + + IDT def __init__(self, embeddings= , model_path= ) IDT IDT self.session = tf.Session() + IDT IDT if model_path is not None: + IDT IDT IDT saver.restore(self.session, model_path) + IDT IDT else: + IDT IDT IDT self.session.run(init) + IDT IDT IDT self.initial_embedding(embeddings[0], embeddings[1]) + + IDT def train(self, input_list, labels): + IDT IDT feed_dict = TaggerModel.get_feed_dict(input_list, labels) + IDT IDT _, iter_loss = , loss], feed_dict= ) IDT IDT return iter_loss + + IDT def predict(self, input_list): + IDT IDT feed_dict = TaggerModel.get_feed_dict(input_list) + IDT IDT prediction = , feed_dict= ) IDT IDT return prediction['tag'] + + IDT def save_model(self, save_path, global_step): + IDT IDT saver.save(self.session, save_path, global_step= ) IDT IDT + + IDT def initial_embedding(self, word_emb_input, subword_emb_input): + IDT IDT word_emb_placeholder = , [word_vocab_size, word_embedding_dim], name= ) IDT IDT subword_emb_placeholder = , [subword_vocab_size, subword_embedding_dim], name= ) IDT IDT word_assign = tf.assign(word_embedding, word_emb_placeholder) + IDT IDT subword_assign = tf.assign(subword_embedding, subword_emb_placeholder) + IDT IDT feed_dict = {word_emb_placeholder: word_emb_input, subword_emb_placeholder: subword_emb_input} + IDT IDT self.session.run([word_assign, subword_assign], feed_dict= ) + IDT @staticmethod + IDT def get_feed_dict(input_list, labels= ) IDT IDT feed_dict = dict() + IDT IDT feed_dict[x['words']] = input_list[0] + IDT IDT feed_dict[x['subwords']] = input_list[1] + IDT IDT feed_dict[x['subwords_len']] = input_list[2] + IDT IDT if labels is not None: + IDT IDT IDT feed_dict[label['tag']], feed_dict[label['word_freq']] = labels + IDT IDT return feed_dict +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import dihedral as nn + + +class CNN: + + IDT def __init__(self): + IDT IDT self.tfx = None + IDT IDT self.tfp = None + IDT IDT self.tfy = None + IDT IDT self.tftrain_step = None + IDT IDT self.mse = None + IDT IDT self.tfkp = None + IDT IDT self.tfacc = None + IDT IDT self.train_counter = 0 + IDT IDT self.test = None + + IDT def NN(self, x): + IDT IDT assert x.get_shape().as_list() == [None, 424, 424, 3] + IDT IDT x = , 8 * 4, w= , s= , input_repr= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 209, 209, 8 * 4] + IDT IDT x = , 8 * 8, w= , s= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 101, 101, 8 * 8] + IDT IDT x = , 8 * 16, w= , s= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 47, 47, 8 * 16] + IDT IDT x = , 8 * 32, w= , s= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 20, 20, 8 * 32] + IDT IDT x = , 8 * 64, w= , s= ) IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 9, 9, 8 * 64] + IDT IDT x = , 128, output_repr= ) IDT IDT x = , 8 * 256, w= , input_repr= ) IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT assert x.get_shape().as_list() == [None, 1, 1, 8 * 256] + IDT IDT x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + IDT IDT x = nn.relu(nn.fullyconnected(x, 8 * 256)) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.relu(nn.fullyconnected(x, 8 * 256)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT self.test = x + IDT IDT x = , 37, output_repr= ) IDT IDT assert x.get_shape().as_list() == [None, 37] + IDT IDT c1 = tf.nn.softmax(x[:, 0:3]) + IDT IDT c2 = tf.nn.softmax(x[:, 3:5]) * c1[:, 1:2] + IDT IDT c3 = tf.nn.softmax(x[:, 5:7]) * c2[:, 1:2] + IDT IDT c4 = tf.nn.softmax(x[:, 7:9]) * c2[:, 1:2] + IDT IDT c5 = tf.nn.softmax(x[:, 9:13]) * c2[:, 1:2] + IDT IDT c6 = tf.nn.softmax(x[:, 13:15]) + IDT IDT c7 = tf.nn.softmax(x[:, 15:18]) * c1[:, 0:1] + IDT IDT c8 = tf.nn.softmax(x[:, 18:25]) * c6[:, 0:1] + IDT IDT c9 = tf.nn.softmax(x[:, 25:28]) * c2[:, 0:1] + IDT IDT c10 = tf.nn.softmax(x[:, 28:31]) * c4[:, 0:1] + IDT IDT c11 = tf.nn.softmax(x[:, 31:37]) * c4[:, 0:1] + IDT IDT return tf.concat(1, [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11]) + + IDT def create_architecture(self): + IDT IDT self.tfkp = tf.placeholder_with_default(tf.constant(1.0, tf.float32), []) + IDT IDT self.tfacc = tf.placeholder_with_default(tf.constant(0.0, tf.float32), []) + IDT IDT x = self.tfx = tf.placeholder(tf.float32, [None, 424, 424, 3]) + IDT IDT self.tfp = self.NN(x) + IDT IDT self.tfy = tf.placeholder(tf.float32, [None, 37]) + IDT IDT self.mse = tf.reduce_mean(tf.square(self.tfp - self.tfy)) + IDT IDT self.tftrain_step = , epsilon= ) + IDT @staticmethod + IDT def split_test_train(images_path, labels_csv): + IDT IDT import csv + IDT IDT import os + IDT IDT with open(labels_csv) as f: + IDT IDT IDT reader = csv.reader(f) + IDT IDT IDT rows = [row for row in reader] + IDT IDT IDT labels = np.array([[float(x) for x in r[1:]] for r in rows[1:]]).astype(np.float32) + IDT IDT files = [(images_path + '/' + f) for f in sorted(os.listdir(images_path))] + IDT IDT n = 2000 + IDT IDT return (files[:n], labels[:n]), (files[n:], labels[n:]) + + IDT @staticmethod + IDT def load(files): + IDT IDT from scipy.ndimage import imread + IDT IDT n = len(files) + IDT IDT xs = , 424, 424, 3), dtype= ) IDT IDT for i in range(n): + IDT IDT IDT xs[i] = , mode= ) IDT IDT return CNN.prepare(xs) + + IDT @staticmethod + IDT def prepare(images): + IDT IDT images = images - np.array([0.04543276, 0.04002843, 0.02984124]) + IDT IDT images = images / np.array([0.08930177, 0.0741211, 0.0656323]) + IDT IDT return images + + IDT @staticmethod + IDT def batch(files, labels): + IDT IDT ids = , 20, replace= ) IDT IDT xs = CNN.load([files[i] for i in ids]) + IDT IDT ys = labels[ids] + IDT IDT for i in range(len(xs)): + IDT IDT IDT s = np.random.uniform(0.8, 1.2) + IDT IDT IDT u = np.random.uniform(-0.1, 0.1) + IDT IDT IDT xs[i] = xs[i] * s + u + IDT IDT return xs, ys + + IDT def train(self, session, xs, ys, options= , run_metadata= ) IDT IDT acc = 0.8 ** (self.train_counter / 1000.0) + IDT IDT _, mse = , self.mse], feed_dict= , self.tfy: ys, self.tfkp: 0.5, self.tfacc: acc}, options= , run_metadata= ) IDT IDT self.train_counter += 1 + IDT IDT return mse + + IDT def predict(self, session, xs): + IDT IDT return session.run(self.tfp, feed_dict= , self.tfkp: 1.0, self.tfacc: 0.0}) ) + IDT def predict_mse(self, session, xs, ys): + IDT IDT return session.run([self.tfp, self.mse], feed_dict= , self.tfy: ys, self.tfkp: 1.0, self.tfacc: 0.0}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle + +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT + +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT beta = 0.05 + IDT loss_without_reg = , logits= ) IDT loss = loss_without_reg + beta * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_layer1_size = 1024 + IDT hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + IDT hidden1_biases = bias_variable([hidden_layer1_size]) + IDT hidden1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + IDT output_weights = weight_variable([1024, num_labels]) + IDT output_biases = bias_variable([num_labels]) + IDT logits = tf.matmul(hidden1_layer, output_weights) + output_biases + IDT loss_without_reg = , labels= ) IDT beta = 0.01 + IDT loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + IDT valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + IDT test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_layer1_size = 1024 + IDT hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + IDT hidden1_biases = bias_variable([hidden_layer1_size]) + IDT hidden1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + IDT output_weights = weight_variable([1024, num_labels]) + IDT output_biases = bias_variable([num_labels]) + IDT logits = tf.matmul(hidden1_layer, output_weights) + output_biases + IDT loss_without_reg = , labels= ) IDT beta = 0 + IDT loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + IDT valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + IDT test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 5 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT ) + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT ) + IDT IDT ) + IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_layer1_size = 1024 + IDT hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + IDT hidden1_biases = bias_variable([hidden_layer1_size]) + IDT keep_prob = 0.5 + IDT hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + IDT hidden1_layer = tf.nn.dropout(hidden1, keep_prob) + IDT output_weights = weight_variable([1024, num_labels]) + IDT output_biases = bias_variable([num_labels]) + IDT logits = tf.matmul(hidden1_layer, output_weights) + output_biases + IDT loss_without_reg = , labels= ) IDT beta = 0.01 + IDT loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + IDT valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + IDT test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_layer1_size = 1024 + IDT hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + IDT hidden1_biases = bias_variable([hidden_layer1_size]) + IDT keep_prob = 0.5 + IDT hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + IDT hidden1_layer = tf.nn.dropout(hidden1, keep_prob) + IDT output_weights = weight_variable([1024, num_labels]) + IDT output_biases = bias_variable([num_labels]) + IDT logits = tf.matmul(hidden1_layer, output_weights) + output_biases + IDT loss_without_reg = , labels= ) IDT beta = 0.01 + IDT loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + IDT valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + IDT test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 5 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT ) + IDT IDT ) + IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_layer1_size = 1024 + IDT hidden_layer2_size = 305 + IDT hidden_layer3_size = 75 + IDT hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + IDT hidden1_biases = bias_variable([hidden_layer1_size]) + IDT hidden2_weights = weight_variable([hidden_layer1_size, hidden_layer2_size]) + IDT hidden2_biases = bias_variable([hidden_layer2_size]) + IDT hidden3_weights = weight_variable([hidden_layer2_size, hidden_layer3_size]) + IDT hidden3_biases = bias_variable([hidden_layer3_size]) + IDT keep_prob = 0.5 + IDT hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + IDT hidden1_layer = tf.nn.dropout(hidden1, keep_prob) + IDT hidden2 = tf.nn.relu(tf.matmul(hidden1_layer, hidden2_weights) + hidden2_biases) + IDT hidden2_layer = tf.nn.dropout(hidden2, keep_prob) + IDT hidden3 = tf.nn.relu(tf.matmul(hidden2_layer, hidden3_weights) + hidden3_biases) + IDT hidden3_layer = tf.nn.dropout(hidden3, keep_prob) + IDT output_weights = weight_variable([hidden_layer3_size, num_labels]) + IDT output_biases = bias_variable([num_labels]) + IDT logits = tf.matmul(hidden3_layer, output_weights) + output_biases + IDT loss_without_reg = , labels= ) IDT beta = 0.01 + IDT loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + IDT global_step = tf.Variable(0) + IDT learning_rate = , global_step, 2000, 0.8, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + IDT valid_hidden2 = tf.nn.relu(tf.matmul(valid_hidden1, hidden2_weights) + hidden2_biases) + IDT valid_hidden3 = tf.nn.relu(tf.matmul(valid_hidden2, hidden3_weights) + hidden3_biases) + IDT valid_logits = tf.matmul(valid_hidden3, output_weights) + output_biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + IDT test_hidden2 = tf.nn.relu(tf.matmul(test_hidden1, hidden2_weights) + hidden2_biases) + IDT test_hidden3 = tf.nn.relu(tf.matmul(test_hidden2, hidden3_weights) + hidden3_biases) + IDT test_logits = tf.matmul(test_hidden3, output_weights) + output_biases + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 10001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 1000 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = '../Lesson 1/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT num_neurons = 1024 + IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_neurons])) + IDT hidden_biases = tf.Variable(tf.zeros([num_neurons])) + IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + IDT hidden_out_weights = tf.Variable(tf.truncated_normal([num_neurons, num_labels])) + IDT hidden_out_biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(hidden_layer, hidden_out_weights) + hidden_out_biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_relu = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_relu, hidden_out_weights) + hidden_out_biases) + IDT test_relu = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_relu, hidden_out_weights) + hidden_out_biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 64 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT num_neurons = 1024 + IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_neurons])) + IDT hidden_biases = tf.Variable(tf.zeros([num_neurons])) + IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + IDT keep_prob = tf.placeholder(tf.float32) + IDT hidden_layer_dropout = tf.nn.dropout(hidden_layer, keep_prob) + IDT hidden_out_weights = tf.Variable(tf.truncated_normal([num_neurons, num_labels])) + IDT hidden_out_biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(hidden_layer_dropout, hidden_out_weights) + hidden_out_biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_relu = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_relu, hidden_out_weights) + hidden_out_biases) + IDT test_relu = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_relu, hidden_out_weights) + hidden_out_biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_neurons = 1024 +start_learning_rate = 0.005 +decay_steps = 1000 +decay_size = 0.95 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_neurons])) + IDT hidden_biases_1 = tf.Variable(tf.zeros([num_neurons])) + IDT hidden_layer_1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights_1) + hidden_biases_1) + IDT keep_prob = tf.placeholder(tf.float32) + IDT hidden_layer_dropout_1 = tf.nn.dropout(hidden_layer_1, keep_prob) + IDT hidden_weights_2 = tf.Variable(tf.truncated_normal([num_neurons, num_neurons])) + IDT hidden_biases_2 = tf.Variable(tf.zeros([num_neurons])) + IDT hidden_layer_2 = tf.nn.relu(tf.matmul(hidden_layer_dropout_1, hidden_weights_2) + hidden_biases_2) + IDT hidden_layer_dropout_2 = tf.nn.dropout(hidden_layer_2, keep_prob) + IDT hidden_out_weights = tf.Variable(tf.truncated_normal([num_neurons, num_labels])) + IDT hidden_out_biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(hidden_layer_dropout_2, hidden_out_weights) + hidden_out_biases + IDT loss = , logits= ) IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(start_learning_rate, global_step, decay_steps, decay_size) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_relu_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights_1) + hidden_biases_1) + IDT valid_relu_2 = tf.nn.relu(tf.matmul(valid_relu_1, hidden_weights_2) + hidden_biases_2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_relu_2, hidden_out_weights) + hidden_out_biases) + IDT test_relu_1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights_1) + hidden_biases_1) + IDT test_relu_2 = tf.nn.relu(tf.matmul(test_relu_1, hidden_weights_2) + hidden_biases_2) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_relu_2, hidden_out_weights) + hidden_out_biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +class MainModel(object): + + IDT def __init__(self, sentMaxl, sentMaxp, sentMaxr, num_classes, wv, emb_size, l2_reg_lambda, learning_rate): + IDT IDT tf.reset_default_graph() + IDT IDT self.X_lids = , [None, sentMaxl], name= ) IDT IDT self.X_pids = , [None, sentMaxp], name= ) IDT IDT self.X_rids = , [None, sentMaxr], name= ) IDT IDT self.X_llen = , [None], name= ) IDT IDT self.X_plen = , [None], name= ) IDT IDT self.X_rlen = , [None], name= ) IDT IDT self.input_y = , [None, num_classes], name= ) IDT IDT self.dropout_keep_prob = , name= ) IDT IDT W_wemb = tf.Variable(wv) + IDT IDT , '--, self.X_lids ', self.X_lids.get_shape()) + IDT IDT emb_l = tf.nn.embedding_lookup(W_wemb, self.X_lids) + IDT IDT ) + IDT IDT emb_p = tf.nn.embedding_lookup(W_wemb, self.X_pids) + IDT IDT emb_r = tf.nn.embedding_lookup(W_wemb, self.X_rids) + IDT IDT emb_l = tf.expand_dims(emb_l, -1) + IDT IDT ) + IDT IDT emb_p = tf.expand_dims(emb_p, -1) + IDT IDT emb_r = tf.expand_dims(emb_r, -1) + IDT IDT pooled_l = , ksize= , sentMaxl, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT pooled_p = , ksize= , sentMaxp, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT pooled_r = , ksize= , sentMaxr, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT ) + IDT IDT final_l = tf.reshape(pooled_l, [-1, emb_size]) + IDT IDT final_p = tf.reshape(pooled_p, [-1, emb_size]) + IDT IDT final_r = tf.reshape(pooled_r, [-1, emb_size]) + IDT IDT ) + IDT IDT XX = tf.concat(1, [final_l, final_p, final_r]) + IDT IDT ) + IDT IDT h = tf.nn.dropout(XX, self.dropout_keep_prob) + IDT IDT h = tf.nn.relu(h) + IDT IDT W = , num_classes], stddev= , name= ) IDT IDT b = , shape= , name= ) IDT IDT scores = , W, b, name= ) IDT IDT ) + IDT IDT self.predictions = , 1, name= ) IDT IDT losses = tf.nn.softmax_cross_entropy_with_logits(scores, self.input_y) + IDT IDT self.loss = tf.reduce_mean(losses) + l2_reg_lambda * (tf.nn.l2_loss(W) + tf.nn.l2_loss(b)) + IDT IDT self.correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) + IDT IDT self.accuracy = , 'float'), name= ) IDT IDT self.optimizer = tf.train.AdamOptimizer(learning_rate) + IDT IDT self.grads_and_vars = self.optimizer.compute_gradients(self.loss) + IDT IDT self.global_step = , name= , trainable= ) IDT IDT self.train_op = , global_step= ) IDT IDT session_conf = , log_device_placement= ) IDT IDT self.sess = ) IDT IDT self.sess.run(tf.global_variables_initializer()) + IDT IDT self.saver = ) + IDT def train_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y, drop_out): + IDT IDT feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: drop_out} + IDT IDT _, step, loss, accuracy, predictions = self.sess.run([self.train_op, self.global_step, self.loss, self.accuracy, self.predictions], feed_dict) + IDT IDT return loss, accuracy + + IDT def test_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y): + IDT IDT feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: 1.0} + IDT IDT accuracy = self.sess.run([self.accuracy], feed_dict) + IDT IDT return accuracy +import tensorflow as tf +import numpy as np +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT self.rewards = , shape= ,), name= ) IDT IDT self.indices = , shape= , 2), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.prob = tf.nn.softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + IDT IDT self.select_prob = tf.gather_nd(self.prob, self.indices) + IDT IDT self.logprob = tf.log(self.select_prob) + IDT IDT self.result = tf.multiply(self.logprob, self.rewards) + IDT IDT self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + IDT IDT self.pg_optimization = ) IDT IDT self.optimization = ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.saved_path = './saved_model' + IDT IDT self.count = 0 + + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def policy_train(self, session, inputs, outputs, rewards): + IDT IDT form, pos, deprel = inputs + IDT IDT indices = [] + IDT IDT for aid in range(len(outputs)): + IDT IDT IDT indices.append([aid, outputs[aid]]) + IDT IDT _, cost, indices, prob, res = , self.pg_loss, self.indices, self.prob, self.result], feed_dict= , self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) ) IDT IDT self.count += 1 + IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + IDT def variable_output(self, session): + IDT IDT form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + IDT IDT return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + IDT def save(self, session): + IDT IDT self.saved_path = self.saver.save(session, './saved_model') + + IDT def restore(self, session): + IDT IDT self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + IDT IDT session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + IDT IDT session.run(self.update_op) +import tensorflow as tf +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) IDT IDT self.weights = , name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.optimization = ) + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.multiply(self.weights, tf.square(predicted_q - self.output))) + l2 * regularizer + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs, weights): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs, self.weights: weights}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import os +from datetime import datetime +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.ops.nn import dynamic_rnn, bidirectional_dynamic_rnn +from evaluate import exact_match_score, f1_score +from util import ConfusionMatrix, Progbar, minibatches, get_minibatches +from defs import LBLS +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.LSTMCell): + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.hs = encoder_output + IDT IDT super(LSTMAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + IDT IDT return out, tf.nn.rnn_cell.LSTMStateTuple(out, out) + + +class Encoder(object): + + IDT def __init__(self, size, vocab_dim): + IDT IDT self.size = size + IDT IDT self.vocab_dim = vocab_dim + + IDT def length(self, mask): + IDT IDT used = tf.cast(mask, tf.int32) + IDT IDT length = , reduction_indices= ) IDT IDT length = tf.cast(length, tf.int32) + IDT IDT return length + + IDT def encode_questions(self, inputs, masks, encoder_state_input): + IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + IDT IDT :param inputs: Symbolic representations of your input with shape = (batch_size, length/max_length, embed_size) + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT if encoder_state_input == None: + IDT IDT IDT encoder_state_input = tf.zeros([1, self.size]) + IDT IDT cell_size = self.size + IDT IDT inputs_shape = tf.shape(inputs) + IDT IDT batch_size = inputs_shape[0] + IDT IDT cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.size) + IDT IDT cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.size) + IDT IDT with tf.variable_scope('bi_LSTM'): + IDT IDT IDT outputs, final_state = , cell_bw, dtype= , sequence_length= , inputs= , time_major= ) IDT IDT final_state_fw = final_state[0].h + IDT IDT final_state_bw = final_state[1].h + IDT IDT final_state = tf.concat(1, [final_state_fw, final_state_bw]) + IDT IDT states = tf.concat(2, outputs) + IDT IDT return final_state, states + + IDT def encode_w_attn(self, inputs, masks, prev_states, scope= , reuse= ) IDT IDT """ + IDT IDT Run a BiLSTM over the context paragraph conditioned on the question representation. + IDT IDT """ + IDT IDT cell_size = self.size + IDT IDT prev_states_fw, prev_states_bw = tf.split(2, 2, prev_states) + IDT IDT attn_cell_fw = LSTMAttnCell(cell_size, prev_states_fw) + IDT IDT attn_cell_bw = LSTMAttnCell(cell_size, prev_states_bw) + IDT IDT with vs.variable_scope(scope, reuse): + IDT IDT IDT outputs, final_state = , attn_cell_bw, dtype= , sequence_length= , inputs= , time_major= ) IDT IDT final_state_fw = final_state[0].h + IDT IDT final_state_bw = final_state[1].h + IDT IDT final_state = tf.concat(1, [final_state_fw, final_state_bw]) + IDT IDT states = tf.concat(2, outputs) + IDT IDT return final_state, states + + +class Decoder(object): + + IDT def __init__(self, output_size): + IDT IDT self.output_size = 2 * output_size + + IDT def match_LASTM(self, questions_states, paragraph_states, question_length, paragraph_length): + IDT IDT cell = , state_is_tuple= ) IDT IDT fw_states = [] + IDT IDT with tf.variable_scope('Forward_Match-LSTM'): + IDT IDT IDT W_q = , shape= , self.output_size), initializer= ) IDT IDT IDT W_r = , shape= , self.output_size), initializer= ) IDT IDT IDT b_p = , shape= , initializer= ) IDT IDT IDT w = , shape= , 1), initializer= ) IDT IDT IDT b = , shape= , 1), initializer= ) IDT IDT IDT state = tf.zeros([1, self.output_size]) + IDT IDT IDT for time_step in range(paragraph_length): + IDT IDT IDT IDT p_state = paragraph_states[:, (time_step), :] + IDT IDT IDT IDT X_ = tf.reshape(questions_states, [-1, self.output_size]) + IDT IDT IDT IDT G = tf.nn.tanh(tf.matmul(X_, W_q) + tf.matmul(p_state, W_r) + tf.matmul(state, W_r) + b_p) + IDT IDT IDT IDT atten = tf.nn.softmax(tf.matmul(G, w) + b) + IDT IDT IDT IDT atten = tf.reshape(atten, [-1, 1, question_length]) + IDT IDT IDT IDT X_ = tf.reshape(questions_states, [-1, question_length, self.output_size]) + IDT IDT IDT IDT p_z = tf.matmul(atten, X_) + IDT IDT IDT IDT p_z = tf.reshape(p_z, [-1, self.output_size]) + IDT IDT IDT IDT z = tf.concat(1, [p_state, p_z]) + IDT IDT IDT IDT state, o = cell(z, state) + IDT IDT IDT IDT fw_states.append(state) + IDT IDT IDT IDT tf.get_variable_scope().reuse_variables() + IDT IDT fw_states = tf.pack(fw_states) + IDT IDT fw_states = , perm= , 0, 2)) ) IDT IDT cell = , state_is_tuple= ) IDT IDT bk_states = [] + IDT IDT with tf.variable_scope('Backward_Match-LSTM'): + IDT IDT IDT W_q = , shape= , self.output_size), initializer= ) IDT IDT IDT W_r = , shape= , self.output_size), initializer= ) IDT IDT IDT b_p = , shape= , initializer= ) IDT IDT IDT w = , shape= , 1), initializer= ) IDT IDT IDT b = , shape= , 1), initializer= ) IDT IDT IDT state = tf.zeros([1, self.output_size]) + IDT IDT IDT for time_step in range(paragraph_length): + IDT IDT IDT IDT p_state = paragraph_states[:, (time_step), :] + IDT IDT IDT IDT X_ = tf.reshape(questions_states, [-1, self.output_size]) + IDT IDT IDT IDT G = tf.nn.tanh(tf.matmul(X_, W_q) + tf.matmul(p_state, W_r) + tf.matmul(state, W_r) + b_p) + IDT IDT IDT IDT atten = tf.nn.softmax(tf.matmul(G, w) + b) + IDT IDT IDT IDT atten = tf.reshape(atten, [-1, 1, question_length]) + IDT IDT IDT IDT X_ = tf.reshape(questions_states, [-1, question_length, self.output_size]) + IDT IDT IDT IDT p_z = tf.matmul(atten, X_) + IDT IDT IDT IDT p_z = tf.reshape(p_z, [-1, self.output_size]) + IDT IDT IDT IDT z = tf.concat(1, [p_state, p_z]) + IDT IDT IDT IDT state, o = cell(z, state) + IDT IDT IDT IDT bk_states.append(state) + IDT IDT IDT IDT tf.get_variable_scope().reuse_variables() + IDT IDT bk_states = tf.pack(bk_states) + IDT IDT bk_states = , perm= , 0, 2)) ) IDT IDT knowledge_rep = tf.concat(2, [fw_states, bk_states]) + IDT IDT return knowledge_rep + + IDT def decode(self, knowledge_rep, paragraph_length): + IDT IDT """ + IDT IDT takes in a knowledge representation + IDT IDT and output a probability estimation over + IDT IDT all paragraph tokens on which token should be + IDT IDT the start of the answer span, and which should be + IDT IDT the end of the answer span. + IDT IDT :param knowledge_rep: it is a representation of the paragraph and question, + IDT IDT IDT IDT IDT IDT IDT decided by how you choose to implement the encoder + IDT IDT :return: + IDT IDT """ + IDT IDT output_size = self.output_size + IDT IDT cell = , state_is_tuple= ) IDT IDT beta_s = [] + IDT IDT with tf.variable_scope('Boundary-LSTM_start'): + IDT IDT IDT V = , shape= , output_size), initializer= ) IDT IDT IDT b_a = , shape= , output_size), initializer= ) IDT IDT IDT W_a = , shape= , output_size), initializer= ) IDT IDT IDT c = , shape= , 1), initializer= ) IDT IDT IDT v = , shape= , 1), initializer= ) IDT IDT IDT state = tf.zeros([1, output_size]) + IDT IDT IDT for time_step in range(paragraph_length): + IDT IDT IDT IDT H_r = tf.reshape(knowledge_rep, [-1, 2 * output_size]) + IDT IDT IDT IDT F_s = tf.nn.tanh(tf.matmul(H_r, V) + tf.matmul(state, W_a) + b_a) + IDT IDT IDT IDT probab_s = , v) + c), shape= , paragraph_length]) ) IDT IDT IDT IDT beta_s.append(probab_s) + IDT IDT IDT IDT z = tf.matmul(probab_s, H_r) + IDT IDT IDT IDT state, _ = , state, scope= ) IDT IDT IDT IDT tf.get_variable_scope().reuse_variables() + IDT IDT beta_s = tf.pack(beta_s) + IDT IDT beta_s = , perm= , 0, 2)) ) IDT IDT beta_e = [] + IDT IDT with tf.variable_scope('Boundary-LSTM_end'): + IDT IDT IDT cell = , state_is_tuple= ) IDT IDT IDT V = , shape= , output_size), initializer= ) IDT IDT IDT b_a = , shape= , output_size), initializer= ) IDT IDT IDT W_a = , shape= , output_size), initializer= ) IDT IDT IDT c = , shape= , 1), initializer= ) IDT IDT IDT v = , shape= , 1), initializer= ) IDT IDT IDT state = tf.zeros([1, output_size]) + IDT IDT IDT for time_step in range(paragraph_length): + IDT IDT IDT IDT H_r = tf.reshape(knowledge_rep, [-1, 2 * output_size]) + IDT IDT IDT IDT F_e = tf.nn.tanh(tf.matmul(H_r, V) + tf.matmul(state, W_a) + b_a) + IDT IDT IDT IDT probab_e = , v) + c), shape= , paragraph_length]) ) IDT IDT IDT IDT beta_e.append(probab_e) + IDT IDT IDT IDT z = tf.matmul(probab_e, H_r) + IDT IDT IDT IDT state, _ = , state, scope= ) IDT IDT IDT IDT tf.get_variable_scope().reuse_variables() + IDT IDT beta_e = tf.pack(beta_e) + IDT IDT beta_e = , perm= , 0, 2)) ) IDT IDT return beta_s, beta_e + + +class QASystem(object): + + IDT def __init__(self, encoder, decoder, args, pretrained_embeddings): + IDT IDT """ + IDT IDT Initializes your System + IDT IDT :param encoder: an encoder that you constructed in train.py + IDT IDT :param decoder: a decoder that you constructed in train.py + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.encoder = encoder + IDT IDT self.decoder = decoder + IDT IDT self.config = args + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.p_max_length = self.config.paragraph_size + IDT IDT self.embed_size = encoder.vocab_dim + IDT IDT self.q_max_length = self.config.question_size + IDT IDT self.q_placeholder = tf.placeholder(tf.int32, (None, self.q_max_length)) + IDT IDT self.p_placeholder = tf.placeholder(tf.int32, (None, self.p_max_length)) + IDT IDT self.start_labels_placeholder = tf.placeholder(tf.int32, (None, self.p_max_length)) + IDT IDT self.end_labels_placeholder = tf.placeholder(tf.int32, (None, self.p_max_length)) + IDT IDT self.q_mask_placeholder = tf.placeholder(tf.bool, (None, self.q_max_length)) + IDT IDT self.p_mask_placeholder = tf.placeholder(tf.bool, (None, self.p_max_length)) + IDT IDT self.dropout_placeholder = tf.placeholder(tf.float32, ()) + IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.preds = self.decoder.decode(self.knowledge_rep, self.p_max_length) + IDT IDT IDT self.loss = self.setup_loss(self.preds) + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT encoded_q, self.q_states = self.encoder.encode_questions(self.q_embeddings, self.q_mask_placeholder, None) + IDT IDT encoded_p, self.p_states = , self.p_mask_placeholder, self.q_states, scope= , reuse= ) IDT IDT self.knowledge_rep = self.decoder.match_LASTM(self.q_states, self.p_states, self.q_max_length, self.p_max_length) + + IDT def setup_loss(self, preds): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT preds = np.array(preds) + IDT IDT with vs.variable_scope('start_index_loss'): + IDT IDT IDT loss_tensor = tf.boolean_mask(tf.nn.sparse_softmax_cross_entropy_with_logits(preds[:, (0)], self.start_labels_placeholder), self.p_mask_placeholder) + IDT IDT IDT start_index_loss = tf.reduce_mean(loss_tensor, 0) + IDT IDT with vs.variable_scope('end_index_loss'): + IDT IDT IDT loss_tensor = tf.boolean_mask(tf.nn.sparse_softmax_cross_entropy_with_logits(preds[:, (1)], self.end_labels_placeholder), self.p_mask_placeholder) + IDT IDT IDT end_index_loss = tf.reduce_mean(loss_tensor, 0) + IDT IDT self.loss = [start_index_loss, end_index_loss] + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT self.pretrained_embeddings = , trainable= , dtype= ) IDT IDT IDT q_embeddings = tf.nn.embedding_lookup(self.pretrained_embeddings, self.q_placeholder) + IDT IDT IDT self.q_embeddings = , shape= , self.config.question_size, 1 * self.embed_size]) ) IDT IDT IDT p_embeddings = tf.nn.embedding_lookup(self.pretrained_embeddings, self.p_placeholder) + IDT IDT IDT self.p_embeddings = , shape= , self.config.paragraph_size, 1 * self.embed_size]) ) + IDT def optimize(self, session, dataset, mask, dropout= ) IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT if train_x is not None: + IDT IDT IDT input_feed[self.q_placeholder] = dataset['Questions'] + IDT IDT IDT input_feed[self.p_placeholder] = dataset['Paragraphs'] + IDT IDT if train_y is not None: + IDT IDT IDT input_feed[self.start_labels_placeholder] = dataset['Labels'][:, (0)] + IDT IDT IDT input_feed[self.end_labels_placeholder] = dataset['Labels'][:, (1)] + IDT IDT if mask is not None: + IDT IDT IDT input_feed[self.q_mask_placeholder] = dataset['Questions_masks'] + IDT IDT IDT input_feed[self.p_mask_placeholder] = dataset['Paragraphs_masks'] + IDT IDT input_feed[self.dropout_placeholder] = dropout + IDT IDT output_feed = [] + IDT IDT train_op_start = tf.train.AdamOptimizer(self.config.learning_rate).minimize(self.start_index_loss) + IDT IDT output_feed = [train_op_start, self.start_index_loss] + IDT IDT start_index_pred = session.run(output_feed, input_feed) + IDT IDT train_op_end = tf.train.AdamOptimizer(self.config.learning_rate).minimize(self.end_index_loss) + IDT IDT output_feed = [train_op_end, self.end_index_loss] + IDT IDT end_index_pred = session.run(output_feed, input_feed) + IDT IDT return start_index_loss, end_index_loss + + IDT def test(self, session, valid_x, valid_y): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT output_feed = [] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def decode(self, session, train_x, mask): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT if train_x is not None: + IDT IDT IDT input_feed[self.q_placeholder] = train_x['Questions'] + IDT IDT IDT input_feed[self.p_placeholder] = train_x['Paragraphs'] + IDT IDT if mask is not None: + IDT IDT IDT input_feed[self.q_mask_placeholder] = train_x['Questions_masks'] + IDT IDT IDT input_feed[self.p_mask_placeholder] = train_x['Paragraphs_masks'] + IDT IDT output_feed = [self.preds] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def create_feed_dict(self, question_batch, context_batch, labels_batch= ) IDT IDT """Creates the feed_dict for the model. + IDT IDT NOTE: You do not have to do anything here. + IDT IDT """ + IDT IDT feed_dict = {} + IDT IDT feed_dict[self.q_placeholder] = question_batch + IDT IDT feed_dict[self.p_placeholder] = context_batch + IDT IDT if labels_batch is not None: + IDT IDT IDT feed_dict[self.start_labels_placeholder] = labels_batch[0] + IDT IDT IDT feed_dict[self.end_labels_placeholder] = labels_batch[1] + IDT IDT return feed_dict + + IDT def train_on_batch(self, session, question_batch, context_batch, label_batch): + IDT IDT feed_dict = self.create_feed_dict(question_batch, context_batch, label_batch) + IDT IDT _, loss = , self.loss], feed_dict= ) IDT IDT return loss + + IDT def run_epoch(self, sess, inputs): + IDT IDT """Runs an epoch of training. + IDT IDT Args: + IDT IDT IDT sess: tf.Session() object + IDT IDT IDT inputs: datasets represented as a dictionary + IDT IDT IDT labels: np.ndarray of shape (n_samples, n_classes) + IDT IDT Returns: + IDT IDT IDT average_loss: scalar. Average minibatch loss of model on epoch. + IDT IDT """ + IDT IDT n_minibatches, total_loss = 0, 0 + IDT IDT for [question_batch, context_batch, labels_batch] in get_minibatches([inputs['Questions'], inputs['Paragraphs'], inputs['Labels']], self.config.batch_size): + IDT IDT IDT n_minibatches += 1 + IDT IDT IDT total_loss += self.train_on_batch(sess, question_batch, context_batch, labels_batch) + IDT IDT return total_loss / n_minibatches + + IDT def answer(self, session, test_x, mask): + IDT IDT yp, yp2 = self.decode(session, test_x, mask) + IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, valid_dataset): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + IDT IDT This method calls self.test() which explicitly calculates validation cost. + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + IDT IDT :return: + IDT IDT """ + IDT IDT valid_cost = 0 + IDT IDT for valid_x, valid_y in valid_dataset: + IDT IDT IDT valid_cost = self.test(sess, valid_x, valid_y) + IDT IDT return valid_cost + + IDT def evaluate_answer(self, session, dataset, sample= , log= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT idx_sample = np.random.randint(0, dataset['Questions'].shape[0], sample) + IDT IDT examples = {} + IDT IDT examples['Questions'] = dataset['Questions'][idx_sample] + IDT IDT examples['Paragraphs'] = dataset['Paragraphs'][idx_sample] + IDT IDT examples['Questions_masks'] = dataset['Questions'][idx_sample] + IDT IDT examples['Paragraphs_masks'] = dataset['Paragraphs'][idx_sample] + IDT IDT examples['Labels'] = dataset['Labels'][idx_sample] + IDT IDT correct_preds, total_correct, total_preds = 0.0, 0.0, 0.0 + IDT IDT masks = True + IDT IDT for _, labels, labels_ in self.answer(session, examples, masks): + IDT IDT IDT pred = set() + IDT IDT IDT if labels_[0] <= labels_[1]: + IDT IDT IDT IDT pred = set(range(labels_[0], labels_[1] + 1)) + IDT IDT IDT gold = set(range(labels[0], labels[1] + 1)) + IDT IDT IDT correct_preds += len(gold.intersection(pred)) + IDT IDT IDT total_preds += len(pred) + IDT IDT IDT total_correct += len(gold) + IDT IDT p = correct_preds / total_preds if correct_preds > 0 else 0 + IDT IDT r = correct_preds / total_correct if correct_preds > 0 else 0 + IDT IDT f1 = 2 * p * r / (p + r) if correct_preds > 0 else 0 + IDT IDT em = correct_preds + IDT IDT if log: + IDT IDT IDT logging.info('F1: {}, EM: {}, for {} samples'.format(f1, em, sample)) + IDT IDT return f1, em + + IDT def train(self, session, dataset, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + IDT IDT More ambitious appoarch can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT results_path = os.path.join(train_dir, '{:%Y%m%d_%H%M%S}'.format(datetime.now())) + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT best_score = 0.0 + IDT IDT for epoch in range(self.config.epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.config.epochs) + IDT IDT IDT logging.info('Best score so far: ' + str(best_score)) + IDT IDT IDT loss = self.run_epoch(session, dataset) + IDT IDT IDT f1, em = , dataset, sample= , log= ) IDT IDT IDT logging.info('loss: ' + str(loss) + ' f1: ' + str(f1) + ' em:' + str(em)) + IDT IDT IDT if f1 > best_score: + IDT IDT IDT IDT best_score = f1 + IDT IDT IDT IDT logging.info('New best score! Saving model in %s', results_path) + IDT IDT IDT IDT if self.saver: + IDT IDT IDT IDT IDT self.saver.save(session, results_path) + IDT IDT IDT + IDT IDT return best_score +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +image_size = 28 +num_labels = 10 +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT layer1_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT layer1_biases = tf.Variable(tf.zeros([hidden_nodes])) + IDT layer2_weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT layer2_biases = tf.Variable(tf.zeros([num_labels])) + IDT regularizers = tf.nn.l2_loss(layer1_weights) + tf.nn.l2_loss(layer1_biases) + tf.nn.l2_loss(layer2_weights) + tf.nn.l2_loss(layer2_biases) + + IDT def model(data): + IDT IDT hidden = tf.nn.relu(tf.matmul(data, layer1_weights) + layer1_biases) + IDT IDT return tf.matmul(hidden, layer2_weights) + layer2_biases + IDT logits = model(tf_train_dataset) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss += 0.001 * regularizers + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + IDT test_prediction = tf.nn.softmax(model(tf_test_dataset)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_steps = 5 +train_dataset_small = train_dataset[:batch_size * num_steps] +train_labels_small = train_labels[:batch_size * num_steps] +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels_small.shape[0] - batch_size) + IDT IDT batch_data = train_dataset_small[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels_small[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT ) + IDT IDT ) + IDT IDT , valid_labels)) + IDT , test_labels)) +image_size = 28 +num_labels = 10 +batch_size = 128 +l1_hidden_nodes = 2048 +l2_hidden_nodes = 64 +l3_hidden_nodes = 128 +num_steps = 4001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_keep_prob = tf.placeholder('float') + IDT global_step = tf.Variable(0) + IDT layer1_weights = tf.Variable(tf.truncated_normal([image_size * image_size, l1_hidden_nodes])) + IDT layer1_biases = tf.Variable(tf.zeros([l1_hidden_nodes])) + IDT layer2_weights = tf.Variable(tf.truncated_normal([l1_hidden_nodes, l2_hidden_nodes])) + IDT layer2_biases = tf.Variable(tf.zeros([l2_hidden_nodes])) + IDT layer3_weights = tf.Variable(tf.truncated_normal([l2_hidden_nodes, l3_hidden_nodes])) + IDT layer3_biases = tf.Variable(tf.zeros([l3_hidden_nodes])) + IDT layer4_weights = tf.Variable(tf.truncated_normal([l1_hidden_nodes, num_labels])) + IDT layer4_biases = tf.Variable(tf.zeros([num_labels])) + IDT regularizers = tf.nn.l2_loss(layer1_weights) + tf.nn.l2_loss(layer1_biases) + tf.nn.l2_loss(layer2_weights) + tf.nn.l2_loss(layer2_biases) + tf.nn.l2_loss(layer3_weights) + tf.nn.l2_loss(layer3_biases) + tf.nn.l2_loss(layer4_weights) + tf.nn.l2_loss(layer4_biases) + + IDT def model(data): + IDT IDT l1 = tf.nn.relu(tf.matmul(data, layer1_weights) + layer1_biases) + IDT IDT d1 = tf.nn.dropout(l1, tf_keep_prob) + IDT IDT return tf.matmul(d1, layer4_weights) + layer4_biases + IDT logits = model(tf_train_dataset) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss += 0.001 * regularizers + IDT learning_rate = tf.train.exponential_decay(0.1, global_step, 100, 0.99) + IDT optimizer = , global_step= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + IDT test_prediction = tf.nn.softmax(model(tf_test_dataset)) +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT batch_mask = , size= ) IDT IDT batch_data = train_dataset[(batch_mask), :] + IDT IDT batch_labels = train_labels[(batch_mask), :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +@AuthorCharles Van +@E-mail: williananjhon@hotmail.com +@Time2019/5/14 10:53 +@Project:CTR +@Filename:models.py +""" +import sys +if sys.version[0] == '2': + IDT import cProfile as pkl +else: + IDT import pickle as pkl +import numpy as np +import tensorflow as tf +import utils +dtype = utils.DTYPE + + +class model: + + IDT def __init__(self): + IDT IDT self.sess = None + IDT IDT self.X = None + IDT IDT self.y = None + IDT IDT self.layer_keeps = None + IDT IDT self.vars = None + IDT IDT self.keep_prob_train = None + IDT IDT self.keep_prob_test = None + + IDT def run(self, fetches, X= , y= , mode= ) IDT IDT feed_dict = {} + IDT IDT if type(self.X) is list: + IDT IDT IDT for i in range(len(X)): + IDT IDT IDT IDT feed_dict[self.X[i]] = X[i] + IDT IDT else: + IDT IDT IDT feed_dict[self.X] = X + IDT IDT if y is not None: + IDT IDT IDT feed_dict[self.y] = y + IDT IDT if self.layer_keeps is not None: + IDT IDT IDT if mode == 'train': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_train + IDT IDT IDT elif mode == 'test': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_test + IDT IDT return self.sess.run(fetches, feed_dict) + + IDT def dump(self, model_path): + IDT IDT var_map = {} + IDT IDT for name, var in self.vars.iteritems(): + IDT IDT IDT var_map[name] = self.run(var) + IDT IDT pkl.dump(var_map, open(model_path, 'wb')) + IDT IDT + + +class LR(model): + + IDT def __init__(self, input_dim= , output_dim= , init_path= , opt_algo= , learning_rate= , l2_weight= , random_seed= ) IDT IDT model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT IDT w = self.vars['w'] + IDT IDT IDT IDT b = self.vars['b'] + IDT IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT IDT logits = tf.reshape(xw + b, [-1]) + IDT IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT IDT self.loss = , logits= ) IDT IDT IDT IDT self, optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT IDT self.sess = ) IDT IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FM(Model): + + IDT def __init__(self, input_dim= , output_dim= , factor_order= , init_path= , opt_algo= , learning_rate= , l2_w= , l2_v= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('v', [input_dim, factor_order], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT v = self.vars['v'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + IDT IDT IDT xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + IDT IDT IDT p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FNN(model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT factor_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = factor_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT layer_input = layer_sizes[i] + IDT IDT IDT layer_output = layer_sizes[i + 1] + IDT IDT IDT init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + IDT IDT IDT x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + IDT IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class CCPM(model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT embedding_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = embedding_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('f1', [embedding_order, layer_sizes[2], 1, 2], 'tnormal', dtype)) + IDT IDT init_vars.append(('f2', [embedding_order, layer_sizes[3], 2, 2], 'tnormal', dtype)) + IDT IDT init_vars.append(('w1', [2 * 3 * embedding_order, 1], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [1], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.concat([(tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) + b0[i]) for i in range(num_inputs)], 1), layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT l = tf.transpose(tf.reshape(l, [-1, num_inputs, embedding_order, 1]), [0, 2, 1, 3]) + IDT IDT IDT f1 = self.vars['f1'] + IDT IDT IDT l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), num_inputs / 2), [0, 1, 3, 2]) + IDT IDT IDT f2 = self.vars['f2'] + IDT IDT IDT l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embedding_order * 3 * 2]), layer_acts[1]), self.layer_keeps[1]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1, layer_acts[2]), self.layer_keeps[2]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN1(model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , layer_l2= , kernel_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT factor_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = factor_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('k1', [num_inputs, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT layer_input = layer_sizes[i] + IDT IDT IDT layer_output = layer_sizes[i + 1] + IDT IDT IDT init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + IDT IDT IDT x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT k1 = self.vars['k1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT p = tf.reduce_sum(tf.reshape(tf.matmul(tf.reshape(tf.transpose(tf.reshape(l, [-1, num_inputs, factor_order]), [0, 2, 1]), [-1, num_inputs]), k1), [-1, factor_order, layer_sizes[2]]), 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1 + p, layer_acts[1]), self.layer_keeps[1]) + IDT IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + IDT IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT if kernel_l2 is not None: + IDT IDT IDT IDT self.loss += kernel_l2 * tf.nn.l2_loss(k1) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN2(model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , layer_l2= , kernel_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT factor_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = factor_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('k1', [factor_order * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT layer_input = layer_sizes[i] + IDT IDT IDT layer_output = layer_sizes[i + 1] + IDT IDT IDT init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + IDT IDT IDT x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT k1 = self.vars['k1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT z = tf.reduce_sum(tf.reshape(l, [-1, num_inputs, factor_order]), 1) + IDT IDT IDT p = tf.reshape(tf.matmul(tf.reshape(z, [-1, factor_order, 1]), tf.reshape(z, [-1, 1, factor_order])), [-1, factor_order * factor_order]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + tf.matmul(p, k1) + b1, layer_acts[1]), self.layer_keeps[1]) + IDT IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + IDT IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT if kernel_l2 is not None: + IDT IDT IDT IDT self.loss += kernel_l2 * tf.nn.l2_loss(k1) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os, sys +sys.path.append(os.pardir) +from savefig import * +import tensorflow as tf +reset_graph() +n_inputs = 3 +n_neurons = 5 +X0 = tf.placeholder(tf.float32, [None, n_inputs]) +X1 = tf.placeholder(tf.float32, [None, n_inputs]) +Wx = , n_neurons], dtype= )Wy = , n_neurons], dtype= )b = , n_neurons], dtype= )Y0 = tf.tanh(tf.matmul(X0, Wx) + b) +Y1 = tf.tanh(tf.matmul(Y0, Wy) + tf.matmul(X1, Wx) + b) +init = tf.global_variables_initializer() +import numpy as np +X0_batch = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 1]]) +X1_batch = np.array([[9, 8, 7], [0, 0, 0], [6, 5, 4], [3, 2, 1]]) +with tf.Session() as sess: + IDT init.run() + IDT Y0_val, Y1_val = , Y1], feed_dict= , X1: X1_batch}) )n_inputs = 3 +n_neurons = 5 +reset_graph() +X0 = tf.placeholder(tf.float32, [None, n_inputs]) +X1 = tf.placeholder(tf.float32, [None, n_inputs]) +basic_cell = )output_seqs, states = , [X0, X1], dtype= )Y0, Y1 = output_seqs +init = tf.global_variables_initializer() +X0_batch = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 1]]) +X1_batch = np.array([[9, 8, 7], [0, 0, 0], [6, 5, 4], [3, 2, 1]]) +with tf.Session() as sess: + IDT init.run() + IDT Y0_val, Y1_val = , Y1], feed_dict= , X1: X1_batch}) )n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +X_seqs = , perm= , 0, 2])) )basic_cell = )output_seqs, states = , X_seqs, dtype= )outputs = , perm= , 0, 2]) )init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = ) +[1]) +n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = )outputs, states = , X, dtype= )init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = ) +n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = )seq_length = tf.placeholder(tf.int32, [None]) +outputs, states = , X, dtype= , sequence_length= )init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +seq_length_batch = np.array([2, 1, 2, 2]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val, states_val = , states], feed_dict= , seq_length: seq_length_batch}) ) + +reset_graph() +n_steps = 28 +n_inputs = 28 +n_neurons = 150 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +basic_cell = )outputs, states = , X, dtype= )logits = tf.layers.dense(states, n_outputs) +xentropy = , logits= )loss = tf.reduce_mean(xentropy) +optimizer = )training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('/tmp/data/') +X_test = mnist.test.images.reshape((-1, n_steps, n_inputs)) +y_test = mnist.test.labels +n_epochs = 100 +batch_size = 150 +with tf.Session() as sess: + IDT init.run() + IDT for epoch in range(n_epochs): + IDT IDT for iteration in range(mnist.train.num_examples // batch_size): + IDT IDT IDT X_batch, y_batch = mnist.train.next_batch(batch_size) + IDT IDT IDT X_batch = X_batch.reshape((-1, n_steps, n_inputs)) + IDT IDT IDT sess.run(training_op, feed_dict= , y: y_batch}) ) IDT IDT acc_train = , y: y_batch}) ) IDT IDT acc_test = , y: y_test}) ) IDT IDT +reset_graph() +n_steps = 28 +n_inputs = 28 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +n_neurons = 100 +n_layers = 3 +layers = , activation= )multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers) +outputs, states = , X, dtype= )states_concat = , values= )logits = tf.layers.dense(states_concat, n_outputs) +xentropy = , logits= )loss = tf.reduce_mean(xentropy) +optimizer = )training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +n_epochs = 10 +batch_size = 150 +with tf.Session() as sess: + IDT init.run() + IDT for epoch in range(n_epochs): + IDT IDT for iteration in range(mnist.train.num_examples // batch_size): + IDT IDT IDT X_batch, y_batch = mnist.train.next_batch(batch_size) + IDT IDT IDT X_batch = X_batch.reshape((-1, n_steps, n_inputs)) + IDT IDT IDT sess.run(training_op, feed_dict= , y: y_batch}) ) IDT IDT acc_train = , y: y_batch}) ) IDT IDT acc_test = , y: y_test}) ) IDT IDT +t_min, t_max = 0, 30 +resolution = 0.1 + + +def time_series(t): + IDT return t * np.sin(t) / 3 + 2 * np.sin(t * 5) + + +def next_batch(batch_size, n_steps): + IDT t0 = np.random.rand(batch_size, 1) * (t_max - t_min - n_steps * resolution) + IDT Ts = t0 + np.arange(0.0, n_steps + 1) * resolution + IDT ys = time_series(Ts) + IDT return ys[:, :-1].reshape(-1, n_steps, 1), ys[:, 1:].reshape(-1, n_steps, 1) + + +t = np.linspace(t_min, t_max, int((t_max - t_min) / resolution)) +n_steps = 20 +t_instance = np.linspace(12.2, 12.2 + resolution * (n_steps + 1), n_steps + 1) +plt.figure(figsize= , 4)) )plt.subplot(121) +plt.title('A time series (generated)', fontsize= )plt.plot(t, time_series(t), label= )plt.plot(t_instance[:-1], time_series(t_instance[:-1]), 'b-', linewidth= , label= )plt.legend(loc= , fontsize= )plt.axis([0, 30, -17, 13]) +plt.xlabel('Time') +plt.ylabel('Value') +plt.subplot(122) +plt.title('A training instance', fontsize= )plt.plot(t_instance[:-1], time_series(t_instance[:-1]), 'bo', markersize= , label= )plt.plot(t_instance[1:], time_series(t_instance[1:]), 'w*', markersize= , label= )plt.legend(loc= )plt.xlabel('Time') +plt.show() +X_batch, y_batch = next_batch(1, n_steps) +np.c_[X_batch[0], y_batch[0]] +reset_graph() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = , activation= , output_size= )outputs, states = , X, dtype= )learning_rate = 0.001 +loss = tf.reduce_mean(tf.square(outputs - y)) +optimizer = )training_op = optimizer.minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver() +n_iterations = 1500 +batch_size = 50 +with tf.Session() as sess: + IDT init.run() + IDT for iteration in range(n_iterations): + IDT IDT X_batch, y_batch = next_batch(batch_size, n_steps) + IDT IDT sess.run(training_op, feed_dict= , y: y_batch}) ) IDT IDT if iteration % 100 == 0: + IDT IDT IDT mse = , y: y_batch}) ) IDT IDT IDT + IDT saver.save(sess, './my_time_series_model') +with tf.Session() as sess: + IDT saver.restore(sess, './my_time_series_model') + IDT X_new = time_series(np.array(t_instance[:-1].reshape(-1, n_steps, n_inputs))) + IDT y_pred = , feed_dict= )plt.title('Testing the model', fontsize= )plt.plot(t_instance[:-1], time_series(t_instance[:-1]), 'bo', markersize= , label= )plt.plot(t_instance[1:], time_series(t_instance[1:]), 'w*', markersize= , label= )plt.plot(t_instance[1:], y_pred[(0), :, (0)], 'r.', markersize= , label= )plt.legend(loc= )plt.xlabel('Time') +plt.show() +reset_graph() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = , activation= )rnn_outputs, states = , X, dtype= )n_outputs = 1 +learning_rate = 0.001 +stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons]) +stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs) +outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs]) +loss = tf.reduce_mean(tf.square(outputs - y)) +optimizer = )training_op = optimizer.minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver() +n_iterations = 1500 +batch_size = 50 +with tf.Session() as sess: + IDT init.run() + IDT for iteration in range(n_iterations): + IDT IDT X_batch, y_batch = next_batch(batch_size, n_steps) + IDT IDT sess.run(training_op, feed_dict= , y: y_batch}) ) IDT IDT if iteration % 100 == 0: + IDT IDT IDT mse = , y: y_batch}) ) IDT IDT IDT + IDT X_new = time_series(np.array(t_instance[:-1].reshape(-1, n_steps, n_inputs))) + IDT y_pred = , feed_dict= ) IDT saver.save(sess, './my_time_series_model') +with tf.Session() as sess: + IDT saver.restore(sess, './my_time_series_model') + IDT sequence = [0.0] * n_steps + IDT for iteration in range(300): + IDT IDT X_batch = np.array(sequence[-n_steps:]).reshape(1, n_steps, 1) + IDT IDT y_pred = , feed_dict= ) IDT IDT sequence.append(y_pred[0, -1, 0]) +plt.figure(figsize= , 4)) )plt.plot(np.arange(len(sequence)), sequence, 'b-') +plt.plot(t[:n_steps], sequence[:n_steps], 'b-', linewidth= )plt.xlabel('Time') +plt.ylabel('Value') +plt.show() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from SpeechDataUtils import SpeechDataSet +timit_database = TimitDatabase('G:\\TIMIT') +max_input_sequence_length = timit_database.get_max_mfcc_features_length() +max_output_sequence_length = timit_database.get_max_phonemes_length() +dictionary_size = timit_database.phonemes_dictionary_size +train_data = timit_database.train_dataset +eval_data = timit_database.test_dataset +input_placeholder = , shape= , max_input_sequence_length, 40]) )input_lengths_placeholder = , shape= )output_placeholder = tf.sparse_placeholder(tf.int32) +output_lengths_placeholder = , shape= )cell = tf.nn.rnn_cell.LSTMCell(128) +cell = tf.nn.rnn_cell.MultiRNNCell([cell] * 2) +rnn, _ = , input_placeholder, input_lengths_placeholder, dtype= )rnn = , shape= , max_input_sequence_length * 128]) )weights = , 256], stddev= )biases = )layer = tf.matmul(rnn, weights) + biases +layer = tf.nn.relu(layer) +weights = , max_output_sequence_length * dictionary_size], stddev= )biases = )layer = tf.matmul(layer, weights) + biases +layer = tf.nn.relu(layer) +logits = , shape= , max_output_sequence_length, dictionary_size]) )logits_timemajor = tf.transpose(logits, [1, 0, 2]) +loss = tf.nn.ctc_loss(logits_timemajor, output_placeholder, output_lengths_placeholder) +loss = tf.reduce_mean(loss) +optimizer = tf.train.AdamOptimizer() +train = optimizer.minimize(loss) +session_config = tf.ConfigProto() +session_config.gpu_options.allow_growth = True +with tf.Session(config= ) IDT init = tf.global_variables_initializer() + IDT session.run(init) + IDT for i in tqdm(range(1000)): + IDT IDT batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(11, False) + IDT IDT tmp = [] + IDT IDT for j in range(len(batch_outputs)): + IDT IDT IDT tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + IDT IDT batch_outputs = np.array(tmp) + IDT IDT batch_outputs = SpeechDataSet.tokens_for_sparse(batch_outputs) + IDT IDT feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths, output_placeholder: batch_outputs, output_lengths_placeholder: batch_output_lengths} + IDT IDT session.run(train, feed_dict= ) IDT IDT if i % 100 == 0: + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT + IDT batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(1, False) + IDT feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths} + IDT test = , feed_dict= ) IDT test = session.run(tf.transpose(test, [1, 0, 2])) + IDT decoded, prob = tf.nn.ctc_beam_search_decoder(test, batch_output_lengths, 100, 5) + IDT decoded = session.run(decoded) + IDT prob = session.run(prob) + IDT results = [] + IDT for decoded_path in decoded: + IDT IDT phonemes_ids = decoded_path.values + IDT IDT result_words = '' + IDT IDT for idx in phonemes_ids: + IDT IDT IDT word = timit_database.id_to_phoneme_dictionary[idx] + IDT IDT IDT if word != '': + IDT IDT IDT IDT result_words += word + ' ' + IDT IDT results += [result_words] + IDT target = batch_outputs[0] + IDT target_words = '' + IDT for idx in target: + IDT IDT word = timit_database.id_to_phoneme_dictionary[idx] + IDT IDT if word != '': + IDT IDT IDT target_words += word + ' ' + IDT + IDT + IDT + IDT + IDT + IDT for result_sentence in results: + IDT IDT + IDT + IDT + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import getpass +import sys +import time +import numpy as np +import tensorflow as tf +from q2_initialization import xavier_weight_init +import data_utils.utils as du +import data_utils.ner as ner +from utils import data_iterator +from model import LanguageModel + + +def variable_summaries(variable, name): + IDT with tf.name_scope('summaries'): + IDT IDT mean = tf.reduce_mean(variable) + IDT IDT tf.summary.scalar('mean/' + name, mean) + IDT IDT with tf.name_scope('stddev'): + IDT IDT IDT stddev = tf.sqrt(tf.reduce_sum(tf.square(variable - mean))) + IDT IDT tf.summary.scalar('stddev/' + name, stddev) + IDT IDT tf.summary.scalar('max/' + name, tf.reduce_max(variable)) + IDT IDT tf.summary.scalar('min/' + name, tf.reduce_min(variable)) + IDT IDT tf.summary.histogram(name, variable) + + +class Config(object): + IDT """Holds model hyperparams and data information. + + The config class is used to store various hyperparameters and dataset + information parameters. Model objects are passed a Config() object at + instantiation. + """ + IDT embed_size = 50 + IDT batch_size = 64 + IDT label_size = 5 + IDT hidden_size = 100 + IDT max_epochs = 10 + IDT early_stopping = 5 + IDT dropout = 0.9 + IDT lr = 0.001 + IDT l2 = 0.001 + IDT window_size = 3 + + +class NERModel(LanguageModel): + IDT """Implements a NER (Named Entity Recognition) model. + + This class implements a deep network for named entity recognition. It + inherits from LanguageModel, which has an add_embedding method in addition to + the standard Model method. + """ + + IDT def load_data(self, debug= ) IDT IDT + IDT IDT self.wv, word_to_num, num_to_word = ner.load_wv('data/ner/vocab.txt', 'data/ner/wordVectors.txt') + IDT IDT tagnames = ['O', 'LOC', 'MISC', 'ORG', 'PER'] + IDT IDT self.num_to_tag = dict(enumerate(tagnames)) + IDT IDT tag_to_num = {v: k for k, v in self.num_to_tag.items()} + IDT IDT docs = du.load_dataset('data/ner/train') + IDT IDT self.X_train, self.y_train = , word_to_num, tag_to_num, wsize= ) IDT IDT if debug: + IDT IDT IDT self.X_train = self.X_train[:1024] + IDT IDT IDT self.y_train = self.y_train[:1024] + IDT IDT docs = du.load_dataset('data/ner/dev') + IDT IDT self.X_dev, self.y_dev = , word_to_num, tag_to_num, wsize= ) IDT IDT if debug: + IDT IDT IDT self.X_dev = self.X_dev[:1024] + IDT IDT IDT self.y_dev = self.y_dev[:1024] + IDT IDT docs = du.load_dataset('data/ner/test.masked') + IDT IDT self.X_test, self.y_test = , word_to_num, tag_to_num, wsize= ) + IDT def add_placeholders(self): + IDT IDT """Generate placeholder variables to represent the input tensors + + IDT These placeholders are used as inputs by the rest of the model building + IDT code and will be fed data during training. Note that when "None" is in a + IDT placeholder's shape, it's flexible + + IDT Adds following nodes to the computational graph + + IDT input_placeholder: Input placeholder tensor of shape + IDT IDT IDT IDT IDT (None, window_size), type tf.int32 + IDT labels_placeholder: Labels placeholder tensor of shape + IDT IDT IDT IDT IDT IDT (None, label_size), type tf.float32 + IDT dropout_placeholder: Dropout value placeholder (scalar), + IDT IDT IDT IDT IDT IDT type tf.float32 + + IDT Add these placeholders to self as the instance variables + + IDT self.input_placeholder + IDT self.labels_placeholder + IDT self.dropout_placeholder + + IDT (Don't change the variable names) + IDT """ + IDT IDT self.input_placeholder = , shape= , self.config.window_size]) ) IDT IDT self.labels_placeholder = , shape= , self.config.label_size]) ) IDT IDT self.dropout_placeholder = , name= ) + IDT def create_feed_dict(self, input_batch, dropout, label_batch= ) IDT IDT """Creates the feed_dict for softmax classifier. + + IDT A feed_dict takes the form of: + + IDT feed_dict = { + IDT IDT : , + IDT IDT .... + IDT } + + + IDT Hint: The keys for the feed_dict should be a subset of the placeholder + IDT IDT tensors created in add_placeholders. + IDT Hint: When label_batch is None, don't add a labels entry to the feed_dict. + IDT + IDT Args: + IDT input_batch: A batch of input data. + IDT label_batch: A batch of label data. + IDT Returns: + IDT feed_dict: The feed dictionary mapping from placeholders to values. + IDT """ + IDT IDT feed_dict = {self.input_placeholder: input_batch, self.dropout_placeholder: dropout} + IDT IDT if label_batch is not None: + IDT IDT IDT feed_dict[self.labels_placeholder] = label_batch + IDT IDT return feed_dict + + IDT def add_embedding(self): + IDT IDT """Add embedding layer that maps from vocabulary to vectors. + + IDT Creates an embedding tensor (of shape (len(self.wv), embed_size). Use the + IDT input_placeholder to retrieve the embeddings for words in the current batch. + + IDT (Words are discrete entities. They need to be transformed into vectors for use + IDT in deep-learning. Although we won't do so in this problem, in practice it's + IDT useful to initialize the embedding with pre-trained word-vectors. For this + IDT problem, using the default initializer is sufficient.) + + IDT Hint: This layer should use the input_placeholder to index into the + IDT IDT embedding. + IDT Hint: You might find tf.nn.embedding_lookup useful. + IDT Hint: See following link to understand what -1 in a shape means. + IDT https://www.tensorflow.org/versions/r0.8/api_docs/python/array_ops.html + IDT Hint: Check the last slide from the TensorFlow lecture. + IDT Hint: Here are the dimensions of the variables you will need to create: + + IDT L: (len(self.wv), embed_size) + + IDT Returns: + IDT window: tf.Tensor of shape (-1, window_size*embed_size) + IDT """ + IDT IDT with tf.variable_scope('embedding_layer') as scope: + IDT IDT IDT embedding = , [len(self.wv), self.config.embed_size], initializer= ) IDT IDT IDT window = , ids= ) IDT IDT IDT window = , shape= , self.config.window_size * self.config.embed_size], name= ) IDT IDT IDT variable_summaries(window, window.name) + IDT IDT return window + + IDT def add_model(self, window): + IDT IDT """Adds the 1-hidden-layer NN. + + IDT Hint: Use a variable_scope (e.g. "Layer") for the first hidden layer, and + IDT IDT another variable_scope (e.g. "Softmax") for the linear transformation + IDT IDT preceding the softmax. Make sure to use the xavier_weight_init you + IDT IDT defined in the previous part to initialize weights. + IDT Hint: Make sure to add in regularization and dropout to this network. + IDT IDT Regularization should be an addition to the cost function, while + IDT IDT dropout should be added after both variable scopes. + IDT Hint: You might consider using a tensorflow Graph Collection (e.g + IDT IDT "total_loss") to collect the regularization and loss terms (which you + IDT IDT will add in add_loss_op below). + IDT Hint: Here are the dimensions of the various variables you will need to + IDT IDT create + + IDT IDT W: (window_size*embed_size, hidden_size) + IDT IDT b1: (hidden_size,) + IDT IDT U: (hidden_size, label_size) + IDT IDT b2: (label_size) + + IDT https://www.tensorflow.org/versions/r0.7/api_docs/python/framework.html + IDT Args: + IDT window: tf.Tensor of shape (-1, window_size*embed_size) + IDT Returns: + IDT output: tf.Tensor of shape (batch_size, label_size) + IDT """ + IDT IDT with tf.variable_scope('layer') as layer_scope: + IDT IDT IDT W = , shape= , self.config.hidden_size], initializer= ) IDT IDT IDT b1 = , shape= , initializer= ) IDT IDT IDT variable_summaries(W, W.name) + IDT IDT IDT variable_summaries(b1, b1.name) + IDT IDT IDT with tf.variable_scope('hidden_layer') as hidden_layer: + IDT IDT IDT IDT U = , shape= , self.config.label_size], initializer= ) IDT IDT IDT IDT b2 = , shape= , initializer= ) IDT IDT IDT IDT variable_summaries(U, U.name) + IDT IDT IDT IDT variable_summaries(b2, b2.name) + IDT IDT h_fc1 = tf.nn.relu(tf.matmul(window, W) + b1) + IDT IDT h_fc1 = tf.nn.dropout(h_fc1, self.dropout_placeholder) + IDT IDT h_fc2 = tf.matmul(h_fc1, U) + b2 + IDT IDT h_fc2 = tf.nn.dropout(h_fc2, self.dropout_placeholder) + IDT IDT l2_loss = tf.nn.l2_loss(W) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(U) + tf.nn.l2_loss(b2) + IDT IDT tf.add_to_collection(name= , value= ) IDT IDT output = h_fc2 + IDT IDT return output + + IDT def add_loss_op(self, y): + IDT IDT """Adds cross_entropy_loss ops to the computational graph. + + IDT Hint: You can use tf.nn.softmax_cross_entropy_with_logits to simplify your + IDT IDT implementation. You might find tf.reduce_mean useful. + IDT Args: + IDT y (pred): A tensor of shape (batch_size, n_classes) + IDT Returns: + IDT loss: A 0-d tensor (scalar) + IDT """ + IDT IDT loss = , labels= ) IDT IDT loss += self.config.l2 * tf.get_collection('l2_loss')[0] + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT """Sets up the training Ops. + + IDT Creates an optimizer and applies the gradients to all trainable variables. + IDT The Op returned by this function is what must be passed to the + IDT `sess.run()` call to cause the model to train. See + + IDT https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html + + IDT for more information. + + IDT Hint: Use tf.train.AdamOptimizer for this model. + IDT IDT Calling optimizer.minimize() will return a train_op object. + + IDT Args: + IDT loss: Loss tensor, from cross_entropy_loss. + IDT Returns: + IDT train_op: The Op for training. + IDT """ + IDT IDT train_op = tf.train.AdamOptimizer(self.config.lr).minimize(loss) + IDT IDT tf.summary.scalar('loss', loss) + IDT IDT return train_op + + IDT def __init__(self, config): + IDT IDT + IDT IDT self.config = config + IDT IDT self.load_data(debug= ) IDT IDT self.add_placeholders() + IDT IDT window = self.add_embedding() + IDT IDT y = self.add_model(window) + IDT IDT self.loss = self.add_loss_op(y) + IDT IDT self.predictions = tf.nn.softmax(y) + IDT IDT one_hot_prediction = tf.argmax(self.predictions, 1) + IDT IDT correct_prediction = tf.equal(tf.argmax(self.labels_placeholder, 1), one_hot_prediction) + IDT IDT self.correct_predictions = tf.reduce_sum(tf.cast(correct_prediction, 'int32')) + IDT IDT self.train_op = self.add_training_op(self.loss) + IDT IDT self.merged_summaries = tf.summary.merge_all() + IDT IDT self.summary_writer = None + + IDT def run_epoch(self, session, input_data, input_labels, shuffle= , verbose= , epoch= ) IDT IDT orig_X, orig_y = input_data, input_labels + IDT IDT dp = self.config.dropout + IDT IDT total_loss = [] + IDT IDT total_correct_examples = 0 + IDT IDT total_processed_examples = 0 + IDT IDT total_steps = len(orig_X) / self.config.batch_size + IDT IDT for step, (x, y) in enumerate(data_iterator(orig_X, orig_y, batch_size= , label_size= , shuffle= ) IDT IDT IDT feed = , dropout= , label_batch= ) IDT IDT IDT loss, total_correct, _, merged = , self.correct_predictions, self.train_op, self.merged_summaries], feed_dict= ) IDT IDT IDT if step % 50 == 0: + IDT IDT IDT IDT self.summary_writer.add_summary(merged, epoch * total_steps + step) + IDT IDT IDT total_processed_examples += len(x) + IDT IDT IDT total_correct_examples += total_correct + IDT IDT IDT total_loss.append(loss) + IDT IDT IDT if verbose and step % verbose == 0: + IDT IDT IDT IDT sys.stdout.write('\r{} / {} : loss = , total_steps, np.mean(total_loss))) ) IDT IDT IDT IDT sys.stdout.flush() + IDT IDT if verbose: + IDT IDT IDT sys.stdout.write('\r') + IDT IDT IDT sys.stdout.flush() + IDT IDT return np.mean(total_loss), total_correct_examples / float(total_processed_examples) + + IDT def predict(self, session, X, y= ) IDT IDT + IDT IDT dp = 1 + IDT IDT losses = [] + IDT IDT results = [] + IDT IDT if np.any(y): + IDT IDT IDT data = , y, batch_size= , label_size= , shuffle= ) IDT IDT else: + IDT IDT IDT data = , batch_size= , label_size= , shuffle= ) IDT IDT for step, (x, y) in enumerate(data): + IDT IDT IDT feed = , dropout= ) IDT IDT IDT if np.any(y): + IDT IDT IDT IDT feed[self.labels_placeholder] = y + IDT IDT IDT IDT loss, preds = , self.predictions], feed_dict= ) IDT IDT IDT IDT losses.append(loss) + IDT IDT IDT else: + IDT IDT IDT IDT preds = , feed_dict= ) IDT IDT IDT predicted_indices = ) IDT IDT IDT results.extend(predicted_indices) + IDT IDT if len(losses) == 0: + IDT IDT IDT return 0, results + IDT IDT return np.mean(losses), results + + +def print_confusion(confusion, num_to_tag): + IDT + IDT total_guessed_tags = ) IDT total_true_tags = ) IDT + IDT + IDT for i, tag in sorted(num_to_tag.items()): + IDT IDT prec = confusion[i, i] / float(total_guessed_tags[i]) + IDT IDT recall = confusion[i, i] / float(total_true_tags[i]) + IDT IDT ) + + +def calculate_confusion(config, predicted_indices, y_indices): + IDT + IDT confusion = , config.label_size), dtype= ) IDT for i in range(len(y_indices)): + IDT IDT correct_label = y_indices[i] + IDT IDT guessed_label = predicted_indices[i] + IDT IDT confusion[correct_label, guessed_label] += 1 + IDT return confusion + + +def save_predictions(predictions, filename): + IDT + IDT with open(filename, 'wb') as f: + IDT IDT for prediction in predictions: + IDT IDT IDT f.write(bytes(prediction)) + + +def test_NER(): + IDT """Test NER model implementation. + + You can use this function to test your implementation of the Named Entity + Recognition network. When debugging, set max_epochs in the Config object to 1 + so you can rapidly iterate. + """ + IDT config = Config() + IDT with tf.Graph().as_default(): + IDT IDT model = NERModel(config) + IDT IDT init = tf.initialize_all_variables() + IDT IDT saver = tf.train.Saver() + IDT IDT with tf.Session() as session: + IDT IDT IDT best_val_loss = float('inf') + IDT IDT IDT best_val_epoch = 0 + IDT IDT IDT model.summary_writer = tf.summary.FileWriter('NER_train_log/', session.graph) + IDT IDT IDT session.run(init) + IDT IDT IDT for epoch in range(config.max_epochs): + IDT IDT IDT IDT ) + IDT IDT IDT IDT start = time.time() + IDT IDT IDT IDT train_loss, train_acc = , model.X_train, model.y_train, epoch= ) IDT IDT IDT IDT val_loss, predictions = model.predict(session, model.X_dev, model.y_dev) + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT if val_loss < best_val_loss: + IDT IDT IDT IDT IDT best_val_loss = val_loss + IDT IDT IDT IDT IDT best_val_epoch = epoch + IDT IDT IDT IDT IDT if not os.path.exists('./weights'): + IDT IDT IDT IDT IDT IDT os.makedirs('./weights') + IDT IDT IDT IDT IDT saver.save(session, './weights/ner.weights') + IDT IDT IDT IDT if epoch - best_val_epoch > config.early_stopping: + IDT IDT IDT IDT IDT break + IDT IDT IDT IDT confusion = calculate_confusion(config, predictions, model.y_dev) + IDT IDT IDT IDT cm = confusion.copy() + IDT IDT IDT IDT cm = , keepdims= ) IDT IDT IDT IDT cm = cm[(np.newaxis), :, :, (np.newaxis)].astype(np.float32) + IDT IDT IDT IDT cm_tf_image = tf.convert_to_tensor(cm) + IDT IDT IDT IDT cm_is = tf.summary.image('confusion_matrix', cm_tf_image) + IDT IDT IDT IDT cm_current_epoch = session.run(cm_is) + IDT IDT IDT IDT model.summary_writer.add_summary(cm_current_epoch, epoch) + IDT IDT IDT IDT print_confusion(confusion, model.num_to_tag) + IDT IDT IDT IDT - start)) + IDT IDT IDT saver.restore(session, './weights/ner.weights') + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT _, predictions = model.predict(session, model.X_test, model.y_test) + IDT IDT IDT save_predictions(predictions, 'q2_test.predicted') + + +if __name__ == '__main__': + IDT test_NER() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import imageio +import matplotlib.pyplot as plt +import numpy as np +import os +import sys +import tarfile +from IPython.display import display, Image +from sklearn.linear_model import LogisticRegression +from six.moves.urllib.request import urlretrieve +from six.moves import cPickle as pickle +import tensorflow as tf +from six.moves import range +url = 'https://commondatastorage.googleapis.com/books1000/' +last_percent_reported = None +data_root = '/Users/rakadalal/Desktop/Deep_learning/' +num_classes = 10 +np.random.seed(133) +image_size = 28 +pixel_depth = 255.0 + + +def download_progress_hook(count, blockSize, totalSize): + IDT """A hook to report the progress of a download. This is mostly intended for users with + slow internet connections. Reports every 5% change in download progress. + """ + IDT global last_percent_reported + IDT percent = int(count * blockSize * 100 / totalSize) + IDT if last_percent_reported != percent: + IDT IDT if percent % 5 == 0: + IDT IDT IDT sys.stdout.write('%s%%' % percent) + IDT IDT IDT sys.stdout.flush() + IDT IDT else: + IDT IDT IDT sys.stdout.write('.') + IDT IDT IDT sys.stdout.flush() + IDT IDT last_percent_reported = percent + + +def maybe_download(filename, expected_bytes, force= ) IDT + IDT dest_filename = os.path.join(data_root, filename) + IDT if force or not os.path.exists(dest_filename): + IDT IDT + IDT IDT filename, _ = , dest_filename, reporthook= ) IDT IDT + IDT statinfo = os.stat(dest_filename) + IDT if statinfo.st_size == expected_bytes: + IDT IDT + IDT else: + IDT IDT raise Exception('Failed to verify ' + dest_filename + '. Can you get to it with a browser?') + IDT return dest_filename + + +def maybe_extract(filename, force= ) IDT root = os.path.splitext(os.path.splitext(filename)[0])[0] + IDT if os.path.isdir(root) and not force: + IDT IDT ) + IDT else: + IDT IDT + IDT IDT tar = tarfile.open(filename) + IDT IDT sys.stdout.flush() + IDT IDT tar.extractall(data_root) + IDT IDT tar.close() + IDT data_folders = [os.path.join(root, d) for d in sorted(os.listdir(root)) if os.path.isdir(os.path.join(root, d))] + IDT if len(data_folders) != num_classes: + IDT IDT raise Exception('Expected %d folders, one per class. Found %d instead.' % (num_classes, len(data_folders))) + IDT + IDT return data_folders + + +def load_letter(folder, min_num_images): + IDT + IDT image_files = os.listdir(folder) + IDT dataset = , image_size, image_size), dtype= ) IDT + IDT num_images = 0 + IDT for image in image_files: + IDT IDT image_file = os.path.join(folder, image) + IDT IDT try: + IDT IDT IDT image_data = (imageio.imread(image_file).astype(float) - pixel_depth / 2) / pixel_depth + IDT IDT IDT if image_data.shape != (image_size, image_size): + IDT IDT IDT IDT raise Exception('Unexpected image shape: %s' % str(image_data.shape)) + IDT IDT IDT dataset[(num_images), :, :] = image_data + IDT IDT IDT num_images = num_images + 1 + IDT IDT except (IOError, ValueError) as e: + IDT IDT IDT + IDT dataset = dataset[0:num_images, :, :] + IDT if num_images < min_num_images: + IDT IDT raise Exception('Many fewer images than expected: %d < %d' % (num_images, min_num_images)) + IDT + IDT ) + IDT ) + IDT return dataset + + +def maybe_pickle(data_folders, min_num_images_per_class, force= ) IDT dataset_names = [] + IDT for folder in data_folders: + IDT IDT set_filename = folder + '.pickle' + IDT IDT dataset_names.append(set_filename) + IDT IDT if os.path.exists(set_filename) and not force: + IDT IDT IDT + IDT IDT else: + IDT IDT IDT + IDT IDT IDT dataset = load_letter(folder, min_num_images_per_class) + IDT IDT IDT try: + IDT IDT IDT IDT with open(set_filename, 'wb') as f: + IDT IDT IDT IDT IDT pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL) + IDT IDT IDT except Exception as e: + IDT IDT IDT IDT + IDT return dataset_names + + +def make_arrays(nb_rows, img_size): + IDT if nb_rows: + IDT IDT dataset = , img_size, img_size), dtype= ) IDT IDT labels = , dtype= ) IDT else: + IDT IDT dataset, labels = None, None + IDT return dataset, labels + + +def merge_datasets(pickle_files, train_size, valid_size= ) IDT num_classes = len(pickle_files) + IDT valid_dataset, valid_labels = make_arrays(valid_size, image_size) + IDT train_dataset, train_labels = make_arrays(train_size, image_size) + IDT vsize_per_class = valid_size // num_classes + IDT tsize_per_class = train_size // num_classes + IDT start_v, start_t = 0, 0 + IDT end_v, end_t = vsize_per_class, tsize_per_class + IDT end_l = vsize_per_class + tsize_per_class + IDT for label, pickle_file in enumerate(pickle_files): + IDT IDT try: + IDT IDT IDT with open(pickle_file, 'rb') as f: + IDT IDT IDT IDT letter_set = pickle.load(f) + IDT IDT IDT IDT np.random.shuffle(letter_set) + IDT IDT IDT IDT if valid_dataset is not None: + IDT IDT IDT IDT IDT valid_letter = letter_set[:vsize_per_class, :, :] + IDT IDT IDT IDT IDT valid_dataset[start_v:end_v, :, :] = valid_letter + IDT IDT IDT IDT IDT valid_labels[start_v:end_v] = label + IDT IDT IDT IDT IDT start_v += vsize_per_class + IDT IDT IDT IDT IDT end_v += vsize_per_class + IDT IDT IDT IDT train_letter = letter_set[vsize_per_class:end_l, :, :] + IDT IDT IDT IDT train_dataset[start_t:end_t, :, :] = train_letter + IDT IDT IDT IDT train_labels[start_t:end_t] = label + IDT IDT IDT IDT start_t += tsize_per_class + IDT IDT IDT IDT end_t += tsize_per_class + IDT IDT except Exception as e: + IDT IDT IDT + IDT IDT IDT raise + IDT return valid_dataset, valid_labels, train_dataset, train_labels + + +train_filename = maybe_download('notMNIST_large.tar.gz', 247336696) +test_filename = maybe_download('notMNIST_small.tar.gz', 8458043) +train_folders = maybe_extract(train_filename) +test_folders = maybe_extract(test_filename) +train_datasets = maybe_pickle(train_folders, 45000) +test_datasets = maybe_pickle(test_folders, 1800) +train_size = 200000 +valid_size = 10000 +test_size = 10000 +valid_dataset, valid_labels, train_dataset, train_labels = merge_datasets(train_datasets, train_size, valid_size) +_, _, test_dataset, test_labels = merge_datasets(test_datasets, test_size) + + +def randomize(dataset, labels): + IDT permutation = np.random.permutation(labels.shape[0]) + IDT shuffled_dataset = dataset[(permutation), :, :] + IDT shuffled_labels = labels[permutation] + IDT return shuffled_dataset, shuffled_labels + + +train_dataset, train_labels = randomize(train_dataset, train_labels) +test_dataset, test_labels = randomize(test_dataset, test_labels) +valid_dataset, valid_labels = randomize(valid_dataset, valid_labels) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +image_size = 28 +num_labels = 10 +num_channels = 1 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size, image_size, num_channels)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +batch_size = 16 +patch_size = 5 +depth = 32 +num_hidden = 64 +num_steps = 30001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size, image_size, num_channels)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT layer1_weights = , patch_size, num_channels, depth], stddev= ) IDT layer1_biases = tf.Variable(tf.zeros([depth])) + IDT layer2_weights = , patch_size, depth, depth], stddev= ) IDT layer2_biases = , shape= ) IDT layer3_weights = , num_hidden], stddev= ) IDT layer3_biases = , shape= ) IDT layer4_weights = , num_labels], stddev= ) IDT layer4_biases = , shape= ) + IDT def model(data, keep_prob): + IDT IDT conv1 = , layer1_weights, [1, 1, 1, 1], padding= ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT conv2 = , layer2_weights, [1, 1, 1, 1], padding= ) IDT IDT pool2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT shape = pool2.get_shape().as_list() + IDT IDT reshape = tf.reshape(pool2, [shape[0], shape[1] * shape[2] * shape[3]]) + IDT IDT fc1 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases) + IDT IDT fc1_drop = tf.nn.dropout(fc1, keep_prob) + IDT IDT y_conv = tf.matmul(fc1_drop, layer4_weights) + layer4_biases + IDT IDT return y_conv + IDT logits = model(tf_train_dataset, 0.5) + IDT loss = , logits= ) IDT loss = loss + 0.001 * (tf.nn.l2_loss(layer3_weights) + tf.nn.l2_loss(layer3_biases) + tf.nn.l2_loss(layer4_weights) + tf.nn.l2_loss(layer4_biases)) + IDT learning_rate = , global_step, num_steps, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset, 1)) + IDT test_prediction = tf.nn.softmax(model(tf_test_dataset, 1)) +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :, :, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 300 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +""" +@author sourabhxiii + +""" +import numpy as np +import tensorflow as tf +from tensorflow import layers +from collections import namedtuple + + +class Actor: + + IDT def __init__(self, batch_size, input_dim, output_dim, action_low, action_high, np_index, seed): + IDT IDT tf.set_random_seed(seed) + IDT IDT self.lr = 0.01 + IDT IDT bounds = , field_names= , 'high']) ) IDT IDT self.action_bound = bounds(action_low, action_high) + IDT IDT self.batch_size = batch_size + IDT IDT with tf.name_scope('input'): + IDT IDT IDT self.features = , shape= , input_dim), name= ) IDT IDT IDT self.grads = , shape= , output_dim), name= ) IDT IDT with tf.name_scope('output'): + IDT IDT IDT self.y = , shape= , 1), name= ) IDT IDT with tf.name_scope('actor_net'): + IDT IDT IDT nn = , kernel_initializer= , bias_initializer= , activation= , name= ) IDT IDT IDT nn = , kernel_initializer= , bias_initializer= , activation= , name= ) IDT IDT IDT self.axn = , kernel_initializer= , bias_initializer= , activation= , name= ) IDT IDT self.network_params = tf.trainable_variables()[np_index:] + IDT IDT with tf.name_scope('actor_gradient'): + IDT IDT IDT self.unnormalized_actor_gradients = tf.gradients(self.axn, self.network_params, -self.grads) + IDT IDT with tf.name_scope('actor_train'): + IDT IDT IDT self.train_op = tf.train.AdamOptimizer(self.lr).apply_gradients(zip(self.unnormalized_actor_gradients, self.network_params)) + IDT IDT with tf.name_scope('init'): + IDT IDT IDT self.init = tf.global_variables_initializer() + + IDT def set_session(self, sess): + IDT IDT self.session = sess + IDT IDT self.session.run(self.init) + + IDT def model_session(self): + IDT IDT return self.session + + IDT def get_action(self, obs): + IDT IDT axn = , feed_dict= ) IDT IDT return axn + + IDT def update_weight(self, obs, grads): + IDT IDT feed_dict = {self.features: obs, self.grads: grads} + IDT IDT _ = self.session.run([self.train_op], feed_dict) + IDT IDT return + + IDT def get_weights(self): + IDT IDT return self.network_params +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +import os +import matplotlib.pyplot as plt +from skimage import io +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = , encoding= ) IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +plt.imshow(test_dataset[233], cmap= )image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +num_nodes_1 = 3048 +num_nodes_2 = 2048 +num_nodes_3 = 1024 +num_nodes_4 = 500 +batch_size = 250 +hidden_layer_1_keep_prob = 0.5 +hidden_layer_2_keep_prob = 0.6 +hidden_layer_3_keep_prob = 0.7 +hidden_layer_4_keep_prob = 0.8 +beta_1 = 1e-05 +beta_2 = 1e-05 +beta_3 = 1e-05 +beta_4 = 1e-05 +hidden_layer_1_stddev = np.sqrt(5.5 / 784) +hidden_layer_2_stddev = np.sqrt(3.5 / (num_nodes_1 + 784)) +hidden_layer_3_stddev = np.sqrt(2.5 / (num_nodes_2 + num_nodes_1)) +hidden_layer_4_stddev = np.sqrt(1.5 / (num_nodes_3 + num_nodes_2)) +output_layer_stddev = np.sqrt(0.5 / (num_nodes_4 + num_nodes_3)) + +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size), name= ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_1 = , num_nodes_1], stddev= ) IDT biases_1 = tf.Variable(tf.zeros([num_nodes_1])) + IDT weights_2 = , num_nodes_2], stddev= ) IDT biases_2 = tf.Variable(tf.zeros([num_nodes_2])) + IDT weights_3 = , num_nodes_3], stddev= ) IDT biases_3 = tf.Variable(tf.zeros([num_nodes_3])) + IDT weights_4 = , num_nodes_4], stddev= ) IDT biases_4 = tf.Variable(tf.zeros([num_nodes_4])) + IDT weights_5 = , num_labels], stddev= ) IDT biases_5 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT relu_layer = tf.nn.dropout(tf.nn.relu(logits_1), hidden_layer_1_keep_prob) + IDT logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + IDT relu_layer_2 = tf.nn.dropout(tf.nn.relu(logits_2), hidden_layer_2_keep_prob) + IDT logits_3 = tf.matmul(relu_layer_2, weights_3) + biases_3 + IDT relu_layer_3 = tf.nn.dropout(tf.nn.relu(logits_3), hidden_layer_3_keep_prob) + IDT logits_4 = tf.matmul(relu_layer_3, weights_4) + biases_4 + IDT relu_layer_4 = tf.nn.dropout(tf.nn.relu(logits_4), hidden_layer_4_keep_prob) + IDT out = tf.matmul(relu_layer_4, weights_5) + biases_5 + IDT loss = , labels= ) IDT global_step = , trainable= ) IDT starter_learning_rate = 0.3 + IDT learning_rate = , global_step, 100000, 0.96, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = , name= ) IDT validation_hidden_layer_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1) + IDT validation_hidden_layer_2 = tf.nn.relu(tf.matmul(validation_hidden_layer_1, weights_2) + biases_2) + IDT validation_hidden_layer_3 = tf.nn.relu(tf.matmul(validation_hidden_layer_2, weights_3) + biases_3) + IDT validation_hidden_layer_4 = tf.nn.relu(tf.matmul(validation_hidden_layer_3, weights_4) + biases_4) + IDT validation_logits = tf.matmul(validation_hidden_layer_4, weights_5) + biases_5 + IDT valid_prediction = tf.nn.softmax(validation_logits) + IDT test_hidden_layer_1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1) + IDT test_hidden_layer_2 = tf.nn.relu(tf.matmul(test_hidden_layer_1, weights_2) + biases_2) + IDT test_hidden_layer_3 = tf.nn.relu(tf.matmul(test_hidden_layer_2, weights_3) + biases_3) + IDT test_hidden_layer_4 = tf.nn.relu(tf.matmul(test_hidden_layer_3, weights_4) + biases_4) + IDT test_logits = tf.matmul(test_hidden_layer_4, weights_5) + biases_5 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 20000 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import pandas as pd +import numpy as np +import pickle +import matplotlib.pyplot as plt +from scipy import stats +import tensorflow as tf +import seaborn as sns +from pylab import rcParams +from sklearn import metrics +from sklearn.model_selection import train_test_split +import tensorflow as tf +import pandas as pd +import numpy as np +from tensorflow.contrib import rnn +from sklearn.model_selection import train_test_split +from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score +"""' + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = )outputs, states = , X, dtype= )init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = )""" + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +n_steps = 1 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = )outputs, states = , X, dtype= )init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]], [[9, 0, 1]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = )""" + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 1 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = )outputs, states = , X, dtype= )init = tf.global_variables_initializer() +X_batch = np.array([[[0, 8, 6], [4, 6, 7]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = )""" + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 1 +reset_graph() +X = tf.placeholder(tf.float32, [5, 1, 3]) +basic_cell = )outputs, states = , X, dtype= )init = tf.global_variables_initializer() +X_batch = np.array([[[0, 8, 6]], [[0, 8, 6]], [[0, 8, 6]], [[0, 8, 6]], [[0, 8, 6]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = )""" + + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 1 +reset_graph() +X = tf.placeholder(tf.float32, [None, 1, 7]) +basic_cell = )lstm_cell = , state_is_tuple= )outputs, states = , X, dtype= )init = tf.global_variables_initializer() +X_batch = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = )""" + + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +reset_graph() +X = tf.placeholder(tf.float32, [None, 1, 7]) +lstm_cell = , state_is_tuple= )cells = , state_is_tuple= )init_state = cells.zero_state(1, tf.float32) +outputs, states = , X, initial_state= , dtype= )init = tf.global_variables_initializer() +X_batch = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) +with tf.Session() as sess: + IDT init.run() + IDT outputs_val = )""" + + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +reset_graph() +X = , [None, 1, 7], name= )y = , [None, 1, 7], name= )lstm_cell = , state_is_tuple= )cells = , state_is_tuple= )init_state = cells.zero_state(1, tf.float32) +outputs, states = , X, initial_state= , dtype= )outputs = tf.transpose(outputs, [1, 0, 2]) +last = tf.gather(outputs, int(outputs.get_shape()[0]) - 1) +weight = , initializer= , int(y.get_shape()[1])])) )bias = , initializer= , shape= )prediction = tf.nn.softmax(tf.matmul(last, weight) + bias) +cross_entropy = -tf.reduce_sum(y * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0))) +optimizer = tf.train.AdamOptimizer() +minimize = optimizer.minimize(cross_entropy) +mistakes = tf.not_equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) +error = tf.reduce_mean(tf.cast(mistakes, tf.float32)) +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT init.run() + IDT X_train = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + IDT y_train = np.array([[[1535.0, -335353.68, -55.04, 2.32, -550.1531, -155.1523, 0.111]]]) + IDT X_test = np.array([[[41.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + IDT y_test = np.array([[[14535.0, -335353.68, -55.04, 2.32, -550.1531, -155.1523, 0.111]]]) + IDT + IDT outputs_val = , y: y_train}) ) IDT minimize_val = , feed_dict= , y: y_train}) ) IDT incorrect_val = , feed_dict= , y: y_test}) ) IDT prediction_val = , feed_dict= , y: y_test}) )""" +2do : seq_lens checks IDT gather IDT loops change the data shape +""" +""" + + +""" + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() +X = , [None, 1, 7], name= )y = , [None, 1, 7], name= )lstm_cell = , state_is_tuple= )cells = , state_is_tuple= )init_state = cells.zero_state(1, tf.float32) +outputs, states = , X, initial_state= , dtype= )outputs = tf.transpose(outputs, [1, 0, 2]) +X_train = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + +sess = tf.Session() + +used = sess.run(tf.sign(tf.reduce_max(tf.abs(X_train), 2))) + +length = tf.reduce_sum(used, 1) +length = sess.run(tf.cast(length, tf.int32)) + + +batch_size = sess.run(tf.shape(outputs)[0]) + + +max_length = sess.run(tf.shape(outputs)[1]) + + +out_size = int(outputs.get_shape()[2]) + + +index = tf.range(0, batch_size) * max_length + (length - 1) +flat = tf.reshape(outputs, [-1, out_size]) +relevant = tf.gather(flat, index) +out_size = int(outputs.get_shape()[2]) +index = tf.range(0, 1) * 2 +flat = tf.reshape(outputs, [-1, out_size]) +relevant = tf.gather(flat, index) +sess.close() + +weight = , initializer= , int(y.get_shape()[1])])) )bias = , initializer= , shape= )prediction = tf.nn.softmax(tf.matmul(relevant, weight) + bias) +cross_entropy = -tf.reduce_sum(y * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0))) +optimizer = tf.train.AdamOptimizer() +minimize = optimizer.minimize(cross_entropy) +mistakes = tf.not_equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) +error = tf.reduce_mean(tf.cast(mistakes, tf.float32)) +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT init.run() + IDT X_train = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + IDT y_train = np.array([[[1, 1, 1, 1, 1, 1, 1]]]) + IDT X_test = np.array([[[41.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + IDT y_test = np.array([[[0, 0, 0, 0, 0, 0, 0]]]) + IDT + IDT + IDT + IDT + IDT outputs_val = , y: y_train}) ) IDT + IDT + IDT minimize_val = , feed_dict= , y: y_train}) ) IDT incorrect_val = , feed_dict= , y: y_test}) ) IDT + IDT + IDT prediction_val = , feed_dict= , y: y_test}) ) IDT +""" +2do : seq_lens checks IDT gather loops understand the prediction matrix label shapes tensorboarc +""" +""" + + +""" +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +ni = 3 +nn = 5 +X0 = tf.placeholder(tf.float32, [None, ni]) +X1 = tf.placeholder(tf.float32, [None, ni]) +Wx = , nn], dtype= )Wy = , nn], dtype= )b = , nn], dtype= )Y0 = tf.tanh(tf.matmul(X0, Wx) + b) +Y1 = tf.tanh(tf.matmul(Y0, Wy) + tf.matmul(X1, Wx) + b) +init = tf.global_variables_initializer() +X0_batch = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 1]]) +X1_batch = np.array([[9, 8, 7], [0, 0, 0], [6, 5, 4], [3, 2, 1]]) + +with tf.Session() as sess: + IDT init.run() + IDT Y0_val, Y1_val = , Y1], feed_dict= , X1: X1_batch}) ) IDT + IDT + +tf.reset_default_graph() +X0 = tf.placeholder(tf.float32, [None, ni]) +X1 = tf.placeholder(tf.float32, [None, ni]) +basic_cell = )output_seqs, states = , [X0, X1], dtype= )Y0, Y1 = output_seqs +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT init.run() + IDT Y0_val, Y1_val = , Y1], feed_dict= , X1: X1_batch}) ) IDT + IDT + +tf.reset_default_graph() +n_steps = 2 +X = tf.placeholder(tf.float32, [None, n_steps, ni]) +X_seqs = , perm= , 0, 2])) )basic_cell1 = )output_seqs, states = , X_seqs, dtype= )outputs = , perm= , 0, 2]) )X_batch = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 0, 1]], [[9, 8, 7], [0, 0, 0]], [[6, 5, 4], [3, 2, 1]]]) +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT init.run() + IDT output_val = , feed_dict= ) IDT +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net, n_layers, n_fc_layers): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k, n_layers, n_fc_layers) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import time +from tensorflow.examples.tutorials.mnist import input_data +RESTORE_CHECKPOINT = False +x = , shape= , 784], name= )x_ = tf.reshape(x, [-1, 28, 28, 1]) +y = , shape= , 10], name= )is_training = tf.placeholder(tf.bool) +with tf.name_scope('conv1'): + IDT W1 = , 5, 1, 4], 0, 1), name= ) IDT b1 = , tf.float32, [4]), name= ) IDT l1 = tf.nn.lrn(tf.nn.relu(tf.nn.conv2d(x_, W1, [1, 1, 1, 1], 'SAME') + b1)) +with tf.name_scope('conv2'): + IDT W2 = , 5, 4, 8], 0, 1), name= ) IDT b2 = , tf.float32, [8]), name= ) IDT l2 = tf.nn.lrn(tf.nn.relu(tf.nn.conv2d(l1, W2, [1, 2, 2, 1], 'SAME') + b2)) +with tf.name_scope('conv3'): + IDT W3 = , 4, 8, 12], 0, 1), name= ) IDT b3 = , tf.float32, [12]), name= ) IDT l3 = tf.nn.lrn(tf.nn.relu(tf.nn.conv2d(l2, W3, [1, 2, 2, 1], 'SAME') + b3)) +with tf.name_scope('dense1'): + IDT l3_ = tf.reshape(l3, [-1, 7 * 7 * 12]) + IDT W4 = , 200], 0, 1), name= ) IDT b4 = , tf.float32, [200]), name= ) IDT l4 = tf.nn.relu(tf.matmul(l3_, W4) + b4) + IDT l4 = , rate= , training= )with tf.name_scope('out'): + IDT W5 = , 10], 0, 1), name= ) IDT b5 = , tf.float32, [10]), name= ) IDT l5 = tf.matmul(l4, W5) + b5 +out = tf.nn.softmax(l5) +with tf.name_scope('loss'): + IDT loss = , logits= ) IDT train_loss_stat = tf.summary.scalar('Train loss', loss) + IDT test_loss_stat = tf.summary.scalar('Test loss', loss) +with tf.name_scope('accuracy'): + IDT correct_predictions = tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(y, 1)), 'float') + IDT class_accuracy_op = tf.cast(tf.reduce_mean(correct_predictions), 'float') + IDT train_accuracy_stat = tf.summary.scalar('Train accuracy', class_accuracy_op) + IDT test_accuracy_stat = tf.summary.scalar('Test accuracy', class_accuracy_op) +train_step = tf.train.AdamOptimizer(0.01).minimize(loss) +train_stats = tf.summary.merge([train_loss_stat, train_accuracy_stat]) +test_stats = tf.summary.merge([test_loss_stat, test_accuracy_stat]) +batch_size = 100 +epochs = 200 +num_step = 0 +last_epoch = 0 +ckpt_path = 'models/conv.ckpt' +init = tf.global_variables_initializer() +session = tf.Session() +saver = tf.train.Saver() +if RESTORE_CHECKPOINT and tf.train.checkpoint_exists(ckpt_path): + IDT saver.restore(session, ckpt_path) +else: + IDT session.run(init) +log_dir = time.strftime('logs/conv/%Y-%m-%d-%H:%M:%S', time.gmtime()) +summary_writer = tf.summary.FileWriter(log_dir, session.graph) +mnist = , one_hot= )while mnist.train.epochs_completed < epochs: + IDT batch_xs, batch_ys = mnist.train.next_batch(batch_size) + IDT session.run(train_step, feed_dict= , y: batch_ys, is_training: True}) ) IDT num_step += 1 + IDT if num_step % 200 == 0: + IDT IDT tb_test = , feed_dict= , y: mnist.test.labels, is_training: False}) ) IDT IDT summary_writer.add_summary(tb_test, global_step= ) IDT IDT tb_train = , feed_dict= , y: mnist.train.labels, is_training: False}) ) IDT IDT summary_writer.add_summary(tb_train, global_step= ) IDT if last_epoch != mnist.train.epochs_completed: + IDT IDT last_epoch = mnist.train.epochs_completed + IDT IDT saver.save(session, ckpt_path) + IDT IDT train_loss, train_accuracy = , class_accuracy_op], feed_dict= , y: mnist.train.labels, is_training: False}) ) IDT IDT test_loss, test_accuracy = , class_accuracy_op], feed_dict= , y: mnist.test.labels, is_training: False}) ) IDT IDT ) +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +""" +Reformat into a shape that's more adapted to the models we're going to train: + + IDT data as a flat matrix, + IDT labels as float 1-hot encodings. + +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +""" +Problem 1 + +Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor t using nn.l2_loss(t). The right amount of regularization should improve your validation / test accuracy. +""" +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y1 = tf.nn.dropout(y1, 0.5) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT global_step = tf.Variable(0) + IDT W1 = , fc1_size], stddev= ) IDT b1 = tf.Variable(tf.zeros([fc1_size])) + IDT W2 = , fc2_size], stddev= ) IDT b2 = tf.Variable(tf.zeros([fc2_size])) + IDT W3 = , fc3_size], stddev= ) IDT b3 = tf.Variable(tf.zeros([fc3_size])) + IDT W4 = , num_labels], stddev= ) IDT b4 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + IDT y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + IDT logits = tf.matmul(y3, W4) + b4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + IDT learning_rate = , global_step, 1000, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + IDT y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + IDT valid_logits = tf.matmul(y3_valid, W4) + b4 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + IDT y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + IDT test_logits = tf.matmul(y3_test, W4) + b4 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) +, test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import input_data +import cv2 +import numpy as np +import math +from scipy import ndimage +import os + + +def train(iterations): + IDT mnist = , one_hot= ) IDT x_input = , [None, 784], name= ) IDT y_target = , [None, 10], name= ) IDT x_input_image = tf.reshape(x_input, [-1, 28, 28, 1]) + IDT init_random_dist_1 = , 6, 1, 32], stddev= ) IDT W_layer_1 = , shape= , 6, 1, 32], initializer= ) IDT b_layer_1 = , shape= , initializer= ) IDT convolution_layaer_1 = , W_layer_1, strides= , 1, 1, 1], padding= ) IDT convolution_pooling_1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT init_random_dist_2 = , 6, 32, 64], stddev= ) IDT W_layer_2 = , shape= , 6, 32, 64], initializer= ) IDT b_layer_2 = , shape= , initializer= ) IDT convolution_layaer_2 = , W_layer_2, strides= , 1, 1, 1], padding= ) IDT convolution_pooling_2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT covolution_layer_2_flat = tf.reshape(convolution_pooling_2, [-1, 7 * 7 * 64]) + IDT init_random_dist_fully_layer = , 1024], stddev= ) IDT W_fully_layer = , shape= , 1024], initializer= ) IDT b_fully_layer = , shape= , initializer= ) IDT fully_connected_layer = tf.nn.relu(tf.matmul(covolution_layer_2_flat, W_fully_layer) + b_fully_layer) + IDT hold_prob = tf.placeholder(tf.float32) + IDT fully_connected_layer_dropout = , keep_prob= ) IDT init_random_dist_final = , 10], stddev= ) IDT W_final = , shape= , 10], initializer= ) IDT b_final = , shape= , initializer= ) IDT y_prediction = tf.nn.relu(tf.matmul(fully_connected_layer_dropout, W_final) + b_final) + IDT cross_entropy = , logits= ) IDT optimizer = ) IDT train = optimizer.minimize(cross_entropy) + IDT saver = ) IDT config = ) IDT session = ) IDT with session: + IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT for i in range(iterations): + IDT IDT IDT batch_xs, batch_ys = mnist.train.next_batch(100) + IDT IDT IDT session.run(train, feed_dict= , y_target: batch_ys, hold_prob: 0.2}) ) IDT IDT IDT correct_prediction = tf.equal(tf.argmax(y_prediction, 1), tf.argmax(y_target, 1)) + IDT IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + IDT IDT IDT if i % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT saver.save(session, './model') + + +def make_prediction(image_1): + IDT + IDT tf.reset_default_graph() + IDT x_input = , [None, 784], name= ) IDT y_target = , [None, 10], name= ) IDT x_input_image = tf.reshape(x_input, [-1, 28, 28, 1]) + IDT init_random_dist_1 = , 6, 1, 32], stddev= ) IDT W_layer_1 = , shape= , 6, 1, 32], initializer= ) IDT b_layer_1 = , shape= , initializer= ) IDT convolution_layaer_1 = , W_layer_1, strides= , 1, 1, 1], padding= ) IDT convolution_pooling_1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT init_random_dist_2 = , 6, 32, 64], stddev= ) IDT W_layer_2 = , shape= , 6, 32, 64], initializer= ) IDT b_layer_2 = , shape= , initializer= ) IDT convolution_layaer_2 = , W_layer_2, strides= , 1, 1, 1], padding= ) IDT convolution_pooling_2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT covolution_layer_2_flat = tf.reshape(convolution_pooling_2, [-1, 7 * 7 * 64]) + IDT init_random_dist_fully_layer = , 1024], stddev= ) IDT W_fully_layer = , shape= , 1024], initializer= ) IDT b_fully_layer = , shape= , initializer= ) IDT fully_connected_layer = tf.nn.relu(tf.matmul(covolution_layer_2_flat, W_fully_layer) + b_fully_layer) + IDT hold_prob = tf.placeholder(tf.float32) + IDT fully_connected_layer_dropout = , keep_prob= ) IDT init_random_dist_final = , 10], stddev= ) IDT W_final = , shape= , 10], initializer= ) IDT b_final = , shape= , initializer= ) IDT y_prediction = tf.nn.relu(tf.matmul(fully_connected_layer_dropout, W_final) + b_final) + IDT cross_entropy = , logits= ) IDT optimizer = ) IDT train = optimizer.minimize(cross_entropy) + IDT flatten = image_1.flatten() / 255.0 + IDT images = np.zeros((1, 784)) + IDT correct_val = np.zeros((1, 10)) + IDT images[0] = flatten + IDT new_saver = tf.train.Saver() + IDT with tf.Session() as sess: + IDT IDT new_saver = tf.train.Saver(tf.all_variables()) + IDT IDT new_saver = tf.train.import_meta_graph('model.meta') + IDT IDT new_saver.restore(sess, 'model') + IDT IDT prediction = tf.argmax(y_prediction, 1) + IDT IDT ) +import sys +import numpy as np +import tensorflow as tf +import cv2 +NUM_CLASSES = 9 +IMAGE_SIZE = 28 +IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE * 3 + + +def inference(images_placeholder, keep_prob): + IDT """ + + IDT : + IDT images_placeholder: inputs()placeholder + IDT keep_prob: dropoutplace_holder + + IDT : + IDT cross_entropy: + IDT """ + + IDT def weight_variable(shape): + IDT IDT initial = , stddev= ) IDT IDT return tf.Variable(initial) + + IDT def bias_variable(shape): + IDT IDT initial = , shape= ) IDT IDT return tf.Variable(initial) + + IDT def conv2d(x, W): + IDT IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + IDT def max_pool_2x2(x): + IDT IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT x_image = tf.reshape(images_placeholder, [-1, IMAGE_SIZE, IMAGE_SIZE, 3]) + IDT with tf.name_scope('conv1') as scope: + IDT IDT W_conv1 = weight_variable([5, 5, 3, 32]) + IDT IDT b_conv1 = bias_variable([32]) + IDT IDT h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + IDT with tf.name_scope('pool1') as scope: + IDT IDT h_pool1 = max_pool_2x2(h_conv1) + IDT with tf.name_scope('conv2') as scope: + IDT IDT W_conv2 = weight_variable([5, 5, 32, 64]) + IDT IDT b_conv2 = bias_variable([64]) + IDT IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + IDT with tf.name_scope('pool2') as scope: + IDT IDT h_pool2 = max_pool_2x2(h_conv2) + IDT with tf.name_scope('fc1') as scope: + IDT IDT W_fc1 = weight_variable([7 * 7 * 64, 1024]) + IDT IDT b_fc1 = bias_variable([1024]) + IDT IDT h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + IDT IDT h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + IDT IDT h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + IDT with tf.name_scope('fc2') as scope: + IDT IDT W_fc2 = weight_variable([1024, NUM_CLASSES]) + IDT IDT b_fc2 = bias_variable([NUM_CLASSES]) + IDT with tf.name_scope('softmax') as scope: + IDT IDT y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) + IDT return y_conv + + +if __name__ == '__main__': + IDT test_image = [] + IDT for i in range(1, len(sys.argv)): + IDT IDT img = cv2.imread(sys.argv[i]) + IDT IDT img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE)) + IDT IDT test_image.append(img.flatten().astype(np.float32) / 255.0) + IDT test_image = np.asarray(test_image) + IDT images_placeholder = , shape= , IMAGE_PIXELS)) ) IDT labels_placeholder = , shape= , NUM_CLASSES)) ) IDT keep_prob = tf.placeholder('float') + IDT logits = inference(images_placeholder, keep_prob) + IDT session = tf.InteractiveSession() + IDT saver = tf.train.Saver() + IDT session.run(tf.global_variables_initializer()) + IDT saver.restore(session, 'model.ckpt') + IDT for i in range(len(test_image)): + IDT IDT pred = , keep_prob: 1.0})[0]) ) IDT IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Fri Jun 16 17:42:27 2017 + +@author: kashu +""" +import os +import numpy as np +import pandas as pd +import tensorflow as tf +filenameTrain = 'all_train.csv' +filenameTest = 'all_test.csv' +data_root = 'C:/MASTERS/AdvancedML/Exercise/all_massData' +dest_filenameTrain = os.path.join(data_root, filenameTrain) +dest_filenameTest = os.path.join(data_root, filenameTest) +dataTrain = pd.read_csv(dest_filenameTrain) +dataTest = pd.read_csv(dest_filenameTest) +completeDataTrain = , dtype= )completeDataTest = , dtype= )lablesTrain = completeDataTrain[:, 0:1] +completeDataTrain = completeDataTrain[:, 1:29] +lablesTest = completeDataTest[:, 0:1] +completeDataTest = completeDataTest[:, 1:29] +num_labels = 1 +num_features = 28 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(predictions = ) + +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(completeDataTrain[:train_subset, :]) + IDT tf_train_labels = tf.constant(lablesTrain[:train_subset]) + IDT tf_valid_dataset = tf.constant(completeDataTrain[train_subset:20000, :]) + IDT tf_test_dataset = tf.constant(completeDataTest[:train_subset, :]) + IDT valid_labels = lablesTrain[train_subset:20000] + IDT test_labels = lablesTest[:train_subset] + IDT weights = , num_labels], mean= , stddev= , dtype= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.sigmoid(logits) + IDT valid_prediction = tf.nn.sigmoid(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.sigmoid(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 10001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +twoThird = int(2 / 3 * completeDataTrain.shape[0]) +trainingData = completeDataTrain[:twoThird] +trainingLabels = lablesTrain[:twoThird] +validData = completeDataTrain[twoThird:] +validLabels = lablesTrain[twoThird:] +del dest_filenameTest, dest_filenameTrain, filenameTest, filenameTrain +del dataTest, dataTrain, data_root, lablesTrain, completeDataTrain +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , num_features)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(validData) + IDT tf_test_dataset = tf.constant(completeDataTest) + IDT weights = , num_labels], mean= , stddev= , dtype= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) + IDT train_prediction = tf.nn.sigmoid(logits) + IDT valid_prediction = tf.nn.sigmoid(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.sigmoid(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 5500001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (trainingLabels.shape[0] - batch_size) + IDT IDT batch_data = trainingData[offset:offset + batch_size, :] + IDT IDT batch_labels = trainingLabels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , validLabels)) + IDT , lablesTest)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , num_features)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(validData) + IDT tf_test_dataset = tf.constant(completeDataTest) + IDT weights = , num_labels], mean= , stddev= , dtype= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.AdamOptimizer(0.05).minimize(loss) + IDT train_prediction = tf.nn.sigmoid(logits) + IDT valid_prediction = tf.nn.sigmoid(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.sigmoid(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 2000001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (trainingLabels.shape[0] - batch_size) + IDT IDT batch_data = trainingData[offset:offset + batch_size, :] + IDT IDT batch_labels = trainingLabels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , validLabels)) + IDT , lablesTest)) +twoThird = int(2 / 3 * completeDataTrain.shape[0]) +trainingData = completeDataTrain[:5000] +trainingLabels = lablesTrain[:5000] +validData = completeDataTrain[twoThird:twoThird + 1000] +validLabels = lablesTrain[twoThird:twoThird + 1000] +testData = completeDataTest[:1000] +testLabels = lablesTest[:1000] +del dest_filenameTest, dest_filenameTrain, filenameTest, filenameTrain +del dataTest, dataTrain, data_root, lablesTrain, completeDataTrain, completeDataTest, lablesTest +num_nodes = 3 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , num_features)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(validData) + IDT tf_test_dataset = tf.constant(testData) + IDT weights_1 = tf.Variable(tf.zeros([num_features, num_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([num_nodes])) + IDT weights_2 = tf.Variable(tf.zeros([num_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT sigmoid_layer = tf.nn.sigmoid(logits_1) + IDT logits_2 = tf.matmul(sigmoid_layer, weights_2) + biases_2 + IDT loss = , logits= ) IDT optimizer = tf.train.AdadeltaOptimizer(0.05).minimize(loss) + IDT train_prediction = tf.nn.sigmoid(logits_2) + IDT logits_1 = tf.matmul(tf_valid_dataset, weights_1) + biases_1 + IDT sigmoid_layer = tf.nn.sigmoid(logits_1) + IDT logits_2 = tf.matmul(sigmoid_layer, weights_2) + biases_2 + IDT valid_prediction = tf.nn.sigmoid(logits_2) + IDT logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + IDT sigmoid_layer = tf.nn.sigmoid(logits_1) + IDT logits_2 = tf.matmul(sigmoid_layer, weights_2) + biases_2 + IDT test_prediction = tf.nn.sigmoid(logits_2) +num_steps = 300000 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (trainingLabels.shape[0] - batch_size) + IDT IDT batch_data = trainingData[offset:offset + batch_size, :] + IDT IDT batch_labels = trainingLabels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 1000 == 0: + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , validLabels)) + IDT , testLabels)) +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT data_do = , keep_prob= ) IDT output1 = tf.matmul(data_do, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 28 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 2001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT data_do = , keep_prob= ) IDT output1 = tf.matmul(data_do, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 28 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 2001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +from tensorflow.examples.tutorials.mnist import input_data + + +def add_layer(inputs, in_size, out_size, activation_function= ) IDT Weights = tf.Variable(tf.random_normal([in_size, out_size])) + IDT biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) + IDT Wx_plus_b = tf.matmul(inputs, Weights) + biases + IDT if activation_function is None: + IDT IDT outputs = Wx_plus_b + IDT else: + IDT IDT outputs = activation_function(Wx_plus_b) + IDT return outputs + + +def compute_accuracy(v_xs, v_ys): + IDT global prediction + IDT y_pre = , feed_dict= , keep_prob: 1}) ) IDT correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT result = , feed_dict= , ys: v_ys, keep_prob: 1}) ) IDT return result + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, filter): + IDT return tf.nn.conv2d(x, filter, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +mnist = , one_hot= )xs = tf.placeholder(tf.float32, [None, 28 * 28]) / 255.0 +ys = tf.placeholder(tf.float32, [None, 10]) +keep_prob = tf.placeholder(tf.float32) +x_image = tf.reshape(xs, [-1, 28, 28, 1]) +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +cross_entropy = , reduction_indices= )train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) +session = tf.Session() +if int(tf.VERSION.split('.')[1]) < 12 and int(tf.VERSION.split('.')[0]) < 1: + IDT init = tf.initialize_all_variables() +else: + IDT init = tf.global_variables_initializer() +session.run(init) +for i in range(500): + IDT batch_xs, batch_ys = mnist.train.next_batch(100) + IDT session.run(train_step, feed_dict= , ys: batch_ys, keep_prob: 0.5}) ) IDT if i % 50 == 0: + IDT IDT ) +_W_conv1 = W_conv1.eval(session) +_b_conv1 = b_conv1.eval(session) +_W_conv2 = W_conv2.eval(session) +_b_conv2 = b_conv2.eval(session) +_W_fc1 = W_fc1.eval(session) +_b_fc1 = b_fc1.eval(session) +_W_fc2 = W_fc2.eval(session) +_b_fc2 = b_fc2.eval(session) +session.close() +g_2 = tf.Graph() +with g_2.as_default(): + IDT x_export = , [None, 28 * 28], name= ) IDT x_image_export = tf.reshape(x_export, [-1, 28, 28, 1]) + IDT W_conv1_export = , name= ) IDT b_conv1_export = , name= ) IDT h_conv1_export = tf.nn.relu(conv2d(x_image_export, W_conv1_export) + b_conv1_export) + IDT h_pool1_export = max_pool_2x2(h_conv1_export) + IDT W_conv2_export = , name= ) IDT b_conv2_export = , name= ) IDT h_conv2_export = tf.nn.relu(conv2d(h_pool1_export, W_conv2_export) + b_conv2_export) + IDT h_pool2_export = max_pool_2x2(h_conv2_export) + IDT W_fc1_export = , name= ) IDT b_fc1_export = , name= ) IDT h_pool2_flat_export = tf.reshape(h_pool2_export, [-1, 7 * 7 * 64]) + IDT h_fc1_export = tf.nn.relu(tf.matmul(h_pool2_flat_export, W_fc1_export) + b_fc1_export) + IDT W_fc2_export = , name= ) IDT b_fc2_export = , name= ) IDT prediction = , W_fc2_export) + b_fc2_export, name= ) IDT sess_2 = tf.Session() + IDT sess_2.run(tf.global_variables_initializer()) + IDT graph_def = g_2.as_graph_def() + IDT tf.train.write_graph(graph_def, './model/beginner-export', 'beginner-graph-CNN.pb', as_text= ) IDT sess_2.close() + IDT +import tensorflow as tf +import numpy as np +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT self.rewards = , shape= ,), name= ) IDT IDT self.indices = , shape= , 2), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + IDT IDT self.select_prob = tf.gather_nd(self.prob, self.indices) + IDT IDT self.result = tf.multiply(self.select_prob, self.rewards) + IDT IDT self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + IDT IDT self.pg_optimization = ) IDT IDT self.optimization = ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.saved_path = './saved_model' + IDT IDT self.count = 0 + + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def policy_train(self, session, inputs, outputs, rewards): + IDT IDT form, pos, deprel = inputs + IDT IDT indices = [] + IDT IDT for aid in range(len(outputs)): + IDT IDT IDT indices.append([aid, outputs[aid]]) + IDT IDT _, cost, indices, prob, res = , self.pg_loss, self.indices, self.prob, self.result], feed_dict= , self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) ) IDT IDT self.count += 1 + IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + IDT def variable_output(self, session): + IDT IDT form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + IDT IDT return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + IDT def save(self, session): + IDT IDT self.saved_path = self.saver.save(session, './saved_model') + + IDT def restore(self, session): + IDT IDT self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + IDT IDT session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + IDT IDT session.run(self.update_op) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +from src.nn.config import NnConfig as config +from src.nn.config import NnConst as const +from src.nn.config import setup_training_environment, load_training_data_set + + +def create_fully_connected_layer(name, in_node, in_size, num_of_neurons, is_output_layer, dropout): + IDT """ + IDT Create fully connected layer's variables and use them to transform in-node + IDT into out-node with optional activation and specified dropout. There is no + IDT need to add activation rescaling for no-dropout case, because tensorflow + IDT rescales outputs with dropout automatically (scales up by 1 / keep_prob). + + IDT :param name: name which will be given to the layer + IDT :param in_node: input tensorflow graph node (vector) + IDT :param in_size: size of the input vector + IDT :param num_of_neurons: number of neurons for created layer + IDT :param is_output_layer: states whether created layer is network's last + IDT IDT IDT IDT IDT IDT IDT layer. In this case no activation function is + IDT IDT IDT IDT IDT IDT IDT used + IDT :param dropout: probability of dropout for this layer (used in training) + IDT :return: + IDT """ + IDT weights = , shape= , num_of_neurons], initializer= ) IDT biases = , shape= , num_of_neurons], initializer= ) IDT out = tf.matmul(in_node, weights) + biases + IDT tf.summary.histogram(weights.op.name, weights) + IDT tf.summary.histogram(biases.op.name, biases) + IDT tf.summary.histogram(out.op.name, out) + IDT if is_output_layer: + IDT IDT return tf.nn.dropout(out, 1 - dropout) + IDT else: + IDT IDT return tf.nn.dropout(tf.maximum(out, out * 0.1), 1 - dropout) + + +def create_graph(in_placeholder, in_size, layer_sizes, labels, learning_rate_placeholder, dropout_placeholder): + IDT """ + IDT Creates graph for basic neural network both for training and teest case. + + IDT :param in_placeholder: placeholder for input data batch + IDT :param in_size: size of input vector + IDT :param layer_sizes: list of neurons count for each layer + IDT :param labels: set of labels used to evaluate network + IDT :param learning_rate_placeholder: placeholder for network run's learning + IDT IDT IDT IDT IDT IDT IDT IDT IDT rate + IDT :param dropout_placeholder: placeholder for network's dropout probability + IDT :return: created out, loss, train and accuracy nodes + IDT """ + IDT previous_in = in_placeholder + IDT previous_in_size = in_size + IDT for layer_size, i in zip(layer_sizes, range(len(layer_sizes))): + IDT IDT previous_in = , previous_in, previous_in_size, layer_size, i = , dropout_placeholder) ) IDT IDT previous_in_size = layer_size + IDT out = tf.nn.softmax(previous_in) + IDT loss = , logits= ) IDT train = tf.train.AdamOptimizer(learning_rate_placeholder).minimize(loss) + IDT accuracy = , axis= , tf.argmax(labels, axis= , tf.float32)) ) IDT return out, loss, train, accuracy + + +def perform_usage_run(run_index, data_set_path, out_path): + IDT """ + IDT Perform single network's forward pass by using best model saved for given + IDT run index. Save results to output file on given path. + + IDT :param run_index: index of run which will be source for used model + IDT :param data_set_path: path to data set file which will be processed + IDT :param out_path: path to file which will contain forward pass results + IDT :return: - + IDT """ + IDT usage_model_path = const.USAGE_MODEL_PATH_PATTERN.format(run_index) + IDT data = np.load(data_set_path) + IDT in_placeholder = , shape= , config.IN_SIZE]) ) IDT labels_placeholder = , shape= , config.NUM_OF_LABELS]) ) IDT learning_rate_placeholder = , shape= ) IDT dropout_placeholder = , shape= ) IDT out, _, _, _ = create_graph(in_placeholder, config.IN_SIZE, config.LAYER_SIZES, labels_placeholder, learning_rate_placeholder, dropout_placeholder) + IDT session = tf.Session() + IDT session.run(tf.global_variables_initializer()) + IDT saver = tf.train.Saver() + IDT saver.restore(session, usage_model_path) + IDT calculated_out = , feed_dict= , dropout_placeholder: config.NO_DROPOUT}) ) IDT np.save(out_path, calculated_out) + + +def train_network(run_index, set_index, mode, learning_rate): + IDT """ + IDT Start/continue basic nn training for given run index and with data set + IDT described by set index. + + IDT :param run_index: + IDT :param set_index: + IDT :param mode: + IDT :param learning_rate: + IDT :return: + IDT """ + IDT data_set_dir, train_logs_dir, validation_logs_dir, checkpoints_dir, best_model_dir = setup_training_environment(const, run_index, set_index, mode) + IDT train_data, train_labels, validation_data, validation_labels, test_data, test_labels = load_training_data_set(data_set_dir) + IDT in_placeholder = , shape= , config.IN_SIZE]) ) IDT labels_placeholder = , shape= , config.NUM_OF_LABELS]) ) IDT learning_rate_placeholder = , shape= ) IDT dropout_placeholder = , shape= ) IDT out, loss, train, accuracy = create_graph(in_placeholder, config.IN_SIZE, config.LAYER_SIZES, labels_placeholder, learning_rate_placeholder, dropout_placeholder) + IDT tf.summary.scalar('loss', loss) + IDT tf.summary.scalar('accuracy', accuracy) + IDT merged_summary = tf.summary.merge_all() + IDT train_logger = tf.summary.FileWriter(train_logs_dir) + IDT validation_logger = tf.summary.FileWriter(validation_logs_dir) + IDT session = tf.Session() + IDT session.run(tf.global_variables_initializer()) + IDT lowest_loss = None + IDT if config.MODE == config.CONTINUE_MODE: + IDT IDT pass + IDT saver = ) IDT for i in range(config.MAX_ITERATIONS): + IDT IDT batch_indices = np.random.permutation(train_data.shape[0]) + IDT IDT batch_indices = batch_indices[:config.TRAIN_BATCH_SIZE] + IDT IDT batch_data = train_data[batch_indices] + IDT IDT batch_labels = train_labels[batch_indices] + IDT IDT summary, _ = , train], feed_dict= , labels_placeholder: batch_labels, learning_rate_placeholder: learning_rate, dropout_placeholder: config.DROPOUT}) ) IDT IDT train_logger.add_summary(summary, i) + IDT IDT if i % config.CHECKPOINT_INTERVAL == 0: + IDT IDT IDT saver.save(session, checkpoints_dir + '/model_checkpoint', i) + IDT IDT if i % config.VALIDATION_INTERVAL == 0: + IDT IDT IDT summary, loss_value, accuracy_value = , loss, accuracy], feed_dict= , labels_placeholder: validation_labels, dropout_placeholder: config.NO_DROPOUT}) ) IDT IDT IDT if lowest_loss is None or loss_value < lowest_loss: + IDT IDT IDT IDT lowest_loss = loss_value + IDT IDT IDT IDT saver.save(session, best_model_dir + '/best_model') + IDT IDT IDT validation_logger.add_summary(summary, i) + IDT IDT IDT ) + IDT IDT if i % config.DECAY_INTERVAL == 0 and i != 0: + IDT IDT IDT learning_rate *= config.LEARNING_RATE_DECAY + IDT saver.save(session, checkpoints_dir + '/model_checkpoint_last') + IDT saver.restore(session, best_model_dir + '/best_model') + IDT loss_value, accuracy_value = , accuracy], feed_dict= , labels_placeholder: test_labels, dropout_placeholder: config.NO_DROPOUT}) ) IDT ) + + +def main(MODE): + IDT """ + IDT Main function of this script. + IDT :param MODE: run mode + IDT :return: - + IDT """ + IDT if MODE == const.USE_MODE: + IDT IDT perform_usage_run(const.RUN_INDEX, const.USAGE_DATA_SET_PATH, const.USAGE_OUT_PATH) + IDT else: + IDT IDT train_network(const.RUN_INDEX, const.SET_INDEX, const.MODE, config.LEARNING_RATE) + + +if __name__ == '__main__': + IDT main(const.USE_MODE) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + + +import tensorflow as tf +import math +from tensorflow.examples.tutorials.mnist import input_data +IMAGE_PIXEL_COUNT = 784 +layer_sizes = [400, 200, 100, 60, 30, 10] +weights = , layer_sizes[0]], stddev= , tf.Variable(tf.truncated_normal([layer_sizes[0], layer_sizes[1]], stddev= , tf.Variable(tf.truncated_normal([layer_sizes[1], layer_sizes[2]], stddev= , tf.Variable(tf.truncated_normal([layer_sizes[2], layer_sizes[3]], stddev= ) IDT ]], stddev= , tf.Variable(tf.truncated_normal([layer_sizes[4], layer_sizes[5]], stddev= )biases = [tf.zeros([layer_sizes[0]]), tf.zeros([layer_sizes[1]]), tf.zeros([layer_sizes[2]]), tf.zeros([layer_sizes[3]]), tf.zeros([layer_sizes[4]]), tf.zeros([layer_sizes[5]])] +learning_rate_placeholder = tf.placeholder(tf.float32) +keep_probability = tf.placeholder(tf.float32) +TRAINING_KEEP_PROBABILITY = 0.75 +TEST_KEEP_PROBABILITY = 1.0 +mnist_data = , reshape= , one_hot= )images = tf.placeholder(tf.float32, [None, 28, 28, 1]) +flattened_images = tf.reshape(images, [-1, IMAGE_PIXEL_COUNT]) +layer1 = tf.nn.relu(tf.matmul(flattened_images, weights[0]) + biases[0]) +layer1_dropout = tf.nn.dropout(layer1, keep_probability) +layer2 = tf.nn.relu(tf.matmul(layer1_dropout, weights[1]) + biases[1]) +layer2_dropout = tf.nn.dropout(layer2, keep_probability) +layer3 = tf.nn.relu(tf.matmul(layer2_dropout, weights[2]) + biases[2]) +layer3_dropout = tf.nn.dropout(layer3, keep_probability) +layer4 = tf.nn.relu(tf.matmul(layer3_dropout, weights[3]) + biases[3]) +layer4_dropout = tf.nn.dropout(layer4, keep_probability) +layer5 = tf.nn.relu(tf.matmul(layer4_dropout, weights[4]) + biases[4]) +layer5_dropout = tf.nn.dropout(layer5, keep_probability) +logits = tf.matmul(layer5_dropout, weights[5]) + biases[5] +layer6 = tf.nn.softmax(logits) +model_answers = tf.placeholder(tf.float32, [None, 10]) +cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, model_answers) +cross_entropy = tf.reduce_mean(cross_entropy) * 100 +is_correct = tf.equal(tf.argmax(layer6, 1), tf.argmax(model_answers, 1)) +accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) +optimizer = tf.train.AdamOptimizer(learning_rate_placeholder) +training_step = optimizer.minimize(cross_entropy) +init = tf.global_variables_initializer() +session = tf.Session() +session.run(init) +training_results = [] +test_results = [] + + +def run_training_step(results, lr): + IDT curr_images, currmodel_answers = mnist_data.train.next_batch(100) + IDT training_data = {images: curr_images, model_answers: currmodel_answers, learning_rate_placeholder: lr, keep_probability: TRAINING_KEEP_PROBABILITY} + IDT session.run(training_step, feed_dict= ) IDT results.append(session.run([accuracy, cross_entropy], feed_dict= ) + +def run_test_step(results, lr): + IDT + IDT results.append(session.run([accuracy, cross_entropy], feed_dict= , model_answers: mnist_data.test.labels, learning_rate_placeholder: lr, keep_probability: TEST_KEEP_PROBABILITY})) ) + +for i in range(10000): + IDT min_learning_rate = 0.0001 + IDT max_learning_rate = 0.004 + IDT decay_speed = 1800.0 + IDT lr = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i / decay_speed) + IDT run_training_step(training_results, lr) + IDT if i % 100 == 0: + IDT IDT run_test_step(test_results, lr) +run_test_step(test_results, lr) +for i, step in enumerate(training_results): + IDT + ': ' + str(step)) +for i, step in enumerate(test_results): + IDT + ': ' + str(step)) +import numpy as np +import tensorflow as tf +import os +import argparse +import struct +import cv2 as cv +from tensorflow.python.tools import optimize_for_inference_lib +from tensorflow.tools.graph_transforms import TransformGraph +np.random.seed(2701) + + +def gen_data(placeholder): + IDT shape = placeholder.shape.as_list() + IDT shape[0] = shape[0] if shape[0] else 1 + IDT return np.random.standard_normal(shape).astype(placeholder.dtype.as_numpy_dtype()) + + +def prepare_for_dnn(sess, graph_def, in_node, out_node, out_graph, dtype, optimize= , quantize= ) IDT graph_def = tf.graph_util.convert_variables_to_constants(sess, graph_def, [out_node]) + IDT if optimize: + IDT IDT graph_def = optimize_for_inference_lib.optimize_for_inference(graph_def, [in_node], [out_node], dtype.as_datatype_enum) + IDT IDT transforms = ) IDT IDT if quantize: + IDT IDT IDT transforms += ) IDT IDT transforms += ['sort_by_execution_order'] + IDT IDT graph_def = TransformGraph(graph_def, [in_node], [out_node], transforms) + IDT with tf.gfile.FastGFile(out_graph, 'wb') as f: + IDT IDT f.write(graph_def.SerializeToString()) + + +tf.reset_default_graph() +tf.Graph().as_default() +tf.set_random_seed(324) +sess = tf.Session() +isTraining = , name= ) + +def writeBlob(data, name): + IDT if data.ndim == 4: + IDT IDT np.save(name + '.npy', data.transpose(0, 3, 1, 2).astype(np.float32)) + IDT elif data.ndim == 5: + IDT IDT np.save(name + '.npy', data.transpose(0, 4, 1, 2, 3).astype(np.float32)) + IDT else: + IDT IDT np.save(name + '.npy', data.astype(np.float32)) + + +def runModel(inp, out, name): + IDT with tf.Session(graph= ) IDT IDT localSession.graph.as_default() + IDT IDT with tf.gfile.FastGFile(name + '_net.pb') as f: + IDT IDT IDT graph_def = tf.GraphDef() + IDT IDT IDT graph_def.ParseFromString(f.read()) + IDT IDT tf.import_graph_def(graph_def, name= ) IDT IDT inputData = gen_data(inp) + IDT IDT outputData = , feed_dict= ) IDT IDT writeBlob(inputData, name + '_in') + IDT IDT writeBlob(outputData, name + '_out') + + +def save(inp, out, name, quantize= , optimize= ) IDT sess.run(tf.global_variables_initializer()) + IDT inputData = gen_data(inp) + IDT outputData = , feed_dict= , isTraining: False}) ) IDT writeBlob(inputData, name + '_in') + IDT writeBlob(outputData, name + '_out') + IDT prepare_for_dnn(sess, sess.graph.as_graph_def(), inp.name[:inp.name.rfind(':')], out.name[:out.name.rfind(':')], name + '_net.pb', inp.dtype, optimize, quantize) + IDT with tf.gfile.FastGFile(name + '_net.pb') as f: + IDT IDT graph_def = tf.GraphDef() + IDT IDT graph_def.ParseFromString(f.read()) + IDT IDT for node in graph_def.node: + IDT IDT IDT if 'value' in node.attr: + IDT IDT IDT IDT halfs = node.attr['value'].tensor.half_val + IDT IDT IDT IDT if not node.attr['value'].tensor.tensor_content and halfs: + IDT IDT IDT IDT IDT node.attr['value'].tensor.tensor_content = struct.pack('H' * len(halfs), *halfs) + IDT IDT IDT IDT IDT node.attr['value'].tensor.ClearField('half_val') + IDT IDT tf.train.write_graph(graph_def, '', name + '_net.pb', as_text= ) + +for dtype, prefix in zip([tf.float32, tf.float16], ['', 'fp16_']): + IDT inp = tf.placeholder(dtype, [1, 6, 5, 3], 'input') + IDT conv = , filters= , kernel_size= , 1], activation= , bias_initializer= ) IDT save(inp, conv, prefix + 'single_conv') + IDT inp = tf.placeholder(dtype, [3, 7, 5, 4], 'input') + IDT conv = , filters= , kernel_size= , 3], padding= , use_bias= ) IDT activation_abs = tf.abs(conv) + IDT save(inp, activation_abs, prefix + 'padding_same') + IDT inp = tf.placeholder(dtype, [2, 4, 6, 5], 'input') + IDT conv = , filters= , kernel_size= , 5], padding= , activation= , bias_initializer= ) IDT save(inp, conv, prefix + 'padding_valid') + IDT inp = tf.placeholder(dtype, [3, 2, 3, 4], 'input') + IDT conv = , filters= , kernel_size= , 1], activation= , bias_initializer= , 1)) ) IDT conv2 = , filters= , kernel_size= , 1], activation= , bias_initializer= ) IDT eltwise_add_mul = (inp * 0.31 + 2 * conv) * conv2 + IDT save(inp, eltwise_add_mul, prefix + 'eltwise_add_mul') + IDT inp = tf.placeholder(dtype, [1, 4, 5, 1], 'input') + IDT conv = , filters= , kernel_size= , 1], padding= ) IDT padded = tf.pad(conv, [[0, 0], [0, 2], [0, 0], [0, 0]]) + IDT merged = , inp], axis= ) IDT save(inp, merged, prefix + 'pad_and_concat') + IDT inp = tf.placeholder(dtype, [1, 6, 6, 2], 'input') + IDT conv = , filters= , kernel_size= , 3], padding= ) IDT pool = , pool_size= , strides= ) IDT save(inp, pool, prefix + 'max_pool_even') + IDT inp = tf.placeholder(dtype, [1, 7, 7, 2], 'input') + IDT conv = , filters= , kernel_size= , 3], padding= ) IDT pool = , pool_size= , strides= , padding= ) IDT save(inp, pool, prefix + 'max_pool_odd_valid') + IDT inp = tf.placeholder(dtype, [1, 7, 7, 2], 'input') + IDT conv = , filters= , kernel_size= , 3], padding= ) IDT relu = tf.nn.relu6(conv * 10) + IDT pool = , pool_size= , strides= , padding= ) IDT save(inp, pool, prefix + 'max_pool_odd_same') + IDT inp = tf.placeholder(dtype, [1, 5, 6, 2], 'input') + IDT deconv_weights = , 3, 4, 2], dtype= , name= ) IDT deconv = , filter= , output_shape= , 9, 8, 4], strides= , 1, 1, 1], padding= ) IDT deconv_bias = , activation_fn= , initializer= ) IDT save(inp, deconv_bias, prefix + 'deconvolution') +inp = tf.placeholder(tf.float32, [2, 5, 4, 3], 'input') +bn = , fused= , is_training= , scale= , param_initializers= , 'gamma': tf.random_normal_initializer(), 'moving_mean': tf.random_uniform_initializer(-2, -1), 'moving_variance': tf.random_uniform_initializer(1, 2)}) )save(inp, bn, 'fused_batch_norm') +inp = tf.placeholder(tf.float32, [2, 5, 6, 3], 'input') +weights = , 3, 3, 4]), name= )conv = , weights, rate= , padding= )relu = tf.nn.relu(conv) +save(inp, relu, 'atrous_conv2d_valid') +inp = tf.placeholder(tf.float32, [2, 5, 10, 3], 'input') +weights = , 5, 3, 4]), name= )conv = , weights, rate= , padding= )relu = tf.nn.relu(conv) +save(inp, relu, 'atrous_conv2d_same') +inp = tf.placeholder(tf.float32, [2, 5, 4, 3], 'input') +bn = , is_training= , scale= , param_initializers= , 'gamma': tf.random_normal_initializer(), 'moving_mean': tf.random_uniform_initializer(-2, -1), 'moving_variance': tf.random_uniform_initializer(1, 2)}) )save(inp, bn, 'batch_norm') +inp = tf.placeholder(tf.float32, [2, 10, 9, 6], 'input') +weights = , 3, 6, 4]), name= )conv = , filter= , strides= , 1, 1, 1], padding= )save(inp, conv, 'depthwise_conv2d') +inp = tf.placeholder(tf.float32, [2, 3], 'input') +biases = , name= )weights = , 4]), name= )mm = tf.matmul(inp, weights) + biases +save(inp, mm, 'matmul') +from tensorflow.python.framework import function + + +@function.Defun(tf.float32, func_name= )def my_dropout(x): + IDT return tf.layers.dropout(x, rate= , training= ) + +inp = tf.placeholder(tf.float32, [1, 10, 10, 3], 'input') +conv = , filters= , kernel_size= , 1]) )dropout = my_dropout(conv) +relu = tf.nn.relu(dropout) +save(inp, relu, 'defun_dropout') +inp = tf.placeholder(tf.float32, [2, 3, 4], 'input') +shift = , 3, 4]), name= )shifted = , shift, name= )reshape = tf.reshape(shifted, [4, 3, 2], 'reshaped') +save(inp, reshape, 'shift_reshape_no_reorder') +inp = tf.placeholder(tf.float32, [1, 2, 3], 'input') +reshape = tf.reshape(inp, [3, 1, 2], 'reshaped') +save(inp, reshape, 'reshape_no_reorder') +inp = tf.placeholder(tf.float32, [2, 10, 10, 3], 'input') +pad = tf.pad(inp, [[0, 0], [3, 3], [3, 3], [0, 0]]) +conv = , filters= , kernel_size= , 5], strides= , 2), bias_initializer= )save(inp, conv, 'spatial_padding') +inp = tf.placeholder(tf.float32, [1, 2, 3], 'input') +bn = tf.add(inp, tf.Variable(tf.random_normal(inp.shape))) +reshape = tf.reshape(bn, [-1, 3]) +save(inp, reshape, 'reshape_reduce') +times = 4 +batch_size = 2 +input_size = 5 * 6 * 3 +output_size = 10 +inp = tf.placeholder(tf.float32, [times, batch_size, input_size], 'input') +lstm_cell = , forget_bias= , cell_clip= , use_peephole= )outputs, state = , dtype= )last_output = tf.slice(outputs, [times - 1, 0, 0], [1, -1, output_size]) +last_output = tf.reshape(last_output, [-1, 10]) +weights = tf.Variable(tf.random_normal([10, 2])) +biases = tf.Variable(tf.random_normal([2])) +sigmoid = tf.nn.sigmoid(tf.matmul(last_output, weights) + biases) +save(inp, sigmoid, 'lstm') +bgr = tf.placeholder(tf.float32, [4, 5, 6, 3], 'input') +b, g, r = , num_or_size_splits= , axis= )rgb = , g, b], axis= )alpha, beta = , num_or_size_splits= , axis= )res = , filters= , kernel_size= , 1]) + tf.layers.conv2d(beta, filters= , kernel_size= , 1]) )save(bgr, res, 'split_equals') +inp = tf.placeholder(tf.float32, [2, 10, 11, 3], 'input') +conv = , filters= , kernel_size= , 1]) )scaled = , size= , 8)) )scaled = , size= , 12)) )save(inp, scaled, 'resize_nearest_neighbor') +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = , training= , fused= , name= , beta_initializer= , gamma_initializer= , moving_mean_initializer= , 1), moving_variance_initializer= , 2)) )save(inp, bn, 'batch_norm_text') +inp = tf.placeholder(tf.float32, [2, 4, 5], 'input') +flatten = tf.contrib.layers.flatten(inp) +save(inp, flatten, 'flatten') +with tf.gfile.FastGFile('../ssd_mobilenet_v1_coco.pb') as f: + IDT graph_def = tf.GraphDef() + IDT graph_def.ParseFromString(f.read()) +with tf.Session(graph= ) IDT localSession.graph.as_default() + IDT tf.import_graph_def(graph_def, name= ) IDT inp = cv.imread('../street.png') + IDT inp = cv.resize(inp, (300, 300)) + IDT inp = inp[:, :, ([2, 1, 0])] + IDT out = localSession.run([localSession.graph.get_tensor_by_name('concat:0'), localSession.graph.get_tensor_by_name('concat_1:0'), localSession.graph.get_tensor_by_name('num_detections:0'), localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0')], feed_dict={ + IDT IDT 'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)}) + IDT np.save('ssd_mobilenet_v1_coco.concat.npy', out[0]) + IDT np.save('ssd_mobilenet_v1_coco.concat_1.npy', out[1]) + IDT num_detections = int(out[2][0]) + IDT detections = np.zeros([1, 1, num_detections, 7], np.float32) + IDT detections[(0), (0), :, (0)] = 0 + IDT detections[(0), (0), :, (1)] = out[5][0] + IDT detections[(0), (0), :, (2)] = out[3][0] + IDT detections[(0), (0), :, 3:] = , ([1, 0, 3, 2]) ) IDT detections = , (0), :, :], cmp= , y: -1 if x[1] < y[1] and x[2] < y[2] else 0) ) IDT np.save('ssd_mobilenet_v1_coco.detection_out.npy', detections) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +conv = , filters= , kernel_size= , 1], activation= , bias_initializer= )flattened = tf.reshape(conv, [1, -1], 'reshaped') +biases = , name= )weights = , 10]), name= )mm = tf.matmul(flattened, weights) + biases +save(inp, mm, 'nhwc_reshape_matmul') +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +conv = , filters= , kernel_size= , 1], activation= , bias_initializer= )transposed = tf.transpose(conv, [0, 1, 2, 3]) +flattened = tf.reshape(transposed, [1, -1], 'reshaped') +biases = , name= )weights = , 10]), name= )mm = tf.matmul(flattened, weights) + biases +save(inp, flattened, 'nhwc_transpose_reshape_matmul') +inp = tf.placeholder(tf.float32, [1, 6, 5, 3], 'input') +conv = , filters= , kernel_size= , 1], activation= , bias_initializer= )save(inp, conv, 'uint8_single_conv', quantize= )runModel(inp, conv, 'uint8_single_conv') +inp = tf.placeholder(tf.float32, [1, 4, 4, 1], 'input') +conv = , filters= , kernel_size= , 3], padding= )pool = , pool_size= , strides= , padding= )save(inp, pool, 'ave_pool_same') +inp = tf.placeholder(tf.float32, [1, 4, 6, 1], 'input') +conv = , filters= , kernel_size= , 1], padding= )sliced = tf.slice(conv, [0, 1, 2, 0], [-1, 3, 4, 1]) +save(inp, sliced, 'slice_4d') +inp = tf.placeholder(tf.float32, [1, 4, 4, 1], 'input') +deconv_weights = , 3, 2, 1], dtype= )deconv = , deconv_weights, output_shape= , 4, 4, 2], strides= , 1, 1, 1], padding= )leakyRelu = , alpha= )save(inp, leakyRelu, 'deconvolution_same') +inp = tf.placeholder(tf.float32, [1, 3, 3, 1], 'input') +deconv_weights = , 3, 2, 1], dtype= )deconv = , deconv_weights, output_shape= , 5, 5, 2], strides= , 2, 2, 1], padding= )save(inp, deconv, 'deconvolution_stride_2_same') +inp = tf.placeholder(tf.float32, [1, 3, 2, 1], 'input') +deconv_weights = , 3, 2, 1], dtype= )deconv = , deconv_weights, output_shape= , 8, 6, 2], strides= , 2, 2, 1], padding= )save(inp, deconv, 'deconvolution_adj_pad_valid') +inp = tf.placeholder(tf.float32, [1, 2, 2, 1], 'input') +deconv_weights = , 3, 1, 1]), dtype= )deconv = , deconv_weights, output_shape= , 4, 4, 1], strides= , 2, 2, 1], padding= )save(inp, deconv, 'deconvolution_adj_pad_same') +inp = tf.placeholder(tf.float32, [1, 3, 4, 5], 'input') +gamma = , dtype= )beta = , dtype= )bn = , gamma, beta, epsilon= , is_training= )save(inp, bn, 'mvn_batch_norm') +inp = tf.placeholder(tf.float32, [1, 1, 1, 5], 'input') +gamma = , dtype= )beta = , dtype= )bn = , gamma, beta, epsilon= , is_training= )save(inp, bn, 'mvn_batch_norm_1x1') +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = , training= , fused= , name= , beta_initializer= , gamma_initializer= , moving_mean_initializer= , 1), moving_variance_initializer= , 2)) )save(inp, bn, 'unfused_batch_norm', optimize= )inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = , training= , fused= , name= , beta_initializer= , scale= , moving_mean_initializer= , 1), moving_variance_initializer= , 2)) )save(inp, bn, 'fused_batch_norm_no_gamma', optimize= )inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = , training= , fused= , name= , beta_initializer= , scale= , moving_mean_initializer= , 1), moving_variance_initializer= , 2)) )save(inp, bn, 'unfused_batch_norm_no_gamma', optimize= )inp = tf.placeholder(tf.float32, [1, 2, 3], 'input') +flatten = tf.contrib.layers.flatten(inp) +save(inp, flatten, 'unfused_flatten', optimize= )inp = tf.placeholder(tf.float32, [None, 2, 3], 'input') +flatten = tf.contrib.layers.flatten(inp) +save(inp, flatten, 'unfused_flatten_unknown_batch', optimize= )inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +relu = , 0.01 * inp, name= )save(inp, relu, 'leaky_relu_order1', optimize= )inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +relu = , inp * 0.01, name= )save(inp, relu, 'leaky_relu_order2', optimize= )inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +relu = , inp, name= )save(inp, relu, 'leaky_relu_order3', optimize= )from tensorflow import keras as K +model = K.models.Sequential() +model.add(K.layers.Softmax(name= , input_shape= , 3, 4))) )sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_softmax_input:0'), sess.graph.get_tensor_by_name('keras_softmax/truediv:0'), 'keras_softmax', optimize= )model = K.models.Sequential() +model.add(K.layers.Conv2D(filters= , kernel_size= , data_format= , name= , input_shape= , 3, 4))) )model.add(K.layers.GlobalAveragePooling2D(name= )model.add(K.layers.Reshape((1, 1, 4), name= )sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_mobilenet_head_conv_input:0'), sess.graph.get_tensor_by_name('keras_mobilenet_head_reshape/Reshape:0'), 'keras_mobilenet_head', optimize= ) + +def keras_relu6(x): + IDT return K.activations.relu(x, max_value= ) + +inp = , 3, 4), name= )relu = , name= )model = K.Model(inp, relu) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_relu6_input:0'), sess.graph.get_tensor_by_name('keras_relu6/clip_by_value:0'), 'keras_relu6', optimize= )inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +reduced = , axis= , 2], keepdims= )save(inp, reduced, 'reduce_mean') +inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +pool = , pool_size= , strides= , padding= )l2norm = , axis= )l2norm = , axis= , 3, 1]) )save(inp, l2norm, 'l2_normalize') +inp = tf.placeholder(tf.float32, [2, 3, 4], 'input') +l2norm = , axis= )l2norm = , axis= )l2norm = , axis= , 1]) )save(inp, l2norm, 'l2_normalize_3d') +model = K.models.Sequential() +model.add(K.layers.Conv2DTranspose(filters= , kernel_size= , strides= , 2), data_format= , name= , input_shape= , 5, 2))) )sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_deconv_valid_input:0'), sess.graph.get_tensor_by_name('keras_deconv_valid/BiasAdd:0'), 'keras_deconv_valid', optimize= )model = K.models.Sequential() +model.add(K.layers.Conv2DTranspose(filters= , kernel_size= , strides= , 2), data_format= , name= , input_shape= , 5, 2), padding= )sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_deconv_same_input:0'), sess.graph.get_tensor_by_name('keras_deconv_same/BiasAdd:0'), 'keras_deconv_same', optimize= )inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +resized = , size= , 8], name= )save(inp, resized, 'resize_bilinear') +inp = tf.placeholder(tf.float32, [None, 3, 4, 5], 'input') +resized = , size= , tf.shape(inp)[2] * 3], name= )sub_add = resized - 0.3 + 0.3 +save(inp, sub_add, 'resize_bilinear_factor', optimize= )model = K.models.Sequential() +model.add(K.layers.SeparableConv2D(filters= , kernel_size= , strides= , 1), dilation_rate= , 3), name= , input_shape= , 12, 2), padding= )sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_atrous_conv2d_same_input:0'), sess.graph.get_tensor_by_name('keras_atrous_conv2d_same/BiasAdd:0'), 'keras_atrous_conv2d_same', optimize= )for name in ['faster_rcnn_inception_v2_coco_2018_01_28', 'faster_rcnn_resnet50_coco_2018_01_28']: + IDT with tf.gfile.FastGFile(os.path.join('..', name + '.pb')) as f: + IDT IDT graph_def = tf.GraphDef() + IDT IDT graph_def.ParseFromString(f.read()) + IDT with tf.Session(graph= ) IDT IDT localSession.graph.as_default() + IDT IDT tf.import_graph_def(graph_def, name= ) IDT IDT inp = cv.imread('../dog416.png') + IDT IDT inp = cv.resize(inp, (800, 600)) + IDT IDT inp = inp[:, :, ([2, 1, 0])] + IDT IDT out = , localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0')], feed_dict= , inp.shape[0], inp.shape[1], 3)}) ) IDT IDT num_detections = int(out[0][0]) + IDT IDT detections = np.zeros([1, 1, num_detections, 7], np.float32) + IDT IDT detections[(0), (0), :, (1)] = ) IDT IDT detections[(0), (0), :, (2)] = ) IDT IDT detections[(0), (0), :, 3:] = , :num_detections, ([1, 0, 3, 2]) ) IDT IDT np.save(name + '.detection_out.npy', detections) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +conv1 = , filters= , kernel_size= , 1]) )conv2 = , filters= , kernel_size= , 1]) )flatten1 = tf.contrib.layers.flatten(conv1) +flatten2 = tf.contrib.layers.flatten(conv2) +concat = , flatten2], axis= )bias = tf.contrib.layers.bias_add(concat) +save(inp, bias, 'concat_axis_1') +inp = tf.placeholder(tf.float32, [1, 3, 5, 8], 'input') +conv = , filters= , kernel_size= , 3], data_format= )pool = , pool_size= , strides= , data_format= )save(inp, pool, 'conv_pool_nchw') +for name in ['conv_pool_nchw_in.npy', 'conv_pool_nchw_out.npy']: + IDT np.save(name, np.load(name).transpose(0, 2, 3, 1)) +model = K.models.Sequential() +model.add(K.layers.UpSampling2D(size= , 2), data_format= , name= , input_shape= , 3, 4))) )sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_upsampling2d_input:0'), sess.graph.get_tensor_by_name('keras_upsampling2d/ResizeNearestNeighbor:0'), 'keras_upsampling2d') +with tf.gfile.FastGFile('../ssd_mobilenet_v1_ppn_coco.pb') as f: + IDT graph_def = tf.GraphDef() + IDT graph_def.ParseFromString(f.read()) +with tf.Session(graph= ) IDT localSession.graph.as_default() + IDT tf.import_graph_def(graph_def, name= ) IDT img = cv.imread('../dog416.png') + IDT inp = cv.resize(img, (300, 300)) + IDT inp = inp[:, :, ([2, 1, 0])] + IDT out = , localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0')], feed_dict= , inp.shape[0], inp.shape[1], 3)}) ) IDT num_detections = int(out[0][0]) + IDT detections = np.zeros([1, 1, num_detections, 7], np.float32) + IDT detections[(0), (0), :, (1)] = ) IDT detections[(0), (0), :, (2)] = ) IDT detections[(0), (0), :, 3:] = , :num_detections, ([1, 0, 3, 2]) ) IDT np.save('ssd_mobilenet_v1_ppn_coco.detection_out.npy', detections) +inp = tf.placeholder(tf.float32, [None, 2, 3], 'input') +flatten = tf.reshape(inp, [-1, 2 * 3], 'planar') +reshaped = tf.reshape(flatten, tf.shape(inp), 'reshape') +save(inp, reshaped, 'reshape_as_shape', optimize= )with tf.gfile.FastGFile('../mask_rcnn_inception_v2_coco_2018_01_28.pb') as f: + IDT graph_def = tf.GraphDef() + IDT graph_def.ParseFromString(f.read()) +with tf.Session(graph= ) IDT localSession.graph.as_default() + IDT tf.import_graph_def(graph_def, name= ) IDT img = cv.imread('../street.png') + IDT inp = cv.resize(img, (800, 800)) + IDT inp = inp[:, :, ([2, 1, 0])] + IDT out = localSession.run([localSession.graph.get_tensor_by_name('num_detections:0'), localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0'), localSession.graph.get_tensor_by_name('detection_masks:0')], feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp. + IDT IDT shape[1], 3)}) + IDT num_detections = int(out[0][0]) + IDT detections = np.zeros([1, 1, num_detections, 7], np.float32) + IDT detections[(0), (0), :, (1)] = ) IDT detections[(0), (0), :, (2)] = ) IDT detections[(0), (0), :, 3:] = , :num_detections, ([1, 0, 3, 2]) ) IDT np.save('mask_rcnn_inception_v2_coco_2018_01_28.detection_out.npy', detections) + IDT np.save('mask_rcnn_inception_v2_coco_2018_01_28.detection_masks.npy', out[4]) +inp = , 3, 4), name= , batch_size= )conv = , kernel_size= , data_format= , name= , input_shape= , 3, 4))(inp) ) + +def pad_depth(x, desired_channels): + IDT y = , low= , high= ) IDT return K.layers.concatenate([x, y]) + + +pad = , arguments= , name= )sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_pad_concat_input:0'), sess.graph.get_tensor_by_name('keras_pad_concat/concatenate/concat:0'), 'keras_pad_concat', optimize= )inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +conv = , filters= , kernel_size= , 1], bias_initializer= )sub = conv - inp +save(inp, sub, 'eltwise_sub') +inp = tf.placeholder(tf.float32, [None, 2, 3, 4], 'input') +conv = , filters= , kernel_size= , 1]) )softmax = tf.contrib.slim.softmax(conv) +save(inp, softmax, 'slim_softmax') +inp_node = 'img_inputs' +out_node = 'MobileFaceNet/MobileFaceNet/Conv2d_0/add' +with tf.Session(graph= ) IDT localSession.graph.as_default() + IDT with tf.gfile.FastGFile('frozen_model.pb') as f: + IDT IDT graph_def = tf.GraphDef() + IDT IDT graph_def.ParseFromString(f.read()) + IDT IDT for node in graph_def.node: + IDT IDT IDT if node.name == inp_node: + IDT IDT IDT IDT del node.attr['shape'] + IDT tf.import_graph_def(graph_def, name= ) IDT inputData = gen_data(tf.placeholder(tf.float32, [1, 4, 5, 3], inp_node)) + IDT outputData = , feed_dict= ) IDT writeBlob(inputData, 'slim_batch_norm_in') + IDT writeBlob(outputData, 'slim_batch_norm_out') + IDT graph_def = TransformGraph(graph_def, [inp_node], [out_node], ['fold_constants', 'strip_unused_nodes']) + IDT with tf.gfile.FastGFile('slim_batch_norm_net.pb', 'wb') as f: + IDT IDT f.write(graph_def.SerializeToString()) +inp_node = 'PNet/conv3/add' +out_node = 'PNet/cls_prob' +with tf.Session(graph= ) IDT localSession.graph.as_default() + IDT with tf.gfile.FastGFile('PNet_pnet.pb') as f: + IDT IDT graph_def = tf.GraphDef() + IDT IDT graph_def.ParseFromString(f.read()) + IDT IDT graph_def = TransformGraph(graph_def, [inp_node], [out_node], ['strip_unused_nodes']) + IDT tf.import_graph_def(graph_def, name= ) IDT inputData = gen_data(tf.placeholder(tf.float32, [1, 4, 5, 16], inp_node)) + IDT outputData = , feed_dict= ) IDT writeBlob(inputData, 'slim_softmax_v2_in') + IDT writeBlob(outputData, 'slim_softmax_v2_out') + IDT with tf.gfile.FastGFile('slim_softmax_v2_net.pb', 'wb') as f: + IDT IDT f.write(graph_def.SerializeToString()) +inp = tf.placeholder(tf.float32, [1, 4, 6, 5, 3], 'input') +conv3d = , filters= , kernel_size= , 3, 4], use_bias= , padding= )save(inp, conv3d, 'conv3d') +inp = tf.placeholder(tf.float32, [1, 4, 6, 5, 3], 'input') +maxpool3d = , pool_size= , 2, 3), strides= , 2, 1), padding= )save(inp, maxpool3d, 'max_pool3d') +inp = tf.placeholder(tf.float32, [1, 5, 4, 5, 2], 'input') +avepool3d = , pool_size= , 3, 2), strides= , 1, 1), padding= )save(inp, avepool3d, 'ave_pool3d') +inp_node = 'input_image' +out_node = 'SUBPIXEL/SUBPIXEL/subpixel_image/Identity' +with tf.Session(graph= ) IDT localSession.graph.as_default() + IDT with tf.gfile.FastGFile('simple_subpixel.optimized.pb') as f: + IDT IDT graph_def = tf.GraphDef() + IDT IDT graph_def.ParseFromString(f.read()) + IDT tf.import_graph_def(graph_def, name= ) IDT inputData = gen_data(tf.placeholder(tf.float32, [1, 1, 1, 4], inp_node)) + IDT outputData = , feed_dict= ) IDT writeBlob(inputData, 'subpixel_in') + IDT writeBlob(outputData, 'subpixel_out') + IDT for node in graph_def.node: + IDT IDT if node.op == 'Placeholder': + IDT IDT IDT node.attr['data_format'].s = 'NHWC' + IDT with tf.gfile.FastGFile('subpixel_net.pb', 'wb') as f: + IDT IDT f.write(graph_def.SerializeToString()) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import os +import tensorflow as tf +from six.moves import urllib +import errno +import os +import zipfile +from collections import Counter +from collections import deque +from sklearn.manifold import TSNE + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +import matplotlib +import matplotlib.pyplot as plt +plt.rcParams['axes.labelsize'] = 14 +plt.rcParams['xtick.labelsize'] = 12 +plt.rcParams['ytick.labelsize'] = 12 +PROJECT_ROOT_DIR = '.' +CHAPTER_ID = 'rnn' + + +def save_fig(fig_id, tight_layout= ) IDT path = os.path.join(PROJECT_ROOT_DIR, 'images', CHAPTER_ID, fig_id + '.png') + IDT + IDT if tight_layout: + IDT IDT plt.tight_layout() + IDT plt.savefig(path, format= , dpi= ) + +reset_graph() +(X_train, y_train), (X_test, y_test) = )X_train = X_train.astype(np.float32).reshape(-1, 28 * 28) / 255.0 +X_test = X_test.astype(np.float32).reshape(-1, 28 * 28) / 255.0 +y_train = y_train.astype(np.int32) +y_test = y_test.astype(np.int32) +X_valid, X_train = X_train[:5000], X_train[5000:] +y_valid, y_train = y_train[:5000], y_train[5000:] + + +def shuffle_batch(X, y, batch_size): + IDT rnd_idx = np.random.permutation(len(X)) + IDT n_batches = len(X) // batch_size + IDT for batch_idx in np.array_split(rnd_idx, n_batches): + IDT IDT X_batch, y_batch = X[batch_idx], y[batch_idx] + IDT IDT yield X_batch, y_batch + + +t_min, t_max = 0, 30 +resolution = 0.1 + + +def time_series(t): + IDT return t * np.sin(t) / 3 + 2 * np.sin(5 * t) + + +def next_batch(batch_size, n_steps): + IDT t0 = np.random.rand(batch_size, 1) * (t_max - t_min - n_steps * resolution) + IDT Ts = t0 + np.arange(0.0, n_steps + 1) * resolution + IDT ys = time_series(Ts) + IDT return ys[:, :-1].reshape(-1, n_steps, 1), ys[:, 1:].reshape(-1, n_steps, 1) + + +t = np.linspace(t_min, t_max, int((t_max - t_min) / resolution)) +n_steps = 20 +t_instance = np.linspace(12.2, 12.2 + resolution * (n_steps + 1), n_steps + 1) + + +class DeviceCellWrapper(tf.nn.rnn_cell.RNNCell): + + IDT def __init__(self, device, cell): + IDT IDT self._cell = cell + IDT IDT self._device = device + + IDT @property + IDT def state_size(self): + IDT IDT return self._cell.state_size + + IDT @property + IDT def output_size(self): + IDT IDT return self._cell.output_size + + IDT def __call__(self, inputs, state, scope= ) IDT IDT with tf.device(self._device): + IDT IDT IDT return self._cell(inputs, state, scope) + + +reset_graph() +WORDS_PATH = 'datasets/words' +WORDS_URL = 'http://mattmahoney.net/dc/text8.zip' + + +def mkdir_p(path): + IDT try: + IDT IDT os.makedirs(path) + IDT except OSError as exc: + IDT IDT if exc.errno == errno.EEXIST and os.path.isdir(path): + IDT IDT IDT pass + IDT IDT else: + IDT IDT IDT raise + + +def fetch_word_data(words_url= , words_path= ) IDT os.makedirs(words_path, exist_ok= ) IDT zip_path = os.path.join(words_path, 'words.zip') + IDT if not os.path.exists(zip_path): + IDT IDT urllib.request.urlretrieve(words_url, zip_path) + IDT with zipfile.ZipFile(zip_path) as f: + IDT IDT data = f.read(f.namelist()[0]) + IDT return data.decode('ascii').split() + + +words = fetch_word_data() +vocabulary_size = 50000 +vocabulary = [('UNK', None)] + Counter(words).most_common(vocabulary_size - 1) +vocabulary = np.array([word for word, _ in vocabulary]) +dictionary = {word: code for code, word in enumerate(vocabulary)} +data = np.array([dictionary.get(word, 0) for word in words]) + + +def generate_batch(batch_size, num_skips, skip_window): + IDT global data_index + IDT assert batch_size % num_skips == 0 + IDT assert num_skips <= 2 * skip_window + IDT batch = , dtype= ) IDT labels = , 1], dtype= ) IDT span = 2 * skip_window + 1 + IDT buffer = ) IDT for _ in range(span): + IDT IDT buffer.append(data[data_index]) + IDT IDT data_index = (data_index + 1) % len(data) + IDT for i in range(batch_size // num_skips): + IDT IDT target = skip_window + IDT IDT targets_to_avoid = [skip_window] + IDT IDT for j in range(num_skips): + IDT IDT IDT while target in targets_to_avoid: + IDT IDT IDT IDT target = np.random.randint(0, span) + IDT IDT IDT targets_to_avoid.append(target) + IDT IDT IDT batch[i * num_skips + j] = buffer[skip_window] + IDT IDT IDT labels[i * num_skips + j, 0] = buffer[target] + IDT IDT buffer.append(data[data_index]) + IDT IDT data_index = (data_index + 1) % len(data) + IDT return batch, labels + + +data_index = 0 +batch_size = 128 +embedding_size = 128 +skip_window = 1 +num_skips = 2 +valid_size = 16 +valid_window = 100 +valid_examples = , valid_size, replace= )num_sampled = 64 +learning_rate = 0.01 +reset_graph() +train_labels = , shape= , 1]) )valid_dataset = , dtype= )vocabulary_size = 50000 +embedding_size = 150 +init_embeds = tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0) +embeddings = tf.Variable(init_embeds) +train_inputs = , shape= )embed = tf.nn.embedding_lookup(embeddings, train_inputs) +nce_weights = , embedding_size], stddev= )nce_biases = tf.Variable(tf.zeros([vocabulary_size])) +loss = tf.reduce_mean(tf.nn.nce_loss(nce_weights, nce_biases, train_labels, embed, num_sampled, vocabulary_size)) +optimizer = tf.train.AdamOptimizer(learning_rate) +trianing_op = optimizer.minimize(loss) +norm = , axis= , keepdims= )normalized_embeddings = embeddings / norm +valid_embeddings = tf.nn.embedding_lookup(normalized_embeddings, valid_dataset) +similarity = , normalized_embeddings, transpose_b= )init = tf.global_variables_initializer() +num_steps = 10001 +with tf.Session() as sess: + IDT init.run() + IDT average_loss = 0 + IDT for step in range(num_steps): + IDT IDT , end='\t') + IDT IDT batch_inputs, batch_labels = generate_batch(batch_size, num_skips, skip_window) + IDT IDT feed_dict = {train_inputs: batch_inputs, train_labels: batch_labels} + IDT IDT _, loss_val = , loss], feed_dict= ) IDT IDT average_loss += loss_val + IDT IDT if step % 2000 == 0: + IDT IDT IDT if step > 0: + IDT IDT IDT IDT average_loss /= 2000 + IDT IDT IDT + IDT IDT IDT average_loss = 0 + IDT IDT if step % 10000 == 0: + IDT IDT IDT sim = similarity.eval() + IDT IDT IDT for i in range(valid_size): + IDT IDT IDT IDT valid_word = vocabulary[valid_examples[i]] + IDT IDT IDT IDT top_k = 8 + IDT IDT IDT IDT nearst = (-sim[(i), :]).argsort()[1:top_k + 1] + IDT IDT IDT IDT log_str = 'Nearest to %s:' % valid_word + IDT IDT IDT IDT for k in range(top_k): + IDT IDT IDT IDT IDT close_word = vocabulary[nearst[k]] + IDT IDT IDT IDT IDT log_str = '%s %s,' % (log_str, close_word) + IDT IDT IDT IDT + IDT final_embedding = normalized_embeddings.eval() +np.save('./my_final_embeddings.npy', final_embedding) + + +def plot_with_labels(low_dim_embs, labels): + IDT assert low_dim_embs.shape[0] >= len(labels), 'More labels than embeddings' + IDT plt.figure(figsize= , 18)) ) IDT for i, label in enumerate(labels): + IDT IDT x, y = low_dim_embs[(i), :] + IDT IDT plt.scatter(x, y) + IDT IDT plt.annotate(label, xy= , y), xytext= , 2), textcoords= , ha= , va= ) IDT plt.show() + + +tsne = , n_components= , init= , n_iter= )plot_only = 500 +low_dim_embs = tsne.fit_transform(final_embedding[:plot_only, :]) +labels = [vocabulary[i] for i in range(plot_only)] +plot_with_labels(low_dim_embs, labels) +from __future__ import print_function +import numpy as np +import tensorflow as tf +from neural.full_connect import load_reformat_not_mnist, accuracy + + +def tf_better_nn(offset_range= , regular= , drop_out= , lrd= ) IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_node_count = 1024 + IDT IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count])) + IDT IDT biases1 = tf.Variable(tf.zeros([hidden_node_count])) + IDT IDT weights2 = tf.Variable(tf.truncated_normal([hidden_node_count, num_labels])) + IDT IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT ys = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT hidden = tf.nn.relu(ys) + IDT IDT h_fc = hidden + IDT IDT valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + IDT IDT valid_hidden1 = tf.nn.relu(valid_y0) + IDT IDT test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + IDT IDT test_hidden1 = tf.nn.relu(test_y0) + IDT IDT keep_prob = tf.placeholder(tf.float32) + IDT IDT if drop_out: + IDT IDT IDT hidden_drop = tf.nn.dropout(hidden, keep_prob) + IDT IDT IDT h_fc = hidden_drop + IDT IDT logits = tf.matmul(h_fc, weights2) + biases2 + IDT IDT logits_predict = tf.matmul(hidden, weights2) + biases2 + IDT IDT valid_predict = tf.matmul(valid_hidden1, weights2) + biases2 + IDT IDT test_predict = tf.matmul(test_hidden1, weights2) + biases2 + IDT IDT l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(biases1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(biases2) + IDT IDT if not regular: + IDT IDT IDT l2_loss = 0 + IDT IDT beta = 0.002 + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT if lrd: + IDT IDT IDT cur_step = tf.Variable(0) + IDT IDT IDT starter_learning_rate = 0.1 + IDT IDT IDT learning_rate = , cur_step, 10000, 0.96, staircase= ) IDT IDT IDT optimizer = , global_step= ) IDT IDT train_prediction = tf.nn.softmax(logits_predict) + IDT IDT valid_prediction = tf.nn.softmax(valid_predict) + IDT IDT test_prediction = tf.nn.softmax(test_predict) + IDT num_steps = 30001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT if offset_range == -1: + IDT IDT IDT IDT offset_range = train_labels.shape[0] - batch_size + IDT IDT IDT offset = step * batch_size % offset_range + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +def tf_deep_nn(regular= , drop_out= , lrd= , layer_cnt= ) IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_node_count = 1024 + IDT IDT hidden_stddev = np.sqrt(2.0 / 784) + IDT IDT weights1 = , hidden_node_count], stddev= ) IDT IDT biases1 = tf.Variable(tf.zeros([hidden_node_count])) + IDT IDT weights = [] + IDT IDT biases = [] + IDT IDT hidden_cur_cnt = hidden_node_count + IDT IDT for i in range(layer_cnt - 2): + IDT IDT IDT if hidden_cur_cnt > 2: + IDT IDT IDT IDT hidden_next_cnt = int(hidden_cur_cnt / 2) + IDT IDT IDT else: + IDT IDT IDT IDT hidden_next_cnt = 2 + IDT IDT IDT hidden_stddev = np.sqrt(2.0 / hidden_cur_cnt) + IDT IDT IDT weights.append(tf.Variable(tf.truncated_normal([hidden_cur_cnt, hidden_next_cnt], stddev= ) IDT IDT IDT biases.append(tf.Variable(tf.zeros([hidden_next_cnt]))) + IDT IDT IDT hidden_cur_cnt = hidden_next_cnt + IDT IDT y0 = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT hidden = tf.nn.relu(y0) + IDT IDT hidden_drop = hidden + IDT IDT keep_prob = 0.5 + IDT IDT if drop_out: + IDT IDT IDT hidden_drop = tf.nn.dropout(hidden, keep_prob) + IDT IDT valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + IDT IDT valid_hidden = tf.nn.relu(valid_y0) + IDT IDT test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + IDT IDT test_hidden = tf.nn.relu(test_y0) + IDT IDT for i in range(layer_cnt - 2): + IDT IDT IDT y1 = tf.matmul(hidden_drop, weights[i]) + biases[i] + IDT IDT IDT hidden_drop = tf.nn.relu(y1) + IDT IDT IDT if drop_out: + IDT IDT IDT IDT keep_prob += 0.5 * i / (layer_cnt + 1) + IDT IDT IDT IDT hidden_drop = tf.nn.dropout(hidden_drop, keep_prob) + IDT IDT IDT y0 = tf.matmul(hidden, weights[i]) + biases[i] + IDT IDT IDT hidden = tf.nn.relu(y0) + IDT IDT IDT valid_y0 = tf.matmul(valid_hidden, weights[i]) + biases[i] + IDT IDT IDT valid_hidden = tf.nn.relu(valid_y0) + IDT IDT IDT test_y0 = tf.matmul(test_hidden, weights[i]) + biases[i] + IDT IDT IDT test_hidden = tf.nn.relu(test_y0) + IDT IDT weights2 = , num_labels], stddev= ) IDT IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden_drop, weights2) + biases2 + IDT IDT logits_predict = tf.matmul(hidden, weights2) + biases2 + IDT IDT valid_predict = tf.matmul(valid_hidden, weights2) + biases2 + IDT IDT test_predict = tf.matmul(test_hidden, weights2) + biases2 + IDT IDT l2_loss = 0 + IDT IDT if regular: + IDT IDT IDT l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + IDT IDT IDT for i in range(len(weights)): + IDT IDT IDT IDT l2_loss += tf.nn.l2_loss(weights[i]) + IDT IDT IDT beta = 0.25 / batch_size + IDT IDT IDT beta = 1e-05 + IDT IDT IDT l2_loss *= beta + IDT IDT loss = , labels= ) IDT IDT if lrd: + IDT IDT IDT cur_step = , trainable= ) IDT IDT IDT starter_learning_rate = 0.4 + IDT IDT IDT learning_rate = , cur_step, 100000, 0.96, staircase= ) IDT IDT IDT optimizer = , global_step= ) IDT IDT else: + IDT IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits_predict) + IDT IDT valid_prediction = tf.nn.softmax(valid_predict) + IDT IDT test_prediction = tf.nn.softmax(test_predict) + IDT num_steps = 20001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset_range = train_labels.shape[0] - batch_size + IDT IDT IDT offset = step * batch_size % offset_range + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +if __name__ == '__main__': + IDT image_size = 28 + IDT num_labels = 10 + IDT train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = load_reformat_not_mnist(image_size, num_labels) + IDT tf_deep_nn(layer_cnt= , lrd= , drop_out= , regular= )from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import os +import numpy as np +import tensorflow as tf + + +class MLP(object): + + IDT def __init__(self, is_training, n_layers, keep_prob, last_nl_fun, loss_fun, top_k, aux_classes= , flag_with_saver= , gpu= ) IDT IDT nb_feature = n_layers[0] + IDT IDT nb_classes = n_layers[-1] + IDT IDT if not is_training: + IDT IDT IDT keep_prob = 1.0 + IDT IDT with tf.device('/gpu:%d' % gpu): + IDT IDT IDT with tf.name_scope('input'): + IDT IDT IDT IDT self._input_data = , [None, nb_feature], name= ) IDT IDT IDT IDT self._targets = , [None, nb_classes], name= ) IDT IDT IDT IDT self._aux_targets = , [None, aux_classes], name= ) + IDT IDT IDT def weight_variable(shape): + IDT IDT IDT IDT + IDT IDT IDT IDT initial = , stddev= ) IDT IDT IDT IDT return tf.Variable(initial) + + IDT IDT IDT def bias_variable(shape): + IDT IDT IDT IDT + IDT IDT IDT IDT initial = , shape= ) IDT IDT IDT IDT return tf.Variable(initial) + + IDT IDT IDT def nn_layer(input_tensor, input_dim, output_dim, layer_name, act= ) IDT IDT IDT IDT """Reusable code for making a simple neural net layer. + + IDT IDT It does a matrix multiply, bias add, and then uses relu to nonlinearize. + IDT IDT It also sets up name scoping so that the resultant graph is easy to read, + IDT IDT and adds a number of summary ops. + IDT IDT """ + IDT IDT IDT IDT with tf.name_scope(layer_name): + IDT IDT IDT IDT IDT with tf.name_scope('weights'): + IDT IDT IDT IDT IDT IDT weights = weight_variable([input_dim, output_dim]) + IDT IDT IDT IDT IDT with tf.name_scope('biases'): + IDT IDT IDT IDT IDT IDT biases = bias_variable([output_dim]) + IDT IDT IDT IDT IDT with tf.name_scope('Wx_plus_b'): + IDT IDT IDT IDT IDT IDT preactivate = tf.matmul(input_tensor, weights) + biases + IDT IDT IDT IDT IDT if act is not None: + IDT IDT IDT IDT IDT IDT activations = , name= ) IDT IDT IDT IDT IDT IDT return activations + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT return preactivate + IDT IDT IDT if len(n_layers) == 3: + IDT IDT IDT IDT hidden1 = nn_layer(x, n_layers[0], n_layers[1], 'layer1') + IDT IDT IDT IDT with tf.name_scope('dropout'): + IDT IDT IDT IDT IDT dropped = tf.nn.dropout(hidden1, keep_prob) + IDT IDT IDT elif len(n_layers) == 4: + IDT IDT IDT IDT hidden1 = nn_layer(x, n_layers[0], n_layers[1], 'hidden_layer_1') + IDT IDT IDT IDT with tf.name_scope('dropout'): + IDT IDT IDT IDT IDT dropped1 = tf.nn.dropout(hidden1, keep_prob) + IDT IDT IDT IDT hidden2 = nn_layer(dropped1, n_layers[1], n_layers[2], 'hidden_layer_2') + IDT IDT IDT IDT with tf.name_scope('dropout'): + IDT IDT IDT IDT IDT dropped = tf.nn.dropout(hidden2, keep_prob) + IDT IDT IDT elif len(n_layers) == 5: + IDT IDT IDT IDT hidden1 = nn_layer(x, n_layers[0], n_layers[1], 'hidden_layer_1') + IDT IDT IDT IDT with tf.name_scope('dropout'): + IDT IDT IDT IDT IDT self._keep_prob = keep_prob = tf.placeholder(tf.float32) + IDT IDT IDT IDT IDT dropped1 = tf.nn.dropout(hidden1, keep_prob) + IDT IDT IDT IDT hidden2 = nn_layer(dropped1, n_layers[1], n_layers[2], 'hidden_layer_2') + IDT IDT IDT IDT with tf.name_scope('dropout'): + IDT IDT IDT IDT IDT dropped2 = tf.nn.dropout(hidden2, keep_prob) + IDT IDT IDT IDT hidden3 = nn_layer(dropped2, n_layers[2], n_layers[3], 'hidden_layer_3') + IDT IDT IDT IDT with tf.name_scope('dropout'): + IDT IDT IDT IDT IDT dropped = tf.nn.dropout(hidden3, keep_prob) + IDT IDT IDT self._logit = , n_layers[-2], n_layers[-1], 'layer2', act= ) IDT IDT IDT if aux_classes > 0: + IDT IDT IDT IDT self._aux_logit = , n_layers[-2], aux_classes, 'layer3', act= ) IDT IDT IDT if last_nl_fun == 'softmax': + IDT IDT IDT IDT self._outputs = y = tf.nn.softmax(logit) + IDT IDT IDT IDT if aux_classes > 0: + IDT IDT IDT IDT IDT self._aux_outputs = y2 = tf.nn.softmax(aux_logit) + IDT IDT IDT IDT if loss_fun == 'cross_entropy': + IDT IDT IDT IDT IDT with tf.name_scope('cross_entropy'): + IDT IDT IDT IDT IDT IDT self._loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logit, y_)) + IDT IDT IDT IDT IDT IDT if aux_classes > 0: + IDT IDT IDT IDT IDT IDT IDT self._loss += tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(aux_logit, y2_)) + IDT IDT IDT IDT if loss_fun == 'logistic': + IDT IDT IDT IDT IDT with tf.name_scope('logistic'): + IDT IDT IDT IDT IDT IDT self._loss = tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y * y_)), 1)) + IDT IDT IDT IDT IDT IDT if aux_classes > 0: + IDT IDT IDT IDT IDT IDT IDT self._loss += tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y2 * y2_)), 1)) + IDT IDT IDT elif last_nl_fun == 'sigmoid': + IDT IDT IDT IDT self._outputs = y = tf.sigmoid(logit) + IDT IDT IDT IDT if aux_classes > 0: + IDT IDT IDT IDT IDT self._aux_outputs = y2 = tf.sigmoid(aux_logit) + IDT IDT IDT IDT if loss_fun == 'cross_entropy': + IDT IDT IDT IDT IDT with tf.name_scope('cross_entropy'): + IDT IDT IDT IDT IDT IDT self._loss = , labels= ) IDT IDT IDT IDT IDT IDT if aux_classes > 0: + IDT IDT IDT IDT IDT IDT IDT self._loss += , labels= ) IDT IDT IDT IDT if loss_fun == 'logistic': + IDT IDT IDT IDT IDT with tf.name_scope('logistic'): + IDT IDT IDT IDT IDT IDT self._loss = tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y * y_)), 1)) + IDT IDT IDT IDT IDT IDT if aux_classes > 0: + IDT IDT IDT IDT IDT IDT IDT self._loss += tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y2 * y2_)), 1)) + IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y * y_, 1)) + IDT IDT IDT IDT self._accuracy = accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT IDT IDT IDT self._pred_top_values, self._pred_top_indices = , k= ) IDT IDT IDT if flag_with_saver: + IDT IDT IDT IDT self.saver = ) IDT IDT IDT else: + IDT IDT IDT IDT self.saver = None + IDT IDT IDT if not is_training: + IDT IDT IDT IDT return + IDT IDT IDT self._lr = , trainable= ) IDT IDT optimizer = tf.train.AdamOptimizer(self.lr) + IDT IDT self._train_op = optimizer.minimize(self._loss) + + IDT def assign_lr(self, session, lr_value): + IDT IDT session.run(tf.assign(self.lr, lr_value)) + + IDT @property + IDT def input_data(self): + IDT IDT return self._input_data + + IDT @property + IDT def targets(self): + IDT IDT return self._targets + + IDT @property + IDT def aux_targets(self): + IDT IDT return self._aux_targets + + IDT @property + IDT def outputs(self): + IDT IDT return self._outputs + + IDT @property + IDT def logit(self): + IDT IDT return self._logit + + IDT @property + IDT def loss(self): + IDT IDT return self._loss + + IDT @property + IDT def train_op(self): + IDT IDT return self._train_op + + IDT @property + IDT def lr(self): + IDT IDT return self._lr + + IDT @property + IDT def pred_top_values(self): + IDT IDT return self._pred_top_values + + IDT @property + IDT def pred_top_indices(self): + IDT IDT return self._pred_top_indices + + IDT @property + IDT def target_top_values(self): + IDT IDT return self._target_top_values + + IDT @property + IDT def target_top_indices(self): + IDT IDT return self._target_top_indices + + IDT @property + IDT def inter_indices(self): + IDT IDT return self._inter_indices + + +class MLPPredictor(object): + IDT + + IDT def __init__(self, n_layers, last_nl_fun= , loss_fun= , top_k= , aux_classes= , ses_threads= , gpu_memory_fraction= , gpu= ) IDT IDT gpu_options = ) IDT IDT config_proto = , gpu_options= , allow_soft_placement= ) IDT IDT self.session = ) IDT IDT with tf.variable_scope('MLPModel'): + IDT IDT IDT self.model = , n_layers= , keep_prob= , last_nl_fun= , loss_fun= , top_k= , aux_classes= , flag_with_saver= , gpu= ) IDT IDT self.model_ready = False + + IDT def load_model(self, model_path): + IDT IDT self.model.saver.restore(self.session, model_path) + IDT IDT self.model_path = model_path + IDT IDT self.model_ready = True + + IDT def predict(self, input_data): + IDT IDT assert self.model_ready + IDT IDT model = self.model + IDT IDT pred_top_values, pred_top_indices = , model.pred_top_indices], feed_dict= ) IDT IDT return pred_top_values, pred_top_indices +import getpass +import sys +import time +import numpy as np +from copy import deepcopy +from utils import calculate_perplexity, get_ptb_dataset, Vocab +from utils import ptb_iterator, sample +import tensorflow as tf +from tensorflow.contrib.seq2seq import sequence_loss +from model import LanguageModel + + +class Config(object): + IDT """Holds model hyperparams and data information. + + The config class is used to store various hyperparameters and dataset + information parameters. Model objects are passed a Config() object at + instantiation. + """ + IDT batch_size = 64 + IDT embed_size = 50 + IDT hidden_size = 128 + IDT num_steps = 10 + IDT max_epochs = 20 + IDT early_stopping = 2 + IDT dropout = 0.9 + IDT lr = 0.001 + + +class RNNLM_Model(LanguageModel): + + IDT def load_data(self, debug= ) IDT IDT + IDT IDT self.vocab = Vocab() + IDT IDT self.vocab.construct(get_ptb_dataset('train')) + IDT IDT self.encoded_train = , dtype= ) IDT IDT self.encoded_valid = , dtype= ) IDT IDT self.encoded_test = , dtype= ) IDT IDT if debug: + IDT IDT IDT num_debug = 1024 + IDT IDT IDT self.encoded_train = self.encoded_train[:num_debug] + IDT IDT IDT self.encoded_valid = self.encoded_valid[:num_debug] + IDT IDT IDT self.encoded_test = self.encoded_test[:num_debug] + + IDT def add_placeholders(self): + IDT IDT """Generate placeholder variables to represent the input tensors + + IDT These placeholders are used as inputs by the rest of the model building + IDT code and will be fed data during training. Note that when "None" is in a + IDT placeholder's shape, it's flexible + + IDT Adds following nodes to the computational graph. + IDT (When None is in a placeholder's shape, it's flexible) + + IDT input_placeholder: Input placeholder tensor of shape + IDT IDT IDT IDT IDT (None, num_steps), type tf.int32 + IDT labels_placeholder: Labels placeholder tensor of shape + IDT IDT IDT IDT IDT IDT (None, num_steps), type tf.float32 + IDT dropout_placeholder: Dropout value placeholder (scalar), + IDT IDT IDT IDT IDT IDT type tf.float32 + + IDT Add these placeholders to self as the instance variables + + IDT self.input_placeholder + IDT self.labels_placeholder + IDT self.dropout_placeholder + + IDT (Don't change the variable names) + IDT """ + IDT IDT self.input_placeholder = , shape= , self.config.num_steps]) ) IDT IDT self.labels_placeholder = , shape= , self.config.num_steps]) ) IDT IDT self.loss_weights = tf.ones([self.config.batch_size, self.config.num_steps]) + IDT IDT self.dropout_placeholder = , shape= ) + IDT def add_embedding(self): + IDT IDT """Add embedding layer. + + IDT Hint: This layer should use the input_placeholder to index into the + IDT IDT embedding. + IDT Hint: You might find tf.nn.embedding_lookup useful. + IDT Hint: You might find tf.split, tf.squeeze useful in constructing tensor inputs + IDT Hint: Check the last slide from the TensorFlow lecture. + IDT Hint: Here are the dimensions of the variables you will need to create: + + IDT L: (len(self.vocab), embed_size) + + IDT Returns: + IDT inputs: List of length num_steps, each of whose elements should be + IDT IDT IDT a tensor of shape (batch_size, embed_size). + IDT """ + IDT IDT with tf.variable_scope('RNN', reuse= ) IDT IDT IDT L = , shape= , self.config.embed_size], initializer= ) IDT IDT inputs = tf.nn.embedding_lookup(L, self.input_placeholder) + IDT IDT return inputs + IDT IDT """ + IDT inputs = [] + IDT + IDT split = ,self.config.num_steps,axis = ) IDT with tf.device('/cpu:0'): + IDT + IDT for ii in range(self.config.num_steps): + IDT IDT embed = tf.nn.embedding_lookup(L,split[ii]) + + IDT IDT inputs.append(embed) IDT + IDT + IDT + IDT return inputs + IDT """ + + IDT def add_model(self, inputs): + IDT IDT """Creates the RNN LM model. + + IDT In the space provided below, you need to implement the equations for the + IDT RNN LM model. You can use built in rnn_cell functions from + IDT tensorflow. + + IDT Hint: Use a zeros tensor of shape (batch_size, hidden_size) as + IDT IDT initial state for the RNN. Add this to self as instance variable + + IDT IDT self.initial_state + + IDT IDT (Don't change variable name) + IDT Hint: Add the last RNN output to self as instance variable + + IDT IDT self.final_state + + IDT IDT (Don't change variable name) + IDT Hint: Make sure to apply dropout to the inputs and the outputs. + IDT Hint: Use a variable scope (e.g. "RNN") to define RNN variables. + IDT Hint: Perform an explicit for-loop over inputs. You can use + IDT IDT scope.reuse_variables() to ensure that the weights used at each + IDT IDT iteration (each time-step) are the same. (Make sure you don't call + IDT IDT this for iteration 0 though or nothing will be initialized!) + IDT Hint: Here are the dimensions of the various variables you will need to + IDT IDT create: + IDT + IDT IDT H: (hidden_size, hidden_size) + IDT IDT I: (embed_size, hidden_size) + IDT IDT b_1: (hidden_size,) + + IDT Args: + IDT inputs: List of length num_steps, each of whose elements should be + IDT IDT IDT a tensor of shape (batch_size, embed_size). + IDT Returns: + IDT outputs: List of length num_steps, each of whose elements should be + IDT IDT IDT a tensor of shape (batch_size, hidden_size) + IDT """ + IDT IDT batch_size = self.config.batch_size + IDT IDT hidden_size = self.config.hidden_size + IDT IDT embed_size = self.config.embed_size + IDT IDT rnn_cell = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT self.initial_state = , dtype= ) IDT IDT dropped_inputs = , keep_prob= ) IDT IDT with tf.variable_scope('RNN'): + IDT IDT IDT rnn_output, rnn_final_state = , dropped_inputs, initial_state= , dtype= ) IDT IDT rnn_output_dropped = tf.nn.dropout(rnn_output, self.dropout_placeholder) + IDT IDT self.final_state = rnn_final_state + IDT IDT return rnn_output_dropped + IDT IDT """ + IDT list way + IDT rnn_outputs = [] + IDT with tf.variable_scope("RNN"): + IDT for timestep, input_t in enumerate(inputs): + IDT IDT batch = tf.nn.dropout(inputs[timestep], self.dropout_placeholder) + IDT IDT rnn_output, rnn_final_state = tf.nn.dynamic_rnn(rnn_cell, batch, + IDT IDT IDT IDT IDT IDT IDT IDT IDT initial_state=self.initial_state, + IDT IDT IDT IDT IDT IDT IDT IDT IDT dtype=tf.float32) + + IDT IDT + + IDT IDT if timestep > 0: + IDT IDT tf.get_variable_scope().reuse_variables() + IDT IDT with tf.variable_scope("BasicRNNCell"): + IDT IDT rnn_outputs.append(rnn_output) + + IDT self.final_state = rnn_final_state + + IDT rnn_outputs_reshaped = tf.reshape(tf.concat(rnn_outputs,1),[self.config.batch_size, self.config.num_steps,hidden_size]) + IDT rnn_outputs_dropped = tf.nn.dropout(rnn_outputs_reshaped, self.dropout_placeholder) + IDT """ + IDT IDT return rnn_outputs_dropped + + IDT def add_projection(self, rnn_outputs): + IDT IDT """Adds a projection layer. + + IDT The projection layer transforms the hidden representation to a distribution + IDT over the vocabulary. + + IDT Hint: Here are the dimensions of the variables you will need to + IDT IDT create + IDT IDT + IDT IDT U: (hidden_size, len(vocab)) + IDT IDT b_2: (len(vocab),) + + IDT Args: + IDT rnn_outputs: List of length num_steps, each of whose elements should be + IDT IDT IDT IDT a tensor of shape (batch_size, embed_size). + IDT IDT IDT IDT This might be (batch_size, hidden_size) + IDT Returns: + IDT outputs: List of length num_steps, each a tensor of shape + IDT IDT IDT (batch_size, len(vocab) + IDT """ + IDT IDT with tf.variable_scope('RNN', reuse= ) IDT IDT IDT U = , shape= , len(self.vocab)]) ) IDT IDT IDT b_2 = , shape= ) IDT IDT outputs = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, self.config.hidden_size]), U), [self.config.batch_size, self.config.num_steps, -1]) + b_2 + IDT IDT """ + IDT list version + IDT outputs = [] + IDT for rnn_output in rnn_outputs: + IDT rnn_output = tf.reshape(rnn_output,[self.config.batch_size,hidden_size]) + IDT output = tf.matmul(rnn_output,U)+b_2 + IDT outputs.append(output) + IDT """ + IDT IDT return outputs + + IDT def add_loss_op(self, output): + IDT IDT """Adds loss ops to the computational graph. + + IDT Hint: Use tensorflow.python.ops.seq2seq.sequence_loss to implement sequence loss. + + IDT Args: + IDT output: A tensor of shape (None, self.vocab) + IDT Returns: + IDT loss: A 0-d tensor (scalar) + IDT """ + IDT IDT loss = tf.contrib.seq2seq.sequence_loss(output, self.labels_placeholder, self.loss_weights) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT """Sets up the training Ops. + + IDT Creates an optimizer and applies the gradients to all trainable variables. + IDT The Op returned by this function is what must be passed to the + IDT `sess.run()` call to cause the model to train. See + + IDT https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html + + IDT for more information. + + IDT Hint: Use tf.train.AdamOptimizer for this model. + IDT IDT Calling optimizer.minimize() will return a train_op object. + + IDT Args: + IDT loss: Loss tensor, from cross_entropy_loss. + IDT Returns: + IDT train_op: The Op for training. + IDT """ + IDT IDT with tf.variable_scope('RNN', reuse= ) IDT IDT IDT opt = tf.train.AdamOptimizer(self.config.lr) + IDT IDT IDT train_op = opt.minimize(loss) + IDT IDT return train_op + + IDT def __init__(self, config): + IDT IDT self.config = config + IDT IDT self.load_data(debug= ) IDT IDT self.add_placeholders() + IDT IDT self.inputs = self.add_embedding() + IDT IDT self.rnn_outputs = self.add_model(self.inputs) + IDT IDT self.outputs = self.add_projection(self.rnn_outputs) + IDT IDT self.predictions = [tf.nn.softmax(tf.cast(self.outputs, 'float64'))] + IDT IDT output = tf.reshape(tf.concat(self.outputs, 1), [self.config.batch_size, self.config.num_steps, len(self.vocab)]) + IDT IDT self.calculate_loss = self.add_loss_op(output) + IDT IDT self.train_step = self.add_training_op(self.calculate_loss) + + IDT def run_epoch(self, session, data, train_op= , verbose= ) IDT IDT config = self.config + IDT IDT dp = config.dropout + IDT IDT if not train_op: + IDT IDT IDT train_op = tf.no_op() + IDT IDT IDT dp = 1 + IDT IDT total_steps = sum(1 for x in ptb_iterator(data, config.batch_size, config.num_steps)) + IDT IDT total_loss = [] + IDT IDT state = self.initial_state.eval() + IDT IDT for step, (x, y) in enumerate(ptb_iterator(data, config.batch_size, config.num_steps)): + IDT IDT IDT feed = {self.input_placeholder: x, self.labels_placeholder: y, self.initial_state: state, self.dropout_placeholder: dp} + IDT IDT IDT loss, state, _ = , self.final_state, train_op], feed_dict= ) IDT IDT IDT total_loss.append(loss) + IDT IDT IDT if verbose and step % verbose == 0: + IDT IDT IDT IDT sys.stdout.write('\r{} / {} : pp = , total_steps, np.exp(np.mean(total_loss)))) ) IDT IDT IDT IDT sys.stdout.flush() + IDT IDT if verbose: + IDT IDT IDT sys.stdout.write('\r') + IDT IDT return np.exp(np.mean(total_loss)) + + +def generate_text(session, model, config, starting_text= , stop_length= , stop_tokens= , temp= ) IDT """Generate text from the model. + + Hint: Create a feed-dictionary and use sess.run() to execute the model. Note + IDT IDT that you will need to use model.initial_state as a key to feed_dict + Hint: Fetch model.final_state and model.predictions[-1]. (You set + IDT IDT model.final_state in add_model() and model.predictions is set in + IDT IDT __init__) + Hint: Store the outputs of running the model in local variables state and + IDT IDT y_pred (used in the pre-implemented parts of this function.) + + Args: + IDT session: tf.Session() object + IDT model: Object of type RNNLM_Model + IDT config: A Config() object + IDT starting_text: Initial text passed to model. + Returns: + IDT output: List of word idxs + """ + IDT state = model.initial_state.eval() + IDT tokens = [model.vocab.encode(word) for word in starting_text.split()] + IDT for token in tokens: + IDT IDT feed = {model.input_placeholder: [[token]], model.initial_state: state, model.dropout_placeholder: 1} + IDT IDT state = , feed_dict= ) IDT last_word = token + IDT for i in range(stop_length): + IDT IDT feed = {model.input_placeholder: [[last_word]], model.initial_state: state, model.dropout_placeholder: 1} + IDT IDT state, y_pred = , model.predictions[-1]], feed_dict= ) IDT IDT next_word_idx = , 0], temperature= ) IDT IDT tokens.append(next_word_idx) + IDT IDT last_word = next_word_idx + IDT IDT if stop_tokens and model.vocab.decode(tokens[-1]) in stop_tokens: + IDT IDT IDT break + IDT output = [model.vocab.decode(word_idx) for word_idx in tokens] + IDT return output + + +def generate_sentence(session, model, config, *args, **kwargs): + IDT + IDT return generate_text(session, model, config, *args, stop_tokens= , **kwargs) ) + +def test_RNNLM(): + IDT config = Config() + IDT gen_config = deepcopy(config) + IDT gen_config.batch_size = gen_config.num_steps = 1 + IDT tf.reset_default_graph() + IDT with tf.variable_scope('RNNLM') as scope: + IDT IDT model = RNNLM_Model(config) + IDT IDT scope.reuse_variables() + IDT IDT gen_model = RNNLM_Model(gen_config) + IDT init = tf.initialize_all_variables() + IDT saver = tf.train.Saver() + IDT with tf.Session() as session: + IDT IDT best_val_pp = float('inf') + IDT IDT best_val_epoch = 0 + IDT IDT session.run(init) + IDT IDT for epoch in range(Config.max_epochs): + IDT IDT IDT ) + IDT IDT IDT start = time.time() + IDT IDT IDT train_pp = , model.encoded_train, train_op= ) IDT IDT IDT valid_pp = model.run_epoch(session, model.encoded_valid) + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT if valid_pp < best_val_pp: + IDT IDT IDT IDT best_val_pp = valid_pp + IDT IDT IDT IDT best_val_epoch = epoch + IDT IDT IDT IDT saver.save(session, './ptb_rnnlm.weights') + IDT IDT IDT if epoch - best_val_epoch > config.early_stopping: + IDT IDT IDT IDT break + IDT IDT IDT - start)) + IDT IDT saver.restore(session, 'ptb_rnnlm.weights') + IDT IDT test_pp = model.run_epoch(session, model.encoded_test) + IDT IDT + IDT IDT ) + IDT IDT + IDT IDT compare_test_get = False + IDT IDT if compare_test_get: + IDT IDT IDT test_gen = gen_model.run_epoch(session, model.encoded_test) + IDT IDT IDT ) + IDT IDT IDT + IDT IDT starting_snippets = ['in boston', 'they have', 'please', 'today', 'the president', 'in winter', 'i want', 'look at', 'come to', 'he said'] + IDT IDT for starting_text in starting_snippets: + IDT IDT IDT + IDT IDT IDT )) + + +if __name__ == '__main__': + IDT test_RNNLM() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +data_dir = '/home/jsemko/data/udacity' +pickle_file = 'notMNIST.pickle' +pickle_file = os.path.join(data_dir, pickle_file) +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels, return_wrong= ) IDT correct = , 1) = , 1) ) IDT acc = 100.0 * np.sum(correct) / predictions.shape[0] + IDT if return_wrong: + IDT IDT return acc, ~correct + IDT else: + IDT IDT return acc + + +batch_size = 128 +beta = 0.02 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 101 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +beta = 0.001 +HL_size = 1024 +keep_prob_conn = 1 +keep_prob_neurons = 0.9 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.constant(beta) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, HL_size])) + IDT biases_1 = tf.Variable(tf.zeros([HL_size])) + IDT weights_2 = tf.Variable(tf.truncated_normal([HL_size, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + IDT neurons = tf.nn.relu(tf.matmul(tf.nn.dropout(tf_train_dataset, keep_prob_neurons), tf.nn.dropout(weights_1, keep_prob_conn)) + biases_1) + IDT logits = tf.matmul(tf.nn.dropout(neurons, keep_prob_neurons), tf.nn.dropout(weights_2, keep_prob_conn)) + biases_2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + tf.mul(tf_beta, tf.add(tf.nn.l2_loss(weights_1), tf.nn.l2_loss(weights_2)))) + IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(0.5, global_step, 500, 0.95) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 15001 +plot_incorrect = False +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT if step % 100 == 0: + IDT IDT IDT + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT acc, incorrect = , test_labels, return_wrong= ) IDT + IDT if plot_incorrect: + IDT IDT import matplotlib.pyplot as plt + IDT IDT bad_pics = test_dataset[(incorrect), :].reshape((sum(incorrect), 28, 28)) + IDT IDT for i in range(bad_pics.shape[0]): + IDT IDT IDT plt.imshow(bad_pics[(i), :, :], cmap= ) IDT IDT IDT plt.show() +import tensorflow as tf +import tensorflow.contrib.rnn as rnn +from math import sqrt +learning_rate = 0.01 +gaussian_noise = 0.2 +dropout_prob = 0.7 +regularize_beta = 1e-07 +word_vocab_size = 100004 +subword_vocab_size = 100004 +word_embedding_dim = 64 +subword_embedding_dim = 64 +input_subword_dim = 100 +input_dim = 100 +hidden_dim = 150 +hidden_2_dim = 100 +output_types = ['tag', 'word_freq'] +n_output = dict() +n_output['tag'] = 60 +n_output['word_freq'] = 15 +n_saved_models = 2 +x = dict() +x['words'] = , [None, None], name= )x['subwords'] = , [None, None], name= )x['subwords_len'] = , [None], name= )label = dict() +label['tag'] = , [None], name= )label['word_freq'] = , [None], name= )word_embedding = , word_embedding_dim]), trainable= , name= )subword_embedding = , subword_embedding_dim]), trainable= , name= ) + +def nn_run_subword_processing(subwords): + IDT with tf.variable_scope('bpos_subword_lstm') as vs: + IDT IDT mapped_subwords = tf.nn.embedding_lookup(subword_embedding, subwords) + IDT IDT lstm_fw_cell = , reuse= ) IDT IDT lstm_bw_cell = , reuse= ) IDT IDT lstm_outputs, _ = , lstm_bw_cell, mapped_subwords, dtype= , scope= ) IDT IDT fw_outputs, bw_outputs = lstm_outputs + IDT IDT return tf.concat([fw_outputs[:, (-1), :], bw_outputs[:, (-1), :]], 1) + + +def nn_run_input_layer(input_dict): + IDT mapped_words = tf.nn.embedding_lookup(word_embedding, input_dict['words']) + IDT subword_vec = nn_run_subword_processing(input_dict['subwords']) + IDT expanded_subword_vec = tf.expand_dims(subword_vec, 0) + IDT word_subword = , expanded_subword_vec], axis= ) IDT with tf.variable_scope('bpos_word_lstm') as vs: + IDT IDT lstm_fw_cell = , reuse= ) IDT IDT lstm_bw_cell = , reuse= ) IDT IDT lstm_outputs, _ = , lstm_bw_cell, word_subword, dtype= , scope= ) IDT concat_data = , lstm_outputs[1]], axis= ) IDT input_data = tf.reshape(concat_data, [-1, input_dim * 2]) + IDT input_noise = , input_dim * 2], stddev= , name= ) IDT return tf.add(input_data, input_noise) + + +def nn_run_hidden_layer(input_vec): + IDT hidden_weight = , hidden_dim], stddev= , name= ) IDT hidden_bias = , name= ) IDT hidden_2_weight = , hidden_2_dim], stddev= , name= ) IDT hidden_2_bias = , name= ) IDT hidden_1_output = tf.nn.tanh(tf.add(tf.matmul(input_vec, hidden_weight), hidden_bias)) + IDT hidden_2_output = tf.nn.relu(tf.add(tf.matmul(hidden_1_output, hidden_2_weight), hidden_2_bias)) + IDT return hidden_2_output + + +def nn_classify(hidden_vec): + IDT out_weights = dict() + IDT out_biases = dict() + IDT outputs = dict() + IDT for output_type in output_types: + IDT IDT out_weights[output_type] = , n_output[output_type]], stddev= , name= ) IDT IDT out_biases[output_type] = , name= ) IDT IDT outputs[output_type] = tf.matmul(hidden_vec, out_weights[output_type]) + out_biases[output_type] + IDT return outputs + + +def nn_calculate_loss(predicted_outputs): + IDT all_loss = 0.0 + IDT for output_type in output_types: + IDT IDT y = , n_output[output_type], on_value= , off_value= , axis= ) IDT IDT dropped_output = tf.nn.dropout(predicted_outputs[output_type], dropout_prob) + IDT IDT ce_loss = , logits= ) IDT IDT all_loss += ce_loss + IDT all_weights = [tensor for tensor in tf.global_variables() if 'weights' in tensor.name] + IDT l2_score = regularize_beta * sum([tf.nn.l2_loss(tensor) for tensor in all_weights]) + IDT all_loss += l2_score + IDT return all_loss + + +processed_input = nn_run_input_layer(x) +h = nn_run_hidden_layer(processed_input) +outputs = nn_classify(h) +loss = nn_calculate_loss(outputs) +optimize = )init = tf.global_variables_initializer() +saver = ) + +class TaggerModel: + + IDT def __init__(self, embeddings= , model_path= ) IDT IDT self.session = tf.Session() + IDT IDT if model_path is not None: + IDT IDT IDT saver.restore(self.session, model_path) + IDT IDT else: + IDT IDT IDT self.session.run(init) + IDT IDT IDT self.initial_embedding(embeddings[0], embeddings[1]) + + IDT def train(self, input_list, labels): + IDT IDT feed_dict = TaggerModel.get_feed_dict(input_list, labels) + IDT IDT _, iter_loss = , loss], feed_dict= ) IDT IDT return iter_loss + + IDT def predict(self, input_list): + IDT IDT feed_dict = TaggerModel.get_feed_dict(input_list) + IDT IDT prediction = , feed_dict= ) IDT IDT return prediction['tag'] + + IDT def save_model(self, save_path, global_step): + IDT IDT saver.save(self.session, save_path, global_step= ) IDT IDT + + IDT def initial_embedding(self, word_emb_input, subword_emb_input): + IDT IDT word_emb_placeholder = , [word_vocab_size, word_embedding_dim], name= ) IDT IDT subword_emb_placeholder = , [subword_vocab_size, subword_embedding_dim], name= ) IDT IDT word_assign = tf.assign(word_embedding, word_emb_placeholder) + IDT IDT subword_assign = tf.assign(subword_embedding, subword_emb_placeholder) + IDT IDT feed_dict = {word_emb_placeholder: word_emb_input, subword_emb_placeholder: subword_emb_input} + IDT IDT self.session.run([word_assign, subword_assign], feed_dict= ) + IDT @staticmethod + IDT def get_feed_dict(input_list, labels= ) IDT IDT feed_dict = dict() + IDT IDT feed_dict[x['words']] = input_list[0] + IDT IDT feed_dict[x['subwords']] = input_list[1] + IDT IDT feed_dict[x['subwords_len']] = input_list[2] + IDT IDT if labels is not None: + IDT IDT IDT feed_dict[label['tag']], feed_dict[label['word_freq']] = labels + IDT IDT return feed_dict +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import sys +if sys.version[0] == '2': + IDT import cPickle as pkl +else: + IDT import pickle as pkl +import numpy as np +import tensorflow as tf +from PNN.author import utils +dtype = utils.DTYPE + + +class Model: + + IDT def __init__(self): + IDT IDT self.sess = None + IDT IDT self.X = None + IDT IDT self.y = None + IDT IDT self.layer_keeps = None + IDT IDT self.vars = None + IDT IDT self.keep_prob_train = None + IDT IDT self.keep_prob_test = None + + IDT def run(self, fetches, X= , y= , mode= ) IDT IDT feed_dict = {} + IDT IDT if type(self.X) is list: + IDT IDT IDT for i in range(len(X)): + IDT IDT IDT IDT feed_dict[self.X[i]] = X[i] + IDT IDT else: + IDT IDT IDT feed_dict[self.X] = X + IDT IDT if y is not None: + IDT IDT IDT feed_dict[self.y] = y + IDT IDT if self.layer_keeps is not None: + IDT IDT IDT if mode == 'train': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_train + IDT IDT IDT elif mode == 'test': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_test + IDT IDT return self.sess.run(fetches, feed_dict) + + IDT def dump(self, model_path): + IDT IDT var_map = {} + IDT IDT for name, var in self.vars.iteritems(): + IDT IDT IDT var_map[name] = self.run(var) + IDT IDT pkl.dump(var_map, open(model_path, 'wb')) + IDT IDT + + +class LR(Model): + + IDT def __init__(self, input_dim= , output_dim= , init_path= , opt_algo= , learning_rate= , l2_weight= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , logits= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FM(Model): + + IDT def __init__(self, input_dim= , output_dim= , factor_order= , init_path= , opt_algo= , learning_rate= , l2_w= , l2_v= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT v = self.vars['v'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + IDT IDT IDT xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + IDT IDT IDT p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FNN(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT node_in = num_inputs * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class DeepFM(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('weight_%d' % i, [field_sizes[i], 1], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('bias', [1], 'zero', dtype)) + IDT IDT node_in = num_inputs * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = [self.vars['weight_%d' % i] for i in range(num_inputs)] + IDT IDT IDT v = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b = self.vars['bias'] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xv = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], v[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xv + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT xv = tf.reshape(xv, [-1, num_inputs, embed_size]) + IDT IDT IDT p = 0.5 * tf.reduce_sum(tf.square(tf.reduce_sum(xv, 1)) - tf.reduce_sum(tf.square(xv), 1), 1) + IDT IDT IDT xw = tf.reduce_sum(xw, 1) + IDT IDT IDT logits = tf.reshape(l + xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class CCPM(Model): + + IDT def __init__(self, field_sizes= , embed_size= , filter_sizes= , layer_acts= , drop_out= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + IDT IDT init_vars.append(('b1', [1], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + IDT IDT IDT f1 = self.vars['f1'] + IDT IDT IDT l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + IDT IDT IDT f2 = self.vars['f2'] + IDT IDT IDT l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT l = tf.matmul(l, w1) + b1 + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN1(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + IDT IDT IDT l = tf.concat([xw, ip], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN2(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= , layer_norm= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT init_vars.append(('kernel', [embed_size, num_pairs, embed_size], 'xavier', dtype)) + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT k = self.vars['kernel'] + IDT IDT IDT p = tf.expand_dims(p, 1) + IDT IDT IDT kp = tf.reduce_sum(tf.multiply(tf.transpose(tf.reduce_sum(tf.multiply(p, k), -1), [0, 2, 1]), q), -1) + IDT IDT IDT l = tf.concat([xw, kp], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +class FullyConnectedLayer(object): + + IDT def __init__(self, inp, dim, nonlinearity= ) IDT IDT self.W = tf.Variable(tf.random_normal(dim)) + IDT IDT self.b = , shape= , dim[1]))) ) IDT IDT if nonlinearity: + IDT IDT IDT self.out = nonlinearity(tf.matmul(inp, self.W) + self.b) + IDT IDT else: + IDT IDT IDT self.out = tf.matmul(inp, self.W) + self.b + + +class NeuralNetwork(object): + + IDT def __init__(self, inp, indim, enddim, hidden_layers= , nonlinearity= , name= ) IDT IDT self.layers = [] + IDT IDT self.x = inp + IDT IDT self.indim = indim + IDT IDT self.enddim = enddim + IDT IDT inp = self.x + IDT IDT prev_dim = indim + IDT IDT for out_dim in hidden_layers: + IDT IDT IDT self.layers.append(FullyConnectedLayer(inp, (prev_dim, out_dim), nonlinearity= ) IDT IDT IDT inp = self.layers[-1].out + IDT IDT IDT prev_dim = out_dim + IDT IDT self.layers.append(FullyConnectedLayer(inp, (prev_dim, enddim), nonlinearity= ) IDT IDT self.out = self.layers[-1].out + + IDT def variables(self): + IDT IDT variables = [] + IDT IDT for layer in self.layers: + IDT IDT IDT variables.extend([layer.W, layer.b]) + IDT IDT return variables + + +class CriticNeuralNetwork(object): + + IDT def __init__(self, statedim, actiondim, enddim, hidden_layers= , nonlinearity= , name= ) IDT IDT self.state = tf.placeholder(tf.float32, [None, statedim]) + IDT IDT self.action = tf.placeholder(tf.float32, [None, actiondim]) + IDT IDT inp = tf.concat(1, [self.state, self.action]) + IDT IDT self.network = NeuralNetwork(inp, statedim + actiondim, enddim, hidden_layers, nonlinearity, name) + IDT IDT self.out = self.network.out + + +class ActorNeuralNetwork(object): + + IDT def __init__(self, indim, enddim, hidden_layers= , nonlinearity= , name= ) IDT IDT self.x = tf.placeholder(tf.float32, [None, indim]) + IDT IDT self.network = NeuralNetwork(self.x, indim, enddim, hidden_layers, nonlinearity, name) + IDT IDT self.out = self.network.out + + IDT def variables(self): + IDT IDT return self.network.variables() + + +class Critic(object): + + IDT def __init__(self, nn, parameters, discount= ) IDT IDT self.discount = tf.constant(discount) + IDT IDT self._setup_q_calculation(nn) + IDT IDT self._setup_next_q_calulcation(nn) + IDT IDT self._setup_train_step(parameters) + IDT IDT init = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.session.run(init) + + IDT def _setup_q_calculation(self, nn): + IDT IDT self.state = nn.state + IDT IDT self.action = nn.action + IDT IDT self.q = nn.out + + IDT def _setup_next_q_calulcation(self, nn): + IDT IDT self.r = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.terminal = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.update = self.q * (self.terminal * self.discount) + self.r + + IDT def _setup_train_step(self, parameters): + IDT IDT self.target = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.loss = tf.reduce_sum(tf.square(self.target - self.q)) + IDT IDT self.train_step = ) IDT IDT self.gradients = tf.gradients(self.q, self.action) + + IDT def next_q(self, rewards, next_state, next_action, terminals): + IDT IDT return self.session.run(self.update, feed_dict= , self.state: next_state, self.action: next_action, self.terminal: terminals}) ) + IDT def current_q(self, state, action): + IDT IDT return self.session.run(self.q, feed_dict= , self.action: action}) ) + IDT def calcloss(self, state, action, rewards, next_state, terminals): + IDT IDT target = self.calcq(rewards, next_state, terminals) + IDT IDT return self.session.run(self.loss, feed_dict= , self.x: state}) ) + IDT def calcgradient(self, state, action): + IDT IDT return self.session.run(self.gradients, feed_dict= , self.action: action}) ) + IDT def trainstep(self, state, action, rewards, next_state, next_action, terminals): + IDT IDT target = self.next_q(rewards, next_state, next_action, terminals) + IDT IDT return self.session.run(self.train_step, feed_dict= , self.state: state, self.action: action}) ) + IDT def coldstart(self, state, action): + IDT IDT target = [[-4] for _ in range(len(x))] + IDT IDT return self.session.run(self.train_step, feed_dict= , self.state: state, self.action: action}) ) + IDT def __exit__(self): + IDT IDT self.session.close() + + +class Actor(object): + + IDT def __init__(self, nn, scaler, parameters, discount= ) IDT IDT self.discount = tf.constant(discount) + IDT IDT self._setup_q_calculation(nn, scaler) + IDT IDT self._setup_train_step(nn, parameters) + IDT IDT self.session = tf.Session() + IDT IDT init = tf.global_variables_initializer() + IDT IDT self.session.run(init) + + IDT def _setup_train_step(self, nn, parameters): + IDT IDT self.critic_gradients = tf.placeholder(tf.float32, [None, nn.out.get_shape()[1]]) + IDT IDT self.trainable_variables = nn.variables() + IDT IDT self.gradients = tf.gradients(nn.out, self.trainable_variables, -self.critic_gradients) + IDT IDT self.train_step = , self.trainable_variables)) ) + IDT def _setup_q_calculation(self, nn, scaler): + IDT IDT self.x = nn.x + IDT IDT self.act = tf.nn.tanh(nn.out) * scaler + + IDT def action(self, state): + IDT IDT return self.session.run(self.act, feed_dict= ) + IDT def actions(self, state): + IDT IDT return self.session.run(self.act, feed_dict= ) + IDT def train(self, state, gradients): + IDT IDT self.session.run(self.train_step, feed_dict= , self.critic_gradients: map(lambda x, d= , gradients)}) ) + IDT def __exit__(self): + IDT IDT self.session.close() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +class MainModel(object): + + IDT def __init__(self, sentMaxl, sentMaxp, sentMaxr, num_classes, wv, emb_size, l2_reg_lambda, learning_rate): + IDT IDT tf.reset_default_graph() + IDT IDT self.X_lids = , [None, sentMaxl], name= ) IDT IDT self.X_pids = , [None, sentMaxp], name= ) IDT IDT self.X_rids = , [None, sentMaxr], name= ) IDT IDT self.X_llen = , [None], name= ) IDT IDT self.X_plen = , [None], name= ) IDT IDT self.X_rlen = , [None], name= ) IDT IDT self.input_y = , [None, num_classes], name= ) IDT IDT self.dropout_keep_prob = , name= ) IDT IDT W_wemb = tf.Variable(wv) + IDT IDT , '--, self.X_lids ', self.X_lids.get_shape()) + IDT IDT emb_l = tf.nn.embedding_lookup(W_wemb, self.X_lids) + IDT IDT ) + IDT IDT emb_p = tf.nn.embedding_lookup(W_wemb, self.X_pids) + IDT IDT emb_r = tf.nn.embedding_lookup(W_wemb, self.X_rids) + IDT IDT emb_l = tf.expand_dims(emb_l, -1) + IDT IDT ) + IDT IDT emb_p = tf.expand_dims(emb_p, -1) + IDT IDT emb_r = tf.expand_dims(emb_r, -1) + IDT IDT pooled_l = , ksize= , sentMaxl, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT pooled_p = , ksize= , sentMaxp, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT pooled_r = , ksize= , sentMaxr, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT ) + IDT IDT final_l = tf.reshape(pooled_l, [-1, emb_size]) + IDT IDT final_p = tf.reshape(pooled_p, [-1, emb_size]) + IDT IDT final_r = tf.reshape(pooled_r, [-1, emb_size]) + IDT IDT ) + IDT IDT XX = tf.concat(1, [final_l, final_p, final_r]) + IDT IDT ) + IDT IDT h = tf.nn.dropout(XX, self.dropout_keep_prob) + IDT IDT h = tf.nn.relu(h) + IDT IDT W = , num_classes], stddev= , name= ) IDT IDT b = , shape= , name= ) IDT IDT scores = , W, b, name= ) IDT IDT ) + IDT IDT self.predictions = , 1, name= ) IDT IDT losses = tf.nn.softmax_cross_entropy_with_logits(scores, self.input_y) + IDT IDT self.loss = tf.reduce_mean(losses) + l2_reg_lambda * (tf.nn.l2_loss(W) + tf.nn.l2_loss(b)) + IDT IDT self.correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) + IDT IDT self.accuracy = , 'float'), name= ) IDT IDT self.optimizer = tf.train.AdamOptimizer(learning_rate) + IDT IDT self.grads_and_vars = self.optimizer.compute_gradients(self.loss) + IDT IDT self.global_step = , name= , trainable= ) IDT IDT self.train_op = , global_step= ) IDT IDT session_conf = , log_device_placement= ) IDT IDT self.sess = ) IDT IDT self.sess.run(tf.global_variables_initializer()) + IDT IDT self.saver = ) + IDT def train_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y, drop_out): + IDT IDT feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: drop_out} + IDT IDT _, step, loss, accuracy, predictions = self.sess.run([self.train_op, self.global_step, self.loss, self.accuracy, self.predictions], feed_dict) + IDT IDT return loss, accuracy + + IDT def test_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y): + IDT IDT feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: 1.0} + IDT IDT accuracy = self.sess.run([self.accuracy], feed_dict) + IDT IDT return accuracy +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import tensorflow.examples.tutorials.mnist.input_data +mnist = , one_hot= )x = tf.placeholder(tf.float32, [None, 784]) +W = tf.Variable(tf.zeros([784, 10])) +b = tf.Variable(tf.zeros([10])) +y = tf.nn.softmax(tf.matmul(x, W) + b) +y_ = tf.placeholder('float', [None, 10]) + + +saver = tf.train.Saver() +cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) +train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) +init = tf.global_variables_initializer() +sess = tf.Session() +sess.run(init) +for i in range(10000): + IDT batch_xs, batch_ys = mnist.train.next_batch(100) + IDT sess.run(train_step, feed_dict= , y_: batch_ys}) ) + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder('float') +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) +train_step = tf.train.AdamOptimizer(0.0001).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.run(tf.initialize_all_variables()) +for i in range(20000): + IDT batch = mnist.train.next_batch(50) + IDT if i % 100 == 0: + IDT IDT train_accuracy = , feed_dict= , y_: batch[1], keep_prob: 1.0}) ) IDT IDT ) + IDT train_step.run(session= , feed_dict= , y_: batch[1], keep_prob: 0.5}) )) +saver.save(sess, './check/Mymodel.ckpt') +with open('myNote.txt', 'w+') as fi: + IDT for i in b.eval(sess): + IDT IDT fi.write(i + ' ') +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +hello_constant = tf.constant('Hello World!') +with tf.Session() as sess: + IDT output = sess.run(hello_constant) + IDT +A = tf.constant(1234) +B = tf.constant([123, 456, 789]) +C = tf.constant([[123, 456, 789], [222, 333, 444]]) +with tf.Session() as sess2: + IDT output1 = sess2.run(C) + IDT +x = tf.placeholder(tf.string) +y = tf.placeholder(tf.int32) +z = tf.placeholder(tf.float32) + + +def placeHolder(string): + IDT output = None + IDT x = tf.placeholder(tf.string) + IDT with tf.Session() as sess: + IDT IDT output = , feed_dict= ) IDT return + + +placeHolder('Good Morning World!') +x = tf.add(5, 2) +x = tf.subtract(10, 4) +y = tf.multiply(2, 5) +tf.subtract(tf.cast(tf.constant(2.0), tf.int32), tf.constant(1)) +x = tf.constant(10) +y = tf.constant(2) +z = tf.subtract(tf.divide(x, y), tf.cast(tf.constant(1), tf.float64)) +with tf.Session() as sess: + IDT output = sess.run(z) + IDT +matW = tf.constant([[-0.5, 0.2, 0.1], [0.7, -0.8, 0.2]]) +matX = tf.constant([[0.2], [0.5], [0.6]]) +b = tf.constant([[0.1], [0.2]]) +prod = tf.matmul(matW, matX) +sum1 = tf.add(prod, b) +with tf.Session() as sess: + IDT output = sess.run(sum1) + IDT +x = tf.Variable(5) +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT sess.run(init) +n_features = 120 +n_labels = 5 +weights = tf.Variable(tf.truncated_normal((n_features, n_labels))) +n_labels = 5 +bias = tf.Variable(tf.zeros(n_labels)) + + +def get_weights(n_features, n_labels): + IDT return tf.Variable(tf.truncated_normal((n_features, n_labels))) + + +def get_biases(n_labels): + IDT return tf.Variable(tf.zeros(n_labels)) + + +def linear(input, w, b): + IDT return tf.add(tf.matmul(input, w), b) + + +x = tf.nn.softmax([2.0, 1.0, 0.2]) + + +def run(): + IDT output = None + IDT logit_data = [2.0, 1.0, 0.1] + IDT logits = tf.placeholder(tf.float32) + IDT softmax = tf.nn.softmax(logit_data) + IDT with tf.Session() as sess: + IDT IDT output = , feed_dict= ) IDT return output + + +softmax_data = [0.7, 0.2, 0.1] +one_hot_data = [1.0, 0.0, 0.0] +softmax = tf.placeholder(tf.float32) +one_hot = tf.placeholder(tf.float32) +cross_entropy = -tf.reduce_sum(tf.multiply(one_hot, tf.log(softmax))) +with tf.Session() as sess: + IDT ) +from tensorflow.examples.tutorials.mnist import input_data +import tensorflow as tf +n_input = 784 +n_classes = 10 +mnist = , one_hot= )train_features = mnist.train.images +test_features = mnist.test.images +train_labels = mnist.train.labels.astype(np.float32) +test_labels = mnist.test.labels.astype(np.float32) +weights = tf.Variable(tf.random_normal([n_input, n_classes])) +bias = tf.Variable(tf.random_normal([n_classes])) +features = tf.placeholder(tf.float32, [None, n_input]) +labels = tf.placeholder(tf.float32, [None, n_classes]) +example_features = [['F11', 'F12', 'F13', 'F14'], ['F21', 'F22', 'F23', 'F24'], ['F31', 'F32', 'F33', 'F34'], ['F41', 'F42', 'F43', 'F44']] +example_labels = [['L11', 'L12'], ['L21', 'L22'], ['L31', 'L32'], ['L41', 'L42']] +import math + + +def batches(batch_size, features, labels): + IDT """ + IDT Create batches of features and labels + IDT :param batch_size: The batch size + IDT :param features: List of features + IDT :param labels: List of labels + IDT :return: Batches of (Features, Labels) + IDT """ + IDT assert len(features) = ) IDT output_batches = [] + IDT sample_size = len(features) + IDT for start_i in range(0, sample_size, batch_size): + IDT IDT end_i = start_i + batch_size + IDT IDT batch = [features[start_i:end_i], labels[start_i:end_i]] + IDT IDT output_batches.append(batch) + IDT return output_batches + + +example_batches = batches(batch_size, example_features, example_labels) +hidden_layer = tf.add(tf.matmul(features, hidden_weights), hidden_biases) +hidden_layer = tf.nn.relu(hidden_layer) +output = tf.add(tf.matmul(hidden_layer, output_weights), output_biases) +import tensorflow as tf +output = None +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[1.0, 2.0, 3.0, 4.0], [-1.0, -2.0, -3.0, -4.0], [11.0, 12.0, 13.0, 14.0]]) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT ) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +import tensorflow as tf +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[0.0, 2.0, 3.0, 4.0], [0.1, 0.2, 0.3, 0.4], [11.0, 12.0, 13.0, 14.0]]) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) +) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +def main(): + IDT with tf.name_scope('input'): + IDT IDT + IDT IDT NX = 60 + IDT IDT NY = 60 + IDT IDT RENDER = True + IDT IDT n_step = 500 + IDT IDT px = NY * (NX // 3) + IDT IDT py_ = NY * NX // (5 * 5) * 8 + IDT with tf.name_scope('model'): + IDT IDT + IDT IDT s0 = , shape= , NX // 3], name= ) IDT IDT s1 = , shape= , NX // 3], name= ) IDT IDT s2 = , shape= , NX // 3], name= ) IDT IDT _red_ = , shape= , name= ) IDT IDT _green_ = , shape= , name= ) IDT IDT _blue_ = , shape= , name= ) IDT IDT P_r = _red_ * s0 + _red_ * -1 * (s0 * -1 + 1) + IDT IDT P_g = _green_ * s1 + _green_ * -1 * (s1 * -1 + 1) + IDT IDT P_b = _blue_ * s2 + _blue_ * -1 * (s2 * -1 + 1) + IDT IDT Pool_red = , [1, NY, NX // 3, 1]), ksize= , NY, NX // 3, 1], strides= , 1, 1, 1], padding= ) IDT IDT Pool_green = , [1, NY, NX // 3, 1]), ksize= , NY, NX // 3, 1], strides= , 1, 1, 1], padding= ) IDT IDT Pool_blue = , [1, NY, NX // 3, 1]), ksize= , NY, NX // 3, 1], strides= , 1, 1, 1], padding= ) IDT IDT Pool = , [1, NY, NX // 3, 1]), ksize= , NY, NX // 3, 1], strides= , 1, 1, 1], padding= ) IDT IDT Loss_red = tf.nn.l2_loss(Pool - Pool_red, 'Loss_red') + IDT IDT Loss_green = tf.nn.l2_loss(Pool - Pool_green, 'Loss_green') + IDT IDT Loss_blue = tf.nn.l2_loss(Pool - Pool_blue, 'Loss_blue') + IDT IDT Loss_Pool = tf.nn.l2_loss(Pool, 'Loss_Pool') + IDT IDT Relu = tf.nn.relu(Loss_red) + IDT with tf.name_scope('train'): + IDT IDT + IDT with tf.name_scope('init'): + IDT IDT + IDT IDT init = tf.global_variables_initializer() + IDT with tf.name_scope('session'): + IDT IDT + IDT IDT sess = tf.Session() + IDT IDT sess.run(init) + IDT IDT i = 0 + IDT IDT with open('BdD', 'r') as f: + IDT IDT IDT for line in f.readlines(): + IDT IDT IDT IDT i += 1 + IDT IDT IDT IDT if i % 3 == 1: + IDT IDT IDT IDT IDT red_point, row = line.strip().split(':') + IDT IDT IDT IDT IDT stat00, foo = row.split(';') + IDT IDT IDT IDT IDT stat0 = [[(0) for x in range(NX // 3)] for y in range(NY)] + IDT IDT IDT IDT IDT for y in range(NY): + IDT IDT IDT IDT IDT IDT for x in range(NX // 3): + IDT IDT IDT IDT IDT IDT IDT value = stat00[y * 41 + (x + 1) * 2] + IDT IDT IDT IDT IDT IDT IDT stat0[y][x] = int(value) + IDT IDT IDT IDT if i % 3 == 2: + IDT IDT IDT IDT IDT green_point, row = line.strip().split(':') + IDT IDT IDT IDT IDT stat01, foo = row.split(';') + IDT IDT IDT IDT IDT stat1 = [[(0) for x in range(NX // 3)] for y in range(NY)] + IDT IDT IDT IDT IDT for y in range(NY): + IDT IDT IDT IDT IDT IDT for x in range(NX // 3): + IDT IDT IDT IDT IDT IDT IDT value = stat01[y * 41 + (x + 1) * 2] + IDT IDT IDT IDT IDT IDT IDT stat1[y][x] = int(value) + IDT IDT IDT IDT if i % 3 == 0: + IDT IDT IDT IDT IDT blue_point, row = line.strip().split(':') + IDT IDT IDT IDT IDT stat02, foo = row.split(';') + IDT IDT IDT IDT IDT stat2 = [[(0) for x in range(NX // 3)] for y in range(NY)] + IDT IDT IDT IDT IDT for y in range(NY): + IDT IDT IDT IDT IDT IDT for x in range(NX // 3): + IDT IDT IDT IDT IDT IDT IDT value = stat02[y * 41 + (x + 1) * 2] + IDT IDT IDT IDT IDT IDT IDT stat2[y][x] = int(value) + IDT IDT IDT IDT IDT p = , feed_dict= , _red_: [red_point], s1: stat1, _green_: [green_point], s2: stat2, _blue_: [blue_point]}) ) IDT IDT IDT IDT IDT + IDT IDT + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import tensorflow as tf +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' +import random +import math +import csv +import ast +import numpy as np +import sys +import inspectcheckpoint + + +def init_weights(shape): + IDT return tf.Variable(tf.random_normal(shape, stddev= ) + +def model1(X, w_h, w_h2, w_o): + IDT h = tf.nn.tanh(tf.matmul(X, w_h)) + IDT h2 = tf.nn.tanh(tf.matmul(h, w_h2)) + IDT return tf.matmul(h2, w_o) + + +X = tf.placeholder('float', [None, 4]) +Y = tf.placeholder('float', [None, 3]) +w_h = init_weights([4, 60]) +w_h2 = init_weights([60, 60]) +w_o = init_weights([60, 3]) +p_keep_input = tf.placeholder('float') +p_keep_hidden = tf.placeholder('float') +py_x = model1(X, w_h, w_h2, w_o) +predict_op1 = tf.argmax(py_x, 1) +secondplayersess1 = tf.Session() +init = tf.global_variables_initializer() +saver = tf.train.Saver() +secondplayersess2 = tf.Session() +savefile2 = 'models/turnmodel1.ckpt' +saver.restore(secondplayersess2, savefile2) +savefile = 'models/rivermodel1.ckpt' +saver.restore(secondplayersess1, savefile) +player1sess1 = tf.Session() +player1sess2 = tf.Session() +player1sess3 = tf.Session() +savefile3 = 'models/flopmodel2.ckpt' +savefile4 = 'models/turnmodel2.ckpt' +savefile5 = 'models/rivermodel2.ckpt' +saver.restore(player1sess1, savefile3) +saver.restore(player1sess2, savefile4) +saver.restore(player1sess3, savefile5) +nn3sess1 = tf.Session() +nn3sess2 = tf.Session() +nn3sess3 = tf.Session() +nn3savefile1 = 'models/flopmodel3.ckpt' +nn3savefile2 = 'models/turnmodel3.ckpt' +nn3savefile3 = 'models/rivermodel3.ckpt' +saver.restore(nn3sess1, nn3savefile1) +saver.restore(nn3sess2, nn3savefile2) +saver.restore(nn3sess3, nn3savefile3) +nn3sess1.run(init) +tf.reset_default_graph() +nn4sess1 = tf.Session() +nn4sess2 = tf.Session() +nn4sess3 = tf.Session() +init = tf.global_variables_initializer() + + +def model2(X, w_h, w_h2, w_h3, w_o): + IDT h = tf.nn.tanh(tf.matmul(X, w_h)) + IDT h2 = tf.nn.tanh(tf.matmul(h, w_h2)) + IDT h3 = tf.nn.tanh(tf.matmul(h2, w_h3)) + IDT return tf.matmul(h3, w_o) + + +w_h2 = init_weights([4, 60]) +w_h22 = init_weights([60, 60]) +w_h32 = init_weights([60, 60]) +w_o2 = init_weights([60, 3]) +""" +p_keep_input = tf.placeholder("float") +p_keep_hidden = tf.placeholder("float") +""" +X2 = tf.placeholder('float', [None, 4]) +Y2 = tf.placeholder('float', [None, 3]) +py_x2 = model2(X2, w_h2, w_h22, w_h32, w_o2) +predict_op2 = tf.argmax(py_x2, 1) +saver2 = tf.train.Saver() +nn4savefile1 = 'models/flopmodel4.ckpt' +nn4savefile2 = 'models/turnmodel4.ckpt' +nn4savefile3 = 'models/rivermodel4.ckpt' +saver2.restore(nn4sess1, nn4savefile1) +saver2.restore(nn4sess2, nn4savefile2) +saver2.restore(nn4sess3, nn4savefile3) +nn6sess1 = tf.Session() +nn6sess2 = tf.Session() +nn6sess3 = tf.Session() +nn6savefile1 = 'models/flopmodel6.ckpt' +nn6savefile2 = 'models/turnmodel6.ckpt' +nn6savefile3 = 'models/rivermodel6.ckpt' +saver2.restore(nn6sess1, nn6savefile1) +saver2.restore(nn6sess2, nn6savefile2) +saver2.restore(nn6sess3, nn6savefile3) +nn7sess1 = tf.Session() +nn7sess2 = tf.Session() +nn7sess3 = tf.Session() +nn7savefile1 = 'models/flopmodel7.ckpt' +nn7savefile2 = 'models/turnmodel7.ckpt' +nn7savefile3 = 'models/rivermodel7.ckpt' +saver2.restore(nn7sess1, nn7savefile1) +saver2.restore(nn7sess2, nn7savefile2) +saver2.restore(nn7sess3, nn7savefile3) +nn4sess2.run(init) +tf.reset_default_graph() + + +def model3(X, w_h, w_h2, w_h3, w_o): + IDT h = tf.nn.tanh(tf.matmul(X, w_h)) + IDT h2 = tf.nn.tanh(tf.matmul(h, w_h2)) + IDT h3 = tf.nn.tanh(tf.matmul(h2, w_h3)) + IDT return tf.matmul(h3, w_o) + + +X3 = tf.placeholder('float', [None, 5]) +Y3 = tf.placeholder('float', [None, 3]) +w_h3 = init_weights([5, 60]) +w_h23 = init_weights([60, 60]) +w_h33 = init_weights([60, 60]) +w_o3 = init_weights([60, 3]) +p_keep_input = tf.placeholder('float') +p_keep_hidden = tf.placeholder('float') +py_x = model3(X3, w_h3, w_h23, w_h33, w_o3) +predict_op3 = tf.argmax(py_x, 1) +nn5sess1 = tf.Session() +nn5sess2 = tf.Session() +nn5sess3 = tf.Session() +saver3 = tf.train.Saver() +nn5savefile1 = 'models/flopmodel5.ckpt' +nn5savefile2 = 'models/turnmodel5.ckpt' +nn5savefile3 = 'models/rivermodel5.ckpt' +saver3.restore(nn5sess1, nn5savefile1) +saver3.restore(nn5sess2, nn5savefile2) +saver3.restore(nn5sess3, nn5savefile3) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +import tflearn +DEFAULT_PADDING = 'valid' +DEFAULT_ACTIVATION = 'relu' +DEFAULT_STRIDES = [1, 1] +DEFAULT_WEIGHT_DECAY = 0.0 + + +class CNNEnsemble: + + IDT def __init__(self, session, assets, window_size, config): + IDT IDT self.layers = [] + IDT IDT self.session = session + IDT IDT self.config = config + IDT IDT self.global_step = , trainable= ) IDT IDT self.input_prices = , shape= , 3, assets, window_size]) ) IDT IDT self.input_prev_omega = , shape= , assets]) ) IDT IDT self.input_future_prices = , shape= , assets]) ) IDT IDT self.out_nn = self._build_output_network() + IDT IDT self.train_op = self._build_train_operation() + IDT IDT self._saver = tf.train.Saver(tf.trainable_variables()) + + IDT def _build_output_network(self): + IDT IDT layers_cnf = self.config['layers'] + IDT IDT nn = tf.transpose(self.input_prices, [0, 2, 3, 1]) + IDT IDT for lc in layers_cnf[:-1]: + IDT IDT IDT nn = self._make_layer(nn, lc) + IDT IDT IDT self.layers.append(nn) + IDT IDT nn = self._make_output_layer(nn, layers_cnf[-1]) + IDT IDT self.layers.append(nn) + IDT IDT return nn + + IDT @staticmethod + IDT def _make_layer(prev, config): + IDT IDT return tflearn.layers.conv_2d(prev, config['out_channels'], config['kernel'], config.get('strides', DEFAULT_STRIDES), config.get('padding', DEFAULT_PADDING), config.get('activation', DEFAULT_ACTIVATION), regularizer= , None), weight_decay= , DEFAULT_WEIGHT_DECAY)) ) + IDT def _make_output_layer(self, prev, config): + IDT IDT prev = self._add_previous_omega(prev) + IDT IDT prev = self._make_layer(prev, config) + IDT IDT prev = prev[:, :, (0), (0)] + IDT IDT prev = self._attach_cash_bias(prev) + IDT IDT return tflearn.layers.activation(prev, activation= ) + IDT def _add_previous_omega(self, nn): + IDT IDT shape = nn.shape + IDT IDT nn = tf.reshape(nn, [-1, shape[1], 1, shape[2] * shape[3]]) + IDT IDT w = tf.reshape(self.input_prev_omega, [-1, shape[1], 1, 1]) + IDT IDT nn = , w], axis= ) IDT IDT return nn + + IDT @staticmethod + IDT def _attach_cash_bias(nn): + IDT IDT cash_bias = , [1, 1], dtype= , initializer= ) IDT IDT cash_bias = tf.tile(cash_bias, [tf.shape(nn)[0], 1]) + IDT IDT nn = , nn], axis= ) IDT IDT return nn + + IDT def _build_train_operation(self): + IDT IDT future_prices = self._add_cash(self.input_future_prices) + IDT IDT mu = self._calc_commission(future_prices, self.out_nn) + IDT IDT portfolio_values = , reduction_indices= , mu], axis= ) IDT IDT loss = -tf.reduce_mean(tf.log(portfolio_values)) + IDT IDT train_cnf = self.config['training'] + IDT IDT lr = , self.global_step, train_cnf['decay_steps'], train_cnf['decay_rate'], staircase= ) IDT IDT return tf.train.AdadeltaOptimizer(lr).minimize(loss, global_step= ) + IDT @staticmethod + IDT def _add_cash(relative_prices): + IDT IDT return tf.concat([tf.ones([tf.shape(relative_prices)[0], 1]), relative_prices], axis= ) + IDT def _calc_commission(self, future_prices, omega): + IDT IDT future_omega = self._calc_future_omega(future_prices, omega) + IDT IDT w_prime = future_omega[:-1] + IDT IDT w = omega[1:] + IDT IDT return 1 - tf.reduce_sum(tf.abs(w_prime[:, 1:] - w[:, 1:]), axis= ) + IDT @staticmethod + IDT def _calc_future_omega(future_prices, omega): + IDT IDT return future_prices * omega / tf.reduce_sum(future_prices * omega, axis= , (None) ) + IDT def predict(self, prices, previous_omega): + IDT IDT res = , feed_dict= , axis= , self.input_prev_omega: np.expand_dims(previous_omega, axis= ) IDT IDT return res[0] + + IDT def train(self, states, future_prices): + IDT IDT prices, omegas = states + IDT IDT result = , self.out_nn], feed_dict= , self.input_prev_omega: omegas, self.input_future_prices: future_prices}) ) IDT IDT return result[-1] + + IDT def save(self, path): + IDT IDT self._saver.save(self.session, path) + + IDT def restore(self, path): + IDT IDT self._saver.restore(self.session, path) +import tensorflow as tf +""" +matrix1 = tf.constant([[3., 3.]]) +matrix2 = tf.constant([[2.], [2.]]) +product = tf.matmul(matrix1, matrix2) +sess = tf.Session() +with tf.Session() as sess: + result = sess.run(product) + +sess.close() +""" +""" +sess = tf.InteractiveSession() +x = tf.Variable([1.0, 2.0]) +a = tf.constant([3.0, 3.0]) +x.initializer.run() +sub = tf.sub(x, a) +) + +""" +""" +state = , name= )one = tf.constant(1) +new_value = tf.add(state, one) +update = tf.assign(state, new_value) +init_op = tf.initialize_all_variables() +with tf.Session() as sess: + sess.run(init_op) + ) + for _ in range(3): + sess.run(update) + ) +""" +""" +input1 = tf.constant(1) +input2 = tf.constant(2) +input3 = tf.constant(2) + +a = tf.add(input3, input2) +b = tf.matmul(input1, a) + +with tf.Session() as sess: + result = sess.run([a, b]) + + +""" +""" +input1 = tf.placeholder(tf.float32) +input2 = tf.placeholder(tf.float32) +output = tf.mul(input1, input2) + +with tf.Session() as sess: + ) + + + +""" +import tensorflow.examples.tutorials.mnist.input_data as input_data +mnist = , one_hot= )x = tf.placeholder(tf.float32, [None, 784]) +W = tf.Variable(tf.zeros([784, 10])) +b = tf.Variable(tf.zeros([10])) +y = tf.nn.softmax(tf.matmul(x, W) + b) +y_ = tf.placeholder('float', [None, 10]) +cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) +train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) +init = tf.initialize_all_variables() +sess = tf.Session() +sess.run(init) +for i in range(1000): + IDT batch_xs, batch_ys = mnist.train.next_batch(100) + IDT sess.run(train_step, feed_dict= , y_: batch_ys}) )correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) +) + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder('float') +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) +train_step = tf.train.AdamOptimizer(0.0001).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')) +with sess.as_default(): + IDT sess.run(tf.initialize_all_variables()) + IDT for i in range(20000): + IDT IDT batch = mnist.train.next_batch(50) + IDT IDT if i % 100 == 0: + IDT IDT IDT train_accuracy = , y_: batch[1], keep_prob: 1.0}) ) IDT IDT IDT ) + IDT IDT train_step.run(feed_dict= , y_: batch[1], keep_prob: 0.5}) ) IDT ) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import random +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +from tensorflow.examples.tutorials.mnist import input_data +batch_size = 100 +mnist_idx = 20 + + +def forth_test(): + IDT mnist = , one_hot= ) IDT digit = mnist.train.images[mnist_idx] + IDT digitReshape = np.reshape(digit, [1, 28, 28, 1]) + IDT x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + IDT L1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT init = tf.initialize_all_variables() + IDT with tf.Session() as session: + IDT IDT session.run(init) + IDT IDT max_op = , feed_dict= ) IDT plt.imshow(max_op[(0), :, :, (0)], cmap= ) IDT plt.show() + + +def third_test(): + IDT mnist = , one_hot= ) IDT digit = mnist.train.images[mnist_idx] + IDT digitReshape = np.reshape(digit, [1, 28, 28, 1]) + IDT x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + IDT W1 = , 3, 1, 32], stddev= ) IDT L1 = , W1, strides= , 1, 1, 1], padding= ) IDT L1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT W2 = , 3, 32, 64], stddev= ) IDT L2 = , W2, strides= , 1, 1, 1], padding= ) IDT L2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT W3 = , 3, 64, 128], stddev= ) IDT L3 = , W3, strides= , 1, 1, 1], padding= ) IDT L3 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT init = tf.initialize_all_variables() + IDT with tf.Session() as session: + IDT IDT session.run(init) + IDT IDT conv_op = , feed_dict= ) IDT for i in range(1, conv_op.shape[3]): + IDT IDT aa = conv_op[(0), :, :, (i)] + IDT IDT plt.imshow(aa, cmap= ) IDT IDT plt.show() + + +def second_test(): + IDT mnist = , one_hot= ) IDT digit = mnist.train.images[mnist_idx] + IDT digitReshape = np.reshape(digit, [1, 28, 28, 1]) + IDT x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + IDT W1 = , 3, 1, 32], stddev= ) IDT conv = , W1, strides= , 1, 1, 1], padding= ) IDT init = tf.initialize_all_variables() + IDT with tf.Session() as session: + IDT IDT session.run(init) + IDT IDT conv_op = , feed_dict= ) IDT for i in range(1, conv_op.shape[3]): + IDT IDT aa = conv_op[(0), :, :, (i)] + IDT IDT plt.imshow(aa, cmap= ) IDT IDT plt.show() + + +def first_test(): + IDT mnist = , one_hot= ) IDT digit = mnist.train.images[mnist_idx] + IDT digitReshape = np.reshape(digit, [1, 28, 28, 1]) + IDT x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + IDT kernel = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]).astype(np.float32) + IDT kernelReshape = np.reshape(kernel, [3, 3, 1, 1]) + IDT conv = , kernelReshape, strides= , 1, 1, 1], padding= ) IDT init = tf.initialize_all_variables() + IDT with tf.Session() as session: + IDT IDT session.run(init) + IDT IDT conv_op = , feed_dict= ) IDT im = np.reshape(conv_op, [28, 28]) + IDT plt.imshow(im, cmap= ) IDT plt.show() + + +if __name__ == '__main__': + IDT third_test() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import tensorflow as tf +a = tf.placeholder('float') +b = tf.placeholder('float') +y = tf.multiply(a, b) + + +def multiplication_basics(): + IDT with tf.Session() as sess: + IDT IDT ) + IDT IDT ) + + +def hello_world(): + IDT hello = tf.constant('Hello,Tensorflow!') + IDT sess = tf.Session() + IDT ) + + +def matrix_multiplication(): + IDT matrix1 = tf.constant([[3.0, 3.0]]) + IDT matrix2 = tf.constant([[2.0], [2.0]]) + IDT product = tf.matmul(matrix1, matrix2) + IDT with tf.Session() as sess: + IDT IDT result = sess.run(product) + IDT IDT + + +def nearest_neighbor(): + IDT import numpy as np + IDT from tensorflow.examples.tutorials.mnist import input_data + IDT mnist = , one_hot= ) IDT Xtr, Ytr = mnist.train.next_batch(5000) + IDT Xte, Yte = mnist.test.next_batch(200) + IDT xtr = tf.placeholder('float', [None, 784]) + IDT xte = tf.placeholder('float', [784]) + IDT distance = , tf.negative(xte))), reduction_indices= ) IDT pred = tf.arg_min(distance, 0) + IDT accuracy = 0.0 + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT for i in range(len(Xte)): + IDT IDT IDT nn_index = , feed_dict= , xte: Xte[(i), :]}) ) IDT IDT IDT , 'True Class:', np.argmax(Yte[i])) + IDT IDT IDT if np.argmax(Ytr[nn_index]) = ) IDT IDT IDT IDT accuracy += 1.0 / len(Xte) + IDT IDT + IDT IDT + + +def linear_regression(): + IDT import numpy + IDT import matplotlib.pyplot as plt + IDT rng = numpy.random + IDT learning_rate = 0.01 + IDT training_epochs = 1000 + IDT display_step = 50 + IDT train_X = numpy.asarray([3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1]) + IDT train_Y = numpy.asarray([1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3]) + IDT n_samples = train_X.shape[0] + IDT X = tf.placeholder('float') + IDT Y = tf.placeholder('float') + IDT W = , name= ) IDT b = , name= ) IDT pred = tf.add(tf.multiply(X, W), b) + IDT cost = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples) + IDT optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT for epoch in range(training_epochs): + IDT IDT IDT for x, y in zip(train_X, train_Y): + IDT IDT IDT IDT sess.run(optimizer, feed_dict= , Y: y}) ) IDT IDT IDT if (epoch + 1) % display_step == 0: + IDT IDT IDT IDT c = , feed_dict= , Y: train_Y}) ) IDT IDT IDT IDT , 'cost= , '{:.9f}'.format(c), 'W= , sess.run(W), 'b= , sess.run(b)) ) IDT IDT + IDT IDT training_cost = , feed_dict= , Y: train_Y}) ) IDT IDT , 'b=', sess.run(b), '\n') + IDT IDT plt.plot(train_X, train_Y, 'ro', label= ) IDT IDT plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label= ) IDT IDT plt.legend() + IDT IDT plt.show() + IDT IDT test_X = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1]) + IDT IDT test_Y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03]) + IDT IDT + IDT IDT testing_cost = , 2)) / (2 * test_X.shape[0]), feed_dict= , Y: test_Y}) ) IDT IDT + IDT IDT ) + IDT IDT plt.plot(test_X, test_Y, 'bo', label= ) IDT IDT plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label= ) IDT IDT plt.legend() + IDT IDT plt.show() + + +def logistic_regression(): + IDT from tensorflow.examples.tutorials.mnist import input_data + IDT mnist = , one_hot= ) IDT learning_rate = 0.01 + IDT training_epochs = 25 + IDT batch_size = 100 + IDT display_step = 1 + IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT y = tf.placeholder(tf.float32, [None, 10]) + IDT W = tf.Variable(tf.zeros([784, 10])) + IDT b = tf.Variable(tf.zeros([10])) + IDT pred = tf.nn.softmax(tf.matmul(x, W) + b) + IDT cost = , reduction_indices= ) IDT optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT for epoch in range(training_epochs): + IDT IDT IDT avg_cost = 0.0 + IDT IDT IDT total_batch = int(mnist.train.num_examples / batch_size) + IDT IDT IDT for i in range(total_batch): + IDT IDT IDT IDT batch_xs, batch_ys = mnist.train.next_batch(batch_size) + IDT IDT IDT IDT _, c = , cost], feed_dict= , y: batch_ys}) ) IDT IDT IDT IDT avg_cost += c / total_batch + IDT IDT IDT if (epoch + 1) % display_step == 0: + IDT IDT IDT IDT , 'cost=', '{:.9f}'.format(avg_cost)) + IDT IDT + IDT IDT correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) + IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT IDT ) + + +def multilayer_perceptron(): + IDT from tensorflow.examples.tutorials.mnist import input_data + IDT mnist = , one_hot= ) IDT learning_rate = 0.001 + IDT training_epochs = 15 + IDT batch_size = 100 + IDT display_step = 1 + IDT n_hidden_1 = 256 + IDT n_hidden_2 = 256 + IDT n_input = 784 + IDT n_classes = 10 + IDT x = tf.placeholder('float', [None, n_input]) + IDT y = tf.placeholder('float', [None, n_classes]) + + IDT def perceptron(x, weights, biases): + IDT IDT layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + IDT IDT layer_1 = tf.nn.relu(layer_1) + IDT IDT layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) + IDT IDT layer_2 = tf.nn.relu(layer_2) + IDT IDT out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + IDT IDT return out_layer + IDT weights = {'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))} + IDT biases = {'b1': tf.Variable(tf.random_normal([n_hidden_1])), 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 'out': tf.Variable(tf.random_normal([n_classes]))} + IDT pred = perceptron(x, weights, biases) + IDT cost = , labels= ) IDT optimizer = ) IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT for epoch in range(training_epochs): + IDT IDT IDT avg_cost = 0.0 + IDT IDT IDT total_batch = int(mnist.train.num_examples / batch_size) + IDT IDT IDT for i in range(total_batch): + IDT IDT IDT IDT batch_x, batch_y = mnist.train.next_batch(batch_size) + IDT IDT IDT IDT _, c = , cost], feed_dict= , y: batch_y}) ) IDT IDT IDT IDT avg_cost += c / total_batch + IDT IDT IDT if epoch % display_step == 0: + IDT IDT IDT IDT , 'cost=', '{:.9f}'.format(avg_cost)) + IDT IDT + IDT IDT correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) + IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + IDT IDT ) + + +def convolutional_network(): + IDT from tensorflow.examples.tutorials.mnist import input_data + IDT mnist = , one_hot= ) IDT learning_rate = 0.001 + IDT training_iters = 200000 + IDT batch_size = 128 + IDT display_step = 10 + IDT n_input = 784 + IDT n_classes = 10 + IDT dropout = 0.75 + IDT x = tf.placeholder(tf.float32, [None, n_input]) + IDT y = tf.placeholder(tf.float32, [None, n_classes]) + IDT keep_prob = tf.placeholder(tf.float32) + + IDT def conv2d(x, W, b, strides= ) IDT IDT x = , W, strides= , strides, strides, 1], padding= ) IDT IDT x = tf.nn.bias_add(x, b) + IDT IDT return tf.nn.relu(x) + + IDT def maxpool2d(x, k= ) IDT IDT return tf.nn.max_pool(x, ksize= , k, k, 1], strides= , k, k, 1], padding= ) + IDT def conv_net(x, weights, biases, dropout): + IDT IDT x = , shape= , 28, 28, 1]) ) IDT IDT conv1 = conv2d(x, weights['wc1'], biases['bc1']) + IDT IDT conv1 = , k= ) IDT IDT conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) + IDT IDT conv2 = , k= ) IDT IDT fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) + IDT IDT fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) + IDT IDT fc1 = tf.nn.relu(fc1) + IDT IDT fc1 = tf.nn.dropout(fc1, dropout) + IDT IDT out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) + IDT IDT return out + IDT weights = {'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 'wd1': tf.Variable(tf.random_normal([7 * 7 * 64, 1024])), 'out': tf.Variable(tf.random_normal([1024, n_classes]))} + IDT biases = {'bc1': tf.Variable(tf.random_normal([32])), 'bc2': tf.Variable(tf.random_normal([64])), 'bd1': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_classes]))} + IDT pred = conv_net(x, weights, biases, keep_prob) + IDT cost = , labels= ) IDT optimizer = ) IDT correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT step = 1 + IDT IDT while step * batch_size < training_iters: + IDT IDT IDT batch_x, batch_y = mnist.train.next_batch(batch_size) + IDT IDT IDT sess.run(optimizer, feed_dict= , y: batch_y, keep_prob: dropout}) ) IDT IDT IDT if step % display_step == 0: + IDT IDT IDT IDT loss, acc = , accuracy], feed_dict= , y: batch_y, keep_prob: 0.1}) ) IDT IDT IDT IDT + ', Minibatch Loss= , Training Accuracy= ) IDT IDT IDT step += 1 + IDT IDT + IDT IDT ) + + +def second_convnet(): + IDT import random + IDT import numpy as np + IDT import matplotlib.pyplot as plt + IDT import datetime + IDT from tensorflow.examples.tutorials.mnist import input_data + IDT mnist = , one_hot= ) IDT tf.reset_default_graph() + IDT sess = tf.InteractiveSession() + IDT x = , shape= , 28, 28, 1]) ) IDT y_ = , shape= , 10]) ) IDT W_conv1 = , 5, 1, 32], stddev= ) IDT b_conv1 = , shape= ) IDT + IDT + IDT h_conv1 = , filter= , strides= , 1, 1, 1], padding= ) IDT h_conv1 = tf.nn.relu(h_conv1) + IDT h_pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + IDT def conv2d(x, W): + IDT IDT return tf.nn.conv2d(input= , filter= , strides= , 1, 1, 1], padding= ) + IDT def max_pool_2x2(x): + IDT IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT W_conv2 = , 5, 32, 64], stddev= ) IDT b_conv2 = , shape= ) IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + IDT h_pool2 = max_pool_2x2(h_conv2) + IDT W_fcl = , 1024], stddev= ) IDT b_fcl = , shape= ) IDT h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + IDT h_fcl = tf.nn.relu(tf.matmul(h_pool2_flat, W_fcl) + b_fcl) + IDT keep_prob = tf.placeholder('float') + IDT h_fcl_drop = tf.nn.dropout(h_fcl, keep_prob) + IDT W_fc2 = , 10], stddev= ) IDT b_fc2 = , shape= ) IDT y = tf.matmul(h_fcl_drop, W_fc2) + b_fc2 + IDT crossEntropyLoss = , logits= ) IDT trainStep = tf.train.AdamOptimizer().minimize(crossEntropyLoss) + IDT correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + IDT sess.run(tf.global_variables_initializer()) + IDT tf.summary.scalar('Cross Entropy Loss', crossEntropyLoss) + IDT tf.summary.scalar('Accuracy', accuracy) + IDT merged = tf.summary.merge_all() + IDT logdir = 'tensorboard/' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S') + '/' + IDT writer = tf.summary.FileWriter(logdir, sess.graph) + IDT b = mnist.train.next_batch(1) + IDT + IDT image = tf.reshape(b[0], [-1, 28, 28, 1]) + IDT + IDT my_img = image.eval() + IDT my_i = my_img.squeeze() + IDT plt.imshow(my_i, cmap= ) IDT plt.show() + IDT batchSize = 50 + IDT for i in range(1000): + IDT IDT batch = mnist.train.next_batch(batchSize) + IDT IDT trainingInputs = batch[0].reshape([batchSize, 28, 28, 1]) + IDT IDT trainingLabels = batch[1] + IDT IDT if i % 10 == 0: + IDT IDT IDT summary = sess.run(merged, {x: trainingInputs, y_: trainingLabels, keep_prob: 1.0}) + IDT IDT IDT writer.add_summary(summary, i) + IDT IDT if i % 100 == 0: + IDT IDT IDT trainAccuracy = , feed_dict= , y_: trainingLabels, keep_prob: 1.0}) ) IDT IDT IDT ) + IDT IDT trainStep.run(session= , feed_dict= , y_: trainingLabels, keep_prob: 0.5}) ) IDT testInputs = mnist.test.images.reshape([-1, 28, 28, 1]) + IDT testLabels = mnist.test.labels + IDT acc = , y_: testLabels, keep_prob: 1.0}) ) IDT ) + + +def recurrent_neural_network_for_spam_detection(): + IDT import os + IDT import re + IDT import io + IDT import requests + IDT import numpy as np + IDT import matplotlib.pyplot as plt + IDT from zipfile import ZipFile + IDT from tensorflow.python.framework import ops + IDT ops.reset_default_graph() + IDT sess = tf.Session() + IDT epochs = 20 + IDT batch_size = 250 + IDT max_sequence_length = 25 + IDT rnn_size = 10 + IDT embedding_size = 50 + IDT min_word_frequency = 10 + IDT learning_rate = 0.0005 + IDT dropout_keep_prob = tf.placeholder(tf.float32) + IDT data_dir = 'temp' + + +def gaussian_mixture_models(): + IDT import numpy as np + IDT import matplotlib.pyplot as plt + IDT from scipy import stats + IDT import seaborn as sns + IDT sns.set() + + +def modern_multilayer_perceptron(): + IDT import numpy as np + IDT import tensorflow as tf + IDT import matplotlib.pyplot as plt + IDT from tensorflow.examples.tutorials.mnist import input_data + IDT mnist = , one_hot= ) IDT X_train = mnist.train.images + IDT Y_train = mnist.train.labels + IDT X_test = mnist.test.images + IDT Y_test = mnist.test.labels + IDT dimX = X_train.shape[1] + IDT dimY = Y_train.shape[1] + IDT nTrain = X_train.shape[0] + IDT nTest = X_test.shape[0] + IDT ') + IDT + + IDT def xavier_init(n_inputs, n_outputs, uniform= ) IDT IDT if uniform: + IDT IDT IDT init_range = tf.sqrt(6.0 / (n_inputs + n_outputs)) + IDT IDT IDT return tf.random_uniform_initializer(-init_range, init_range) + IDT IDT else: + IDT IDT IDT stddev = tf.sqrt(3.0, n_inputs + n_outputs) + IDT IDT return tf.truncated_normal_initializer(stddev= ) IDT nLayer0 = dimX + IDT nLayer1 = 256 + IDT nLayer2 = 256 + IDT nLayer3 = dimY + IDT sigma_init = 0.1 + IDT W = , nLayer1], stddev= , 'W2': tf.Variable(tf.random_normal([nLayer1, nLayer2], stddev= , 'W3': tf.Variable(tf.random_normal([nLayer2, nLayer3], stddev= ) IDT b = {'b1': tf.Variable(tf.random_normal([nLayer1])), 'b2': tf.Variable(tf.random_normal([nLayer2])), 'b3': tf.Variable(tf.random_normal([nLayer3]))} + + IDT def model_myNN(_X, _W, _b): + IDT IDT Layer1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _W['W1']), _b['b1'])) + IDT IDT Layer2 = tf.nn.sigmoid(tf.add(tf.matmul(Layer1, _W['W2']), _b['b2'])) + IDT IDT Layer3 = tf.add(tf.matmul(Layer2, _W['W3']), _b['b3']) + IDT IDT return Layer3 + IDT X = , [None, dimX], name= ) IDT Y = , [None, dimY], name= ) IDT Y_pred = model_myNN(X, W, b) + IDT loss = , labels= ) IDT learning_rate = 0.001 + IDT optimizer = ) IDT training_epochs = 30 + IDT display_epoch = 5 + IDT batch_size = 100 + IDT correct_prediction = tf.equal(tf.argmax(Y_pred, 1), tf.argmax(Y, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + + +def chi_squared_feature_selection(): + IDT import numpy as np + IDT import pandas as pd + IDT from sklearn.preprocessing import LabelBinarizer + IDT from sklearn.feature_selection import chi2, SelectKBest + IDT from sklearn.feature_extraction.text import CountVectorizer + IDT X = np.array(['call you tonight', 'Call me a cab', 'please call me... please', 'he will call me']) + IDT y = [1, 1, 2, 0] + IDT vect = CountVectorizer() + IDT X_dtm = vect.fit_transform(X) + IDT X_dtm = X_dtm.toarray() + IDT )) + IDT y_binarized = LabelBinarizer().fit_transform(y) + IDT + IDT + IDT observed = np.dot(y_binarized.T, X_dtm) + IDT + IDT class_prob = , -1) ) IDT feature_count = , -1) ) IDT expected = np.dot(class_prob.T, feature_count) + IDT + IDT chisq = (observed - expected) ** 2 / expected + IDT chisq_score = ) IDT + IDT chi2score = chi2(X_dtm, y) + IDT + IDT kbest = , k= ) IDT X_dtm_kbest = kbest.fit_transform(X_dtm, y) + IDT + + +def genetic(): + IDT import numpy as np + IDT import pandas as pd + IDT import matplotlib.pyplot as plt + IDT chromo_size = 5 + IDT low = 0 + IDT high = 100 + IDT lol = np.random.randint(low, high + 1, chromo_size) + IDT + IDT pop_size = 6 + IDT pop = np.random.randint(low, high + 1, (pop_size, chromo_size)) + IDT + IDT target = 200 + IDT cost = , axis= ) IDT graded = [(c, list(p)) for p, c in zip(pop, cost)] + IDT for cost, chromo in graded: + IDT IDT ) + + +def main(): + IDT genetic() + IDT chi_squared_feature_selection() + IDT multiplication_basics() + IDT hello_world() + IDT matrix_multiplication() + IDT nearest_neighbor() + IDT logistic_regression() + IDT multilayer_perceptron() + IDT linear_regression() + IDT convolutional_network() + IDT second_convnet() + IDT recurrent_neural_network_for_spam_detection() + IDT gaussian_mixture_models() + IDT modern_multilayer_perceptron() + + +main() +import tensorflow as tf + + +class CNN_YoonKim: + + IDT def __init__(self, model_name= , session= , learning_rate= , optimizer= , learning_decay_rate= , filter_sizes= , num_filters= , max_sentence_length= , num_classes= , embeddings= , new_embeddings= , embedding_dim= , vocabulary_size= , static= , max_l2_norm= , regularization_lambda= , dropout_keep_prob= ) IDT IDT if model_name == None: + IDT IDT IDT return + IDT IDT self.model_name = model_name + IDT IDT self.session = session + IDT IDT self.learning_rate = learning_rate + IDT IDT self.optimizer = optimizer + IDT IDT self.dropout_keep_prob_train = dropout_keep_prob + IDT IDT self.regularization_lambda = regularization_lambda + IDT IDT self.input_x = , max_sentence_length), dtype= , name= ) IDT IDT self.input_y = , num_classes), dtype= , name= ) IDT IDT self.dropout_keep_prob = , name= ) IDT IDT self.embeddings_placeholder = , shape= , embedding_dim)) ) IDT IDT self.embeddings = , trainable= ) IDT IDT self.new_embeddings = , trainable= ) IDT IDT self.all_embeddings = , self.new_embeddings], axis= ) IDT IDT self.embedded_words = tf.nn.embedding_lookup(self.all_embeddings, self.input_x) + IDT IDT self.input_x_expanded = tf.expand_dims(self.embedded_words, -1) + IDT IDT self.pool_results = [] + IDT IDT for i, filter_size in enumerate(filter_sizes): + IDT IDT IDT filter = , shape= , embedding_dim, 1, num_filters), dtype= , initializer= ) IDT IDT IDT if max_l2_norm != 0: + IDT IDT IDT IDT filter = tf.clip_by_norm(filter, max_l2_norm) + IDT IDT IDT bias = , shape= ,))) ) IDT IDT IDT conv = , filter= , strides= , 1, 1, 1], padding= ) IDT IDT IDT relu = tf.nn.relu(tf.nn.bias_add(conv, bias)) + IDT IDT IDT conv_dim = max_sentence_length - filter_size + 1 + IDT IDT IDT pooled = , ksize= , conv_dim, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT self.pool_results.append(pooled) + IDT IDT num_filters_total = num_filters * len(filter_sizes) + IDT IDT self.flat = tf.reshape(tf.concat(self.pool_results, 3), [-1, num_filters_total]) + IDT IDT self.dropout = tf.nn.dropout(self.flat, self.dropout_keep_prob) + IDT IDT W = , shape= , num_classes), dtype= , initializer= ) IDT IDT b = , shape= ,))) ) IDT IDT self.output = , W, b, name= ) IDT IDT self.predictions = , 1, name= ) IDT IDT losses = , logits= ) IDT IDT if regularization_lambda != 0: + IDT IDT IDT l2_loss = tf.nn.l2_loss(W) + IDT IDT IDT self.loss = , tf.multiply(self.regularization_lambda, l2_loss)), name= ) IDT IDT else: + IDT IDT IDT self.loss = , name= ) IDT IDT self.optimizer = ) IDT IDT self.train_op = self.optimizer.minimize(self.loss) + IDT IDT self.saver = tf.train.Saver() + IDT IDT self.session.run(tf.global_variables_initializer(), feed_dict= ) + IDT def train_step(self, input_x, input_y): + IDT IDT _, loss = , self.loss], feed_dict= , self.input_y: input_y, self.dropout_keep_prob: self.dropout_keep_prob_train}) ) IDT IDT return loss + + IDT def feed(self, input_x): + IDT IDT return self.session.run([self.output, self.predictions], feed_dict= , self.dropout_keep_prob: 1}) )from unittest import TestCase +import pkg_resources +import logging, warnings +import tensorflow as tf +import numpy as np +from deepexplain.tensorflow import DeepExplain +from deepexplain.tensorflow.methods import original_grad +activations = {'Relu': tf.nn.relu, 'Sigmoid': tf.nn.sigmoid, 'Softplus': tf.nn.softplus, 'Tanh': tf.nn.tanh} + + +def simple_model(activation, session): + IDT X = tf.placeholder('float', [None, 2]) + IDT w1 = , -1.0], [-1.0, 1.0]]) ) IDT b1 = , -1.0]) ) IDT w2 = , 1.4], [-0.5, 1.0]]) ) IDT b2 = , 2.0]) ) IDT layer1 = activation(tf.matmul(X, w1) + b1) + IDT out = tf.matmul(layer1, w2) + b2 + IDT session.run(tf.global_variables_initializer()) + IDT return X, out + + +def simpler_model(session): + IDT """ + IDT Implements ReLU( ReLU(x1 - 1) - ReLU(x2) ) + IDT : + IDT """ + IDT X = tf.placeholder('float', [None, 2]) + IDT w1 = , 0.0], [0.0, 1.0]], trainable= ) IDT b1 = , 0], trainable= ) IDT w2 = , [-1.0]], trainable= ) IDT l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + IDT out = tf.nn.relu(tf.matmul(l1, w2)) + IDT session.run(tf.global_variables_initializer()) + IDT return X, out + + +def simple_multi_inputs_model(session): + IDT """ + IDT Implements Relu (3*x1|2*x2) | is a concat op + IDT : + IDT """ + IDT X1 = tf.placeholder('float', [None, 2]) + IDT X2 = tf.placeholder('float', [None, 2]) + IDT w1 = , 0.0], [0.0, 3.0]], trainable= ) IDT w2 = , 0.0], [0.0, 2.0]], trainable= ) IDT out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + IDT session.run(tf.global_variables_initializer()) + IDT return X1, X2, out + + +def simple_multi_inputs_model2(session): + IDT """ + IDT Implements Relu (3*x1|2*x2) | is a concat op + IDT : + IDT """ + IDT X1 = tf.placeholder('float', [None, 2]) + IDT X2 = tf.placeholder('float', [None, 1]) + IDT w1 = , 0.0], [0.0, 3.0]], trainable= ) IDT w2 = , [2.0]], trainable= ) IDT out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + IDT session.run(tf.global_variables_initializer()) + IDT return X1, X2, out + + +def train_xor(session): + IDT X = tf.placeholder('float', [None, 2]) + IDT Y = tf.placeholder('float', [None, 1]) + IDT w1 = , -0.0987727], [-1.57625198, 1.34942603]]) ) IDT b1 = , -0.14483099]) ) IDT w2 = , [-0.16255915]]) ) IDT b2 = ) IDT l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + IDT out = tf.matmul(l1, w2) + b2 + IDT session.run(tf.global_variables_initializer()) + IDT loss = tf.reduce_mean(tf.losses.mean_squared_error(Y, out)) + IDT train_step = tf.train.GradientDescentOptimizer(0.05).minimize(loss) + IDT np.random.seed(10) + IDT x = , 2, size= , 2)) ) IDT y = np.expand_dims(np.logical_or(x[:, (0)], x[:, (1)]), -1) + IDT l = None + IDT for _ in range(100): + IDT IDT l, _ = , train_step], feed_dict= , Y: y}) ) IDT return np.abs(l - 0.1) < 0.01 + + +class TestDeepExplainGeneralTF(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_tf_available(self): + IDT IDT try: + IDT IDT IDT pkg_resources.require('tensorflow>= ) IDT IDT except Exception: + IDT IDT IDT self.fail('Tensorflow requirement not met') + + IDT def test_simple_model(self): + IDT IDT X, out = simple_model(tf.nn.relu, self.session) + IDT IDT xi = np.array([[1, 0]]) + IDT IDT r = self.session.run(out, {X: xi}) + IDT IDT self.assertEqual(r.shape, xi.shape) + IDT IDT np.testing.assert_equal(r[0], [2.75, 5.5]) + + IDT def test_simpler_model(self): + IDT IDT X, out = simpler_model(self.session) + IDT IDT xi = np.array([[3.0, 1.0]]) + IDT IDT r = self.session.run(out, {X: xi}) + IDT IDT self.assertEqual(r.shape, (xi.shape[0], 1)) + IDT IDT np.testing.assert_equal(r[0], [1.0]) + + IDT def test_training(self): + IDT IDT session = tf.Session() + IDT IDT r = train_xor(session) + IDT IDT self.assertTrue(r) + + IDT def test_context(self): + IDT IDT """ + IDT IDT DeepExplain overrides nonlinearity gradient + IDT IDT """ + IDT IDT from deepexplain.tensorflow import DeepExplain + IDT IDT X = tf.placeholder('float', [None, 1]) + IDT IDT for name in activations: + IDT IDT IDT x1 = activations[name](X) + IDT IDT IDT x1_g = tf.gradients(x1, X)[0] + IDT IDT IDT self.assertEqual(x1_g.op.type, '%sGrad' % name) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT for name in activations: + IDT IDT IDT IDT x2 = activations[name](X) + IDT IDT IDT IDT self.assertEqual(x2.op.get_attr('_gradient_op_type').decode('utf-8'), 'DeepExplainGrad') + + IDT def test_mismatch_input_lens(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1 = tf.placeholder('float', [None, 1]) + IDT IDT IDT X2 = tf.placeholder('float', [None, 1]) + IDT IDT IDT w1 = ) IDT IDT IDT w2 = ) IDT IDT IDT out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + IDT IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('grad*input', out, [X1, X2], [[1], [2], [3]]) + IDT IDT IDT self.assertIn('List of input tensors and input data have different lengths', str(cm.exception)) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1 = tf.placeholder('float', [None, 1]) + IDT IDT IDT X2 = tf.placeholder('float', [None, 1]) + IDT IDT IDT w1 = ) IDT IDT IDT w2 = ) IDT IDT IDT out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + IDT IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT IDT attributions = de.explain('grad*input', out, [X1, X2], [[[2]], [[3]]]) + IDT IDT IDT self.assertEqual(len(attributions), 2) + IDT IDT IDT self.assertEqual(attributions[0][0], 20.0) + IDT IDT IDT self.assertEqual(attributions[1][0], 30.0) + + IDT def test_supported_activations(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT xi = [[-1, 0, 1]] + IDT IDT IDT Y = tf.nn.relu(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [0, 0, 1], 7) + IDT IDT IDT Y = tf.nn.elu(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [-0.632120558, 0, 1], 7) + IDT IDT IDT Y = tf.nn.sigmoid(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [0.268941421, 0.5, 0.731058578], 7) + IDT IDT IDT Y = tf.nn.tanh(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [-0.761594155, 0, 0.761594155], 7) + IDT IDT IDT Y = tf.nn.softplus(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [0.313261687, 0.693147181, 1.31326168], 7) + + IDT def test_original_grad(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT for name in activations: + IDT IDT IDT Y = activations[name](X) + IDT IDT IDT grad = original_grad(Y.op, tf.ones_like(X)) + IDT IDT IDT self.assertTrue('Tensor' in str(type(grad))) + + IDT def test_warning_unsupported_activations(self): + IDT IDT with warnings.catch_warnings(record= ) IDT IDT IDT warnings.simplefilter('always') + IDT IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT IDT IDT Y = tf.nn.relu6(X) + IDT IDT IDT IDT xi = [[-1, 0, 1]] + IDT IDT IDT IDT de.explain('elrp', Y, X, xi) + IDT IDT IDT IDT assert any([('unsupported activation' in str(wi.message)) for wi in w]) + + IDT def test_override_as_default(self): + IDT IDT """ + IDT IDT In DeepExplain context, nonlinearities behave as default, including training time + IDT IDT """ + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT r = train_xor(self.session) + IDT IDT IDT self.assertTrue(r) + + IDT def test_explain_not_in_context(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT pass + IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT de.explain('grad*input', None, None, None) + IDT IDT self.assertEqual('Explain can be called only within a DeepExplain context.', str(cm.exception)) + + IDT def test_invalid_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('invalid', None, None, None) + IDT IDT IDT self.assertIn('Method must be in', str(cm.exception)) + + IDT def test_gradient_was_not_overridden(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT Y = tf.nn.relu(X) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with warnings.catch_warnings(record= ) IDT IDT IDT IDT warnings.simplefilter('always') + IDT IDT IDT IDT de.explain('grad*input', Y, X, [[0, 0, 0]]) + IDT IDT IDT IDT assert any([('DeepExplain detected you are trying' in str(wi.message)) for wi in w]) + + +class TestDummyMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_dummy_zero(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.sigmoid, self.session) + IDT IDT IDT xi = np.array([[10, -10]]) + IDT IDT IDT attributions = de.explain('zero', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [0.0, 0.0], 10) + + IDT def test_gradient_restored(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.sigmoid, self.session) + IDT IDT IDT xi = np.array([[10, -10]]) + IDT IDT IDT de.explain('zero', out, X, xi) + IDT IDT IDT r = train_xor(self.session) + IDT IDT IDT self.assertTrue(r) + + +class TestSaliencyMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_saliency_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('saliency', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, 1.0]], 10) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('saliency', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[2.0, 2.0]], 10) + + +class TestGradInputMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_saliency_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('grad*input', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 10) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('grad*input', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + +class TestIntegratedGradientsMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_int_grad(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('intgrad', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 1) + + IDT def test_int_grad_higher_precision(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT attributions = , out, X, xi, steps= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 2) + + IDT def test_int_grad_baseline(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[3, 1]]) + IDT IDT IDT attributions = , out, X, xi, baseline= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0]], 5) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('intgrad', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + IDT def test_multiple_inputs_different_sizes(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model2(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3]])] + IDT IDT IDT attributions = de.explain('intgrad', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[12]], 10) + + +class TestEpsilonLRPMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_elrp_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('elrp', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 3) + + IDT def test_elrp_epsilon(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = , out, X, xi, epsilon= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 7) + + IDT def test_elrp_zero_epsilon(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT with self.assertRaises(AssertionError): + IDT IDT IDT IDT de.explain('elrp', out, X, xi, epsilon= ) + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = , out, [X1, X2], xi, epsilon= ) IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + +class TestDeepLIFTMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_deeplift(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('deeplift', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [2.0, -1.0]], 10) + + IDT def test_deeplift_baseline(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[3, 1]]) + IDT IDT IDT attributions = , out, X, xi, baseline= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0]], 5) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('deeplift', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + IDT def test_multiple_inputs_different_sizes(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model2(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3]])] + IDT IDT IDT attributions = de.explain('deeplift', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[12]], 10) + + +class TestOcclusionMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_occlusion(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('occlusion', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, -1.0]], 10) + + IDT def test_window_shape(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = , out, X, xi, window_shape= ,)) ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [0.5, 0.5]], 10) + + IDT def test_nan_warning(self): + IDT IDT with warnings.catch_warnings(record= ) IDT IDT IDT warnings.simplefilter('always') + IDT IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT IDT attributions = , out, X, xi, step= ) IDT IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, np.nan], [1.0, np.nan]], 10) + IDT IDT IDT IDT assert any([('nans' in str(wi.message)) for wi in w]) + + IDT def test_multiple_inputs_error(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('occlusion', out, [X1, X2], xi) + IDT IDT IDT self.assertIn('not yet supported', str(cm.exception)) +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = '../Assignment 1/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2): + IDT h = tf.nn.relu(tf.matmul(tf_train_dataset, w_h1) + b1) + IDT return tf.matmul(h, w_h2) + b2 + + +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT b1 = tf.Variable(tf.zeros([hidden_units])) + IDT w_h2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT net = model(tf_train_dataset, w_h1, b1, w_h2, b2) + IDT l2_loss = 0.0001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2)) + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(net) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w_h1) + b1), w_h2) + b2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w_h1) + b1), w_h2) + b2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2): + IDT h = tf.nn.relu(tf.matmul(tf_train_dataset, w_h1) + b1) + IDT return tf.matmul(h, w_h2) + b2 + + +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT b1 = tf.Variable(tf.zeros([hidden_units])) + IDT w_h2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT net = model(tf_train_dataset, w_h1, b1, w_h2, b2) + IDT l2_loss = 0.0001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2)) + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(net) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w_h1) + b1), w_h2) + b2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w_h1) + b1), w_h2) + b2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (200 - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2, p_keep_input, p_keep_hidden): + IDT X = tf.nn.dropout(X, p_keep_input) + IDT h = tf.nn.relu(tf.matmul(tf_train_dataset, w_h1) + b1) + IDT return tf.matmul(h, w_h2) + b2 + + +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT b1 = tf.Variable(tf.zeros([hidden_units])) + IDT w_h2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT p_keep_input = tf.placeholder('float') + IDT p_keep_hidden = tf.placeholder('float') + IDT net = model(tf_train_dataset, w_h1, b1, w_h2, b2, p_keep_input, p_keep_hidden) + IDT l2_loss = 0.0001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2)) + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(net) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w_h1) + b1), w_h2) + b2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w_h1) + b1), w_h2) + b2) +num_steps = 5001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, p_keep_input: 0.5, p_keep_hidden: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 256 +hidden_units = 512 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o, p_keep_input, p_keep_hidden): + IDT X = tf.nn.dropout(X, p_keep_input) + IDT h1 = tf.nn.relu(tf.matmul(X, w_h1) + b1) + IDT h1 = tf.nn.dropout(h1, p_keep_hidden) + IDT h2 = tf.nn.relu(tf.matmul(h1, w_h2) + b2) + IDT h2 = tf.nn.dropout(h2, p_keep_hidden) + IDT h3 = tf.nn.relu(tf.matmul(h2, w_h3) + b3) + IDT h3 = tf.nn.dropout(h3, p_keep_hidden) + IDT out = tf.nn.softmax(tf.matmul(h3, w_o) + b_o) + IDT return out + + +def validate_model(X, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o): + IDT h1 = tf.nn.relu(tf.matmul(X, w_h1) + b1) + IDT h2 = tf.nn.relu(tf.matmul(h1, w_h2) + b2) + IDT h3 = tf.nn.relu(tf.matmul(h2, w_h3) + b3) + IDT out = tf.matmul(h3, w_o) + b_o + IDT return tf.nn.softmax(out) + + +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT b1 = tf.Variable(tf.zeros([hidden_units])) + IDT w_h2 = tf.Variable(tf.truncated_normal([hidden_units, hidden_units])) + IDT b2 = tf.Variable(tf.zeros([hidden_units])) + IDT w_h3 = tf.Variable(tf.truncated_normal([hidden_units, hidden_units])) + IDT b3 = tf.Variable(tf.zeros([hidden_units])) + IDT w_o = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT b_o = tf.Variable(tf.zeros([num_labels])) + IDT p_keep_input = tf.placeholder('float') + IDT p_keep_hidden = tf.placeholder('float') + IDT net = model(tf_train_dataset, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o, p_keep_input, p_keep_hidden) + IDT l2_loss = 0.001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2) + tf.nn.l2_loss(w_h3) + tf.nn.l2_loss(w_o)) + IDT loss = , logits= ) IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(0.5, global_step, 10000, 0.95) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(net) + IDT valid_prediction = validate_model(tf_valid_dataset, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o) + IDT test_prediction = validate_model(tf_test_dataset, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o) +num_steps = 20001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, p_keep_input: 0.5, p_keep_hidden: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 1000 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Tue May 16 12:30:50 2017 + +@author: Seagle + +""" +import os +os.chdir('E:/SeagleDLTrial') +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT logits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_bacthes + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.0} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT drop1 = tf.nn.dropout(lay1_train, keep_prob) + IDT logits = tf.matmul(drop1, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_bacthes + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.0} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_labels], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT logits = tf.matmul(lay2_train, weights3) + biases3 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) + IDT learning_rate = , global_step, 1000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +"""3_regularization.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + IDT https://colab.research.google.com/drive/1kIhA8d-ZLbUegSaKn3FK29R2T4NkS5mK + +Deep Learning +============= + +Assignment 3 +------------ + +Previously in `2_fullyconnected.ipynb`, you trained a logistic regression and a neural network model. + +The goal of this assignment is to explore regularization techniques. +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle + +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +"""Reformat into a shape that's more adapted to the models we're going to train: +- data as a flat matrix, +- labels as float 1-hot encodings. +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +"""--- +Problem 1 +--------- + +Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor `t` using `nn.l2_loss(t)`. The right amount of regularization should improve your validation / test accuracy. + +--- +""" +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT regularizer = tf.nn.l2_loss(weights) + IDT loss = loss + beta * regularizer + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT B1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT B2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, W1) + B1 + IDT relu_1 = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(relu_1, W2) + B2 + IDT loss = , logits= ) IDT w1_regularizer = tf.nn.l2_loss(W1) + IDT w2_regularizer = tf.nn.l2_loss(W2) + IDT loss = loss + beta * (w1_regularizer + w2_regularizer) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT valid_y1 = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + B1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_y1, W2) + B2) + IDT test_y1 = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + B1) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_y1, W2) + B2) +num_step = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_step): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +"""--- +Problem 2 +--------- +Let's demonstrate an extreme case of overfitting. Restrict your training data to just a few batches. What happens? + +--- +""" +num_step = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_step): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +"""--- +Problem 3 +--------- +Introduce Dropout on the hidden layer of the neural network. Remember: Dropout should only be introduced during training, not evaluation, otherwise your evaluation results would be stochastic as well. TensorFlow provides `nn.dropout()` for that, but you have to make sure it's only inserted during training. + +What happens to our extreme overfitting case? + +--- +""" +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT B1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT B2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, W1) + B1 + IDT relu_1 = tf.nn.relu(logits_1) + IDT keep_prob = tf.placeholder('float') + IDT dropout_logits_1 = tf.nn.dropout(relu_1, keep_prob) + IDT logits_2 = tf.matmul(dropout_logits_1, W2) + B2 + IDT loss = , logits= ) IDT w1_regularizer = tf.nn.l2_loss(W1) + IDT w2_regularizer = tf.nn.l2_loss(W2) + IDT loss = loss + beta * (w1_regularizer + w2_regularizer) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT valid_y1 = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + B1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_y1, W2) + B2) + IDT test_y1 = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + B1) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_y1, W2) + B2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +"""--- +Problem 4 +--------- + +Try to get the best performance you can using a multi-layer model! The best reported test accuracy using a deep network is [97.1%](http://yaroslavvb.blogspot.com/2011/09/notmnist-dataset.html?showComment=1391023266211 + +One avenue you can explore is to add multiple layers. + +Another one is to use learning rate decay: + + IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(0.5, global_step, ...) + IDT optimizer = , global_step= ) + --- +""" +import math +batch_size = 128 +beta = 0.001 +hidden_size_1 = 1024 +hidden_size_2 = 512 +hidden_size_3 = 256 +hidden_size_4 = 128 +hidden_size_5 = 64 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT W1 = , hidden_size_1], stddev= ) IDT B1 = tf.Variable(tf.zeros([hidden_size_1])) + IDT W2 = , hidden_size_2], stddev= ) IDT B2 = tf.Variable(tf.zeros([hidden_size_2])) + IDT W3 = , hidden_size_3], stddev= ) IDT B3 = tf.Variable(tf.zeros([hidden_size_3])) + IDT W4 = , hidden_size_4], stddev= ) IDT B4 = tf.Variable(tf.zeros([hidden_size_4])) + IDT W5 = , hidden_size_5], stddev= ) IDT B5 = tf.Variable(tf.zeros([hidden_size_5])) + IDT W6 = , num_labels], stddev= ) IDT B6 = tf.Variable(tf.zeros([num_labels])) + IDT keep_prob = tf.placeholder('float') + IDT logits_1 = tf.matmul(tf_train_dataset, W1) + B1 + IDT relu_1 = tf.nn.relu(logits_1) + IDT dropout_logits_1 = tf.nn.dropout(relu_1, keep_prob) + IDT logits_2 = tf.matmul(dropout_logits_1, W2) + B2 + IDT relu_2 = tf.nn.relu(logits_2) + IDT dropout_logits_2 = tf.nn.dropout(relu_2, keep_prob) + IDT logits_3 = tf.matmul(dropout_logits_2, W3) + B3 + IDT relu_3 = tf.nn.relu(logits_3) + IDT dropout_logits_3 = tf.nn.dropout(relu_3, keep_prob) + IDT logits_4 = tf.matmul(dropout_logits_3, W4) + B4 + IDT relu_4 = tf.nn.relu(logits_4) + IDT dropout_logits_4 = tf.nn.dropout(relu_4, keep_prob) + IDT logits_5 = tf.matmul(dropout_logits_4, W5) + B5 + IDT relu_5 = tf.nn.relu(logits_5) + IDT dropout_logits_5 = tf.nn.dropout(relu_5, keep_prob) + IDT logits_6 = tf.matmul(dropout_logits_5, W6) + B6 + IDT loss = , logits= ) IDT w1_regularizer = tf.nn.l2_loss(W1) + IDT w2_regularizer = tf.nn.l2_loss(W2) + IDT w3_regularizer = tf.nn.l2_loss(W3) + IDT w4_regularizer = tf.nn.l2_loss(W4) + IDT w5_regularizer = tf.nn.l2_loss(W5) + IDT w6_regularizer = tf.nn.l2_loss(W6) + IDT loss = loss + beta * (w1_regularizer + w2_regularizer + w3_regularizer + w4_regularizer + w5_regularizer + w6_regularizer) + IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(0.5, global_step, 100000, 0, 96) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits_6) + IDT valid_y1 = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + B1) + IDT valid_y2 = tf.nn.relu(tf.matmul(valid_y1, W2) + B2) + IDT valid_y3 = tf.nn.relu(tf.matmul(valid_y2, W3) + B3) + IDT valid_y4 = tf.nn.relu(tf.matmul(valid_y3, W4) + B4) + IDT valid_y5 = tf.nn.relu(tf.matmul(valid_y4, W5) + B5) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_y5, W6) + B6) + IDT test_y1 = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + B1) + IDT test_y2 = tf.nn.relu(tf.matmul(test_y1, W2) + B2) + IDT test_y3 = tf.nn.relu(tf.matmul(test_y2, W3) + B3) + IDT test_y4 = tf.nn.relu(tf.matmul(test_y3, W4) + B4) + IDT test_y5 = tf.nn.relu(tf.matmul(test_y4, W5) + B5) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_y5, W6) + B6) +num_steps = 15000 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +import numpy as np +import tensorflow as tf +import sys +import os +import math +sys.path.insert(0, os.path.abspath('..')) +from argparse import ArgumentParser +from Util.dataloader import DataLoader +from Util.configs import NNConfig +from ranking.NN import NN +import logging +logFormatter = logging.Formatter('%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s') +fileHandler = logging.FileHandler('{0}/{1}.log'.format('./', 'Fullyconnected_NN')) +fileHandler.setFormatter(logFormatter) +consoleHandler = logging.StreamHandler() +consoleHandler.setFormatter(logFormatter) +logger = logging.getLogger() +logger.setLevel(logging.INFO) +logger.addHandler(fileHandler) +logger.addHandler(consoleHandler) + + +class NN(NN): + + IDT def __init__(self): + IDT IDT self.d_loader = DataLoader() + IDT IDT self.input_vector_size = self.d_loader.d_handler.get_vocab_size() + IDT IDT self.output_vector_size = 1 + IDT IDT self.train_dataset, self.train_labels, self.valid_dataset, self.valid_labels, self.test_dataset, self.test_labels = self.d_loader.get_ttv() + + IDT def simple_NN(self, mode= ) IDT IDT logger.info('creating the computational graph...') + IDT IDT graph = tf.Graph() + IDT IDT with graph.as_default(): + IDT IDT IDT with tf.device(mode): + IDT IDT IDT IDT tf_train_dataset = , shape= , self.input_vector_size)) ) IDT IDT IDT IDT tf_train_labels = , shape= , self.output_vector_size)) ) IDT IDT IDT IDT tf_valid_dataset_init = , shape= ) IDT IDT IDT IDT tf_valid_dataset = tf.Variable(tf_valid_dataset_init) + IDT IDT IDT IDT tf_test_dataset_init = , shape= ) IDT IDT IDT IDT tf_test_dataset = tf.Variable(tf_test_dataset_init) + IDT IDT IDT IDT if self.cfg['NNConfig']['regularization']: + IDT IDT IDT IDT IDT beta_regu = tf.placeholder(tf.float32) + IDT IDT IDT IDT if self.cfg['NNConfig']['learning_rate_decay']: + IDT IDT IDT IDT IDT global_step = tf.Variable(0) + + IDT IDT IDT IDT def init_weights(shape): + IDT IDT IDT IDT IDT return tf.Variable(tf.truncated_normal(shape)) + + IDT IDT IDT IDT def init_biases(shape): + IDT IDT IDT IDT IDT return tf.Variable(tf.zeros(shape)) + IDT IDT IDT IDT w_h = init_weights([self.input_vector_size, NNConfig.num_hidden_nodes]) + IDT IDT IDT IDT b_h = init_biases([NNConfig.num_hidden_nodes]) + IDT IDT IDT IDT w_o = init_weights([NNConfig.num_hidden_nodes, self.output_vector_size]) + IDT IDT IDT IDT b_o = init_biases([self.output_vector_size]) + + IDT IDT IDT IDT def model(dataset, w_h, b_h, w_o, b_o, train): + IDT IDT IDT IDT IDT if NNConfig.dropout and train: + IDT IDT IDT IDT IDT IDT drop_i = tf.nn.dropout(dataset, NNConfig.dropout_keep_prob_input) + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + IDT IDT IDT IDT IDT IDT drop_h = tf.nn.dropout(h_lay_train, NNConfig.dropout_keep_prob_hidden) + IDT IDT IDT IDT IDT IDT return tf.matmul(drop_h, w_o) + b_o + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + IDT IDT IDT IDT IDT IDT return tf.matmul(h_lay_train, w_o) + b_o + IDT IDT IDT IDT logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + IDT IDT IDT IDT loss = tf.reduce_sum(tf.pow(logits - tf_train_labels, 2)) / (2 * tf.cast(tf.shape(tf_train_labels)[0], tf.float32)) + IDT IDT IDT IDT if NNConfig.regularization: + IDT IDT IDT IDT IDT loss += beta_regu * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + IDT IDT IDT IDT if NNConfig.learning_rate_decay: + IDT IDT IDT IDT IDT learning_rate = , global_step, NNConfig.decay_steps, NNConfig.decay_rate, staircase= ) IDT IDT IDT IDT IDT optimizer = , global_step= ) IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT optimizer = tf.train.GradientDescentOptimizer(NNConfig.learning_rate).minimize(loss) + IDT IDT IDT IDT train_prediction = logits + IDT IDT IDT IDT valid_prediction = model(tf_valid_dataset, w_h, b_h, w_o, b_o, False) + IDT IDT IDT IDT test_prediction = model(tf_test_dataset, w_h, b_h, w_o, b_o, False) + IDT IDT IDT IDT """ + IDT IDT IDT IDT run accuracy scope + IDT IDT IDT IDT """ + IDT IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT IDT pre = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT lbl = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT accuracy = tf.reduce_sum(tf.pow(pre - lbl, 2)) / (2 * tf.cast(tf.shape(lbl)[0], tf.float32)) + IDT IDT logger.info('running the session...') + IDT IDT with tf.Session(graph= , config= ) IDT IDT IDT session.run(tf.global_variables_initializer(), feed_dict= , tf_test_dataset_init: self.test_dataset}) ) IDT IDT IDT logger.info('Initialized') + IDT IDT IDT for step in range(NNConfig.num_steps): + IDT IDT IDT IDT offset = step * NNConfig.batch_size % (self.train_labels.shape[0] - NNConfig.batch_size) + IDT IDT IDT IDT batch_data = self.train_dataset[offset:offset + NNConfig.batch_size, :] + IDT IDT IDT IDT batch_labels = self.train_labels[offset:offset + NNConfig.batch_size] + IDT IDT IDT IDT batch_labels = batch_labels.reshape(len(batch_labels), 1) + IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT IDT if NNConfig.regularization: + IDT IDT IDT IDT IDT feed_dict[beta_regu] = NNConfig.beta_regu + IDT IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT IDT if step % NNConfig.summary_steps == 0: + IDT IDT IDT IDT IDT logger.info('Minibatch loss at step %d: %f' % (step, l)) + IDT IDT IDT IDT IDT logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: batch_labels})) ) IDT IDT IDT IDT IDT logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.valid_labels})) ) IDT IDT IDT IDT IDT logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.test_labels})) ) IDT IDT IDT self.print_words(test_prediction.eval(), self.test_labels) + + IDT def simple_NN_w_CandidateSampling(self): + IDT IDT + IDT IDT graph = tf.Graph() + IDT IDT with graph.as_default(): + IDT IDT IDT with tf.device('/cpu:0'): + IDT IDT IDT IDT tf_train_dataset = , shape= , self.input_vector_size)) ) IDT IDT IDT IDT tf_train_labels = , shape= , self.output_vector_size)) ) IDT IDT IDT IDT tf_valid_dataset = tf.constant(self.valid_dataset) + IDT IDT IDT IDT tf_test_dataset = tf.constant(self.test_dataset) + IDT IDT IDT IDT if NNConfig.regularization: + IDT IDT IDT IDT IDT beta_regu = tf.placeholder(tf.float32) + IDT IDT IDT IDT if NNConfig.learning_rate_decay: + IDT IDT IDT IDT IDT global_step = tf.Variable(0) + + IDT IDT IDT IDT def init_biases(shape): + IDT IDT IDT IDT IDT return tf.Variable(tf.zeros(shape)) + IDT IDT IDT IDT w_h = , NNConfig.num_hidden_nodes], stddev= ) IDT IDT IDT IDT b_h = init_biases([NNConfig.num_hidden_nodes]) + IDT IDT IDT IDT w_o = , self.output_vector_size], stddev= ) IDT IDT IDT IDT b_o = init_biases([self.output_vector_size]) + + IDT IDT IDT IDT def model(dataset, w_h, b_h, w_o, b_o, train): + IDT IDT IDT IDT IDT if NNConfig.dropout and train: + IDT IDT IDT IDT IDT IDT drop_i = tf.nn.dropout(dataset, NNConfig.dropout_keep_prob_input) + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + IDT IDT IDT IDT IDT IDT drop_h = tf.nn.dropout(h_lay_train, NNConfig.dropout_keep_prob_hidden) + IDT IDT IDT IDT IDT IDT return tf.matmul(drop_h, w_o) + b_o + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + IDT IDT IDT IDT IDT IDT return tf.matmul(h_lay_train, w_o) + b_o + IDT IDT IDT IDT logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + IDT IDT IDT IDT if NNConfig.candidate_sampling == 'nce_loss': + IDT IDT IDT IDT IDT instances_loss = , b_o, tf_train_dataset, tf_train_labels, NNConfig.num_sampled, self.output_vector_size, num_true= ) IDT IDT IDT IDT elif NNConfig.candidate_sampling == 'softmax_loss': + IDT IDT IDT IDT IDT instances_loss = , b_o, tf_train_dataset, tf_train_labels, NNConfig.num_sampled, self.output_vector_size, num_true= ) IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT instances_loss = , labels= ) IDT IDT IDT IDT IDT + IDT IDT IDT IDT loss = tf.reduce_mean(instances_loss) + IDT IDT IDT IDT if NNConfig.regularization: + IDT IDT IDT IDT IDT loss += beta_regu * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + IDT IDT IDT IDT if NNConfig.learning_rate_decay: + IDT IDT IDT IDT IDT learning_rate = , global_step, NNConfig.decay_steps, NNConfig.decay_rate, staircase= ) IDT IDT IDT IDT IDT optimizer = , global_step= ) IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT optimizer = tf.train.GradientDescentOptimizer(NNConfig.learning_rate).minimize(loss) + IDT IDT IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT IDT IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset, w_h, b_h, w_o, b_o, False)) + IDT IDT IDT IDT test_prediction = tf.nn.softmax(model(tf_test_dataset, w_h, b_h, w_o, b_o, False)) + IDT IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT IDT pre = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT lbl = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT accuracy = , labels= , 'float')) ) IDT IDT logger.info('running the session...') + IDT IDT with tf.Session(graph= , config= ) IDT IDT IDT tf.initialize_all_variables().run() + IDT IDT IDT logger.info('Initialized') + IDT IDT IDT for step in range(NNConfig.num_steps): + IDT IDT IDT IDT offset = step * NNConfig.batch_size % (self.train_labels.shape[0] - NNConfig.batch_size) + IDT IDT IDT IDT batch_data = self.train_dataset[offset:offset + NNConfig.batch_size, :] + IDT IDT IDT IDT batch_labels = self.train_labels[offset:offset + NNConfig.batch_size, :] + IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT IDT if NNConfig.regularization: + IDT IDT IDT IDT IDT feed_dict[beta_regu] = NNConfig.beta_regu + IDT IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT IDT if step % NNConfig.summary_steps == 0: + IDT IDT IDT IDT IDT logger.info('Minibatch loss at step %d: %f' % (step, l)) + IDT IDT IDT IDT IDT logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: batch_labels})) ) IDT IDT IDT IDT IDT logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.valid_labels})) ) IDT IDT IDT logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.test_labels})) ) + +if __name__ == '__main__': + IDT parser = ) IDT parser.add_argument('-m', '--mode', help= , required= ) IDT parser.add_argument('-s', '--query_size', help= , required= ) IDT parser.add_argument('-l', '--loss', help= , pair-wise]', required= ) IDT args = parser.parse_args() + IDT nn = NN() if args.query_size is None else NN(args.query_size) + IDT try: + IDT IDT if args.mode == 'gpu': + IDT IDT IDT nn.mode = '/gpu:0' + IDT IDT else: + IDT IDT IDT nn.mode = '/cpu:0' + IDT IDT if args.loss == 'point-wise' or args.loss == 'pair-wise': + IDT IDT IDT logger.info('learn with pair-wise loss') + IDT IDT IDT nn.lf = args.loss + IDT IDT else: + IDT IDT IDT nn.lf == 'point-wise' + IDT IDT nn.simple_NN() + IDT IDT logger.info('done..') + IDT except Exception as e: + IDT IDT logger.exception(e) + IDT IDT raise +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import sys +import time +import numpy as np +import tensorflow as tf +from sklearn.model_selection import train_test_split +import siuts +import glob +num_epochs = 2 +batch_size = 128 + + +def main(): + IDT start = time.time() + IDT num_channels = 1 + IDT num_labels = len(siuts.species_list) + IDT image_size = siuts.resized_segment_size + IDT num_files = len(glob.glob1(siuts.dataset_dir, '{}-training*'.format(siuts.species_list[0]))) + IDT num_files = 3 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf.set_random_seed(1337) + IDT IDT tf_train_dataset = , shape= , image_size, image_size, num_channels), name= ) IDT IDT tf_train_labels = , shape= , num_labels), name= ) IDT IDT tf_test_dataset = , shape= , image_size, image_size, num_channels), name= ) IDT IDT tf_one_prediction = , shape= , image_size, image_size, num_channels), name= ) + IDT IDT def conv2d(name, data, kernel_shape, bias_shape, stride= ) IDT IDT IDT with tf.variable_scope(name) as scope: + IDT IDT IDT IDT weights = , kernel_shape, initializer= , 0.05)) ) IDT IDT IDT IDT biases = , bias_shape, initializer= ) IDT IDT IDT IDT conv = , weights, [1, stride, stride, 1], padding= , name= ) IDT IDT IDT IDT pre_activation = tf.nn.bias_add(conv, biases) + IDT IDT IDT IDT activation = , name= ) IDT IDT IDT IDT return activation + + IDT IDT def fully_connected(name, data, weights_shape, bias_shape, dropout): + IDT IDT IDT with tf.variable_scope(name) as scope: + IDT IDT IDT IDT weights = , weights_shape, initializer= , 0.05)) ) IDT IDT IDT IDT biases = , bias_shape, initializer= ) IDT IDT IDT IDT activation = , weights), biases), name= ) IDT IDT IDT IDT return tf.nn.dropout(activation, dropout, name= ) + IDT IDT def model(data, input_dropout, fc_dropout): + IDT IDT IDT data = tf.nn.dropout(data, input_dropout) + IDT IDT IDT + IDT IDT IDT conv = conv2d('conv1', data, [5, 5, 1, 32], [32], 2) + IDT IDT IDT pool = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= , name= ) IDT IDT IDT + IDT IDT IDT conv = conv2d('conv2', pool, [5, 5, 32, 64], [64]) + IDT IDT IDT pool = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= , name= ) IDT IDT IDT + IDT IDT IDT conv = conv2d('conv3', pool, [3, 3, 64, 128], [128]) + IDT IDT IDT pool = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= , name= ) IDT IDT IDT + IDT IDT IDT shape = pool.get_shape().as_list() + IDT IDT IDT reshaped_layer = tf.reshape(pool, [shape[0], shape[1] * shape[2] * shape[3]]) + IDT IDT IDT fc = fully_connected('fc1', reshaped_layer, [shape[1] * shape[2] * shape[3], 256], [256], fc_dropout) + IDT IDT IDT fc = fully_connected('fc2', fc, [256, 128], [128], fc_dropout) + IDT IDT IDT return fully_connected('output', fc, [128, num_labels], [num_labels], 1) + IDT IDT logits = model(tf_train_dataset, 1, 0.8) + IDT IDT loss = , labels= ) IDT IDT optimizer = , 0.95, use_locking= , name= , use_nesterov= ) IDT IDT tf.get_variable_scope().reuse_variables() + IDT IDT train_prediction = , 1, 1), name= ) IDT IDT test_prediction = , 1, 1), name= ) IDT IDT one_prediction = , 1, 1), name= ) IDT checkpoint_path = siuts.checkpoints_dir + 'model.ckpt' + IDT with tf.Session(graph= ) IDT IDT writer = tf.summary.FileWriter(siuts.checkpoints_dir, session.graph) + IDT IDT tf.global_variables_initializer().run() + IDT IDT saver = , max_to_keep= ) IDT IDT tf.train.write_graph(session.graph_def, siuts.checkpoints_dir, 'graph.pb', False) + IDT IDT train_dataset = np.empty + IDT IDT train_labels = np.empty + IDT IDT current_file = 0 + IDT IDT current_epoch = 1 + IDT IDT step = 0 + IDT IDT while True: + IDT IDT IDT if step * batch_size % (siuts.samples_in_file * num_labels - batch_size) == 0: + IDT IDT IDT IDT if current_epoch > num_epochs: + IDT IDT IDT IDT IDT break + IDT IDT IDT IDT del train_dataset + IDT IDT IDT IDT del train_labels + IDT IDT IDT IDT sys.stdout.write('Loading datasets nr ' + str(current_file)) + IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT counter = 0 + IDT IDT IDT IDT train_dataset = np.empty + IDT IDT IDT IDT train_labels = np.empty + IDT IDT IDT IDT for specimen in siuts.species_list: + IDT IDT IDT IDT IDT new_data = siuts.load('{0}{1}-training_{2}.pickle'.format(siuts.standardized_dataset_dir, specimen, current_file)) + IDT IDT IDT IDT IDT new_labels = np.empty(new_data.shape[0]) + IDT IDT IDT IDT IDT new_labels.fill(siuts.species_list.index(specimen)) + IDT IDT IDT IDT IDT if counter == 0: + IDT IDT IDT IDT IDT IDT train_dataset = new_data + IDT IDT IDT IDT IDT IDT train_labels = new_labels + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT train_dataset = np.vstack((train_dataset, new_data)) + IDT IDT IDT IDT IDT IDT train_labels = np.concatenate((train_labels, new_labels)) + IDT IDT IDT IDT IDT counter += 1 + IDT IDT IDT IDT IDT sys.stdout.write('.') + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT + IDT IDT IDT IDT current_file += 1 + IDT IDT IDT IDT if current_file >= num_files - 1: + IDT IDT IDT IDT IDT current_file = 0 + IDT IDT IDT IDT IDT current_epoch += 1 + IDT IDT IDT IDT train_dataset, _, train_labels, _ = , siuts.reformat_labels(train_labels), test_size= , random_state= ) IDT IDT IDT IDT shape = np.shape(train_dataset) + IDT IDT IDT IDT train_dataset = np.reshape(train_dataset, (shape[0], shape[1], shape[2], 1)) + IDT IDT IDT offset = step * batch_size % (num_labels * siuts.samples_in_file - batch_size) + IDT IDT IDT sys.stdout.write('.') + IDT IDT IDT sys.stdout.flush() + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :, :, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 25 == 0: + IDT IDT IDT IDT batch_acc = siuts.accuracy(predictions, batch_labels) + IDT IDT IDT IDT if step % 250 == 0: + IDT IDT IDT IDT IDT saver.save(session, checkpoint_path, global_step= ) IDT IDT IDT IDT ) + IDT IDT IDT step += 1 + IDT IDT saver.save(session, checkpoint_path, global_step= ) IDT - start) + ' seconds') + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import matplotlib.pyplot as plt +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +""" +Training set (200000, 28, 28) (200000,) +Validation set (10000, 28, 28) (10000,) +Test set (18724, 28, 28) (18724,) + +Reformat into a shape that's more adapted to the models we're going to train: + + IDT data as a flat matrix, + IDT labels as float 1-hot encodings. +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +""" + + +We're first going to train a multinomial logistic regression using simple gradient descent. + +TensorFlow works like this: + + IDT First you describe the computation that you want to see performed: what the inputs, the variables, and the operations look like. These get created as nodes over a computation graph. This description is all contained within the block below: + + IDT with graph.as_default(): + IDT IDT ... + + IDT Then you can run the operations on this graph as many times as you want by calling session.run(), providing it outputs to fetch from the graph that get returned. This runtime operation is all contained in the block below: + + IDT with tf.Session(graph= ) IDT IDT ... + +Let's load all the data into TensorFlow and build the computation graph corresponding to our training: +""" +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +""" + + +Let's now switch to stochastic gradient descent training instead, which is much faster. + +The graph will be similar, except that instead of holding all the training data into a constant node, we create a Placeholder node which will be fed actual data at every call of session.run(). +""" +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +""" +Problem + +Turn the logistic regression example with SGD into a 1-hidden layer neural network with rectified linear units nn.relu() and 1024 hidden nodes. This model should improve your validation / test accuracy. +""" +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT logits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import tensorflow as tf +import matplotlib.pyplot as plt +import numpy as np +from sklearn.datasets import load_boston +import sklearn.linear_model as sk +boston = load_boston() +features = np.array(boston.data) +target = np.array(boston.target) + + +def normalize(dataset): + IDT mu = , axis= ) IDT sigma = , axis= ) IDT return (dataset - mu) / sigma + + +feature_norm = normalize(features) +np.random.seed(42) +rnd = np.random.rand(len(feature_norm)) < 0.8 +train_x = np.transpose(feature_norm[rnd]) +train_y = np.transpose(target[rnd]) +dev_x = np.transpose(feature_norm[~rnd]) +dev_y = np.transpose(target[~rnd]) +train_y = train_y.reshape(1, len(train_y)) +dev_y = dev_y.reshape(1, len(dev_y)) + + +def create_layer(X, n, activation): + IDT ndim = int(X.shape[0]) + IDT stddev = 2.0 / np.sqrt(ndim) + IDT initialization = , ndim), stddev= ) IDT W = tf.Variable(initialization) + IDT b = tf.Variable(tf.zeros([n, 1])) + IDT Z = tf.matmul(W, X) + b + IDT return activation(Z), W, b + + +tf.reset_default_graph() +n_dim = 13 +n1 = 20 +n2 = 20 +n3 = 20 +n4 = 20 +n_ouputs = 1 +tf.set_random_seed(42) +X = tf.placeholder(tf.float32, [n_dim, None]) +Y = tf.placeholder(tf.float32, [1, None]) +learning_rate = , shape= )hidden1, w1, b1 = , n1, activation= )hidden2, w2, b2 = , n1, activation= )hidden3, w3, b3 = , n2, activation= )hidden4, w4, b4 = , n3, activation= )y_, w5, b5 = , n4, activation= )cost = tf.reduce_mean(tf.square(y_ - Y)) +optimizer = , beta1= , beta2= , epsilon= )sess = tf.Session() +sess.run(tf.global_variables_initializer()) +cost_train_history = [] +cost_dev_history = [] +for epoch in range(10000 + 1): + IDT sess.run(optimizer, feed_dict= , Y: train_y, learning_rate: 0.001}) ) IDT cost_train_ = , feed_dict= , Y: train_y, learning_rate: 0.001}) ) IDT cost_dev_ = , feed_dict= , Y: dev_y, learning_rate: 0.001}) ) IDT cost_train_history = np.append(cost_train_history, cost_train_) + IDT cost_dev_history = np.append(cost_dev_history, cost_dev_) + IDT if epoch % 10 == 0: + IDT IDT = , cost J(test) = , cost_train_, cost_dev_)) )plt.plot(range(len(cost_train_history)), cost_train_history, label= )plt.plot(range(len(cost_dev_history)), cost_dev_history, label= )plt.xlabel('epoch') +plt.ylabel('cost') +plt.legend(loc= )plt.show() +np_w1 = )np_w1 = np_w1.reshape(20 * 13, 1) +np_w1.shape +plt.hist(np_w1, bins= )plt.show() +tf.reset_default_graph() +n_dim = 13 +n1 = 20 +n2 = 20 +n3 = 20 +n4 = 20 +n_ouputs = 1 +tf.set_random_seed(42) +X = tf.placeholder(tf.float32, [n_dim, None]) +Y = tf.placeholder(tf.float32, [1, None]) +learning_rate = , shape= )hidden1, w1, b1 = , n1, activation= )hidden2, w2, b2 = , n1, activation= )hidden3, w3, b3 = , n2, activation= )hidden4, w4, b4 = , n3, activation= )y_, w5, b5 = , n4, activation= )lambd = , shape= )reg = tf.nn.l2_loss(w1) + tf.nn.l2_loss(w2) + tf.nn.l2_loss(w3) + tf.nn.l2_loss(w4) + tf.nn.l2_loss(w5) +cost_mse = tf.reduce_mean(tf.square(y_ - Y)) +cost = tf.reduce_mean(cost_mse + lambd * reg) +optimizer = , beta1= , beta2= , epsilon= )sess = tf.Session() +sess.run(tf.global_variables_initializer()) +cost_train_history = [] +cost_dev_history = [] +learning_r = 0.001 +lambd_val = 0.1 +for epoch in range(10000 + 1): + IDT sess.run(optimizer, feed_dict= , Y: train_y, learning_rate: learning_r, lambd: lambd_val}) ) IDT cost_train_ = , feed_dict= , Y: train_y, learning_rate: learning_r, lambd: lambd_val}) ) IDT cost_dev_ = , feed_dict= , Y: train_y, learning_rate: learning_r, lambd: lambd_val}) ) IDT cost_train_history = np.append(cost_train_history, cost_train_) + IDT cost_dev_history = np.append(cost_dev_history, cost_dev_) + IDT if epoch % 10 == 0: + IDT IDT = , cost J(test) = , cost_train_, cost_dev_)) )plt.plot(range(len(cost_train_history)), cost_train_history, label= )plt.plot(range(len(cost_dev_history)), cost_dev_history, label= )plt.xlabel('epoch') +plt.ylabel('cost') +plt.legend(loc= )plt.show() +tf.reset_default_graph() +n_dim = 13 +n1 = 20 +n2 = 20 +n3 = 20 +n4 = 20 +n_ouputs = 1 +tf.set_random_seed(42) +X = tf.placeholder(tf.float32, [n_dim, None]) +Y = tf.placeholder(tf.float32, [1, None]) +learning_rate = , shape= )keep_prob = , shape= )hidden1, w1, b1 = , n1, activation= )hidden1_drop = tf.nn.dropout(hidden1, keep_prob) +hidden2, w2, b2 = , n1, activation= )hidden2_drop = tf.nn.dropout(hidden2, keep_prob) +hidden3, w3, b3 = , n2, activation= )hidden3_drop = tf.nn.dropout(hidden3, keep_prob) +hidden4, w4, b4 = , n3, activation= )hidden4_drop = tf.nn.dropout(hidden4, keep_prob) +y_, w5, b5 = , n4, activation= )cost = tf.reduce_mean(tf.square(y_ - Y)) +optimizer = , beta1= , beta2= , epsilon= )sess = tf.Session() +sess.run(tf.global_variables_initializer()) +cost_train_history = [] +cost_dev_history = [] +for epoch in range(10000 + 1): + IDT sess.run(optimizer, feed_dict= , Y: train_y, learning_rate: 0.001, keep_prob: 1.0}) ) IDT cost_train_ = , feed_dict= , Y: train_y, learning_rate: 0.001, keep_prob: 1.0}) ) IDT cost_dev_ = , feed_dict= , Y: dev_y, learning_rate: 0.001, keep_prob: 1.0}) ) IDT cost_train_history = np.append(cost_train_history, cost_train_) + IDT cost_dev_history = np.append(cost_dev_history, cost_dev_) + IDT if epoch % 10 == 0: + IDT IDT = , cost J(test) = , cost_train_, cost_dev_)) )plt.plot(range(len(cost_train_history)), cost_train_history, label= )plt.plot(range(len(cost_dev_history)), cost_dev_history, label= )plt.xlabel('epoch') +plt.ylabel('cost') +plt.legend(loc= )plt.show() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y1 = tf.nn.dropout(y1, 0.5) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT global_step = tf.Variable(0) + IDT W1 = , fc1_size], stddev= ) IDT b1 = tf.Variable(tf.zeros([fc1_size])) + IDT W2 = , fc2_size], stddev= ) IDT b2 = tf.Variable(tf.zeros([fc2_size])) + IDT W3 = , fc3_size], stddev= ) IDT b3 = tf.Variable(tf.zeros([fc3_size])) + IDT W4 = , num_labels], stddev= ) IDT b4 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + IDT y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + IDT logits = tf.matmul(y3, W4) + b4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + IDT learning_rate = , global_step, 1000, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + IDT y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + IDT valid_logits = tf.matmul(y3_valid, W4) + b4 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + IDT y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + IDT test_logits = tf.matmul(y3_test, W4) + b4 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +beta = 0.004 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT biases1 = tf.Variable(tf.zeros([hidden_units])) + IDT hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT weights = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(hidden1, weights) + biases + IDT loss = , logits= ) IDT loss = loss + 0.5 * beta * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_prediction1, weights) + biases) + IDT test_prediction1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_prediction1, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT batch_size = 128 + IDT hidden_units = 1024 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT IDT biases1 = tf.Variable(tf.zeros([hidden_units])) + IDT IDT hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT IDT hidden1 = tf.nn.dropout(hidden1, 0.6) + IDT IDT weights = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden1, weights) + biases + IDT IDT loss = , logits= ) IDT IDT loss = loss + 0.5 * beta * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights)) + IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_prediction1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_prediction1, weights) + biases) + IDT IDT test_prediction1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(test_prediction1, weights) + biases) + IDT num_steps = 3001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +from ntmnist import dataset +import numpy as np +pickle_file = 'notMNIST.pickle' +data = dataset(pickle_file) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +train_subset = 1000 +image_size = 28 +num_labels = 10 +hn_num = 1024 +batch_size = 128 +reg_beta = 0.025 +data.train_dataset = data.train_dataset[0:train_subset, :] +data.train_labels = data.train_labels[0:train_subset] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT with tf.name_scope('hidden') as scope_1: + IDT IDT weights_1 = , hn_num]), name= ) IDT IDT biases_1 = , name= ) IDT IDT ) + IDT IDT hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + IDT keep_prob = tf.placeholder('float') + IDT hidden_layer_drop = tf.nn.dropout(hidden1, keep_prob) + IDT with tf.name_scope('out') as scope_2: + IDT IDT weights = , num_labels]), name= ) IDT IDT biases = , name= ) IDT IDT ) + IDT IDT logits = tf.matmul(hidden_layer_drop, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + reg_beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) +num_steps = 5001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (data.train_labels.shape[0] - batch_size) + IDT IDT batch_data = data.train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = data.train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , data.valid_labels)) + IDT IDT IDT .eval()) + IDT feed_dict = {tf_train_dataset: data.test_dataset, keep_prob: 1.0} + IDT predictions = , feed_dict= ) IDT ) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() + +import joker as act_tf +import tensorflow as ans_tf + +import numpy as np +rrr = ,)).astype(np.float32) )sss = ,)).astype(np.float32) ) + +def test_forward_conv(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , shape= , 3, 2, 5]) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2), w: np.arange(3 * 3 * 2 * 5).reshape(3, 3, 2, 5)}) ) IDT IDT return res + + +def test_forward_conv2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , shape= , 3, 2, 5]) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2), w: sss.reshape(3, 3, 2, 5)}) ) IDT IDT return res + + +def test_backward_conv(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , dtype= , 3, 2, 5)) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op, feed_dict= , 4, 4, 2)}) ) IDT IDT res = w.eval() + IDT IDT return res + + +def test_backward_conv2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = tf.Variable(sss.reshape(3, 3, 2, 5)) + IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT grad_W, = tf.gradients(loss, [w]) + IDT IDT res = , 4, 4, 2)}) ) IDT IDT return res + + +def test_backward_conv3(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = tf.Variable(sss.reshape(3, 3, 2, 5)) + IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT grad_W, = tf.gradients(loss, [x]) + IDT IDT res = , 4, 4, 2)}) ) IDT IDT return res + + +def test_forward_maxpool(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2)}) ) IDT IDT return res + + +def test_forward_maxpool2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2)}) ) IDT IDT return res + + +def test_backward_maxpool(tf): + IDT with tf.Session() as sess: + IDT IDT x = , dtype= , 4, 4, 2)) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT loss = tf.reduce_sum(pool1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op) + IDT IDT res = x.eval() + IDT IDT return res + + +def test_backward_maxpool2(tf): + IDT with tf.Session() as sess: + IDT IDT x = tf.Variable(rrr.reshape(3, 4, 4, 2)) + IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT loss = tf.reduce_sum(pool1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op) + IDT IDT res = x.eval() + IDT IDT return res + + +if __name__ == '__main__': + IDT testcases = [test_forward_conv, test_forward_conv2, test_backward_conv, test_backward_conv2, test_backward_conv3, test_forward_maxpool2, test_backward_maxpool2] + IDT for item in testcases: + IDT IDT ans = item(ans_tf) + IDT IDT + IDT IDT + IDT IDT + IDT IDT res = item(act_tf) + IDT IDT np.testing.assert_allclose(res, ans, atol= ) IDT IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import scipy.io as sio +import sys, os, os.path, inspect +filedir = inspect.getframeinfo(inspect.currentframe())[0] +filedir = os.path.dirname(os.path.abspath(filedir)) +mat_contents = sio.loadmat(filedir + '/sampleData_w4RegionLabels.mat') +train_dataset = np.array(mat_contents['train_data']).astype(np.float32) +train_labels = np.array(mat_contents['train_label']).astype(np.float32) +valid_dataset = np.array(mat_contents['test_data']).astype(np.float32) +valid_labels = np.array(mat_contents['test_label']).astype(np.float32) + + +sample_size = train_dataset.shape[1] +num_labels = train_labels.shape[1] + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +hidden_size = 512 +hidden_1_size = hidden_size +hidden_2_size = hidden_size +hidden_3_size = hidden_size +hidden_4_size = hidden_size +beta = 0.01 +SEED = None +dropoutPercent = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , sample_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_predict_label = , shape= , sample_size)) ) IDT weights_1 = , hidden_1_size], stddev= , name= ) IDT biases_1 = , name= ) IDT weights_2 = , hidden_2_size]), name= ) IDT biases_2 = , name= ) IDT weights_3 = , hidden_3_size]), name= ) IDT biases_3 = , name= ) IDT weights_4 = , hidden_4_size]), name= ) IDT biases_4 = , name= ) IDT weights_o = , num_labels], stddev= , name= ) IDT biases_o = , name= ) + IDT def model(data, train= ) IDT IDT hidden = tf.nn.relu(tf.matmul(data, weights_1) + biases_1) + IDT IDT if train: + IDT IDT IDT hidden = , dropoutPercent, seed= ) IDT IDT hidden = tf.nn.relu(tf.matmul(hidden, weights_2) + biases_2) + IDT IDT if train: + IDT IDT IDT hidden = , dropoutPercent, seed= ) IDT IDT hidden = tf.nn.relu(tf.matmul(hidden, weights_3) + biases_3) + IDT IDT if train: + IDT IDT IDT hidden = , dropoutPercent, seed= ) IDT IDT return tf.matmul(hidden, weights_o) + biases_o + IDT logits = model(tf_train_dataset, True) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT regularized_loss = loss + beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_4) + tf.nn.l2_loss(weights_o)) + IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(0.002, global_step, 250, 0.96) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(model(tf_train_dataset)) + IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + IDT data_prediction = tf.nn.softmax(model(tf_predict_label)) + IDT init_op = tf.initialize_all_variables() + IDT saver = tf.train.Saver() +session = ) + +def train(num_steps= , save= , filename= ) IDT with session.as_default(): + IDT IDT session.run(init_op) + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT )) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT IDT IDT save_path = , filedir + '/' + filename, global_step= ) IDT IDT if save: + IDT IDT IDT save_path = saver.save(session, filedir + '/' + filename) + + +def init(): + IDT with session.as_default(): + IDT IDT saver.restore(session, filedir + '/model_regionDNN.ckpt') + + +def predict(input_data): + IDT with session.as_default(): + IDT IDT feed_dict = {tf_predict_label: input_data} + IDT IDT predictionArray = , feed_dict= ) IDT IDT prediction = np.argmax(predictionArray) + IDT IDT ) + IDT IDT return prediction + + +def close(): + IDT session.close() + + +if __name__ == '__main__': + IDT train(save= ) IDT close() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +""" +batch_size = 128 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = ,shape= , image_size * image_size)) ) tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + + + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + + + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = ,logits= ) + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + +num_steps = 3001 +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + + + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + + + + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : 1e-3} + _, l, predictions = , loss, train_prediction], feed_dict= ) if (step % 500 = ) ) + ) + , valid_labels)) + , test_labels)) + +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] + + +for regul in regul_val: + with tf.Session(graph= ) tf.initialize_all_variables().run() + for step in range(num_steps): + + + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + + + + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : regul} + _, l, predictions = , loss, train_prediction], feed_dict= ) accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) + + + + + +batch_size = 128 +num_hidden_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = ,shape= , image_size * image_size)) ) tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + + + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + + + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = ,logits= ) + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) + +num_steps = 3001 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + + + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + + + + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : 1e-3} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 = ) ) + ) + , valid_labels)) + , test_labels)) + + + + +batch_size = 128 +num_hidden_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = tf.placeholder(tf.float32, + IDT IDT IDT IDT IDT IDT IDT IDT IDT shape=(batch_size, image_size * image_size)) + tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + + + weights1 = tf.Variable( + tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable( + tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + + + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean( + IDT tf.nn.softmax_cross_entropy_with_logits(logits= , labels= ) + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) + +num_steps = 101 +num_bacthes = 3 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + + + + offset = step % num_bacthes + + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + + + + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : 1e-3} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 2 = ) ) + ) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + , test_labels)) + + + + + +batch_size = 128 +num_hidden_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + + weights1 = tf.Variable( + tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable( + tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + + + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + + drop1 = tf.nn.dropout(lay1_train, 0.5) + logits = tf.matmul(drop1, weights2) + biases2 + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits= , labels= ) IDT + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) + +num_steps = 101 +num_batches = 3 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + + + + offset = step % num_batches + + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + + + + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 2 = ) ) + ) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + , test_labels)) +""" +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_hidden_nodes3], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + IDT weights4 = , num_labels], stddev= ) IDT biases4 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT drop1 = tf.nn.dropout(lay1_train, 0.5) + IDT lay2_train = tf.nn.relu(tf.matmul(drop1, weights2) + biases2) + IDT drop2 = tf.nn.dropout(lay2_train, 0.5) + IDT lay3_train = tf.nn.relu(tf.matmul(drop2, weights3) + biases3) + IDT drop3 = tf.nn.dropout(lay3_train, 0.5) + IDT logits = tf.matmul(drop3, weights4) + biases4 + IDT loss = , labels= ) IDT learning_rate = , global_step, 4000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 9001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf + + +class NLPModel: + + IDT def __init__(self, input_shape, output_classes, filters): + IDT IDT self.input = , shape= , input_shape[0], input_shape[1], 1]) ) IDT IDT self.labels = , shape= , output_classes]) ) IDT IDT self.dropout = tf.placeholder(tf.float32) + IDT IDT filter_count = 16 + IDT IDT filter_outputs = [] + IDT IDT for index, filter_length in enumerate(filters): + IDT IDT IDT W = , input_shape[1], 1, filter_count], stddev= ) IDT IDT IDT b = , shape= ) IDT IDT IDT conv_l1 = , W, strides= , 1, 1, 1], padding= ) IDT IDT IDT conv_pool = tf.nn.elu(tf.nn.bias_add(conv_l1, b)) + IDT IDT IDT conv_pool = , ksize= , input_shape[0] - filter_length + 1, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT filter_outputs.append(conv_pool) + IDT IDT total_filters = filter_count * len(filters) + IDT IDT final_pool = tf.concat(3, filter_outputs) + IDT IDT final_pool_flat = tf.reshape(final_pool, [-1, total_filters]) + IDT IDT drop = tf.nn.dropout(final_pool_flat, self.dropout) + IDT IDT W_fc = , output_classes], stddev= ) IDT IDT b_fc = , shape= ) IDT IDT raw_scores = tf.nn.xw_plus_b(drop, W_fc, b_fc) + IDT IDT self.probabilities = tf.nn.softmax(raw_scores) + IDT IDT self.predictions = tf.argmax(self.probabilities, 1) + IDT IDT self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.predictions, tf.argmax(self.labels, 1)), tf.float32)) + IDT IDT self.loss = tf.nn.softmax_cross_entropy_with_logits(raw_scores, self.labels) + IDT IDT self.optimize = tf.train.AdamOptimizer(0.0001).minimize(self.loss) + + IDT def predict(self, session, inputs): + IDT IDT return session.run(self.predictions, feed_dict= , self.dropout: 1.0}) ) + IDT def train(self, session, inputs, labels): + IDT IDT session.run(self.optimize, feed_dict= , self.labels: labels, self.dropout: 0.75}) ) + IDT def get_accuracy(self, session, inputs, labels): + IDT IDT return session.run(self.accuracy, feed_dict= , self.labels: labels, self.dropout: 1.0}) )from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +def run_sgd_with_reg(): + IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT beta = tf.constant(0.005) + IDT IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT IDT reg_loss = loss + beta * tf.nn.l2_loss(weights) + IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(reg_loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + IDT num_steps = 3001 + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , reg_loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +def run_sgd_with_hidden_layer_with_reg(num_steps= , learning_rate= , l2_reg_beta= , dropout_keep_prob= ) IDT """Run SGD with a hidden layer and regularization. + + IDT num_steps: + IDT IDT Number of steps to run SGD minibatch training. In each step a new + IDT IDT minibatch is fed through the network. + + IDT learning_rate: + IDT IDT SGD learning rate, passed direction to GradientDescentOptimizer. + + IDT l2_reg_beta: + IDT IDT The loss is regularized with L2 loss on all weights. This is the + IDT IDT regularization coefficient. Setting it to 0 means no L2 loss + IDT IDT regularization. Otherwise, small values work well. + + IDT dropout_keep_prob: + IDT IDT Dropout "keep" probability for outputs of the hidden layer. Set to 1 to + IDT IDT avoid dropout altogether. + IDT """ + IDT batch_size = 128 + IDT nfeatures = image_size * image_size + IDT nhidden = 1024 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , nfeatures)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_dropout_keep_prob = tf.placeholder(tf.float32) + IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT beta = tf.constant(l2_reg_beta) + IDT IDT w1 = tf.Variable(tf.truncated_normal([nfeatures, nhidden])) + IDT IDT b1 = tf.Variable(tf.zeros([nhidden])) + IDT IDT w2 = tf.Variable(tf.truncated_normal([nhidden, num_labels])) + IDT IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT hidden_out = , w1) + b1), keep_prob= ) IDT IDT logits = tf.matmul(hidden_out, w2) + b2 + IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT IDT reg_loss = loss + beta * tf.nn.l2_loss(w1) + beta * tf.nn.l2_loss(w2) + IDT IDT global_step = tf.Variable(0) + IDT IDT learning_rate = , global_step= , decay_steps= , decay_rate= ) IDT IDT optimizer = , global_step= ) IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w1) + b1), w2) + b2) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w1) + b1), w2) + b2) + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_dropout_keep_prob: dropout_keep_prob} + IDT IDT IDT _, l, predictions = , reg_loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +if __name__ == '__main__': + IDT run_sgd_with_hidden_layer_with_reg(num_steps= , learning_rate= , l2_reg_beta= , dropout_keep_prob= )from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +class Model: + IDT HIDDEN_UNIT_SIZE = 32 + IDT HIDDEN_UNIT_SIZE2 = 6 + IDT HIDDEN_UNIT_SIZE3 = 16 + IDT INPUT_SIZE = 120 + + IDT def __init__(self): + IDT IDT self.__setup_placeholder() + IDT IDT self.__setup_model() + IDT IDT self.__setup_ops() + + IDT def __enter__(self): + IDT IDT self.session = tf.Session() + IDT IDT return self + + IDT def __exit__(self, exc_type, exc_value, traceback): + IDT IDT self.session.close() + IDT IDT return False + + IDT def save(self, path): + IDT IDT saver = tf.train.Saver() + IDT IDT saver.save(self.session, path) + + IDT def restore(self, path): + IDT IDT saver = tf.train.Saver() + IDT IDT saver.restore(self.session, path) + + IDT def __setup_placeholder(self): + IDT IDT in_size = Model.INPUT_SIZE + IDT IDT self.trade_data = tf.placeholder('float', [None, in_size]) + IDT IDT self.actual_class = tf.placeholder('float', [None, 2]) + IDT IDT self.keep_prob = tf.placeholder('float') + IDT IDT self.label = tf.placeholder('string') + + IDT def __setup_model(self): + IDT IDT in_size = Model.INPUT_SIZE + IDT IDT w1 = , Estimator.HIDDEN_UNIT_SIZE], stddev= ) IDT IDT b1 = , shape= ) IDT IDT h1 = tf.nn.relu(tf.matmul(self.trade_data, w1) + b1) + IDT IDT w2 = , Estimator.HIDDEN_UNIT_SIZE2], stddev= ) IDT IDT b2 = , shape= ) IDT IDT h2 = tf.nn.relu(tf.matmul(h1, w2) + b2) + IDT IDT w3 = , Estimator.HIDDEN_UNIT_SIZE3], stddev= ) IDT IDT b3 = , shape= ) IDT IDT h3 = tf.nn.relu(tf.matmul(h2, w3) + b3) + IDT IDT h3_drop = tf.nn.dropout(h3, self.keep_prob) + IDT IDT w3 = , 2], stddev= ) IDT IDT b3 = , shape= ) IDT IDT self.output = tf.nn.softmax(tf.matmul(h3_drop, w3) + b3) + + IDT def __setup_ops(self): + IDT IDT cross_entropy = -tf.reduce_sum(self.actual_class * tf.log(self.output)) + IDT IDT self._summary = tf.summary.scalr(self.label, cross_entropy) + IDT IDT self.train_op = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + IDT IDT self.merge_summaries = tf.summary.merge([self._summary]) + IDT IDT correct_prediction = tf.equal(tf.argmax(self.output, 1), tf.argmax(self.actual_class, 1)) + IDT IDT self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + + +class Trainer(Model): + + IDT def train(self, steps, data): + IDT IDT self.__prepare_train(self.session) + IDT IDT for i in range(steps): + IDT IDT IDT self.__do_train(self.session, i, data) + IDT IDT IDT if i % 100 == 0: + IDT IDT IDT IDT self.__add_summary(self.session, i, data) + IDT IDT IDT IDT self.__print_status(self.session, i, data) + + IDT def __prepare_train(self, session): + IDT IDT self.summary_writer = , graph_def= ) IDT IDT session.run(tf.initialize_all_variables()) + + IDT def __do_train(self, session, i, data): + IDT IDT session.run(self.train_op, feed_dict= ) + IDT def __add_summary(self, session, i, data): + IDT IDT summary_str = , feed_dict= ) IDT IDT summary_str += , feed_dict= ) IDT IDT self.summary_writer.add_summary(summary_str, i) + + IDT def __print_status(self, session, i, data): + IDT IDT train_accuracy = , feed_dict= ) IDT IDT test_accuracy = , feed_dict= ) IDT IDT ) + + IDT def train_feed_dict(self, data): + IDT IDT return {self.trade_data: data.train_data(), self.actual_class: data.train_up_down(), self.keep_prob: 0.8, self.label: 'train'} + + IDT def test_feed_dict(self, data): + IDT IDT return {self.trade_data: data.test_data(), self.actual_class: data.test_up_down(), self.keep_prob: 1, self.label: 'test'} + + IDT def __reshape(self, profit_or_loss): + IDT IDT return profit_or_loss.values.reshape(len(profit_or_loss.values), 1) + + +class Estimator(Model): + + IDT def estimate(self, data): + IDT IDT return self.session.run(tf.argmax(self.output, 1), feed_dict= ) + IDT def estimate_feed_dict(self, data): + IDT IDT return {self.trade_data: data, self.keep_prob: 1} + + +def main(): + IDT with Trainer() as trainer: + IDT IDT trainer.train(10001, data) + IDT IDT trainer.save('./model.ckpt') + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +from math import sin, cos +from RLlib import NN +import tensorflow as tf +from collections import deque +import random +MAX_REPLAY_SIZE = 10000 +DISCOUNTFACTOR = 0.99 +CONTROL_DIM = 1 +STATE_DIM = 1 + + +class RLagent: + + IDT def __init__(self): + IDT IDT self.discount_factor = DISCOUNTFACTOR + IDT IDT self.Q_out = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.s_next = tf.placeholder(tf.float32, [None, STATE_DIM]) + IDT IDT self.action = tf.placeholder(tf.float32, [None, CONTROL_DIM]) + IDT IDT self.s = tf.placeholder(tf.float32, [None, STATE_DIM]) + IDT IDT self.mu_out = tf.placeholder(tf.float32, [None, CONTROL_DIM]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.Q_NN = NN(tf.concat([self.s, self.action], 1), self.Q_out, [16, 16], CONTROL_DIM + STATE_DIM, 1) + IDT IDT self.mu_NN = NN(self.s, self.mu_out, [1, 1], STATE_DIM, CONTROL_DIM) + IDT IDT self.target_mu_NN = NN(self.s_next, self.mu_out, [1, 1], STATE_DIM, CONTROL_DIM) + IDT IDT self.target_Q_NN = NN(tf.concat([self.s_next, self.target_mu_NN.y_output], 1), self.Q_out, [16, 16], CONTROL_DIM + STATE_DIM, 1) + IDT IDT self.yt_pl = self.reward + DISCOUNTFACTOR * self.target_Q_NN.y_output + IDT IDT self.losses = tf.squared_difference(self.yt_pl, self.Q_NN.y_output) + IDT IDT self.L = tf.reduce_mean(self.losses) + IDT IDT self.optimizer = tf.train.RMSPropOptimizer(0.00025, 0.99, 0.0, 1e-06) + IDT IDT self.train_op = , global_step= ) IDT IDT copy_op = self.update_target_network(1, self.mu_NN, self.target_mu_NN) + IDT IDT self.R = Replay(MAX_REPLAY_SIZE) + IDT IDT init = tf.global_variables_initializer() + IDT IDT self.sess = tf.Session() + IDT IDT self.sess.run(init) + IDT IDT self.sess.run(copy_op) + + IDT def update_target_network(self, tau, N, N_target): + IDT IDT var_copy = [] + IDT IDT for var, var_target in zip(N.params, N_target.params): + IDT IDT IDT var_copy.append(var_target.assign(tau * var + (1 - tau) * var_target)) + IDT IDT return tf.group(*var_copy) + + IDT def do_action(self, state): + IDT IDT return self.sess.run(self.mu_NN.y_output, {self.s: state}) + + IDT def do_action_target(self, state): + IDT IDT return self.sess.run(self.target_mu_NN.y_output, {self.s: state}) + + IDT def update_critic(self, state, action, next_state, reward): + IDT IDT dictionary = {self.reward: reward, self.s_next: next_state, self.s: state, self.action: action} + IDT IDT self.sess.run(self.train_op, dictionary) + + +class Replay: + + IDT def __init__(self, max_size): + IDT IDT self.max_size = max_size + IDT IDT self.transitions = deque() + + IDT def add(self, observation, action, reward, observation2): + IDT IDT if len(self.transitions) > self.max_size: + IDT IDT IDT self.transitions.popleft() + IDT IDT self.transitions.append((observation, action, reward, observation2)) + + IDT def sample(self, count): + IDT IDT return random.sample(self.transitions, count) + + IDT def size(self): + IDT IDT return len(self.transitions) + +import joker as act_tf +import tensorflow as ans_tf + +import numpy as np +rrr = ,)).astype(np.float32) )sss = ,)).astype(np.float32) ) + +def test_forward_conv(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , shape= , 3, 2, 5]) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2), w: np.arange(3 * 3 * 2 * 5).reshape(3, 3, 2, 5)}) ) IDT IDT return res + + +def test_forward_conv2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , shape= , 3, 2, 5]) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2), w: sss.reshape(3, 3, 2, 5)}) ) IDT IDT return res + + +def test_backward_conv(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , dtype= , 3, 2, 5)) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op, feed_dict= , 4, 4, 2)}) ) IDT IDT res = w.eval() + IDT IDT return res + + +def test_backward_conv2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = tf.Variable(sss.reshape(3, 3, 2, 5)) + IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT grad_W, = tf.gradients(loss, [w]) + IDT IDT res = , 4, 4, 2)}) ) IDT IDT return res + + +def test_backward_conv3(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = tf.Variable(sss.reshape(3, 3, 2, 5)) + IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT grad_W, = tf.gradients(loss, [x]) + IDT IDT res = , 4, 4, 2)}) ) IDT IDT return res + + +def test_forward_maxpool(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2)}) ) IDT IDT return res + + +def test_forward_maxpool2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2)}) ) IDT IDT return res + + +def test_backward_maxpool(tf): + IDT with tf.Session() as sess: + IDT IDT x = , dtype= , 4, 4, 2)) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT loss = tf.reduce_sum(pool1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op) + IDT IDT res = x.eval() + IDT IDT return res + + +def test_backward_maxpool2(tf): + IDT with tf.Session() as sess: + IDT IDT x = tf.Variable(rrr.reshape(3, 4, 4, 2)) + IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT loss = tf.reduce_sum(pool1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op) + IDT IDT res = x.eval() + IDT IDT return res + + +if __name__ == '__main__': + IDT testcases = [test_forward_conv, test_forward_conv2, test_backward_conv, test_backward_conv2, test_backward_conv3, test_forward_maxpool2, test_backward_maxpool2] + IDT for item in testcases: + IDT IDT ans = item(ans_tf) + IDT IDT + IDT IDT + IDT IDT + IDT IDT res = item(act_tf) + IDT IDT np.testing.assert_allclose(res, ans, atol= ) IDT IDT +import numpy as np +import tensorflow as tf +rnn_cell = {'rnn': tf.nn.rnn_cell.BasicRNNCell, 'lstm': tf.nn.rnn_cell.LSTMCell, 'gru': tf.nn.rnn_cell.GRUCell} +opt_method = {'sgd': tf.train.GradientDescentOptimizer, 'adam': tf.train.AdamOptimizer, 'adadelta': tf.train.AdadeltaOptimizer, 'adagrad': tf.train.AdagradOptimizer} + + +class Cnn(object): + IDT """ + IDT Basic CNN model. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'Cnn' + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.filter_sizes = setting.filter_sizes + IDT IDT self.filter_num = setting.filter_num + IDT IDT self.class_num = setting.class_num + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [setting.pos_num, setting.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [setting.pos_num, setting.pos_size]) + IDT IDT IDT self.embed_size_pos = setting.pos_size + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_all_expanded = tf.expand_dims(self.emb_all, -1) + IDT IDT with tf.name_scope('conv_maxpooling'): + IDT IDT IDT pooled_outputs = [] + IDT IDT IDT for i, filter_size in enumerate(self.filter_sizes): + IDT IDT IDT IDT with tf.name_scope('conv-maxpool-{}'.format(filter_size)): + IDT IDT IDT IDT IDT filter_shape = [filter_size, self.embed_size_x + 2 * self.embed_size_pos, 1, self.filter_num] + IDT IDT IDT IDT IDT w = , filter_shape, initializer= ) IDT IDT IDT IDT IDT b = , [self.filter_num], initializer= ) IDT IDT IDT IDT IDT tf.summary.histogram('W_{}'.format(filter_size), w) + IDT IDT IDT IDT IDT tf.summary.histogram('b_{}'.format(filter_size), b) + IDT IDT IDT IDT IDT conv = , w, strides= , 1, 1, 1], padding= , name= ) IDT IDT IDT IDT IDT h = , b), name= ) IDT IDT IDT IDT IDT pooled = , ksize= , self.max_sentence_len - filter_size + 1, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT IDT IDT IDT pooled_outputs.append(pooled) + IDT IDT IDT num_filters_total = self.filter_num * len(self.filter_sizes) + IDT IDT IDT h_pool = tf.concat(pooled_outputs, 3) + IDT IDT IDT h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + IDT IDT IDT self.outputs = tf.nn.dropout(h_pool_flat, self.dropout_keep_rate) + IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [self.filter_num * len(self.filter_sizes), self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT IDT self.fc_out = tf.matmul(self.outputs, self.fc_w) + self.fc_b + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_out) + IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT IDT self.accuracy = , tf.argmax(self.input_labels, 1)), 'float'), name= ) IDT IDT IDT tf.summary.scalar('accuracy', self.accuracy) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class PCnn(object): + IDT """ + IDT PCNN model. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'PCnn' + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.filter_sizes = setting.filter_sizes + IDT IDT self.filter_num = setting.filter_num + IDT IDT self.class_num = setting.class_num + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_mask = , [None, 3, self.max_sentence_len], name= ) IDT IDT IDT self.pcnn_mask = , [0, 2, 1]), axis= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [setting.pos_num, setting.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [setting.pos_num, setting.pos_size]) + IDT IDT IDT self.embed_size_pos = setting.pos_size + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.conv_input = tf.expand_dims(self.emb_all, 1) + IDT IDT with tf.name_scope('conv_maxpooling'): + IDT IDT IDT pooled_outputs = [] + IDT IDT IDT for i, filter_size in enumerate(self.filter_sizes): + IDT IDT IDT IDT with tf.name_scope('conv-maxpool-{}'.format(filter_size)): + IDT IDT IDT IDT IDT with tf.name_scope('conv-{}'.format(filter_size)): + IDT IDT IDT IDT IDT IDT conv_out = , self.filter_num, [1, filter_size], padding= , kernel_initializer= ) IDT IDT IDT IDT IDT IDT conv_out = , [0, 2, 1]), axis= ) IDT IDT IDT IDT IDT with tf.name_scope('maxpool-{}'.format(filter_size)): + IDT IDT IDT IDT IDT IDT pcnn_pool = , axis= ) IDT IDT IDT IDT IDT IDT pcnn_pool = tf.reshape(pcnn_pool, [-1, self.filter_num * 3]) + IDT IDT IDT IDT IDT IDT pooled_outputs.append(pcnn_pool) + IDT IDT IDT feature_size = self.filter_num * 3 * len(self.filter_sizes) + IDT IDT IDT h_pool = tf.concat(pooled_outputs, 3) + IDT IDT IDT h_pool_flat = tf.reshape(h_pool, [-1, feature_size]) + IDT IDT IDT self.outputs = tf.nn.dropout(h_pool_flat, self.dropout_keep_rate) + IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [feature_size, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT IDT self.fc_out = tf.matmul(self.outputs, self.fc_w) + self.fc_b + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_out) + IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT IDT self.accuracy = , tf.argmax(self.input_labels, 1)), 'float'), name= ) IDT IDT IDT tf.summary.scalar('accuracy', self.accuracy) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_mask: input_data.mask, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_mask: input_data.mask, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class Cnn_Deep(object): + IDT """ + IDT Multi-layer CNN model. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'Cnn_Deep' + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT IDT self.filter_sizes = setting.filter_sizes + IDT IDT IDT self.filter_num = setting.filter_num + IDT IDT IDT self.max_pool_sizes = setting.max_pool_sizes + IDT IDT IDT self.fc_sizes = setting.fc_sizes + IDT IDT IDT self.class_num = setting.class_num + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_mask = setting.dropout_mask + IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [setting.pos_num, setting.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [setting.pos_num, setting.pos_size]) + IDT IDT IDT self.embed_size_pos = setting.pos_size + IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.emb_sen = tf.reshape(tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen), [-1, self.max_sentence_len, self.embed_size_x]) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_all_expanded = tf.expand_dims(self.emb_all, -1) + IDT IDT with tf.name_scope('conv_maxpooling'): + IDT IDT IDT nl_input = self.emb_all_expanded + IDT IDT IDT for i in range(len(self.filter_sizes)): + IDT IDT IDT IDT filter_size = self.filter_sizes[i] + IDT IDT IDT IDT filter_num = self.filter_num[i] + IDT IDT IDT IDT with tf.name_scope('conv_{}'.format(i)): + IDT IDT IDT IDT IDT filter_shape = [filter_size, nl_input.shape[2], 1, filter_num] + IDT IDT IDT IDT IDT w = , filter_shape, initializer= ) IDT IDT IDT IDT IDT b = , [filter_num], initializer= ) IDT IDT IDT IDT IDT tf.summary.histogram('W_conv_{}'.format(i), w) + IDT IDT IDT IDT IDT tf.summary.histogram('b_conv_{}'.format(i), b) + IDT IDT IDT IDT IDT conv = , w, strides= , 1, 1, 1], padding= , name= ) IDT IDT IDT IDT IDT nl_input = , b), name= , [0, 1, 3, 2]) ) IDT IDT IDT IDT if self.max_pool_sizes[i] != 0: + IDT IDT IDT IDT IDT with tf.name_scope('max_pool_{}'.format(i)): + IDT IDT IDT IDT IDT IDT max_pool_size = self.max_pool_sizes[i] + IDT IDT IDT IDT IDT IDT nl_input = , ksize= , max_pool_size[0], max_pool_size[1], 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT IDT IDT if self.dropout_mask[i] != 0: + IDT IDT IDT IDT IDT self.nl_input = tf.nn.dropout(nl_input, self.dropout_keep_rate) + IDT IDT with tf.name_scope('fc_layers'): + IDT IDT IDT nl_input = tf.reshape(nl_input, [-1, int(nl_input.shape[1]) * int(nl_input.shape[2])]) + IDT IDT IDT for i in range(len(self.fc_sizes)): + IDT IDT IDT IDT with tf.name_scope('fc_layer_{}'.format(i)): + IDT IDT IDT IDT IDT fc_w = , [nl_input.shape[-1], self.fc_sizes[i]], initializer= ) IDT IDT IDT IDT IDT fc_b = , [self.fc_sizes[i]], initializer= ) IDT IDT IDT IDT IDT tf.summary.histogram('fc_W_{}'.format(i), fc_w) + IDT IDT IDT IDT IDT tf.summary.histogram('fc_b_{}'.format(i), fc_b) + IDT IDT IDT IDT IDT nl_input = tf.matmul(nl_input, fc_w) + fc_b + IDT IDT IDT IDT IDT if self.dropout_mask[i - len(self.filter_sizes)] != 0: + IDT IDT IDT IDT IDT IDT self.nl_input = tf.nn.dropout(nl_input, self.dropout_keep_rate) + IDT IDT IDT self.fc_out = nl_input + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_out) + IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT IDT self.accuracy = , tf.argmax(self.input_labels, 1)), 'float'), name= ) IDT IDT IDT tf.summary.scalar('accuracy', self.accuracy) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT input_x = input_data.x + IDT IDT feed_dict = {self.input_sen: input_x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT input_x = input_data.x + IDT IDT feed_dict = {self.input_sen: input_x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class Rnn(object): + IDT """ + IDT Basic Rnn model. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'Rnn' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.hidden_size = setting.hidden_size + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT self.rnn_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT self.rnn_cell = , output_keep_prob= ) IDT IDT IDT self.outputs, self.states = , self.emb_all_us, dtype= ) IDT IDT IDT if setting.hidden_select == 'last': + IDT IDT IDT IDT self.output_final = self.outputs[-1] + IDT IDT IDT elif setting.hidden_select == 'avg': + IDT IDT IDT IDT self.output_final = , 1), [-1, self.max_sentence_len, self.hidden_size]), axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [self.hidden_size, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.output_final, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_w', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn(object): + IDT """ + IDT Bidirectional RNN model. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.hidden_size = setting.hidden_size + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT self.foward_cell = , output_keep_prob= ) IDT IDT IDT self.backward_cell = , output_keep_prob= ) IDT IDT IDT self.rnn_outputs, _, _ = , self.backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT if setting.hidden_select == 'last': + IDT IDT IDT IDT self.rnn_output = self.rnn_outputs[-1] + IDT IDT IDT elif setting.hidden_select == 'avg': + IDT IDT IDT IDT self.rnn_output = , 1), [-1, self.max_sentence_len, self.hidden_size * 2]), axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [self.hidden_size * 2, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rnn_output, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_w', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_output = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_output, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_output], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Deep(object): + IDT """ + IDT Bidirectional Deep RNN model. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Deep' + IDT IDT self.cell_type = setting.cells + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.hidden_sizes = setting.hidden_sizes + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT outputs_list = [] + IDT IDT IDT for layer in range(len(self.cell_type)): + IDT IDT IDT IDT with tf.name_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT with tf.variable_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type[layer]](self.hidden_sizes[layer]) + IDT IDT IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type[layer]](self.hidden_sizes[layer]) + IDT IDT IDT IDT IDT IDT layer_output, _, _ = , [backward_cell], self.emb_all, dtype= ) IDT IDT IDT IDT IDT IDT outputs_list.append(layer_output) + IDT IDT IDT self.rnn_output = , axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT fc_w = tf.get_variable('fc_W', [self.hidden_sizes[-1] * 2, self.class_num]) + IDT IDT IDT fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rnn_output, fc_w) + fc_b + IDT IDT IDT tf.summary.histogram('fc_W', fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('pred'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Att(object): + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Att' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.hidden_size = setting.hidden_size + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT with tf.name_scope('birnn'): + IDT IDT IDT IDT self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT IDT self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT IDT self.foward_cell = , output_keep_prob= ) IDT IDT IDT IDT self.backward_cell = , output_keep_prob= ) IDT IDT IDT IDT rnn_outputs, _, _ = , self.backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT IDT outputs_forward = [i[:, :self.hidden_size] for i in rnn_outputs] + IDT IDT IDT IDT outputs_backward = [i[:, self.hidden_size:] for i in rnn_outputs] + IDT IDT IDT IDT output_forward = , values= , [-1, self.max_sentence_len, self.hidden_size]) ) IDT IDT IDT IDT output_backward = , values= , [-1, self.max_sentence_len, self.hidden_size]) ) IDT IDT IDT IDT self.rnn_outputs = tf.add(output_forward, output_backward) + IDT IDT IDT with tf.name_scope('attention'): + IDT IDT IDT IDT self.attention_w = tf.get_variable('attention_omega', [self.hidden_size, 1]) + IDT IDT IDT IDT self.attention_A = tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.rnn_outputs), [-1, self.hidden_size]), self.attention_w), [-1, self.max_sentence_len])), [-1, 1, self.max_sentence_len]) + IDT IDT IDT IDT self.rnn_output = tf.reshape(tf.matmul(self.attention_A, self.rnn_outputs), [-1, self.hidden_size]) + IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [self.hidden_size, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rnn_output, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_w', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_output = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_output, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.l2_regular = , weights_list= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.loss) + self.l2_regular + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_output], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_SelfAtt(object): + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_SelfAtt' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.hidden_size = setting.hidden_size + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT with tf.name_scope('birnn'): + IDT IDT IDT IDT self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT IDT self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT IDT self.foward_cell = , output_keep_prob= ) IDT IDT IDT IDT self.backward_cell = , output_keep_prob= ) IDT IDT IDT IDT self.rnn_outputs, _, _ = , self.backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT IDT self.output_h = tf.reshape(tf.concat(self.rnn_outputs, 1), [-1, self.max_sentence_len, self.hidden_size * 2]) + IDT IDT IDT with tf.name_scope('attention'): + IDT IDT IDT IDT self.attention_Ws1 = tf.get_variable('attention_Ws1', [self.hidden_size * 2, setting.da]) + IDT IDT IDT IDT self.attention_Ws2 = tf.get_variable('attention_Ws2', [setting.da, setting.r]) + IDT IDT IDT IDT self.attention_A = tf.nn.softmax(tf.transpose(tf.reshape(tf.matmul(tf.tanh(tf.matmul(tf.reshape(self.output_h, [-1, self.hidden_size * 2]), self.attention_Ws1)), self.attention_Ws2), [-1, self.max_sentence_len, setting.r]), [0, 2, 1])) + IDT IDT IDT IDT self.M = tf.matmul(self.attention_A, self.output_h) + IDT IDT IDT IDT tf.summary.histogram('Ws1', self.attention_Ws1) + IDT IDT IDT IDT tf.summary.histogram('Ws2', self.attention_Ws2) + IDT IDT with tf.name_scope('full_connection'): + IDT IDT IDT self.vec_M = tf.reshape(self.M, [-1, self.hidden_size * 2 * setting.r]) + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [self.hidden_size * 2 * setting.r, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.sen_rep = tf.matmul(self.vec_M, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('vec_M', self.vec_M) + IDT IDT IDT tf.summary.histogram('fc_w', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_output = tf.nn.softmax(self.sen_rep) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_output, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.P_att_matrix = tf.matmul(self.attention_A, tf.transpose(self.attention_A, [0, 2, 1])) - tf.eye(setting.r, setting.r) + IDT IDT IDT self.P_att = tf.pow(tf.norm(self.P_att_matrix), 2) + IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + 0.0001 * self.P_att + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_output], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Res(object): + IDT """ + IDT Bidirectional Residual RNN model. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Res' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.hidden_size = setting.hidden_size + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT outputs_list = [] + IDT IDT IDT for layer in range(2): + IDT IDT IDT IDT with tf.name_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT with tf.variable_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT IDT IDT IDT layer_output, _, _ = , [backward_cell], self.emb_all, dtype= ) IDT IDT IDT IDT IDT IDT outputs_list.append(layer_output) + IDT IDT IDT outputs = outputs_list[0] + outputs_list[1] + IDT IDT with tf.name_scope('max_pooling'): + IDT IDT IDT pooled = , -1), [1, self.max_sentence_len, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT mp_out = tf.squeeze(pooled) + IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT fc_w = tf.get_variable('fc_W', [self.hidden_size * 2, self.class_num]) + IDT IDT IDT fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(mp_out, fc_w) + fc_b + IDT IDT IDT tf.summary.histogram('fc_W', fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('pred'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Ent(object): + IDT """ + IDT Bidirectional RNN model with entity description. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Ent' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sen_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_e1_us = , num= , axis= ) IDT IDT IDT self.emb_e2_us = , num= , axis= ) IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.variable_scope('sentence_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT foward_cell = , output_keep_prob= ) IDT IDT IDT IDT backward_cell = , output_keep_prob= ) IDT IDT IDT IDT sen_output, _, _ = , backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT IDT if setting.hidden_select == 'last': + IDT IDT IDT IDT IDT self.sent_output = sen_output[-1] + IDT IDT IDT IDT elif setting.hidden_select == 'avg': + IDT IDT IDT IDT IDT self.sent_output = , 1), [-1, self.max_sen_len, self.hidden_size_sen * 2]), axis= ) IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , backward_cell, self.emb_e1_us, dtype= ) IDT IDT IDT IDT ent2_outputs, _, _ = , backward_cell, self.emb_e2_us, dtype= ) IDT IDT IDT IDT ent1_output = , 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis= ) IDT IDT IDT IDT ent2_output = , 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis= ) IDT IDT IDT IDT self.ent_out = , ent2_output], axis= ) IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.rep = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [self.hidden_size_sen * 2 + self.hidden_size_ent * 4, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Att_Ent(object): + IDT """ + IDT Bidirectional RNN model with attention and entity spell. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Att_Ent' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_e1_us = , num= , axis= ) IDT IDT IDT self.emb_e2_us = , num= , axis= ) IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.variable_scope('sentence_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT foward_cell = , output_keep_prob= ) IDT IDT IDT IDT backward_cell = , output_keep_prob= ) IDT IDT IDT IDT rnn_outputs, _, _ = , backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT IDT self.sent_outputs = , axis= , [-1, self.max_sentence_len, self.hidden_size_sen * 2]) ) IDT IDT IDT with tf.name_scope('attention'): + IDT IDT IDT IDT self.attention_w = tf.get_variable('attention_omega', [self.sent_outputs.shape[-1], 1]) + IDT IDT IDT IDT self.attention_A = tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]), self.attention_w), [-1, self.max_sentence_len])), [-1, 1, self.max_sentence_len]) + IDT IDT IDT IDT self.att_output = tf.reshape(tf.matmul(self.attention_A, self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]) + IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , backward_cell, self.emb_e1_us, dtype= ) IDT IDT IDT IDT e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + IDT IDT IDT IDT e1_output = tf.reduce_mean(e1_outputs, 1) + IDT IDT IDT IDT ent2_outputs, _, _ = , backward_cell, self.emb_e2_us, dtype= ) IDT IDT IDT IDT e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + IDT IDT IDT IDT e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + IDT IDT IDT IDT e2_output_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_output_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_outputs = tf.add(e2_output_forward, e2_output_backward) + IDT IDT IDT IDT e2_output = tf.reduce_mean(e2_outputs, 1) + IDT IDT IDT IDT self.ent_out = , e2_output], axis= ) IDT IDT IDT IDT self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.rep = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [int(self.att_output.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_SelfAtt_Ent(object): + IDT """ + IDT Bidirectional RNN model with attention and entity spell. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_SelfAtt_Ent' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_e1_us = , num= , axis= ) IDT IDT IDT self.emb_e2_us = , num= , axis= ) IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.variable_scope('sentence_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT foward_cell = , output_keep_prob= ) IDT IDT IDT IDT backward_cell = , output_keep_prob= ) IDT IDT IDT IDT rnn_outputs, _, _ = , backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT IDT self.sent_outputs = , axis= , [-1, self.max_sentence_len, self.hidden_size_sen * 2]) ) IDT IDT IDT IDT with tf.name_scope('attention'): + IDT IDT IDT IDT IDT self.attention_Ws1 = tf.get_variable('attention_Ws1', [self.hidden_size_sen * 2, setting.da]) + IDT IDT IDT IDT IDT self.attention_Ws2 = tf.get_variable('attention_Ws2', [setting.da, setting.r]) + IDT IDT IDT IDT IDT self.attention_A = tf.nn.softmax(tf.transpose(tf.reshape(tf.matmul(tf.tanh(tf.matmul(tf.reshape(self.sent_outputs, [-1, self.hidden_size_sen * 2]), self.attention_Ws1)), self.attention_Ws2), [-1, self.max_sentence_len, setting.r]), [0, 2, 1])) + IDT IDT IDT IDT IDT self.M = tf.matmul(self.attention_A, self.sent_outputs) + IDT IDT IDT IDT IDT self.att_output = tf.reshape(self.M, [-1, self.hidden_size_sen * 2 * setting.r]) + IDT IDT IDT IDT IDT tf.summary.histogram('Ws1', self.attention_Ws1) + IDT IDT IDT IDT IDT tf.summary.histogram('Ws2', self.attention_Ws2) + IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , backward_cell, self.emb_e1_us, dtype= ) IDT IDT IDT IDT e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + IDT IDT IDT IDT e1_output = tf.reduce_mean(e1_outputs, 1) + IDT IDT IDT IDT ent2_outputs, _, _ = , backward_cell, self.emb_e2_us, dtype= ) IDT IDT IDT IDT e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + IDT IDT IDT IDT e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + IDT IDT IDT IDT e2_output_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_output_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_outputs = tf.add(e2_output_forward, e2_output_backward) + IDT IDT IDT IDT e2_output = tf.reduce_mean(e2_outputs, 1) + IDT IDT IDT IDT self.ent_out = , e2_output], axis= ) IDT IDT IDT IDT self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.rep = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [int(self.att_output.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_cAtt_Ent(object): + IDT """ + IDT Bidirectional RNN model with attention and entity spell. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_cAtt_Ent' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_e1_us = , num= , axis= ) IDT IDT IDT self.emb_e2_us = , num= , axis= ) IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.variable_scope('sentence_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT foward_cell = , output_keep_prob= ) IDT IDT IDT IDT backward_cell = , output_keep_prob= ) IDT IDT IDT IDT rnn_outputs, _, _ = , backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT IDT self.sent_outputs = , axis= , [-1, self.max_sentence_len, self.hidden_size_sen * 2]) ) IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , backward_cell, self.emb_e1_us, dtype= ) IDT IDT IDT IDT e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + IDT IDT IDT IDT e1_output = tf.reduce_mean(e1_outputs, 1) + IDT IDT IDT IDT ent2_outputs, _, _ = , backward_cell, self.emb_e2_us, dtype= ) IDT IDT IDT IDT e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + IDT IDT IDT IDT e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + IDT IDT IDT IDT e2_output_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_output_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_outputs = tf.add(e2_output_forward, e2_output_backward) + IDT IDT IDT IDT e2_output = tf.reduce_mean(e2_outputs, 1) + IDT IDT IDT IDT self.ent_out = , e2_output], axis= ) IDT IDT IDT IDT self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + IDT IDT with tf.name_scope('attention_layer'): + IDT IDT IDT self.attention_w = tf.get_variable('attention_omega', [self.sent_outputs.shape[-1], self.ent_att.shape[-2]]) + IDT IDT IDT self.attention_A = tf.reshape(tf.nn.softmax(tf.squeeze(tf.matmul(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]), self.attention_w), [-1, self.max_sentence_len, self.hidden_size_ent]), self.ent_att))), [-1, 1, self.max_sentence_len]) + IDT IDT IDT self.att_output = tf.reshape(tf.matmul(self.attention_A, self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]) + IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.rep = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [int(self.sent_outputs.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Cnn_Ent(object): + IDT """ + IDT A model use birnn and cnn. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Cnn_Ent' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.filter_sizes = setting.filter_sizes + IDT IDT self.filter_num = setting.filter_num + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_sen_us = , num= , axis= ) IDT IDT IDT self.emb_e1_us = , num= , axis= ) IDT IDT IDT self.emb_e2_us = , num= , axis= ) IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT IDT with tf.variable_scope('sentence_encoder'): + IDT IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT IDT foward_cell = , output_keep_prob= ) IDT IDT IDT IDT IDT backward_cell = , output_keep_prob= ) IDT IDT IDT IDT IDT sen_rnn_output, _, _ = , backward_cell, self.emb_sen_us, dtype= ) IDT IDT IDT IDT IDT self.sen_rnn_output = , axis= , [-1, self.max_sentence_len, self.hidden_size_sen * 2]) ) IDT IDT IDT with tf.name_scope('add_pos'): + IDT IDT IDT IDT self.cnn_input = tf.expand_dims(tf.concat([self.sen_rnn_output, self.emb_pos1, self.emb_pos2], 2), -1) + IDT IDT IDT with tf.name_scope('conv_maxpooling'): + IDT IDT IDT IDT pooled_outputs = [] + IDT IDT IDT IDT for i, filter_size in enumerate(self.filter_sizes): + IDT IDT IDT IDT IDT with tf.name_scope('conv-maxpool-{}'.format(filter_size)): + IDT IDT IDT IDT IDT IDT filter_shape = [filter_size, self.hidden_size_sen * 2 + self.pos_size * 2, 1, self.filter_num] + IDT IDT IDT IDT IDT IDT w = , filter_shape, initializer= ) IDT IDT IDT IDT IDT IDT b = , [self.filter_num], initializer= ) IDT IDT IDT IDT IDT IDT tf.summary.histogram('W_{}'.format(filter_size), w) + IDT IDT IDT IDT IDT IDT tf.summary.histogram('b_{}'.format(filter_size), b) + IDT IDT IDT IDT IDT IDT conv = , w, strides= , 1, 1, 1], padding= , name= ) IDT IDT IDT IDT IDT IDT h = , b), name= ) IDT IDT IDT IDT IDT IDT pooled = , ksize= , self.max_sentence_len - filter_size + 1, 1, 1], strides= , 1, 1, 1], padding= , name= ) IDT IDT IDT IDT IDT IDT pooled_outputs.append(pooled) + IDT IDT IDT IDT num_filters_total = self.filter_num * len(self.filter_sizes) + IDT IDT IDT IDT h_pool = tf.concat(pooled_outputs, 3) + IDT IDT IDT IDT h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + IDT IDT IDT IDT self.sen_output = tf.nn.dropout(h_pool_flat, self.dropout_keep_rate) + IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , backward_cell, self.emb_e1_us, dtype= ) IDT IDT IDT IDT ent2_outputs, _, _ = , backward_cell, self.emb_e2_us, dtype= ) IDT IDT IDT IDT ent1_output = , 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis= ) IDT IDT IDT IDT ent2_output = , 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis= ) IDT IDT IDT IDT self.ent_out = , ent2_output], axis= ) IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.outputs = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [self.filter_num * len(self.filter_sizes) + self.hidden_size_ent * 4, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT IDT self.fc_out = tf.matmul(self.outputs, self.fc_w) + self.fc_b + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_out) + IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT IDT self.accuracy = , tf.argmax(self.input_labels, 1)), 'float'), name= ) IDT IDT IDT tf.summary.scalar('accuracy', self.accuracy) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Res_Ent(object): + IDT """ + IDT Bidirectional Residual RNN model with entity spell. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Res_Ent' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sen_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT IDT outputs_list = [] + IDT IDT IDT IDT for layer in range(2): + IDT IDT IDT IDT IDT with tf.name_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT IDT with tf.variable_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT IDT IDT IDT layer_output, _, _ = , [backward_cell], self.emb_all, dtype= ) IDT IDT IDT IDT IDT IDT IDT outputs_list.append(layer_output) + IDT IDT IDT IDT outputs = outputs_list[0] + outputs_list[1] + IDT IDT IDT with tf.name_scope('max_pooling'): + IDT IDT IDT IDT pooled = , -1), [1, self.max_sen_len, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT mp_out = tf.squeeze(pooled) + IDT IDT IDT IDT self.sen_output = mp_out + IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , [backward_cell], self.emb_e1, dtype= ) IDT IDT IDT IDT ent2_outputs, _, _ = , [backward_cell], self.emb_e2, dtype= ) IDT IDT IDT IDT ent1_output = , axis= ) IDT IDT IDT IDT ent2_output = , axis= ) IDT IDT IDT IDT self.ent_out = , ent2_output], axis= ) IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.rep = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT fc_w = tf.get_variable('fc_W', [self.hidden_size_sen * 2 + self.hidden_size_ent * 4, self.class_num]) + IDT IDT IDT fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rep, fc_w) + fc_b + IDT IDT IDT tf.summary.histogram('fc_W', fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('pred'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Res_cAtt_Ent(object): + IDT """ + IDT Bidirectional Residual RNN model with entity spell and attention. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Res_cAtt_Ent' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sen_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.input_sen = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sen_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , [backward_cell], self.emb_e1, dtype= ) IDT IDT IDT IDT ent2_outputs, _, _ = , [backward_cell], self.emb_e2, dtype= ) IDT IDT IDT IDT ent1_output = , axis= ) IDT IDT IDT IDT ent2_output = , axis= ) IDT IDT IDT IDT self.ent_out = , ent2_output], axis= ) IDT IDT IDT IDT self.ent_att = tf.expand_dims(tf.subtract(ent2_output, ent1_output), -1) + IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.name_scope('rnn_layer'): + IDT IDT IDT IDT outputs_list = [] + IDT IDT IDT IDT for layer in range(2): + IDT IDT IDT IDT IDT with tf.name_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT IDT with tf.variable_scope('birnn_layer_{}'.format(layer)): + IDT IDT IDT IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT IDT IDT IDT layer_output, _, _ = , [backward_cell], self.emb_all, dtype= ) IDT IDT IDT IDT IDT IDT IDT outputs_list.append(layer_output) + IDT IDT IDT IDT self.sent_outputs = outputs_list[0] + outputs_list[1] + IDT IDT IDT with tf.name_scope('attention_layer'): + IDT IDT IDT IDT self.attention_w = tf.get_variable('attention_omega', [self.sent_outputs.shape[-1], self.ent_att.shape[-2]]) + IDT IDT IDT IDT self.attention_A = tf.reshape(tf.nn.softmax(tf.squeeze(tf.matmul(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]), self.attention_w), [-1, self.max_sen_len, int(self.ent_att.shape[-2])]), self.ent_att))), [-1, 1, self.max_sen_len]) + IDT IDT IDT IDT self.sen_output = tf.reshape(tf.matmul(self.attention_A, self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]) + IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.rep = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT fc_w = tf.get_variable('fc_W', [self.hidden_size_sen * 2 + self.hidden_size_ent * 4, self.class_num]) + IDT IDT IDT fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rep, fc_w) + fc_b + IDT IDT IDT tf.summary.histogram('fc_W', fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', fc_b) + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('pred'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.instance_loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.instance_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_loss = , self.model_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.class_label, self.softmax_res], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_Mi(object): + IDT """ + IDT Bidirectional RNN model with multi-instance learning. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_Mi' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.hidden_size = setting.hidden_size + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_size + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT self.bag_num = setting.bag_num + IDT IDT with tf.name_scope('input_layer'): + IDT IDT IDT self.bag_shapes = , [None], name= ) IDT IDT IDT self.instance_num = self.bag_shapes[-1] + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + IDT IDT IDT self.foward_cell = , output_keep_prob= ) IDT IDT IDT self.backward_cell = , output_keep_prob= ) IDT IDT IDT self.outputs, _, _ = , self.backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT if setting.hidden_select == 'last': + IDT IDT IDT IDT self.sen_emb = self.outputs[-1] + IDT IDT IDT elif setting.hidden_select == 'avg': + IDT IDT IDT IDT self.sen_emb = , 1), [-1, self.max_sentence_len, self.hidden_size * 2]), axis= ) IDT IDT with tf.name_scope('sentence_attention'): + IDT IDT IDT sen_repre = [] + IDT IDT IDT sen_alpha = [] + IDT IDT IDT sen_s = [] + IDT IDT IDT sen_out = [] + IDT IDT IDT self.prob = [] + IDT IDT IDT self.predictions = [] + IDT IDT IDT self.loss = [] + IDT IDT IDT self.accuracy = [] + IDT IDT IDT self.total_loss = 0.0 + IDT IDT IDT self.sen_a = tf.get_variable('attention_A', [self.hidden_size * 2]) + IDT IDT IDT self.sen_r = tf.get_variable('query_r', [self.hidden_size * 2, 1]) + IDT IDT IDT relation_embedding = tf.get_variable('relation_embedding', [self.class_num, self.hidden_size * 2]) + IDT IDT IDT sen_d = tf.get_variable('bias_d', [self.class_num]) + IDT IDT IDT for i in range(self.bag_num): + IDT IDT IDT IDT sen_repre.append(tf.tanh(self.sen_emb[self.bag_shapes[i]:self.bag_shapes[i + 1]])) + IDT IDT IDT IDT bag_size = self.bag_shapes[i + 1] - self.bag_shapes[i] + IDT IDT IDT IDT sen_alpha.append(tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.multiply(sen_repre[i], self.sen_a), self.sen_r), [bag_size])), [1, bag_size])) + IDT IDT IDT IDT sen_s.append(tf.reshape(tf.matmul(sen_alpha[i], sen_repre[i]), [self.hidden_size * 2, 1])) + IDT IDT IDT IDT sen_out.append(tf.add(tf.reshape(tf.matmul(relation_embedding, sen_s[i]), [self.class_num]), sen_d)) + IDT IDT IDT IDT self.prob.append(tf.nn.softmax(sen_out[i])) + IDT IDT IDT IDT with tf.name_scope('output'): + IDT IDT IDT IDT IDT self.predictions.append(tf.argmax(self.prob[i], 0, name= ) IDT IDT IDT IDT with tf.name_scope('loss'): + IDT IDT IDT IDT IDT self.loss.append(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits= , labels= ) IDT IDT IDT IDT IDT if i == 0: + IDT IDT IDT IDT IDT IDT self.total_loss = self.loss[i] + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT self.total_loss += self.loss[i] + IDT IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT IDT self.accuracy.append(tf.reduce_mean(tf.cast(tf.equal(self.predictions[i], tf.argmax(self.input_labels[i], 0)), 'float'), name= ) IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT tf.summary.histogram('sen_a', self.sen_a) + IDT IDT tf.summary.histogram('sen_r', self.sen_r) + IDT IDT tf.summary.scalar('loss', self.total_loss) + IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT total_shape = [0] + IDT IDT total_num = 0 + IDT IDT total_x = [] + IDT IDT total_pos1 = [] + IDT IDT total_pos2 = [] + IDT IDT for bag_idx in range(len(input_data.x)): + IDT IDT IDT total_num += len(input_data.x[bag_idx]) + IDT IDT IDT total_shape.append(total_num) + IDT IDT IDT for sent in input_data.x[bag_idx]: + IDT IDT IDT IDT total_x.append(sent) + IDT IDT IDT for pos1 in input_data.pos1[bag_idx]: + IDT IDT IDT IDT total_pos1.append(pos1) + IDT IDT IDT for pos2 in input_data.pos2[bag_idx]: + IDT IDT IDT IDT total_pos2.append(pos2) + IDT IDT feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_accuracy, model_loss = , self.accuracy, self.total_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT total_shape = [0] + IDT IDT total_num = 0 + IDT IDT total_x = [] + IDT IDT total_pos1 = [] + IDT IDT total_pos2 = [] + IDT IDT for bag_idx in range(len(input_data.x)): + IDT IDT IDT total_num += len(input_data.x[bag_idx]) + IDT IDT IDT total_shape.append(total_num) + IDT IDT IDT for sent in input_data.x[bag_idx]: + IDT IDT IDT IDT total_x.append(sent) + IDT IDT IDT for pos1 in input_data.pos1[bag_idx]: + IDT IDT IDT IDT total_pos1.append(pos1) + IDT IDT IDT for pos2 in input_data.pos2[bag_idx]: + IDT IDT IDT IDT total_pos2.append(pos2) + IDT IDT feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.predictions, self.prob], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob + + +class BiRnn_SelfAtt_Ent_Mi(object): + IDT """ + IDT Bidirectional RNN model with attention and entity spell using multi-instance learning. + IDT """ + + IDT def __init__(self, x_embedding, setting): + IDT IDT self.model_name = 'BiRnn_SelfAtt_Ent_Mi' + IDT IDT self.cell_type = setting.cell + IDT IDT self.max_sentence_len = setting.sen_len + IDT IDT self.max_ent_len = setting.ent_len + IDT IDT self.hidden_size_sen = setting.hidden_size_sen + IDT IDT self.hidden_size_ent = setting.hidden_size_ent + IDT IDT self.class_num = setting.class_num + IDT IDT self.pos_num = setting.pos_num + IDT IDT self.pos_size = setting.pos_sized + IDT IDT self.learning_rate = setting.learning_rate + IDT IDT self.bag_num = setting.bag_num + IDT IDT with tf.name_scope('model_input'): + IDT IDT IDT self.bag_shapes = , [None], name= ) IDT IDT IDT self.instance_num = self.bag_shapes[-1] + IDT IDT IDT self.input_sen = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_labels = , [None, self.class_num], name= ) IDT IDT IDT self.input_pos1 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_pos2 = , [None, self.max_sentence_len], name= ) IDT IDT IDT self.input_e1 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.input_e2 = , [None, self.max_ent_len], name= ) IDT IDT IDT self.dropout_keep_rate = , name= ) IDT IDT with tf.name_scope('embedding_layer'): + IDT IDT IDT self.embed_matrix_x = , x_embedding.shape, initializer= ) IDT IDT IDT ent_embedding = np.load('data/char_vec.npy') + IDT IDT IDT self.embed_matrix_ent = , ent_embedding.shape, initializer= ) IDT IDT IDT self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + IDT IDT IDT self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + IDT IDT IDT self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + IDT IDT IDT self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + IDT IDT IDT self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + IDT IDT IDT self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + IDT IDT IDT self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + IDT IDT IDT self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + IDT IDT IDT self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + IDT IDT IDT self.emb_e1_us = , num= , axis= ) IDT IDT IDT self.emb_e2_us = , num= , axis= ) IDT IDT IDT self.emb_all_us = , num= , axis= ) IDT IDT with tf.name_scope('sentence_encoder'): + IDT IDT IDT with tf.variable_scope('sentence_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + IDT IDT IDT IDT foward_cell = , output_keep_prob= ) IDT IDT IDT IDT backward_cell = , output_keep_prob= ) IDT IDT IDT IDT rnn_outputs, _, _ = , backward_cell, self.emb_all_us, dtype= ) IDT IDT IDT IDT self.sent_outputs = , axis= , [-1, self.max_sentence_len, self.hidden_size_sen * 2]) ) IDT IDT IDT IDT with tf.name_scope('attention'): + IDT IDT IDT IDT IDT self.attention_Ws1 = tf.get_variable('attention_Ws1', [self.hidden_size_sen * 2, setting.da]) + IDT IDT IDT IDT IDT self.attention_Ws2 = tf.get_variable('attention_Ws2', [setting.da, setting.r]) + IDT IDT IDT IDT IDT self.attention_A = tf.nn.softmax(tf.transpose(tf.reshape(tf.matmul(tf.tanh(tf.matmul(tf.reshape(self.sent_outputs, [-1, self.hidden_size_sen * 2]), self.attention_Ws1)), self.attention_Ws2), [-1, self.max_sentence_len, setting.r]), [0, 2, 1])) + IDT IDT IDT IDT IDT self.M = tf.matmul(self.attention_A, self.sent_outputs) + IDT IDT IDT IDT IDT self.att_output = tf.reshape(self.M, [-1, self.hidden_size_sen * 2 * setting.r]) + IDT IDT IDT IDT IDT tf.summary.histogram('Ws1', self.attention_Ws1) + IDT IDT IDT IDT IDT tf.summary.histogram('Ws2', self.attention_Ws2) + IDT IDT with tf.name_scope('entity_encoder'): + IDT IDT IDT with tf.variable_scope('entity_encoder'): + IDT IDT IDT IDT foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + IDT IDT IDT IDT ent1_outputs, _, _ = , backward_cell, self.emb_e1_us, dtype= ) IDT IDT IDT IDT e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + IDT IDT IDT IDT e1_outputs_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + IDT IDT IDT IDT e1_output = tf.reduce_mean(e1_outputs, 1) + IDT IDT IDT IDT ent2_outputs, _, _ = , backward_cell, self.emb_e2_us, dtype= ) IDT IDT IDT IDT e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + IDT IDT IDT IDT e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + IDT IDT IDT IDT e2_output_forward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_output_backward = , values= , [-1, self.max_ent_len, self.hidden_size_ent]) ) IDT IDT IDT IDT e2_outputs = tf.add(e2_output_forward, e2_output_backward) + IDT IDT IDT IDT e2_output = tf.reduce_mean(e2_outputs, 1) + IDT IDT IDT IDT self.ent_out = , e2_output], axis= ) IDT IDT IDT IDT self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + IDT IDT with tf.name_scope('joint_layer'): + IDT IDT IDT self.rep = , self.ent_out], axis= ) IDT IDT with tf.name_scope('fc_layer'): + IDT IDT IDT self.fc_w = tf.get_variable('fc_W', [int(self.att_output.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + IDT IDT IDT self.fc_b = tf.get_variable('fc_b', [self.class_num]) + IDT IDT IDT self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + IDT IDT IDT tf.summary.histogram('fc_W', self.fc_w) + IDT IDT IDT tf.summary.histogram('fc_b', self.fc_b) + IDT IDT with tf.name_scope('sentence_attention'): + IDT IDT IDT sen_repre = [] + IDT IDT IDT sen_alpha = [] + IDT IDT IDT sen_s = [] + IDT IDT IDT sen_out = [] + IDT IDT IDT self.prob = [] + IDT IDT IDT self.predictions = [] + IDT IDT IDT self.loss = [] + IDT IDT IDT self.accuracy = [] + IDT IDT IDT self.total_loss = 0.0 + IDT IDT IDT self.sen_a = tf.get_variable('attention_A', [self.hidden_size * 2]) + IDT IDT IDT self.sen_r = tf.get_variable('query_r', [self.hidden_size * 2, 1]) + IDT IDT IDT relation_embedding = tf.get_variable('relation_embedding', [self.class_num, self.hidden_size * 2]) + IDT IDT IDT sen_d = tf.get_variable('bias_d', [self.class_num]) + IDT IDT IDT for i in range(self.bag_num): + IDT IDT IDT IDT sen_repre.append(tf.tanh(self.sen_emb[self.bag_shapes[i]:self.bag_shapes[i + 1]])) + IDT IDT IDT IDT bag_size = self.bag_shapes[i + 1] - self.bag_shapes[i] + IDT IDT IDT IDT sen_alpha.append(tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.multiply(sen_repre[i], self.sen_a), self.sen_r), [bag_size])), [1, bag_size])) + IDT IDT IDT IDT sen_s.append(tf.reshape(tf.matmul(sen_alpha[i], sen_repre[i]), [self.hidden_size * 2, 1])) + IDT IDT IDT IDT sen_out.append(tf.add(tf.reshape(tf.matmul(relation_embedding, sen_s[i]), [self.class_num]), sen_d)) + IDT IDT IDT IDT self.prob.append(tf.nn.softmax(sen_out[i])) + IDT IDT IDT IDT with tf.name_scope('output'): + IDT IDT IDT IDT IDT self.predictions.append(tf.argmax(self.prob[i], 0, name= ) IDT IDT IDT IDT with tf.name_scope('loss'): + IDT IDT IDT IDT IDT self.loss.append(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits= , labels= ) IDT IDT IDT IDT IDT if i == 0: + IDT IDT IDT IDT IDT IDT self.total_loss = self.loss[i] + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT self.total_loss += self.loss[i] + IDT IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT IDT self.accuracy.append(tf.reduce_mean(tf.cast(tf.equal(self.predictions[i], tf.argmax(self.input_labels[i], 0)), 'float'), name= ) IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT tf.summary.histogram('sen_a', self.sen_a) + IDT IDT tf.summary.histogram('sen_r', self.sen_r) + IDT IDT tf.summary.scalar('loss', self.total_loss) + IDT IDT self.merge_summary = tf.summary.merge_all() + IDT IDT with tf.name_scope('softmax'): + IDT IDT IDT self.softmax_res = tf.nn.softmax(self.fc_output) + IDT IDT with tf.name_scope('model_predict'): + IDT IDT IDT self.class_label = tf.argmax(self.softmax_res, 1) + IDT IDT with tf.name_scope('model_loss'): + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.model_loss = tf.reduce_mean(self.loss) + IDT IDT IDT tf.summary.scalar('model_loss', self.model_loss) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT self.optimizer = ) IDT IDT self.merge_summary = tf.summary.merge_all() + + IDT def fit(self, session, input_data, dropout_keep_rate): + IDT IDT total_shape = [0] + IDT IDT total_num = 0 + IDT IDT total_x = [] + IDT IDT total_pos1 = [] + IDT IDT total_pos2 = [] + IDT IDT for bag_idx in range(len(input_data.x)): + IDT IDT IDT total_num += len(input_data.x[bag_idx]) + IDT IDT IDT total_shape.append(total_num) + IDT IDT IDT for sent in input_data.x[bag_idx]: + IDT IDT IDT IDT total_x.append(sent) + IDT IDT IDT for pos1 in input_data.pos1[bag_idx]: + IDT IDT IDT IDT total_pos1.append(pos1) + IDT IDT IDT for pos2 in input_data.pos2[bag_idx]: + IDT IDT IDT IDT total_pos2.append(pos2) + IDT IDT feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + IDT IDT session.run(self.optimizer, feed_dict= ) IDT IDT summary, model_accuracy, model_loss = , self.accuracy, self.total_loss], feed_dict= ) IDT IDT return summary, model_loss + + IDT def evaluate(self, session, input_data): + IDT IDT total_shape = [0] + IDT IDT total_num = 0 + IDT IDT total_x = [] + IDT IDT total_pos1 = [] + IDT IDT total_pos2 = [] + IDT IDT for bag_idx in range(len(input_data.x)): + IDT IDT IDT total_num += len(input_data.x[bag_idx]) + IDT IDT IDT total_shape.append(total_num) + IDT IDT IDT for sent in input_data.x[bag_idx]: + IDT IDT IDT IDT total_x.append(sent) + IDT IDT IDT for pos1 in input_data.pos1[bag_idx]: + IDT IDT IDT IDT total_pos1.append(pos1) + IDT IDT IDT for pos2 in input_data.pos2[bag_idx]: + IDT IDT IDT IDT total_pos2.append(pos2) + IDT IDT feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + IDT IDT model_loss, label_pred, label_prob = , self.predictions, self.prob], feed_dict= ) IDT IDT return model_loss, label_pred, label_prob +import json +from collections import OrderedDict, namedtuple +from config import * +from utils import * +from tensorflow.python.framework.graph_util import convert_variables_to_constants + + +class Model: + IDT os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' + IDT _MODEL_DEF_TEMPLATE = {'batch_size': 1, 'dropout': False, 'init': None} + + IDT def __init__(self, name, load_dir, trainable, device): + IDT IDT self.name = name + IDT IDT self.trainable = trainable + IDT IDT self.load_dir = load_dir + IDT IDT self.write_dir = os.path.join(POLICY_PATH, 'tmp/') + IDT IDT self._vars = [] + IDT IDT self._ops = [] + IDT IDT self.scopes = [] + IDT IDT self._device = device + + IDT def _build_graph(self): + IDT IDT pass + + IDT def _constructor(self): + IDT IDT with tf.variable_scope(self.name, reuse= ) IDT IDT IDT try: + IDT IDT IDT IDT with tf.device(self._device): + IDT IDT IDT IDT IDT self._build_graph() + IDT IDT IDT except AttributeError: + IDT IDT IDT IDT self._build_graph() + + IDT def init(self, session, load_dir= ) IDT IDT load_dir = load_dir or self.load_dir + IDT IDT for i, layer_scope in enumerate(self.scopes): + IDT IDT IDT vars_to_load = [v for v in tf.global_variables() if v.name.startswith(layer_scope)] + IDT IDT IDT self._vars.extend(vars_to_load) + IDT IDT IDT session.run(tf.variables_initializer(var_list= ) IDT IDT IDT if load_dir and vars_to_load: + IDT IDT IDT IDT saver = tf.train.Saver(vars_to_load) + IDT IDT IDT IDT saver.restore(session, os.path.join(load_dir, self.name)) + + IDT def save_params(self, session, step= ) IDT IDT assert self.write_dir + IDT IDT info('Saving {0} to {1}'.format(self.name, self.write_dir)) + IDT IDT saver = tf.train.Saver(list(set(self._vars))) + IDT IDT saver.save(session, os.path.join(self.write_dir, self.name), global_step= ) + IDT def save_graph(self, session: tf.Session, fname: str, var_names: list): + IDT IDT frozen_graph = convert_variables_to_constants(session, session.graph_def, var_names) + IDT IDT tf.train.write_graph(frozen_graph, self.write_dir, fname + '.pb', as_text= ) IDT IDT tf.train.write_graph(frozen_graph, self.write_dir, fname + '.txt', as_text= ) + +class Policy(Model): + + IDT def __init__(self, session: tf.Session, name= , load_dir= , trainable= , selection= , device= ) IDT IDT super().__init__(name= , load_dir= , trainable= , device= ) IDT IDT self.session = session + IDT IDT self.state = , shape= , 8, 4), name= ) IDT IDT self.action_label = , shape= , 128), name= ) IDT IDT self.selection = selection + IDT IDT self._constructor() + + IDT def _build_graph(self): + IDT IDT self.batch_size = , dtype= , name= ) IDT IDT self.keep_prob = , dtype= , name= , dtype= , name= ) IDT IDT self.lr = , dtype= , name= ) IDT IDT self.adv = , dtype= , name= ) IDT IDT X = , tf.float32), axis= ) IDT IDT with tf.variable_scope('INCEPTION', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT for i, (ksizes, nkernels) in enumerate(zip(KERNEL_SIZES, N_KERNELS)): + IDT IDT IDT IDT conv = [] + IDT IDT IDT IDT for ks, nk in zip(ksizes, nkernels): + IDT IDT IDT IDT IDT w = , ks[1], X.shape[-1], nk], initializer= , trainable= , name= , ks[0], ks[1])) ) IDT IDT IDT IDT IDT b = , initializer= , trainable= , name= , ks[0], ks[1])) ) IDT IDT IDT IDT IDT c = , w, strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT z = INCEP_ACT(c + b) + IDT IDT IDT IDT IDT conv.append(z) + IDT IDT IDT IDT X = , axis= ) IDT IDT IDT X = , keep_prob= ) IDT IDT conv_out = ) IDT IDT X = conv_out + IDT IDT hwy_size = X.shape[-1] + IDT IDT with tf.variable_scope('HIGHWAY', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT for i in range(HWY_LAYERS): + IDT IDT IDT IDT with tf.variable_scope('HWY_{}'.format(i)): + IDT IDT IDT IDT IDT wh = , hwy_size], initializer= , trainable= , dtype= , name= ) IDT IDT IDT IDT IDT bh = , initializer= , trainable= , dtype= , name= ) IDT IDT IDT IDT IDT wt = , hwy_size], initializer= , trainable= , dtype= , name= ) IDT IDT IDT IDT IDT T = tf.sigmoid(tf.matmul(X, wt) + HWY_BIAS) + IDT IDT IDT IDT IDT H = tf.nn.relu(tf.matmul(X, wh) + bh) + IDT IDT IDT IDT IDT X = T * H + (1.0 - T) * X + IDT IDT IDT X = , keep_prob= ) IDT IDT IDT X = , conv_out], axis= ) IDT IDT with tf.variable_scope('OUTPUT', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT w = , 128], initializer= , trainable= , dtype= , name= ) IDT IDT IDT b = , initializer= , trainable= , dtype= , name= ) IDT IDT IDT self.logits = , w), b, name= ) IDT IDT IDT self.softmax = , axis= , name= ) IDT IDT IDT self.action = , axis= , name= ) IDT IDT IDT self.probs, self.actions = , k= , sorted= ) IDT IDT with tf.variable_scope('METRICS', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT self.top_1_acc = , axis= , predictions= , name= ) IDT IDT IDT self.top_2_acc = , tf.argmax(self.action_label, axis= , 2), tf.float32)) ) IDT IDT IDT self.top_3_acc = , tf.argmax(self.action_label, axis= , 3), tf.float32)) ) IDT IDT IDT self.top_5_acc = , tf.argmax(self.action_label, axis= , 5), tf.float32)) ) IDT IDT IDT self.top_10_acc = , tf.argmax(self.action_label, axis= , 10), tf.float32)) ) IDT IDT if self.trainable: + IDT IDT IDT with tf.variable_scope('LOSS', reuse= ) IDT IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT IDT self.step = , trainable= ) IDT IDT IDT IDT self.reg_loss = LAMBDA * tf.add_n([tf.nn.l2_loss(v) for v in tf.global_variables() if v.name.__contains__('w_')]) + IDT IDT IDT IDT self.cross_entropy = , logits= , name= ) IDT IDT IDT IDT self.loss1 = , self.cross_entropy, name= ) IDT IDT IDT IDT self.optimizer1 = , name= ) IDT IDT IDT IDT self.grad_update1 = , var_list= , global_step= , name= ) IDT IDT IDT IDT self.gradlogprob_adv = self.adv * tf.log(self.softmax) + IDT IDT IDT IDT self.pg_loss = , axis= , name= ) IDT IDT IDT IDT self.optimizer2 = , decay= , epsilon= ) IDT IDT IDT IDT self.policy_update = , self.vars], global_step= ) + IDT @property + IDT def vars(self): + IDT IDT return [v for v in tf.trainable_variables() if v.name.lower().__contains__(self.name.lower())] + + +class Value(Model): + + IDT def __init__(self, session: tf.Session, name= , load_dir= , trainable= , selection= , device= ) IDT IDT super().__init__(name= , load_dir= , trainable= , device= ) IDT IDT self.session = session + IDT IDT self.state = , shape= , 8, 4), name= ) IDT IDT self.action_label = , shape= , 128), name= ) IDT IDT self.selection = selection + IDT IDT self._constructor() + + IDT def _build_graph(self): + IDT IDT self.batch_size = , dtype= , name= ) IDT IDT self.keep_prob = , dtype= , name= , dtype= , name= ) IDT IDT self.lr = , dtype= , name= ) IDT IDT self.adv = , dtype= , name= ) IDT IDT X = , tf.float32), axis= ) IDT IDT with tf.variable_scope('INCEPTION', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT for i, (ksizes, nkernels) in enumerate(zip(KERNEL_SIZES, N_KERNELS)): + IDT IDT IDT IDT conv = [] + IDT IDT IDT IDT for ks, nk in zip(ksizes, nkernels): + IDT IDT IDT IDT IDT w = , ks[1], X.shape[-1], nk], initializer= , trainable= , name= , ks[0], ks[1])) ) IDT IDT IDT IDT IDT b = , initializer= , trainable= , name= , ks[0], ks[1])) ) IDT IDT IDT IDT IDT c = , w, strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT z = INCEP_ACT(c + b) + IDT IDT IDT IDT IDT conv.append(z) + IDT IDT IDT IDT X = , axis= ) IDT IDT IDT X = , keep_prob= ) IDT IDT conv_out = ) IDT IDT X = conv_out + IDT IDT hwy_size = X.shape[-1] + IDT IDT with tf.variable_scope('HIGHWAY', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT for i in range(HWY_LAYERS): + IDT IDT IDT IDT with tf.variable_scope('HWY_{}'.format(i)): + IDT IDT IDT IDT IDT wh = , hwy_size], initializer= , trainable= , dtype= , name= ) IDT IDT IDT IDT IDT bh = , initializer= , trainable= , dtype= , name= ) IDT IDT IDT IDT IDT wt = , hwy_size], initializer= , trainable= , dtype= , name= ) IDT IDT IDT IDT IDT T = tf.sigmoid(tf.matmul(X, wt) + HWY_BIAS) + IDT IDT IDT IDT IDT H = tf.nn.relu(tf.matmul(X, wh) + bh) + IDT IDT IDT IDT IDT X = T * H + (1.0 - T) * X + IDT IDT IDT X = , keep_prob= ) IDT IDT IDT X = , conv_out], axis= ) IDT IDT with tf.variable_scope('OUTPUT', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT w = , 128], initializer= , trainable= , dtype= , name= ) IDT IDT IDT b = , initializer= , trainable= , dtype= , name= ) IDT IDT IDT self.logits = , w), b, name= ) IDT IDT IDT self.softmax = , axis= , name= ) IDT IDT IDT self.action = , axis= , name= ) IDT IDT IDT self.probs, self.actions = , k= , sorted= ) IDT IDT with tf.variable_scope('METRICS', reuse= ) IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT self.top_1_acc = , axis= , predictions= , name= ) IDT IDT IDT self.top_2_acc = , tf.argmax(self.action_label, axis= , 2), tf.float32)) ) IDT IDT IDT self.top_3_acc = , tf.argmax(self.action_label, axis= , 3), tf.float32)) ) IDT IDT IDT self.top_5_acc = , tf.argmax(self.action_label, axis= , 5), tf.float32)) ) IDT IDT IDT self.top_10_acc = , tf.argmax(self.action_label, axis= , 10), tf.float32)) ) IDT IDT if self.trainable: + IDT IDT IDT with tf.variable_scope('LOSS', reuse= ) IDT IDT IDT IDT self.scopes.append(scope.name) + IDT IDT IDT IDT self.step = , trainable= ) IDT IDT IDT IDT self.reg_loss = LAMBDA * tf.add_n([tf.nn.l2_loss(v) for v in tf.global_variables() if v.name.__contains__('w_')]) + IDT IDT IDT IDT self.cross_entropy = , logits= , name= ) IDT IDT IDT IDT self.loss1 = , self.cross_entropy, name= ) IDT IDT IDT IDT self.optimizer1 = , name= ) IDT IDT IDT IDT self.grad_update1 = , var_list= , global_step= , name= ) IDT IDT IDT IDT self.neg_grad_log_prob_adv = self.adv * -tf.log(self.softmax) + IDT IDT IDT IDT self.SFGE = , axis= , name= ) + +class A2CLoss: + + IDT def __init__(self, policy_network, value_network): + IDT IDT self.policy = policy_network + IDT IDT self.value = value_network + IDT IDT self.__build_graph() + + IDT def __build_graph(self): + IDT IDT with tf.variable_scope('AC2_LOSS', reuse= ) IDT IDT IDT self.policy.scopes.append(scope.name) + IDT IDT IDT self.value.scopes.append(scope.name) + IDT IDT IDT self.lrate = , dtype= , name= ) IDT IDT IDT self.rewards = , dtype= , name= ) IDT IDT IDT self.baseline = , dtype= , name= ) IDT IDT IDT self.gradlogp = self.adv * tf.log(self.policy.softmax) + IDT IDT IDT self.logprob = , axis= , name= ) IDT IDT IDT self.policy_entropy = , axis= ) IDT IDT IDT self.optimizer = , decay= , epsilon= ) IDT IDT IDT self.policy_update = , self.policy.vars], global_step= ) + +if __name__ == '__main__': + IDT pass +import tensorflow as tf +import dataset +import random +import sys +from tensorflow.python import debug as tf_debug +NUM_CLASSES = len(dataset.CLASSES) +IMAGE_SIZE = 100 + + +def placeholder_inputs(batch_size): + IDT images_placeholder = , shape= , IMAGE_SIZE, IMAGE_SIZE, 3), name= ) IDT labels_placeholder = , shape= ,), name= ) IDT return images_placeholder, labels_placeholder + + +def get_weights(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial, name= ) + +def get_biases(shape): + IDT initial = tf.zeros(shape) + IDT return tf.Variable(initial, name= ) + +def conv_layer(input, filter_size, name, strides= , 1, 1, 1], pool_size= , padding= ) IDT with tf.name_scope(name): + IDT IDT weights = get_weights(filter_size) + IDT IDT biases = get_biases([filter_size[3]]) + IDT IDT conv = tf.nn.leaky_relu(tf.nn.conv2d(input, weights, strides, padding)) + IDT IDT pool = tf.nn.max_pool(conv, [1, pool_size, pool_size, 1], [1, pool_size, pool_size, 1], padding) + IDT IDT return pool + + +def dense_layer(inputs, input_size, output_size, name): + IDT with tf.name_scope(name): + IDT IDT weights = get_weights([input_size, output_size]) + IDT IDT biases = get_biases([output_size]) + IDT IDT h = tf.matmul(inputs, weights) + biases + IDT IDT return tf.nn.leaky_relu(h) + + +def define_model(images): + IDT conv1 = , [20, 20, 3, 10], 'Conv1', pool_size= ) IDT conv2 = conv_layer(conv1, [5, 5, 10, 20], 'Conv2') + IDT conv2 = tf.reshape(conv2, [-1, 13 * 13 * 20]) + IDT dense1 = dense_layer(conv2, 13 * 13 * 20, 1000, 'Dense1') + IDT dense2 = dense_layer(dense1, 1000, 100, 'Dense2') + IDT logits = dense_layer(dense2, 100, NUM_CLASSES, 'Softmax_Linear') + IDT return logits + + +def define_loss(logits, labels): + IDT labels = tf.to_int64(labels) + IDT cross_entropy = , logits= , name= ) IDT loss = , name= ) IDT return loss + + +def training(loss, learning_rate, momentum= , beta1= , beta2= ) IDT tf.summary.scalar('loss', loss) + IDT optimizer = , beta1= , beta2= ) IDT train_op = optimizer.minimize(loss) + IDT return train_op + + +def evaluation(logits, labels): + IDT correct = tf.nn.in_top_k(logits, labels, 1) + IDT return tf.reduce_sum(tf.cast(correct, tf.int32)) + + +def do_evaluation(session, eval_correct, testDataset, batch_size, images_placeholder, labels_placeholder): + IDT training_data, training_labels = testDataset + IDT batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + IDT totalCorrect = 0 + IDT for batch in batches: + IDT IDT image_data = batch[0] + IDT IDT label_data = batch[1] + IDT IDT feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + IDT IDT totalCorrect += , feed_dict= ) IDT precision = float(totalCorrect) / len(training_data) + IDT , totalCorrect, precision)) + IDT sys.stdout.flush() + IDT return precision > 0.95 + + +def run_training(batch_size, learning_rate, epochs, run_number): + IDT with tf.Graph().as_default(): + IDT IDT images_placeholder, labels_placeholder = placeholder_inputs(batch_size) + IDT IDT logits = define_model(images_placeholder) + IDT IDT lossFunction = define_loss(logits, labels_placeholder) + IDT IDT train_op = training(lossFunction, learning_rate) + IDT IDT eval_correct = evaluation(logits, labels_placeholder) + IDT IDT saver = tf.train.Saver() + IDT IDT init = tf.global_variables_initializer() + IDT IDT with tf.Session() as session: + IDT IDT IDT logdir = 'log/' + str(run_number) + IDT IDT IDT session.run(init) + IDT IDT IDT for step in range(epochs): + IDT IDT IDT IDT training_data, training_labels = dataset.get_training_data() + IDT IDT IDT IDT batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + IDT IDT IDT IDT epochLoss = 0 + IDT IDT IDT IDT for batch in batches: + IDT IDT IDT IDT IDT image_data = batch[0] + IDT IDT IDT IDT IDT label_data = batch[1] + IDT IDT IDT IDT IDT feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + IDT IDT IDT IDT IDT activations, loss_value = , lossFunction], feed_dict= ) IDT IDT IDT IDT IDT epochLoss += loss_value + IDT IDT IDT IDT if step % 2 == 0: + IDT IDT IDT IDT IDT ) + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT early_stop = False + IDT IDT IDT IDT if (step + 1) % 5 == 0 or step + 1 == epochs: + IDT IDT IDT IDT IDT validation_data = dataset.get_validation_data(batch_size) + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT IDT early_stop = do_evaluation(session, eval_correct, validation_data, batch_size, images_placeholder, labels_placeholder) + IDT IDT IDT IDT if step + 1 == epochs or early_stop: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT IDT do_evaluation(session, eval_correct, (training_data, training_labels), batch_size, images_placeholder, labels_placeholder) + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT IDT test_data = dataset.get_test_data(batch_size) + IDT IDT IDT IDT IDT do_evaluation(session, eval_correct, test_data, batch_size, images_placeholder, labels_placeholder) + IDT IDT IDT IDT IDT saver.save(session, 'model.ckpt') + IDT IDT IDT IDT IDT if early_stop: + IDT IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT IDT return + + +run_training(89, 0.001, 40, 13) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +from pretrained.vgg16 import vgg16 +import time +import numpy as np +from pretrained.imagenet_classes import class_names + + +class ImageCaptioner(object): + + IDT def __init__(self, config, word_table): + IDT IDT self.config = config + IDT IDT self.word_table = word_table + IDT IDT self.training_flag = True + IDT IDT self.session = tf.Session() + IDT IDT self.imgs_placeholder = tf.placeholder(tf.float32, [None, 224, 224, 3]) + IDT IDT self.build_cnn() + IDT IDT self.build_rnn() + IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT self.saver = ) IDT IDT checkpoint = tf.train.get_checkpoint_state(config.ckpt_dir) + IDT IDT if checkpoint and checkpoint.model_checkpoint_path: + IDT IDT IDT self.saver.restore(self.session, checkpoint.model_checkpoint_path) + IDT IDT IDT + IDT IDT self.train_writer = tf.summary.FileWriter(config.summary_file) + IDT IDT if config.cnn_model_file: + IDT IDT IDT self.cnn.load_weights(config.cnn_model_file, self.session) + + IDT def build_cnn(self): + IDT IDT + IDT IDT if self.config.cnn_model == 'custom': + IDT IDT IDT self.build_custom_cnn() + IDT IDT else: + IDT IDT IDT self.build_vgg16() + + IDT def build_custom_cnn(self): + IDT IDT + IDT IDT W_conv1 = _weight_variable([5, 5, 1, 32]) + IDT IDT b_conv1 = _bias_variable([32]) + IDT IDT h_conv1 = tf.nn.relu(_conv2d(imgs_placeholder, W_conv1) + b_conv1) + IDT IDT h_pool1 = _max_pool_2x2(h_conv1) + IDT IDT W_conv2 = _weight_variable([5, 5, 32, 64]) + IDT IDT b_conv2 = _bias_variable([64]) + IDT IDT h_conv2 = tf.nn.relu(_conv2d(h_pool1, W_conv2) + b_conv2) + IDT IDT h_pool2 = _max_pool_2x2(h_conv2) + IDT IDT W_conv3 = _weight_variable([5, 5, 64, 128]) + IDT IDT b_conv3 = _bias_variable([128]) + IDT IDT h_conv3 = tf.nn.relu(_conv2d(h_pool2, W_conv3) + b_conv3) + IDT IDT h_pool3 = _max_pool_2x2(h_conv3) + IDT IDT h_flat3 = tf.reshape(h_pool3, [-1]) + IDT IDT self.cnn_output = h_flat3 + + IDT def build_vgg16(self): + IDT IDT + IDT IDT self.cnn = , sess= , trainable= ) IDT IDT self.cnn_output = self.cnn.fc2 + IDT IDT self.img_dim = 4096 + + IDT def build_rnn(self): + IDT IDT + IDT IDT batch_size = self.config.batch_size + IDT IDT hidden_size = self.config.hidden_size + IDT IDT vector_dim = self.config.vector_dim + IDT IDT learning_rate = self.config.learning_rate + IDT IDT num_words = self.word_table.num_words + IDT IDT max_num_words = self.config.max_word_len + IDT IDT vector_dim = self.config.vector_dim + IDT IDT self.rnn_input = tf.placeholder(tf.float32, [None, self.img_dim]) + IDT IDT self.sentences = tf.placeholder(tf.int32, [None, max_num_words]) + IDT IDT self.mask = tf.placeholder(tf.float32, [None, max_num_words]) + IDT IDT gen_captions = [] + IDT IDT W_conv2rnn = _weight_variable([self.img_dim, vector_dim]) + IDT IDT b_conv2rnn = _bias_variable([vector_dim]) + IDT IDT fc_conv2rnn = tf.nn.xw_plus_b(self.rnn_input, W_conv2rnn, b_conv2rnn) + IDT IDT lstm_cell = tf.contrib.rnn.BasicLSTMCell(hidden_size) + IDT IDT state = , dtype= ) IDT IDT self.idx2vec = , dtype= ) IDT IDT W_word = tf.Variable(tf.random_uniform([hidden_size, num_words])) + IDT IDT b_word = tf.Variable(tf.zeros([num_words])) + IDT IDT total_loss = 0.0 + IDT IDT for idx in range(max_num_words): + IDT IDT IDT if idx == 0: + IDT IDT IDT IDT curr_emb = fc_conv2rnn + IDT IDT IDT elif self.training_flag: + IDT IDT IDT IDT curr_emb = tf.nn.embedding_lookup(self.idx2vec, self.sentences[:, (idx - 1)]) + IDT IDT IDT else: + IDT IDT IDT IDT curr_emb = tf.nn.embedding_lookup(self.idx2vec, max_prob_word) + IDT IDT IDT if self.config.num_lstm == 1: + IDT IDT IDT IDT output, state = lstm_cell(curr_emb, state) + IDT IDT IDT else: + IDT IDT IDT IDT output, state = , inputs= , dtype= ) IDT IDT IDT logits = tf.matmul(output, W_word) + b_word + IDT IDT IDT max_prob_word = tf.argmax(logits, 1) + IDT IDT IDT gen_captions.append(max_prob_word) + IDT IDT IDT onehot_labels = , (idx)], dtype= ) IDT IDT IDT logits = , dtype= ) IDT IDT IDT cross_entropy = , labels= , (idx) ) IDT IDT IDT loss = tf.reduce_sum(cross_entropy) + IDT IDT IDT total_loss = total_loss + loss + IDT IDT self.gen_captions = , axis= ) IDT IDT self.total_loss = total_loss / tf.reduce_sum(self.mask) + IDT IDT tf.summary.scalar('total_loss', self.total_loss) + IDT IDT self.train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + IDT IDT self.merged = tf.summary.merge_all() + + IDT def train(self, data): + IDT IDT + IDT IDT start_time = time.time() + IDT IDT self.training_flag = True + IDT IDT word2idx = self.word_table.word2idx + IDT IDT idx2word = self.word_table.idx2word + IDT IDT train_images = data.training_data + IDT IDT train_caps = data.training_annotation + IDT IDT max_word_len = self.config.max_word_len + IDT IDT batch_size = self.config.batch_size + IDT IDT num_epochs = self.config.num_epochs + IDT IDT display_loss = self.config.display_loss + IDT IDT train_idx = np.arange(len(train_caps)) + IDT IDT for epoch in range(1, num_epochs + 1): + IDT IDT IDT + IDT IDT IDT shuffled_train_images = [] + IDT IDT IDT shuffled_train_caps = [] + IDT IDT IDT np.random.shuffle(train_idx) + IDT IDT IDT for old_idx in train_idx: + IDT IDT IDT IDT shuffled_train_images.append(train_images[old_idx]) + IDT IDT IDT IDT shuffled_train_caps.append(train_caps[old_idx]) + IDT IDT IDT for batch_idx in range(0, len(train_caps), batch_size): + IDT IDT IDT IDT if batch_idx + batch_size > len(train_caps): + IDT IDT IDT IDT IDT continue + IDT IDT IDT IDT curr_images = shuffled_train_images[batch_idx:batch_idx + batch_size] + IDT IDT IDT IDT curr_caps = shuffled_train_caps[batch_idx:batch_idx + batch_size] + IDT IDT IDT IDT curr_sentences = np.zeros((batch_size, max_word_len)) + IDT IDT IDT IDT curr_mask = np.zeros((batch_size, max_word_len)) + IDT IDT IDT IDT for cap_idx, cap in enumerate(curr_caps): + IDT IDT IDT IDT IDT for word_idx, word in enumerate(cap.lower().split(' ')): + IDT IDT IDT IDT IDT IDT if word_idx == max_word_len: + IDT IDT IDT IDT IDT IDT IDT break + IDT IDT IDT IDT IDT IDT if word in word2idx and word in self.word_table.word2vec: + IDT IDT IDT IDT IDT IDT IDT curr_sentences[cap_idx][word_idx] = word2idx[word] + IDT IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT IDT curr_sentences[cap_idx][word_idx] = word2idx[''] + IDT IDT IDT IDT IDT IDT curr_mask[cap_idx][word_idx] = 1 + IDT IDT IDT IDT if self.config.train_cnn: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT cnn_output = , feed_dict= ) IDT IDT IDT IDT IDT _, summary, total_loss = , self.merged, self.total_loss], feed_dict= , self.sentences: curr_sentences, self.mask: curr_mask}) ) IDT IDT IDT if epoch % display_loss == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT self.train_writer.add_summary(summary, epoch) + IDT IDT IDT if epoch % self.config.ckpt_freq == 0: + IDT IDT IDT IDT + IDT IDT IDT IDT self.saver.save(self.session, self.config.ckpt_dir + 'Captioner', global_step= ) IDT IDT + IDT IDT - start_time)) + + IDT def elapsed(self, sec): + IDT IDT if sec < 60: + IDT IDT IDT return str(sec) + ' sec' + IDT IDT elif sec < 60 * 60: + IDT IDT IDT return str(sec / 60) + ' min' + IDT IDT else: + IDT IDT IDT return str(sec / (60 * 60)) + ' hr' + + IDT def test(self, data): + IDT IDT + IDT IDT + IDT IDT self.training_flag = False + IDT IDT results_file = self.config.results_file + IDT IDT max_num_words = self.config.max_word_len + IDT IDT test_images = data.training_data + IDT IDT test_caps = data.training_annotation + IDT IDT max_word_len = self.config.max_word_len + IDT IDT captions = [] + IDT IDT empty_sentences = np.zeros((len(test_images), max_num_words)) + IDT IDT empty_mask = np.ones((len(test_images), max_num_words)) + IDT IDT if self.config.train_cnn: + IDT IDT IDT + IDT IDT else: + IDT IDT IDT cnn_output = , feed_dict= ) IDT IDT IDT + IDT IDT IDT + IDT IDT IDT captions_idx = , feed_dict= , self.sentences: empty_sentences, self.mask: empty_mask}) ) IDT IDT captions = [] + IDT IDT for x in range(len(captions_idx)): + IDT IDT IDT captions.append([]) + IDT IDT IDT for y in range(len(captions_idx[0])): + IDT IDT IDT IDT captions[x].append(self.word_table.idx2word[captions_idx[x][y]]) + IDT IDT output_text = '' + IDT IDT for cap_idx in range(len(captions)): + IDT IDT IDT if '.' in captions[cap_idx]: + IDT IDT IDT IDT end_sentence = ) IDT IDT IDT else: + IDT IDT IDT IDT end_sentence = max_num_words - 1 + IDT IDT IDT sentence = ' '.join(captions[cap_idx][:end_sentence + 1]) + IDT IDT IDT output_text += sentence + '\n' + IDT IDT f = open(results_file, 'w') + IDT IDT f.write(output_text) + IDT IDT f.close() + + +def _conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def _weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def _bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def _max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from model import Model +import tensorflow as tf + + +class ModelStandard(Model): + + IDT def __init__(self, activation_func): + IDT IDT super(ModelStandard, self).__init__('standard', activation_func) + IDT IDT with tf.device('/gpu:0'): + IDT IDT IDT self.input = , shape= , 32, 32, 3]) ) IDT IDT IDT self.labels = , shape= , 10]) ) IDT IDT IDT self.dropout = tf.placeholder(tf.float32) + IDT IDT IDT W_conv1 = self.__weights([5, 5, 3, 32]) + IDT IDT IDT b_conv1 = self.__biases([32]) + IDT IDT IDT h_conv1 = , W_conv1, strides= , 1, 1, 1], padding= ) IDT IDT IDT h_pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT W_conv2 = self.__weights([5, 5, 32, 32]) + IDT IDT IDT b_conv2 = self.__biases([32]) + IDT IDT IDT h_conv2 = , W_conv2, strides= , 1, 1, 1], padding= ) IDT IDT IDT h_pool2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT W_conv3 = self.__weights([5, 5, 32, 64]) + IDT IDT IDT b_conv3 = self.__biases([64]) + IDT IDT IDT h_conv3 = , W_conv3, strides= , 1, 1, 1], padding= ) IDT IDT IDT h_pool3 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT W_fc1 = self.__weights([4 * 4 * 64, 1024]) + IDT IDT IDT b_fc1 = self.__biases([1024]) + IDT IDT IDT h_pool3_flat = tf.reshape(h_pool3, [-1, 4 * 4 * 64]) + IDT IDT IDT h_fc1 = self.act_fn(tf.nn.xw_plus_b(h_pool3_flat, W_fc1, b_fc1)) + IDT IDT IDT W_fc2 = self.__weights([1024, 512]) + IDT IDT IDT b_fc2 = self.__biases([512]) + IDT IDT IDT h_fc2 = self.act_fn(tf.nn.xw_plus_b(h_fc1, W_fc2, b_fc2)) + IDT IDT IDT h_fc2_dropout = tf.nn.dropout(h_fc2, self.dropout) + IDT IDT IDT W_fc3 = self.__weights([512, 10]) + IDT IDT IDT b_fc3 = self.__biases([10]) + IDT IDT IDT self.raw_scores = tf.nn.xw_plus_b(h_fc2_dropout, W_fc3, b_fc3) + IDT IDT IDT self.probabilities = tf.nn.softmax(self.raw_scores) + IDT IDT IDT self.prediction = tf.argmax(self.probabilities, 1) + IDT IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.raw_scores, self.labels)) + IDT IDT IDT self.train = tf.train.AdamOptimizer(0.0001).minimize(loss) + IDT IDT IDT self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.prediction, tf.argmax(self.labels, 1)), tf.float32)) + + IDT def train_model(self, session, images, labels): + IDT IDT session.run(self.train, feed_dict= , self.labels: labels, self.dropout: 0.5}) ) + IDT def predict(self, session, images): + IDT IDT return session.run(self.prediction, feed_dict= , self.dropout: 1.0}) ) + IDT def get_accuracy(self, session, images, labels): + IDT IDT return session.run(self.accuracy, feed_dict= , self.labels: labels, self.dropout: 1.0}) ) + IDT def __weights(self, shape): + IDT IDT return tf.Variable(tf.truncated_normal(shape, stddev= ) + IDT def __biases(self, shape): + IDT IDT return tf.Variable(tf.constant(0.1, shape= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() + + +import tensorflow as tf +import math +from tensorflow.examples.tutorials.mnist import input_data +IMAGE_PIXEL_COUNT = 784 +layer_sizes = [6, 12, 24, 200, 10] +weights = , 6, 1, layer_sizes[0]], stddev= , tf.Variable(tf.truncated_normal([5, 5, layer_sizes[0], layer_sizes[1]], stddev= , tf.Variable(tf.truncated_normal([4, 4, layer_sizes[1], layer_sizes[2]], stddev= , tf.Variable(tf.truncated_normal([7 * 7 * layer_sizes[2], layer_sizes[3]], stddev= ) IDT layer_sizes[4]], stddev=0.1))] +biases = [tf.Variable(tf.ones([layer_sizes[0]]) / 10), tf.Variable(tf.ones([layer_sizes[1]]) / 10), tf.Variable(tf.ones([layer_sizes[2]]) / 10), tf.Variable(tf.ones([layer_sizes[3]]) / 10), tf.Variable(tf.ones([layer_sizes[4]]) / 10)] +learning_rate_placeholder = tf.placeholder(tf.float32) +keep_probability = tf.placeholder(tf.float32) +TRAINING_KEEP_PROBABILITY = 0.75 +TEST_KEEP_PROBABILITY = 1.0 +mnist_data = , reshape= , one_hot= )images = tf.placeholder(tf.float32, [None, 28, 28, 1]) +layer1 = , weights[0], strides= , 1, 1, 1], padding= )layer2 = , weights[1], strides= , 2, 2, 1], padding= )layer3 = , weights[2], strides= , 2, 2, 1], padding= )reshaped_layer = , shape= , 7 * 7 * layer_sizes[2]]) )layer4 = tf.nn.relu(tf.matmul(reshaped_layer, weights[3]) + biases[3]) +layer4_dropout = tf.nn.dropout(layer4, keep_probability) +logits = tf.matmul(layer4_dropout, weights[4]) + biases[4] +layer5 = tf.nn.softmax(logits) +model_answers = tf.placeholder(tf.float32, [None, 10]) +cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, model_answers) +cross_entropy = tf.reduce_mean(cross_entropy) * 100 +is_correct = tf.equal(tf.argmax(layer5, 1), tf.argmax(model_answers, 1)) +accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) +optimizer = tf.train.AdamOptimizer(learning_rate_placeholder) +training_step = optimizer.minimize(cross_entropy) +init = tf.global_variables_initializer() +session = tf.Session() +session.run(init) +training_results = [] +test_results = [] + + +def run_training_step(results, lr): + IDT curr_images, currmodel_answers = mnist_data.train.next_batch(100) + IDT training_data = {images: curr_images, model_answers: currmodel_answers, learning_rate_placeholder: lr, keep_probability: TRAINING_KEEP_PROBABILITY} + IDT session.run(training_step, feed_dict= ) IDT results.append(session.run([accuracy, cross_entropy], feed_dict= ) + +def run_test_step(results, lr): + IDT + IDT results.append(session.run([accuracy, cross_entropy], feed_dict= , model_answers: mnist_data.test.labels, learning_rate_placeholder: lr, keep_probability: TEST_KEEP_PROBABILITY})) ) + +for i in range(10000): + IDT min_learning_rate = 0.0001 + IDT max_learning_rate = 0.003 + IDT decay_speed = 2000.0 + IDT lr = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i / decay_speed) + IDT run_training_step(training_results, lr) + IDT if i % 100 == 0: + IDT IDT run_test_step(test_results, lr) +run_test_step(test_results, lr) +for i, step in enumerate(training_results): + IDT + ': ' + str(step)) +for i, step in enumerate(test_results): + IDT + ': ' + str(step)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Sun Aug 7 15:36:12 2016 + +@author: priyankadwivedi +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +os.chdir('/Users/priyankadwivedi') +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +import matplotlib +import matplotlib.pyplot as plt +n = 1000 +image_array = train_dataset[n] +image_array.shape +plt.imshow(image_array, cmap= , interpolation= ) + +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +""" + + + +batch_size = 128 +num_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = tf.placeholder(tf.float32, + IDT IDT IDT IDT IDT IDT IDT IDT IDT shape=(batch_size, image_size * image_size)) + tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + + weights_1 = tf.Variable( + IDT IDT tf.truncated_normal([image_size * image_size, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable( + IDT IDT tf.truncated_normal([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + logits = tf.matmul(hidden1, weights_2) + biases_2 + loss = tf.reduce_mean( + IDT tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + + regularizers = (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(biases_1) + + IDT IDT IDT IDT tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(biases_2)) + + factor = 5e-4 + loss += factor * regularizers + IDT + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax( + IDT tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax( + IDT tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) + +num_steps = 5001 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + IDT + IDT + IDT offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + IDT + IDT batch_data = train_dataset[offset:(offset + batch_size), :] + IDT batch_labels = train_labels[offset:(offset + batch_size), :] + IDT + IDT + IDT + IDT feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + IDT _, l, predictions = session.run( + IDT [optimizer, loss, train_prediction], feed_dict=feed_dict) + IDT if (step % 500 = ) IDT ) + IDT ) + IDT print("Validation accuracy: %.1f%%" % accuracy( + IDT IDT valid_prediction.eval(), valid_labels)) + , test_labels)) + + + + + + +batch_size = 20 +num_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = tf.placeholder(tf.float32, + IDT IDT IDT IDT IDT IDT IDT IDT IDT shape=(batch_size, image_size * image_size)) + tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + + weights_1 = tf.Variable( + IDT IDT tf.truncated_normal([image_size * image_size, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable( + IDT IDT tf.truncated_normal([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + logits = tf.matmul(hidden1, weights_2) + biases_2 + loss = tf.reduce_mean( + IDT tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + + regularizers = (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(biases_1) + + IDT IDT IDT IDT tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(biases_2)) + + factor = 5e-4 + loss += factor * regularizers + IDT + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax( + IDT tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax( + IDT tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) + +num_steps = 3001 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + IDT + IDT + IDT offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + IDT + IDT batch_data = train_dataset[offset:(offset + batch_size), :] + IDT batch_labels = train_labels[offset:(offset + batch_size), :] + IDT + IDT + IDT + IDT feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + IDT _, l, predictions = session.run( + IDT [optimizer, loss, train_prediction], feed_dict=feed_dict) + IDT if (step % 500 = ) IDT ) + IDT ) + IDT print("Validation accuracy: %.1f%%" % accuracy( + IDT IDT valid_prediction.eval(), valid_labels)) + , test_labels)) + + + + +""" +""" + + + + +batch_size = 256 +num_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = tf.placeholder(tf.float32, + IDT IDT IDT IDT IDT IDT IDT IDT IDT shape=(batch_size, image_size * image_size)) + tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + + weights_1 = tf.Variable( + IDT IDT tf.truncated_normal([image_size * image_size, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable( + IDT IDT tf.truncated_normal([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + + + + + + keep_prob = tf.placeholder(tf.float32) + keep_prob = 0.50 + hidden_layer_drop = tf.nn.dropout(hidden1, keep_prob) + + + logits = tf.matmul(hidden_layer_drop, weights_2) + biases_2 + loss = tf.reduce_mean( + IDT tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + + regularizers = (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(biases_1) + + IDT IDT IDT IDT tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(biases_2)) + + factor = 5e-4 + loss += factor * regularizers + + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax( + IDT tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax( + IDT tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) + +num_steps = 5001 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + IDT + IDT + IDT offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + IDT + IDT batch_data = train_dataset[offset:(offset + batch_size), :] + IDT batch_labels = train_labels[offset:(offset + batch_size), :] + IDT + IDT + IDT + IDT feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + IDT _, l, predictions = session.run( + IDT [optimizer, loss, train_prediction], feed_dict=feed_dict) + IDT if (step % 500 = ) IDT ) + IDT ) + IDT print("Validation accuracy: %.1f%%" % accuracy( + IDT IDT valid_prediction.eval(), valid_labels)) + , test_labels)) + + + + + + + + + + + + + + +""" +""" + + +batch_size = 128 +n_hidden_1 = 1024 +n_hidden_2 = 1024 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = tf.placeholder(tf.float32, + IDT IDT IDT IDT IDT IDT IDT IDT IDT shape=(batch_size, image_size * image_size)) + tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + + def multilayer_perceptron(x, weights, biases): + IDT + IDT layer_1 = tf.matmul(x, weights['h1'])+ biases['b1'] + IDT layer_1 = tf.nn.sigmoid(layer_1) + IDT + IDT + IDT layer_2 = tf.matmul(layer_1, weights['h2'])+ biases['b2'] + IDT layer_2 = tf.nn.sigmoid(layer_2) + IDT n + IDT + IDT out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + IDT return out_layer + IDT + + + weights = { + IDT 'h1': tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_1])), + IDT 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])), + IDT 'out': tf.Variable(tf.truncated_normal([n_hidden_2, num_labels])) + IDT } + biases = { + IDT 'b1': tf.Variable(tf.truncated_normal([n_hidden_1])), + IDT 'b2': tf.Variable(tf.truncated_normal([n_hidden_2])), + IDT 'out': tf.Variable(tf.truncated_normal([num_labels])) + IDT } + + + logits = multilayer_perceptron(tf_train_dataset, weights, biases) + + + loss = tf.reduce_mean( + IDT tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + + regularizers = (tf.nn.l2_loss(weights['h1']) + tf.nn.l2_loss(biases['b1']) + + IDT IDT IDT IDT tf.nn.l2_loss(weights['h2']) + tf.nn.l2_loss(biases['b2']) + + IDT IDT IDT IDT tf.nn.l2_loss(weights['out']) + tf.nn.l2_loss(biases['out'])) + IDT IDT IDT IDT + + factor = 5e-4 + loss += factor * regularizers + + + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(multilayer_perceptron(tf_valid_dataset, weights, biases)) + test_prediction = tf.nn.softmax(multilayer_perceptron(tf_test_dataset, weights, biases)) + +num_steps = 5001 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + IDT + IDT + IDT offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + IDT + IDT batch_data = train_dataset[offset:(offset + batch_size), :] + IDT batch_labels = train_labels[offset:(offset + batch_size), :] + IDT + IDT + IDT + IDT feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + IDT _, l, predictions = session.run( + IDT [optimizer, loss, train_prediction], feed_dict=feed_dict) + IDT if (step % 500 = ) IDT ) + IDT ) + IDT print("Validation accuracy: %.1f%%" % accuracy( + IDT IDT valid_prediction.eval(), valid_labels)) + , test_labels)) + + + + + + + + + + + + + + + + +""" +""" + + +batch_size = 128 +n_hidden_1 = 512 +n_hidden_2 = 512 + +graph = tf.Graph() +with graph.as_default(): + + + + tf_train_dataset = tf.placeholder(tf.float32, + IDT IDT IDT IDT IDT IDT IDT IDT IDT shape=(batch_size, image_size * image_size)) + tf_train_labels = , shape= , num_labels)) ) tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + + def multilayer_perceptron(x, weights, biases): + IDT + IDT layer_1 = tf.matmul(x, weights['h1'])+ biases['b1'] + IDT layer_1 = tf.nn.relu(layer_1) + IDT + IDT + IDT layer_2 = tf.matmul(layer_1, weights['h2'])+ biases['b2'] + IDT layer_2 = tf.nn.sigmoid(layer_2) + IDT + IDT + IDT out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + IDT return out_layer + IDT + + + weights = { + IDT 'h1': tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_1])), + IDT 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])), + IDT 'out': tf.Variable(tf.truncated_normal([n_hidden_2, num_labels])) + IDT } + biases = { + IDT 'b1': tf.Variable(tf.truncated_normal([n_hidden_1])), + IDT 'b2': tf.Variable(tf.truncated_normal([n_hidden_2])), + IDT 'out': tf.Variable(tf.truncated_normal([num_labels])) + IDT } + + + logits = multilayer_perceptron(tf_train_dataset, weights, biases) + + + loss = tf.reduce_mean( + IDT tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + + regularizers = (tf.nn.l2_loss(weights['h1']) + tf.nn.l2_loss(biases['b1']) + + IDT IDT IDT IDT tf.nn.l2_loss(weights['h2']) + tf.nn.l2_loss(biases['b2']) + + IDT IDT IDT IDT tf.nn.l2_loss(weights['out']) + tf.nn.l2_loss(biases['out'])) + IDT IDT IDT IDT + + factor = 5e-4 + loss += factor * regularizers + + + global_step = , trainable= ) starter_learning_rate = 0.5 + learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, + IDT IDT IDT IDT IDT IDT IDT IDT IDT IDT 1000, 0.90, staircase=True) + + optimizer = , global_step= ) + + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(multilayer_perceptron(tf_valid_dataset, weights, biases)) + test_prediction = tf.nn.softmax(multilayer_perceptron(tf_test_dataset, weights, biases)) + +num_steps = 5001 + +with tf.Session(graph= ) tf.initialize_all_variables().run() + + for step in range(num_steps): + IDT + IDT + IDT offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + IDT + IDT batch_data = train_dataset[offset:(offset + batch_size), :] + IDT batch_labels = train_labels[offset:(offset + batch_size), :] + IDT + IDT + IDT + IDT feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + IDT _, l, predictions = session.run( + IDT [optimizer, loss, train_prediction], feed_dict=feed_dict) + IDT if (step % 500 = ) IDT ) + IDT ) + IDT print("Validation accuracy: %.1f%%" % accuracy( + IDT IDT valid_prediction.eval(), valid_labels)) + , test_labels)) + + + + + + + + + + + + + + + + + + +""" +batch_size = 128 +n_hidden_1 = 512 +n_hidden_2 = 512 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + + IDT def multilayer_perceptron(x, weights, biases): + IDT IDT layer_1 = tf.matmul(x, weights['h1']) + biases['b1'] + IDT IDT layer_1 = tf.nn.relu(layer_1) + IDT IDT layer_2 = tf.matmul(layer_1, weights['h2']) + biases['b2'] + IDT IDT layer_2 = tf.nn.sigmoid(layer_2) + IDT IDT out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + IDT IDT return out_layer + IDT weights = {'h1': tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_1])), 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])), 'out': tf.Variable(tf.truncated_normal([n_hidden_2, num_labels]))} + IDT biases = {'b1': tf.Variable(tf.truncated_normal([n_hidden_1])), 'b2': tf.Variable(tf.truncated_normal([n_hidden_2])), 'out': tf.Variable(tf.truncated_normal([num_labels]))} + IDT logits = multilayer_perceptron(tf_train_dataset, weights, biases) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT regularizers = tf.nn.l2_loss(weights['h1']) + tf.nn.l2_loss(biases['b1']) + tf.nn.l2_loss(weights['h2']) + tf.nn.l2_loss(biases['b2']) + tf.nn.l2_loss(weights['out']) + tf.nn.l2_loss(biases['out']) + IDT factor = 0.0005 + IDT loss += factor * regularizers + IDT global_step = , trainable= ) IDT starter_learning_rate = 0.5 + IDT learning_rate = , global_step, 3500, 0.86, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(multilayer_perceptron(tf_valid_dataset, weights, biases)) + IDT test_prediction = tf.nn.softmax(multilayer_perceptron(tf_test_dataset, weights, biases)) +num_steps = 20001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 1000 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import tensorflow as tf +import numpy as np +from scipy.stats import norm + + +def fnn(x, output_dim): + IDT w1 = , 20], stddev= , name= ) IDT b1 = , name= ) IDT w2 = , output_dim], stddev= , name= ) IDT b2 = , name= ) IDT y1 = tf.nn.relu(tf.matmul(x, w1) + b1) + IDT y2 = tf.nn.sigmoid(tf.matmul(y1, w2) + b2) + IDT return y2, [w1, w2] + + +x1 = , shape= , 10)) )y1, w1 = fnn(x1, 1) +x2 = , shape= , 10)) )y2, w2 = fnn(x2, 1) +with tf.Session() as sess: + IDT tf.initialize_all_variables().run() + IDT theta1, theta2 = sess.run([w1, w2], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])}) + IDT + IDT + + +def linear(x, out_dim, name, activation_fn= ) IDT with tf.variable_scope(name): + IDT IDT w = , shape= , out_dim], dtype= , initializer= ) IDT IDT b = , shape= , dtype= , initializer= ) IDT IDT out = tf.matmul(x, w) + b + IDT IDT if activation_fn != None: + IDT IDT IDT out = activation_fn(out) + IDT return out, [w, b] + + +with tf.variable_scope('ffn') as scope: + IDT x1 = , shape= , 10)) ) IDT y11, theta11 = , 10, name= , activation_fn= ) IDT y12, theta12 = , 1, name= , activation_fn= ) IDT scope.reuse_variables() + IDT x2 = , shape= , 10)) ) IDT y21, theta21 = , 10, name= , activation_fn= ) IDT y22, theta22 = , 1, name= , activation_fn= )with tf.Session() as sess: + IDT tf.initialize_all_variables().run() + IDT theta1, theta2 = sess.run([theta12, theta22], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])}) + IDT + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import Common +from DataPreprocessing import resize +from DataPreprocessing import labelsToOutputs +from DataPreprocessing import createMapping +import Helper as help +import tensorflow as tf +import CNN +beta = 0.01 +epochs = 100 +batchSize = 512 +learningRate = 0.001 +testSamplesNumber = 50 +classes = range(10, 30) +samplesNumber = 200 +inputImgSize = 16, 16 +filtersShape = [(3, 3, 3, 32), (3, 3, 32, 64)] +biases = , shape= , initializer= , 'bc2': tf.get_variable('B1', shape= , initializer= , 'bd1': tf.get_variable('B2', shape= , initializer= , 'out': tf.get_variable('B3', shape= , initializer= )kwargs = {'inputImgSize': inputImgSize, 'outputsNumber': len(classes), 'convLayersNumber': 2, 'filtersShape': filtersShape, 'bias': biases} +images, labels = Common.readTrafficSigns('../Data/GTSRB_Final_Training_Images/GTSRB/Final_Training/Images', classes, samplesNumber) +images = resize(images, inputImgSize) +outputs = labelsToOutputs(classes, labels) +images, outputs = Common.shuffle(images, outputs) +testImages, testLabels = Common.readTestImages('../Data/GTSRB_Final_Test_Images/GTSRB/Final_Test/Images', classes, testSamplesNumber) +testImages = resize(testImages, inputImgSize) +testOutputs = labelsToOutputs(classes, testLabels) +if len(testImages) != ) IDT raise Exception +if not all([any(x) for x in testOutputs]): + IDT raise ValueError('Not all outputs have value') +classesMapping = createMapping(classes) +cNN = CNN.CNN(**kwargs) +batchesX = [images[batch:batch + batchSize] for batch in range(0, len(images) - batchSize, batchSize)] +batchesY = [outputs[batch:batch + batchSize] for batch in range(0, len(outputs) - batchSize, batchSize)] +if len(batchesX) != ) IDT raise Exception +pred = cNN.conv_net(cNN.xPlaceholder, cNN.keep_prob) +loss = , labels= )classified_indexes = tf.argmax(pred, 1) +expected_indexes = tf.argmax(cNN.yPlaceholder, 1) +expectedAndClassified = , classified_indexes], axis= )correct_prediction = tf.equal(expected_indexes, classified_indexes) +regularizer = tf.nn.l2_loss(cNN.WEIGHTS['wc1']) + tf.nn.l2_loss(cNN.WEIGHTS['wc2']) + tf.nn.l2_loss(cNN.WEIGHTS['wd1']) + tf.nn.l2_loss(cNN.WEIGHTS['out']) +loss = tf.reduce_mean(loss + beta * regularizer) +optimizer = )accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +init = tf.global_variables_initializer() +with tf.Session() as session: + IDT session.run(init) + IDT train_loss_global = [] + IDT train_accuracy_global = [] + IDT test_loss = [] + IDT test_accuracy = [] + IDT for i in range(epochs): + IDT IDT train_loss = [] + IDT IDT train_accuracy = [] + IDT IDT for k in range(len(batchesX)): + IDT IDT IDT trainBatchX = batchesX[k] + IDT IDT IDT trainBatchY = batchesY[k] + IDT IDT IDT op = cNN.run(session, optimizer, trainBatchX, trainBatchY, 0.5) + IDT IDT IDT iteration_loss, acc = cNN.run(session, [loss, accuracy], trainBatchX, trainBatchY, 1.0) + IDT IDT IDT train_accuracy.append(acc) + IDT IDT IDT train_loss.append(iteration_loss) + IDT IDT avg_train_loss = sum(train_loss) / len(train_loss) + IDT IDT avg_train_acc = sum(train_accuracy) / len(train_accuracy) + IDT IDT + ', Loss= , Training Accuracy= ) IDT IDT valid_loss, test_acc, expToClass = cNN.run(session, [loss, accuracy, expectedAndClassified], testImages, testOutputs, 1.0) + IDT IDT ) + IDT IDT ) + IDT IDT train_accuracy_global.append(avg_train_acc * 100.0) + IDT IDT train_loss_global.append(avg_train_loss) + IDT IDT test_accuracy.append(test_acc * 100.0) + IDT IDT test_loss.append(valid_loss) +Common.displayClassificationPlot(help.createClassifiedAsList(expToClass), classes) +Common.displayResults(epochs, train_accuracy_global, train_loss_global, test_accuracy, test_loss) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Sun Aug 12 15:18:20 2018 + +@author: Arc +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +beta = 0.008 +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT l2_loss = tf.nn.l2_loss(weights) + IDT loss = loss + beta * l2_loss + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +beta = 0.018 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_nodes = 1024 + IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + IDT hidden_layer = tf.nn.relu(hidden_layer) + IDT logits = tf.matmul(hidden_layer, weights) + biases + IDT loss = , logits= ) IDT l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + IDT loss = loss + beta * l2_loss + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + IDT test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +beta = 0.018 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_nodes = 1024 + IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + IDT hidden_layer = tf.nn.relu(hidden_layer) + IDT logits = tf.matmul(hidden_layer, weights) + biases + IDT loss = , logits= ) IDT l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + IDT loss = loss + beta * l2_loss + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + IDT test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset_limit = 300 + IDT IDT offset = step * batch_size % offset_limit + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +beta = 0.001 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_nodes = 1024 + IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + IDT hidden_layer = tf.nn.relu(hidden_layer) + IDT keep_prob = tf.placeholder(tf.float32) + IDT hidden_layer_dropout = tf.nn.dropout(hidden_layer, keep_prob) + IDT logits_training = tf.matmul(hidden_layer_dropout, weights) + biases + IDT logits = tf.matmul(hidden_layer, weights) + biases + IDT loss = , logits= ) IDT l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + IDT loss = loss + beta * l2_loss + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + IDT test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 14001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2000 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +beta = 0.001 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_nodes = 1024 + IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + IDT hidden_layer = tf.nn.relu(hidden_layer) + IDT keep_prob = tf.placeholder(tf.float32) + IDT hidden_layer_training = tf.nn.dropout(hidden_layer, keep_prob) + IDT logits_training = tf.matmul(hidden_layer_training, weights) + biases + IDT logits = tf.matmul(hidden_layer, weights) + biases + IDT loss = , logits= ) IDT l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + IDT loss = loss + beta * l2_loss + IDT global_step = tf.Variable(0) + IDT learning_rate = , global_step, 1000, 0.9, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + IDT test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 20001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 1000 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +beta = 0.001 +batch_size = 128 +layer_depth = 5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT hidden_nodes = 1024 + IDT stddev = np.sqrt(2 / hidden_nodes) + IDT weights0 = , hidden_nodes], stddev= ) IDT biases0 = tf.Variable(tf.zeros([hidden_nodes])) + IDT hidden_nodes_pre = hidden_nodes + IDT hidden_weights = [] + IDT hidden_biases = [] + IDT for i in range(layer_depth - 2): + IDT IDT if hidden_nodes_pre > 2: + IDT IDT IDT hidden_nodes_cur = int(hidden_nodes_pre // 2) + IDT IDT else: + IDT IDT IDT hidden_nodes_cur == hidden_nodes_pre + IDT IDT stddev = np.sqrt(2 / hidden_nodes_cur) + IDT IDT hidden_weights.append(tf.Variable(tf.truncated_normal([hidden_nodes_pre, hidden_nodes_cur], stddev= ) IDT IDT hidden_biases.append(tf.Variable(tf.zeros([hidden_nodes_cur]))) + IDT IDT hidden_nodes_pre = hidden_nodes_cur + IDT weights = tf.Variable(tf.truncated_normal([hidden_nodes_cur, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT y0 = tf.add(tf.matmul(tf_train_dataset, weights0), biases0) + IDT y0 = tf.nn.relu(y0) + IDT y0_training = tf.nn.dropout(y0, 0.5) + IDT valid_y0 = tf.matmul(tf_valid_dataset, weights0) + biases0 + IDT valid_y0 = tf.nn.relu(valid_y0) + IDT test_y0 = tf.matmul(tf_test_dataset, weights0) + biases0 + IDT test_y0 = tf.nn.relu(test_y0) + IDT yi_training = y0_training + IDT hidden_layer = y0 + IDT valid_hidden_layer = valid_y0 + IDT test_hidden_layer = test_y0 + IDT for i in range(layer_depth - 2): + IDT IDT yi_training = tf.nn.relu(tf.matmul(yi_training, hidden_weights[i]) + hidden_biases[i]) + IDT IDT keep_prob = 0.5 * (1 + (i + 1) / (layer_depth - 1)) + IDT IDT hidden_layer_training = tf.nn.dropout(yi_training, keep_prob) + IDT IDT hidden_layer = tf.nn.relu(tf.matmul(hidden_layer, hidden_weights[i]) + hidden_biases[i]) + IDT IDT valid_hidden_layer = tf.nn.relu(tf.matmul(valid_hidden_layer, hidden_weights[i]) + hidden_biases[i]) + IDT IDT test_hidden_layer = tf.nn.relu(tf.matmul(test_hidden_layer, hidden_weights[i]) + hidden_biases[i]) + IDT logits_training = tf.matmul(hidden_layer_training, weights) + biases + IDT logits = tf.matmul(hidden_layer, weights) + biases + IDT valid_logits = tf.matmul(valid_hidden_layer, weights) + biases + IDT test_logits = tf.matmul(test_hidden_layer, weights) + biases + IDT loss = , logits= ) IDT l2_loss = tf.nn.l2_loss(weights0) + tf.nn.l2_loss(weights) + IDT for i in range(layer_depth - 2): + IDT IDT l2_loss += tf.nn.l2_loss(hidden_weights[i]) + IDT loss = loss + beta * l2_loss + IDT global_step = tf.Variable(0) + IDT learning_rate = , global_step, 1000, 0.8, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 18001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = '/home/noa.bratman/FERProject/udacity-DL/data/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT l = , logits= ) IDT loss = l + 0.001 * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + IDT biases1 = tf.Variable(tf.zeros([1024])) + IDT weights2 = tf.Variable(tf.truncated_normal([1024, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1), weights2) + biases2 + IDT l = , logits= ) IDT loss = l + 0.001 * tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT l = , logits= ) IDT loss = l + 0.001 * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 1000 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + IDT biases1 = tf.Variable(tf.zeros([1024])) + IDT weights2 = tf.Variable(tf.truncated_normal([1024, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1), weights2) + biases2 + IDT l = , logits= ) IDT loss = l + 0.001 * tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) +num_steps = 2000 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + IDT biases1 = tf.Variable(tf.zeros([1024])) + IDT weights2 = tf.Variable(tf.truncated_normal([1024, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf.nn.relu(tf.nn.dropout(tf.matmul(tf_train_dataset, weights1) + biases1, 0.5)), weights2) + biases2 + IDT l = , logits= ) IDT loss = l + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) +num_steps = 2000 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import random +import skimage.data +import skimage.transform +import matplotlib +import matplotlib.pyplot as plt +import numpy as np +import tensorflow as tf +from tensorflow.contrib.layers import flatten +import cv2 +import time +ROOT = 'A:' +training_imgs = os.path.join(ROOT, '/DataMining/training') +test_imgs = os.path.join(ROOT, '/DataMining/testing') + + +def data_load(dir_path): + IDT dirs = [dir for dir in os.listdir(dir_path) if os.path.isdir(os.path.join(dir_path, dir))] + IDT classes = [] + IDT img = [] + IDT for d in dirs: + IDT IDT label_dir = os.path.join(dir_path, d) + IDT IDT file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if f.endswith('.ppm')] + IDT IDT for f in file_names: + IDT IDT IDT img.append(skimage.data.imread(f)) + IDT IDT IDT classes.append(int(d)) + IDT return img, classes + + +images, classes = data_load(training_imgs) +nclasses = len(set(classes)) +print("""The number of unique classes are: {0} +Count of images in all classes: {1}""".format(nclasses, len(images))) + + +def sample_from_class(images, classes): + IDT tot_class = set(classes) + IDT plt.figure(figsize= , 20)) ) IDT i = 1 + IDT for label in tot_class: + IDT IDT img = images[classes.index(label)] + IDT IDT plt.subplot(10, 7, i) + IDT IDT plt.axis('off') + IDT IDT plt.title('class {0}, Num imgs {1}'.format(label, classes.count(label))) + IDT IDT i += 1 + IDT IDT _ = plt.imshow(img) + IDT plt.show() + + +print(""" + +Images from each class before preprocessing""") +sample_from_class(images, classes) +print(""" +Pre-processing the data""") +print(""" +Images after resizing""") +resized_imgs = , (32, 32), mode= )sample_from_class(resized_imgs, classes) +ready_classes = np.array(classes) +ready_images = np.array(resized_imgs) + + +def net_architecture(img_ph): + IDT mymean = 0 + IDT mydev = 0.1 + IDT weights_conv1 = , 5, 3, 6), mean= , stddev= , name= ) IDT bias_conv1 = , name= ) IDT layer1_out = , weights_conv1, strides= , 1, 1, 1], padding= ) IDT layer1_out = tf.nn.relu(layer1_out) + IDT layer1_out = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT weights_conv2 = , 5, 6, 16), mean= , stddev= , name= ) IDT bias_conv2 = , name= ) IDT conv2_out = , weights_conv2, strides= , 1, 1, 1], padding= ) IDT conv2_out = tf.nn.relu(conv2_out) + IDT conv2_out = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT fc0 = flatten(conv2_out) + IDT weights_fullyconn = , 120), mean= , stddev= , name= ) IDT bias_fullyconn = , name= ) IDT fullyconn_out = tf.matmul(fc0, weights_fullyconn) + bias_fullyconn + IDT fullyconn_out = tf.nn.relu(fullyconn_out) + IDT fullyconn2_weights = , 84), mean= , stddev= , name= ) IDT fullyconn2_bias = , name= ) IDT fullyconn2_out = tf.matmul(fullyconn_out, fullyconn2_weights) + fullyconn2_bias + IDT fullyconn2_out = tf.nn.relu(fullyconn2_out) + IDT hidden_layer = tf.nn.dropout(fullyconn2_out, keep_prob) + IDT fullyconn3_weights = , nclasses), mean= , stddev= , name= ) IDT fullyconn3_bias = , name= ) IDT logits = tf.matmul(fullyconn2_out, fullyconn3_weights) + fullyconn3_bias + IDT return logits + + +graph = tf.Graph() +with graph.as_default(): + IDT placeholder_img = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT placeholder_classes = tf.placeholder(tf.int32, [None]) + IDT keep_prob = tf.placeholder(tf.float32) + IDT logits = net_architecture(placeholder_img) + IDT classify_labels = tf.argmax(logits, 1) + IDT err = , labels= ) IDT train = ) IDT init = tf.global_variables_initializer() +session = )_ = session.run([init]) +start = time.time() +for i in range(201): + IDT _, err_value = , err], feed_dict= , placeholder_classes: ready_classes}) ) IDT if i % 10 == 0: + IDT IDT +end = time.time() +tot = end - start +print(""" +Time taken for training the model is {0} +""".format(tot)) +try_random_20 = random.sample(range(len(resized_imgs)), 20) +img_to_be_classified = [resized_imgs[i] for i in try_random_20] +sample_classes = [classes[i] for i in try_random_20] +classify = , feed_dict= )classiedfig = , 20)) )for img in range(len(img_to_be_classified)): + IDT actual_class = sample_classes[img] + IDT after_classification = classify[img] + IDT plt.subplot(10, 2, 1 + img) + IDT plt.axis('off') + IDT val = 'true' if actual_class == after_classification else 'false' + IDT plt.text(80, 20, 'actual: IDT IDT {0}\nclassified: {1}\ntruth: {2}'.format(actual_class, after_classification, val), fontsize= ) IDT plt.imshow(img_to_be_classified[img]) +test_images, test_classes = data_load(test_imgs) +resized_test = , (32, 32), mode= )sample_from_class(resized_test, test_classes) +classify = , feed_dict= )correct_matches = , classified in zip(test_classes, classify)]) )print(""" +The correct matches were {0} out of a total of {1} test images.""".format(correct_matches, len(test_classes))) +correctness = correct_matches / len(test_classes) * 100 +) +session.close() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT loss += 0.05 * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 12 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + + IDT def forward_prop(input): + IDT IDT h_layer = tf.nn.relu(tf.matmul(input, weights_1) + biases_1) + IDT IDT return tf.matmul(h_layer, weights_2) + biases_2 + IDT logits = forward_prop(tf_train_dataset) + IDT loss = , logits= ) IDT loss += 0.005 * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(forward_prop(tf_valid_dataset)) + IDT test_prediction = tf.nn.softmax(forward_prop(tf_test_dataset)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_keep_prob = tf.placeholder(tf.float32) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + + IDT def forward_prop_dropout(input): + IDT IDT h_layer = tf.nn.relu(tf.matmul(input, weights_1) + biases_1) + IDT IDT drop = tf.nn.dropout(h_layer, tf_keep_prob) + IDT IDT return tf.matmul(drop, weights_2) + biases_2 + + IDT def forward_prop(input): + IDT IDT h_layer = tf.nn.relu(tf.matmul(input, weights_1) + biases_1) + IDT IDT return tf.matmul(h_layer, weights_2) + biases_2 + IDT logits = forward_prop_dropout(tf_train_dataset) + IDT loss = , logits= ) IDT loss += 0.005 * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(forward_prop(tf_valid_dataset)) + IDT test_prediction = tf.nn.softmax(forward_prop(tf_test_dataset)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_datasets = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_datasets = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_datasets = save['test_dataset'] + IDT test_labels = save['test_labels'] + +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_datasets, train_labels = reformat(train_datasets, train_labels) +valid_datasets, valid_labels = reformat(valid_datasets, valid_labels) +test_dataset, test_labels = reformat(test_datasets, test_labels) + + + +train_subset = 10000 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_datasets[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_datasets) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT baises = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + baises + IDT loss = , logits= ) IDT l2 = tf.nn.l2_loss(weights) + IDT loss = tf.reduce_mean(loss + beta * l2) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + baises) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + baises) + IDT num_steps = 1000 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, prediction = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +num_nodes = 1024 +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_datasets) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([num_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT relu_layer = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + IDT loss = , labels= ) IDT regularizers = tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + IDT loss = tf.reduce_mean(loss + beta * regularizers) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT logits_1 = tf.matmul(tf_valid_dataset, weights_1) + biases_1 + IDT relu_layer = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + IDT valid_prediction = tf.nn.softmax(logits_2) + IDT logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + IDT relu_layer = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + IDT test_prediction = tf.nn.softmax(logits_2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for steps in range(num_steps): + IDT IDT offset = steps * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_datasets[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, prediction = , loss, train_prediction], feed_dict= ) IDT IDT if steps % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +train_datasets_2 = train_datasets[:500, :] +train_labels_2 = train_labels[:500, :] +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for steps in range(num_steps): + IDT IDT offset = steps * batch_size % (train_labels_2.shape[0] - batch_size) + IDT IDT batch_data = train_datasets_2[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels_2[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, prediction = , loss, train_prediction], feed_dict= ) IDT IDT if steps % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +num_nodes = 1024 +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +with tf.Session(graph= ) IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_datasets) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT baises_1 = tf.Variable(tf.zeros([num_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT baises_2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + baises_1 + IDT relu_layer = tf.nn.relu(logits_1) + IDT keep_prob = tf.placeholder('float') + IDT relu_layer_dropout = tf.nn.dropout(relu_layer, keep_prob) + IDT logits_2 = tf.matmul(relu_layer_dropout, weights_2) + baises_2 + IDT loss = , labels= ) IDT regularizers = tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + IDT loss = tf.reduce_mean(loss + beta * regularizers) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT logits_1 = tf.matmul(tf_valid_dataset, weights_1) + baises_1 + IDT relu_layer = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(relu_layer, weights_2) + baises_2 + IDT valid_prediction = tf.nn.softmax(logits_2) + IDT logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + IDT relu_layer = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + IDT test_prediction = tf.nn.softmax(logits_2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for steps in range(num_steps): + IDT IDT offset = steps * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_datasets[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + + +_print_success_message() +import tensorflow as tf +import numpy as np +dis = 0.9 + + +class CNN: + + IDT def __init__(self, session, input_size, output_size, name= ) IDT IDT self.session = session + IDT IDT self.input_size = input_size + IDT IDT self.output_size = output_size + IDT IDT self.net_name = name + IDT IDT self._filter_size = 3 + IDT IDT self._keep_prob = tf.placeholder(tf.float32) + IDT IDT self._build_network() + + IDT def _build_network(self, h_size= , l_rate= ) IDT IDT self._X = , [None, 7, 7, 1], name= ) IDT IDT W1 = , shape= , 3, 1, 32], initializer= ) IDT IDT + IDT IDT layer1 = , W1, strides= , 1, 1, 1], padding= ) IDT IDT layer1 = tf.nn.relu(layer1) + IDT IDT + IDT IDT layer1 = , ksize= , 2, 2, 1], strides= , 1, 1, 1], padding= ) IDT IDT + IDT IDT layer1 = , keep_prob= ) IDT IDT W2 = , shape= , 3, 32, 64], initializer= ) IDT IDT + IDT IDT layer2 = , W2, strides= , 1, 1, 1], padding= ) IDT IDT layer2 = tf.nn.relu(layer2) + IDT IDT + IDT IDT layer2 = , ksize= , 2, 2, 1], strides= , 1, 1, 1], padding= ) IDT IDT + IDT IDT layer2 = , keep_prob= ) IDT IDT W3 = , shape= , 512], initializer= ) IDT IDT + IDT IDT layer3 = tf.reshape(layer2, [-1, 7 * 7 * 64]) + IDT IDT layer3 = tf.matmul(layer3, W3) + IDT IDT layer3 = tf.nn.relu(layer3) + IDT IDT + IDT IDT layer3 = , keep_prob= ) IDT IDT W4 = , shape= , 45], initializer= ) IDT IDT self._l_pred = tf.matmul(layer3, W4) + IDT IDT + IDT IDT self._Y = , [None, self.output_size], name= ) IDT IDT self._loss = , labels= ) IDT IDT self._train = ) + IDT def predict(self, x): + IDT IDT return self.session.run(self._l_pred, feed_dict= , self._keep_prob: 0.7}) ) + IDT def update(self, x, y): + IDT IDT return self.session.run([self._loss, self._train], feed_dict= , self._Y: y, self._keep_prob: 0.7}) ) + +class RNN: + + IDT def __init__(self, session, n_input, n_step, n_output, name= ) IDT IDT self.session = session + IDT IDT self._n_input = n_input + IDT IDT self._n_output = n_output + IDT IDT self._n_step = n_step + IDT IDT self.net_name = name + IDT IDT self._n_hidden = 256 + IDT IDT self._build_network() + + IDT def _build_network(self, h_size= , l_rate= ) IDT IDT self._X = , [None, self._n_step, self._n_input], name= ) IDT IDT self._Y = , [None, self._n_output], name= ) IDT IDT W = , shape= , self._n_output], initializer= ) IDT IDT b = , shape= , initializer= ) IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self._n_hidden) + IDT IDT outputs, states = , self._X, dtype= ) IDT IDT outputs = tf.transpose(outputs, [1, 0, 2]) + IDT IDT outputs = outputs[-1] + IDT IDT self._model = tf.matmul(outputs, W) + b + IDT IDT self._cost = , labels= ) IDT IDT self._train = ) + IDT def predict(self, x): + IDT IDT return self.session.run(self._model, feed_dict= ) + IDT def update(self, x, y): + IDT IDT return self.session.run([self._cost, self._train], feed_dict= , self._Y: y}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import types +from tqdm import tqdm +from layers import * +CONFIG = types.SimpleNamespace() +CONFIG.saveDir = './saves/' +CONFIG.diffSaveDir = './diffSaves/' +CONFIG.logDir = './logs/' +CONFIG.maxImages = 200 +HYPERPARAMETER = types.SimpleNamespace() +HYPERPARAMETER.learningRate = 0.001 +HYPERPARAMETER.batchSize = CONFIG.maxImages +HYPERPARAMETER.epoch = 500 +HYPERPARAMETER.latentSpace = 15 +HYPERPARAMETER.diffLatentSpace = 5 +HYPERPARAMETER.contrast = 100 +HYPERPARAMETER.normByImage = True +HYPERPARAMETER.normalize = 'normal' + + +def SET_HYPERPARAMETER(key, value): + IDT global HYPERPARAMETER + IDT HYPERPARAMETER.__dict__[key] = value + + +def jtektModel(name, data, log= , shuffle= ) IDT rawTrainSet, _ = data.getAll(data.goodOnly) + IDT rawTestSet, _ = data.getAll(data.badOnly) + IDT if shuffle is True: + IDT IDT np.random.shuffle(rawTrainSet) + IDT IDT np.random.shuffle(rawTestSet) + IDT trainSet = rawTrainSet + IDT testSet = rawTestSet + IDT return Model(name, trainSet, testSet, log= ) + +def mnistModel(name, log= ) IDT (rawTrainSet, trainLabels), (rawTestSet, testLabels) = ) IDT trainSet = np.expand_dims(rawTrainSet, 3) / 255 + IDT testSet = np.expand_dims(rawTestSet, 3) / 255 + IDT return Model(name, trainSet, testSet, log= ) + +def npzModel(name, path, use, log= , shuffle= ) IDT data = np.load(path) + IDT trainSet = data['arr_0'] + IDT testSet = data['arr_1'] + IDT if shuffle is True: + IDT IDT np.random.shuffle(trainSet) + IDT IDT np.random.shuffle(testSet) + IDT return Model(name, trainSet, testSet, use, log= ) + +def emptyModel(name, use, inputsShape, log= ) IDT return Model(name, None, None, use, log= , inputsShape= ) + +class Model: + + IDT def jtektEncoder(self): + IDT IDT self.use = 'jtekt' + IDT IDT InputLayer(self, self.placeholder.inputs, self.inputsShape, resize= ) IDT IDT ConvutionalLayer(self, [2, 2, 1, 32], [1, 2, 2, 1], activation= ) IDT IDT ConvutionalLayer(self, [2, 2, 32, 64], [1, 2, 2, 1], activation= ) IDT IDT self.features = self.outputs() + IDT IDT Reshape(self, [self.batchSize, self.layers[-1].len()]) + IDT IDT Dense(self, HYPERPARAMETER.latentSpace, activation= ) IDT IDT self.encoder = self.outputs() + IDT IDT Dense(self, self.layers[-2].len(), activation= ) IDT IDT Reshape(self, [self.batchSize] + self.layers[-4].shape) + IDT IDT DeconvutionalLayer(self, [2, 2, 32, 64], [self.batchSize] + self.layers[1].shape, [1, 2, 2, 1], activation= ) IDT IDT DeconvutionalLayer(self, [2, 2, 1, 32], [self.batchSize] + self.inputsShape, [1, 2, 2, 1], activation= ) + IDT def diffEncoder_old(self): + IDT IDT self.use = 'diff' + IDT IDT InputLayer(self, self.placeholder.inputs, self.inputsShape, contrast= , normalize= ) IDT IDT c1 = , [2, 2, 1, 32], [1, 2, 2, 1], activation= ) IDT IDT c2 = , [2, 2, 32, 64], [1, 2, 2, 1], activation= ) IDT IDT self.features = self.outputs() + IDT IDT reshape = Reshape(self, [self.batchSize, self.layers[-1].len()]) + IDT IDT std = , HYPERPARAMETER.diffLatentSpace, activation= , factor= ) IDT IDT mean = , HYPERPARAMETER.diffLatentSpace, activation= ) IDT IDT VAE(self, mean, std) + IDT IDT self.encoder = self.outputs() + IDT IDT self.layers[-1].outputs2 = self.placeholder.code + IDT IDT Dense(self, reshape.len(), activation= ) IDT IDT Reshape(self, [self.batchSize] + c2.shape) + IDT IDT DeconvutionalLayer(self, [2, 2, 32, 64], [self.batchSize] + c1.shape, [1, 2, 2, 1], activation= ) IDT IDT DeconvutionalLayer(self, [2, 2, 1, 32], [self.batchSize] + self.inputsShape, [1, 2, 2, 1], activation= ) + IDT def diffEncoder(self): + IDT IDT self.use = 'diff' + IDT IDT InputLayer(self, self.placeholder.inputs, self.inputsShape, contrast= , normalize= ) IDT IDT c1 = , [2, 2, 1, 16], [1, 2, 2, 1], activation= ) IDT IDT c2 = , [2, 2, 16, 32], [1, 2, 2, 1], activation= ) IDT IDT c3 = , [2, 2, 32, 64], [1, 2, 2, 1], activation= ) IDT IDT self.features = self.outputs() + IDT IDT reshape = Reshape(self, [self.batchSize, self.layers[-1].len()]) + IDT IDT std = , HYPERPARAMETER.diffLatentSpace, factor= ) IDT IDT mean = Dense(self, HYPERPARAMETER.diffLatentSpace) + IDT IDT self.encoder = mean.outputs + IDT IDT mean.outputs2 = self.placeholder.code + IDT IDT VAE(self, mean, std) + IDT IDT Dense(self, reshape.len(), activation= ) IDT IDT Reshape(self, [self.batchSize] + c3.shape) + IDT IDT DeconvutionalLayer(self, [2, 2, 32, 64], [self.batchSize] + c2.shape, [1, 2, 2, 1], activation= ) IDT IDT DeconvutionalLayer(self, [2, 2, 16, 32], [self.batchSize] + c1.shape, [1, 2, 2, 1], activation= ) IDT IDT DeconvutionalLayer(self, [2, 2, 1, 16], [self.batchSize] + self.inputsShape, [1, 2, 2, 1], activation= ) + IDT def __init__(self, name, trainSet, testSet, use= , log= , inputsShape= ) IDT IDT tf.reset_default_graph() + IDT IDT self.name = name + IDT IDT self.layers = [] + IDT IDT self.weights = [] + IDT IDT self.bias = [] + IDT IDT self.layersByType = {} + IDT IDT self.log = log + IDT IDT self.trainSet = trainSet + IDT IDT self.testSet = testSet + IDT IDT if inputsShape is not None: + IDT IDT IDT self.inputsShape = inputsShape + IDT IDT else: + IDT IDT IDT self.inputsShape = list(self.trainSet[0].shape) + IDT IDT ) + IDT IDT self.initInitialization() + IDT IDT self.initPlaceholder() + IDT IDT if use == 'diff': + IDT IDT IDT self.diffEncoder() + IDT IDT else: + IDT IDT IDT self.jtektEncoder() + IDT IDT self.diff = tf.abs(self.inputs() - self.outputs()) + IDT IDT self.cost = tf.reduce_sum(tf.square(self.inputs() - self.outputs())) + IDT IDT self.optim = tf.train.AdamOptimizer(self.placeholder.learningRate).minimize(self.cost) + IDT IDT if self.log: + IDT IDT IDT self.initSummaries() + IDT IDT self.session = tf.Session() + IDT IDT if self.log: + IDT IDT IDT self.writer = tf.summary.FileWriter(os.path.join(CONFIG.logDir, self.name), self.session.graph) + IDT IDT self.saver = tf.train.Saver() + IDT IDT self.reinit() + + IDT def prefix(self): + IDT IDT if self.use == 'diff': + IDT IDT IDT return 'DIFF/' + IDT IDT else: + IDT IDT IDT return '' + + IDT def reinit(self): + IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT self.session.run(tf.local_variables_initializer()) + + IDT def save(self): + IDT IDT if self.use == 'diff': + IDT IDT IDT self.saver.save(self.session, os.path.join(CONFIG.diffSaveDir, self.name)) + IDT IDT else: + IDT IDT IDT self.saver.save(self.session, os.path.join(CONFIG.saveDir, self.name)) + + IDT def restore(self, path): + IDT IDT if self.use == 'diff': + IDT IDT IDT self.saver.restore(self.session, os.path.join(CONFIG.diffSaveDir, path)) + IDT IDT else: + IDT IDT IDT self.saver.restore(self.session, os.path.join(CONFIG.saveDir, path)) + + IDT def initInitialization(self): + IDT IDT self.winit = tf.glorot_uniform_initializer() + IDT IDT self.convinit = ) IDT IDT self.binit = tf.constant_initializer(0.0) + + IDT def initSummaries(self): + IDT IDT self.inputsSummary = , self.placeholder.inputs, max_outputs= ) IDT IDT self.inputsSummary = , self.inputs(), max_outputs= ) IDT IDT self.outputsSummary = , self.outputs(), max_outputs= ) IDT IDT self.diffSummary = , self.diff, max_outputs= ) IDT IDT self.costSummary = tf.summary.scalar('cost', self.cost) + IDT IDT self.layerSummaries = [tf.summary.histogram('layer/raw_inputs', self.placeholder.inputs)] + IDT IDT for layer in self.layers: + IDT IDT IDT self.layerSummaries.append(tf.summary.histogram('layer/' + layer.name, layer.outputs)) + IDT IDT weightGradient = tf.gradients(self.cost, self.weights) + IDT IDT self.weightSummaries = [] + IDT IDT for i in range(len(weightGradient)): + IDT IDT IDT self.weightSummaries.append(tf.summary.histogram('weight/' + str(i), weightGradient[i])) + IDT IDT biasGradient = tf.gradients(self.cost, self.bias) + IDT IDT self.biasSummaries = [] + IDT IDT for i in range(len(biasGradient)): + IDT IDT IDT self.biasSummaries.append(tf.summary.histogram('bias/' + str(i), biasGradient[i])) + IDT IDT self.summaries = tf.summary.merge_all() + + IDT def initPlaceholder(self): + IDT IDT self.placeholder = types.SimpleNamespace() + IDT IDT self.placeholder.inputs = , shape= ) IDT IDT self.batchSize = tf.shape(self.placeholder.inputs)[0] + IDT IDT self.placeholder.learningRate = , shape= ) IDT IDT self.placeholder.code = , shape= , HYPERPARAMETER.diffLatentSpace]) ) + IDT def train(self, epoch= , dataset= ) IDT IDT if dataset is None: + IDT IDT IDT dataset = self.trainSet + IDT IDT batchs = [] + IDT IDT start = 0 + IDT IDT while start < dataset.shape[0]: + IDT IDT IDT end = min(start + HYPERPARAMETER.batchSize, dataset.shape[0]) + IDT IDT IDT batchs.append({'start': start, 'end': end}) + IDT IDT IDT start = end + IDT IDT for i in range(epoch): + IDT IDT IDT + IDT IDT IDT for b in tqdm(batchs): + IDT IDT IDT IDT trainData = dataset[b['start']:b['end']] + IDT IDT IDT IDT self.session.run(self.optim, feed_dict= , self.placeholder.inputs: trainData}) ) IDT IDT IDT data = dataset[0:CONFIG.maxImages] + IDT IDT IDT if self.log: + IDT IDT IDT IDT summaries = , feed_dict= , self.placeholder.inputs: data}) ) IDT IDT IDT if self.log: + IDT IDT IDT IDT self.writer.add_summary(summaries, i) + + IDT def getDiff(self, dataset): + IDT IDT diffs = [] + IDT IDT batchs = [] + IDT IDT start = 0 + IDT IDT while start < dataset.shape[0]: + IDT IDT IDT end = min(start + HYPERPARAMETER.batchSize, dataset.shape[0]) + IDT IDT IDT batchs.append({'start': start, 'end': end}) + IDT IDT IDT start = end + IDT IDT for b in tqdm(batchs): + IDT IDT IDT data = dataset[b['start']:b['end']] + IDT IDT IDT diff = , feed_dict= , self.placeholder.inputs: data}) ) IDT IDT IDT diffs.append(diff) + IDT IDT data = dataset[0:CONFIG.maxImages] + IDT IDT if self.log: + IDT IDT IDT summaries = , feed_dict= , self.placeholder.inputs: data}) ) IDT IDT if self.log: + IDT IDT IDT self.writer.add_summary(summaries) + IDT IDT return np.concatenate(diffs) + + IDT def encode(self, dataset): + IDT IDT result = , feed_dict= ) IDT IDT return result + + IDT def getFeatures(self, dataset): + IDT IDT result = , feed_dict= ) IDT IDT return result + + IDT def generate(self, codes, example): + IDT IDT result = , feed_dict= , self.placeholder.code: codes}) ) IDT IDT return result + + IDT def reproduce(self, dataset): + IDT IDT result = , feed_dict= ) IDT IDT return result + + IDT def outputs(self): + IDT IDT return self.layers[-1].outputs + + IDT def outputs2(self): + IDT IDT return self.layers[-1].outputs2 + + IDT def inputs(self): + IDT IDT return self.layers[0].outputs + + IDT def addLayer(self, layer): + IDT IDT self.layers.append(layer) + IDT IDT if not layer.layerType in self.layersByType: + IDT IDT IDT self.layersByType[layer.layerType] = [] + IDT IDT self.layersByType[layer.layerType].append(layer) + + IDT def getWeight(self, name, shape, dtype, initializer): + IDT IDT w = , shape= , dtype= , initializer= ) IDT IDT self.weights.append(w) + IDT IDT return w + + IDT def getBias(self, name, shape, dtype, initializer): + IDT IDT w = , shape= , dtype= , initializer= ) IDT IDT self.bias.append(w) + IDT IDT return w +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +from __future__ import division +import imageio +import matplotlib.pyplot as plt +import numpy as np +import sys +import tensorflow as tf +from six.moves import cPickle as pickle +import random +image_size = 28 +pickle_file = '../Dataset/cleanShuffledMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT a = pickle.load(f) +train_labels = a['train_labels'] +test_labels = a['test_labels'] +validate_labels = a['valid_labels'] +train_dataset = a['train_dataset'] +test_dataset = a['test_dataset'] +validate_dataset = a['valid_dataset'] +lst = ['I', 'C', 'J', 'F', 'B', 'H', 'A', 'E', 'D', 'G'] + + +def flattenAndOneHot(Dataset, labels): + IDT Dataset = Dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT one_hot = np.zeros((len(labels), 10)) + IDT one_hot[np.arange(len(labels)), labels] = 1 + IDT return Dataset, one_hot + + +train_dataset, train_labels = flattenAndOneHot(train_dataset, train_labels) +test_dataset, test_labels = flattenAndOneHot(test_dataset, test_labels) +validate_dataset, validate_labels = flattenAndOneHot(validate_dataset, validate_labels) +num_category = 10 + + +def Accuracy(prediction1, labels1): + IDT pred1 = np.argmax(prediction1, 1) + IDT label1 = np.argmax(labels1, 1) + IDT acc = ) IDT return acc + + +def GradientDecentDemo(): + IDT train_length = 30000 + IDT test_length = 3000 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train = tf.constant(train_dataset[:train_length, :]) + IDT IDT tf_test = tf.constant(test_dataset[:test_length, :]) + IDT IDT tf_validate = tf.constant(validate_dataset[:test_length, :]) + IDT IDT tf_train_labels = tf.constant(train_labels[:train_length]) + IDT IDT Weight = tf.Variable(tf.truncated_normal([image_size * image_size, num_category])) + IDT IDT bias = tf.Variable(tf.zeros([num_category])) + IDT IDT logits = tf.matmul(tf_train, Weight) + IDT IDT loss = , logits= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_validate, Weight) + bias) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test, Weight) + bias) + IDT noSteps = 800 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(noSteps): + IDT IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT acc = Accuracy(predictions, train_labels[:train_length, :]) + IDT IDT IDT IDT + IDT IDT IDT IDT acc = , validate_labels[0:test_length]) ) IDT IDT IDT IDT + IDT IDT , test_labels[0:test_length])) + IDT IDT session.close() + + +def StocasticGradientDemo(): + IDT batchsize = 200 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_labels = , shape= , num_category)) ) IDT IDT tf_train_data = , shape= , image_size * image_size)) ) IDT IDT tf_validate = tf.constant(validate_dataset) + IDT IDT tf_test = tf.constant(test_dataset) + IDT IDT Weight = tf.Variable(tf.truncated_normal([image_size * image_size, num_category])) + IDT IDT bias = tf.Variable(tf.zeros(num_category)) + IDT IDT mat = tf.matmul(tf_train_data, Weight) + bias + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(mat) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_validate, Weight) + bias) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test, Weight) + bias) + IDT noSteps = 800 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(noSteps): + IDT IDT IDT offset = random.randint(1, train_dataset.shape[0] - batchsize) + IDT IDT IDT batch_data = train_dataset[offset:offset + batchsize, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batchsize, :] + IDT IDT IDT feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, predictions, l = , train_prediction, loss], feed_dict= ) IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT acc = Accuracy(predictions, batch_labels) + IDT IDT IDT IDT + IDT IDT IDT IDT acc = , validate_labels) ) IDT IDT IDT IDT + IDT IDT , test_labels)) + IDT IDT session.close() + + +def StocasticGradientReluDemo(): + IDT batchsize = 200 + IDT middle_layerSize = 1024 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_labels = , shape= , num_category)) ) IDT IDT tf_train_data = , shape= , image_size * image_size)) ) IDT IDT tf_validate = tf.constant(validate_dataset) + IDT IDT tf_test = tf.constant(test_dataset) + IDT IDT Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + IDT IDT bias = tf.Variable(tf.zeros(middle_layerSize)) + IDT IDT WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + IDT IDT biasNext = tf.Variable(tf.zeros(num_category)) + IDT IDT layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + IDT IDT mat = tf.matmul(layer, WeightNext) + biasNext + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(mat) + IDT IDT layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + IDT IDT layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + IDT noSteps = 800 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(noSteps): + IDT IDT IDT offset = random.randint(1, train_dataset.shape[0] - batchsize) + IDT IDT IDT batch_data = train_dataset[offset:offset + batchsize, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batchsize, :] + IDT IDT IDT feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, predictions, l = , train_prediction, loss], feed_dict= ) IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT acc = Accuracy(predictions, batch_labels) + IDT IDT IDT IDT + IDT IDT IDT IDT acc = , validate_labels) ) IDT IDT IDT IDT + IDT IDT , test_labels)) + IDT IDT session.close() + + +StocasticGradientReluDemo() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import random +import math +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.ops import rnn_cell +from tensorflow.python.ops.nn import bidirectional_dynamic_rnn +from tensorflow.python.ops.nn import dynamic_rnn +from tensorflow.python.ops.nn import sparse_softmax_cross_entropy_with_logits +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +import matplotlib +matplotlib.use('Agg') +import matplotlib.pyplot as plt +import time +import datetime +from tensorflow.python.ops import math_ops +from tensorflow.python.util import nest +from tensorflow.python.ops import init_ops +from evaluate import exact_match_score, f1_score +logging.basicConfig(level= ) + +def batch_linear(args, output_size, bias, bias_start= , scope= , name= ) IDT """Linear map: concat(W[i] * args[i]), where W[i] is a variable. + Args: + args: a 3D Tensor with shape [batch x m x n]. + output_size: int, second dimension of W[i] with shape [output_size x m]. + bias: boolean, whether to add a bias term or not. + bias_start: starting value to initialize the bias; 0 by default. + scope: (optional) Variable scope to create parameters in. + name: (optional) variable name. + Returns: + A 3D Tensor with shape [batch x output_size x n] equal to + concat(W[i] * args[i]), where W[i]s are newly created matrices. + Raises: + ValueError: if some of the arguments has unspecified or wrong shape. + """ + IDT if args is None or nest.is_sequence(args) and not args: + IDT IDT raise ValueError('`args` must be specified') + IDT if args.get_shape().ndims != 3: + IDT IDT raise ValueError('`args` must be a 3D Tensor') + IDT shape = args.get_shape() + IDT m = shape[1].value + IDT n = shape[2].value + IDT dtype = args.dtype + IDT scope = vs.get_variable_scope() + IDT with vs.variable_scope(scope) as outer_scope: + IDT IDT w_name = 'weights_' + IDT IDT if name is not None: + IDT IDT IDT w_name += name + IDT IDT weights = , [output_size, m], dtype= ) IDT IDT res = tf.map_fn(lambda x: math_ops.matmul(weights, x), args) + IDT IDT if not bias: + IDT IDT IDT return res + IDT IDT with vs.variable_scope(outer_scope) as inner_scope: + IDT IDT IDT b_name = 'biases_' + IDT IDT IDT if name is not None: + IDT IDT IDT IDT b_name += name + IDT IDT IDT inner_scope.set_partitioner(None) + IDT IDT IDT biases = , [output_size, n], dtype= , initializer= , dtype= ) IDT return tf.map_fn(lambda x: math_ops.add(x, biases), res) + + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +def plot_losses(losses): + IDT plt.plot(losses) + IDT ts = time.time() + IDT st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') + IDT plt.title('Loss') + IDT plt.savefig('loss/Losses_' + st) + IDT plt.clf() + + +def plot_metrics(metrics_dict): + IDT train_f1, = , label= ) IDT val_f1, = , label= ) IDT plt.title('F1 Score') + IDT plt.legend(handles= , val_f1]) ) IDT ts = time.time() + IDT st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H-%M-%S') + IDT plt.savefig('metric_plots/f1_' + st) + IDT plt.clf() + IDT train_em, = , label= ) IDT val_em, = , label= ) IDT plt.legend(handles= , val_em]) ) IDT plt.title('Exact Match') + IDT ts = time.time() + IDT st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H-%M-%S') + IDT plt.savefig('metric_plots/em_' + st) + + +class GRUAttnCell(rnn_cell.GRUCell): + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.hs = encoder_output + IDT IDT super(GRUAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = rnn_cell._linear(gru_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + IDT IDT return out, out + + +def matrix_multiply_with_batch(matrix= , batch= , matrixByBatch= ) IDT ret = None + IDT if matrixByBatch: + IDT IDT ret = tf.scan(lambda a, x: tf.matmul(matrix, x), batch) + IDT else: + IDT IDT n = batch.get_shape().as_list()[1] + IDT IDT m = batch.get_shape().as_list()[2] + IDT IDT c = matrix.get_shape().as_list()[1] + IDT IDT batch = tf.reshape(batch, [-1, m]) + IDT IDT ret = tf.matmul(batch, matrix) + IDT IDT ret = tf.reshape(ret, [-1, n, c]) + IDT return ret + + +class Encoder(object): + + IDT def __init__(self, state_size, embedding_size, output_size): + IDT IDT self.state_size = state_size + IDT IDT self.embedding_size = embedding_size + IDT IDT self.output_size = output_size + IDT IDT self.h_q = None + IDT IDT self.h_p = None + IDT IDT self.H_q = None + IDT IDT self.H_p = None + + IDT def encode_v2(self, question_embeddings, document_embeddings, question_mask, context_mask, encoderb_state_input, dropout_keep_prob, max_question_len): + IDT IDT """ encode_v2() + """ + IDT IDT lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + IDT IDT lstm_cell = , input_keep_prob= ) IDT IDT with tf.variable_scope('question_embedding'): + IDT IDT IDT question_length = , tf.int32), reduction_indices= ) IDT IDT IDT Q_prime, _ = , question_embeddings, sequence_length= , dtype= ) IDT IDT IDT + IDT IDT IDT Q = tf.tanh(batch_linear(Q_prime, max_question_len, True)) + IDT IDT IDT Q = tf.transpose(Q, [0, 2, 1]) + IDT IDT IDT + IDT IDT with tf.variable_scope('context_embedding'): + IDT IDT IDT context_length = , tf.int32), reduction_indices= ) IDT IDT IDT D, _ = , document_embeddings, sequence_length= , dtype= ) IDT IDT IDT D = tf.transpose(D, [0, 2, 1]) + IDT IDT IDT + IDT IDT with tf.variable_scope('coattention'): + IDT IDT IDT L = tf.batch_matmul(tf.transpose(D, [0, 2, 1]), Q) + IDT IDT IDT + IDT IDT IDT A_Q = , L, dtype= ) IDT IDT IDT A_D = , tf.transpose(L, [0, 2, 1]), dtype= ) IDT IDT IDT + IDT IDT IDT + IDT IDT IDT C_Q = batch_matmul(D, A_Q) + IDT IDT IDT + IDT IDT IDT concat = tf.concat(1, [Q, C_Q]) + IDT IDT IDT + IDT IDT IDT C_D = batch_matmul(tf.concat(1, [Q, C_Q]), A_D) + IDT IDT IDT + IDT IDT IDT co_att = tf.concat(1, [D, C_D]) + IDT IDT IDT co_att = tf.transpose(co_att, [0, 2, 1]) + IDT IDT IDT + IDT IDT with tf.variable_scope('encoder'): + IDT IDT IDT cell_fw = tf.nn.rnn_cell.LSTMCell(self.state_size) + IDT IDT IDT cell_bw = tf.nn.rnn_cell.LSTMCell(self.state_size) + IDT IDT IDT cell_fw = , input_keep_prob= ) IDT IDT IDT cell_bw = , input_keep_prob= ) IDT IDT IDT (fw_out, bw_out), _ = , cell_bw, co_att, sequence_length= , dtype= ) IDT IDT IDT + IDT IDT IDT + IDT IDT IDT U = tf.concat(2, [fw_out, bw_out]) + IDT IDT IDT + IDT IDT IDT return U + + IDT def encode(self, question_embeddings, context_embeddings, question_mask, context_mask, encoder_state_input, dropout_keep_prob, batch_size): + IDT IDT """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + IDT IDT with vs.variable_scope('encoder', True): + IDT IDT IDT with vs.variable_scope('question', True): + IDT IDT IDT IDT lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + IDT IDT IDT IDT lstm_cell = , output_keep_prob= ) IDT IDT IDT IDT question_length = , tf.int32), reduction_indices= ) IDT IDT IDT IDT + IDT IDT IDT IDT self.H_q, _ = , question_embeddings, sequence_length= , time_major= , dtype= , swap_memory= ) IDT IDT IDT IDT self.h_q = self.H_q[:, (1), :] + IDT IDT IDT IDT + IDT IDT IDT IDT + IDT IDT IDT with vs.variable_scope('context', True): + IDT IDT IDT IDT context_length = , tf.int32), reduction_indices= ) IDT IDT IDT IDT + IDT IDT IDT IDT context_lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + IDT IDT IDT IDT context_lstm_cell = , output_keep_prob= ) IDT IDT IDT IDT self.H_p, _ = , context_embeddings, sequence_length= , time_major= , dtype= , swap_memory= ) IDT IDT IDT IDT self.h_p = self.H_p[:, (1), :] + IDT IDT IDT IDT + IDT IDT IDT IDT + IDT IDT IDT return self.h_q, self.h_p + + +class Decoder(object): + + IDT def __init__(self, output_size, state_size): + IDT IDT self.state_size = state_size + IDT IDT self.output_size = output_size + + IDT def decode_v2(self, U, W, W_prime): + IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT a_s = , batch= , matrixByBatch= ) IDT IDT IDT + IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT a_e = , batch= , matrixByBatch= ) IDT IDT IDT + IDT IDT return a_s, a_e + + IDT def decode(self, h_q, h_p): + IDT IDT """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT a_s = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT a_e = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + IDT IDT return a_s, a_e + + +class QASystem(object): + + IDT def __init__(self, encoder, decoder, **kwargs): + IDT IDT """ + Initializes your System + + :param encoder: an encoder that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + IDT IDT self.max_question_len = 60 + IDT IDT self.max_context_len = 301 + IDT IDT self.max_answer_len = 46 + IDT IDT self.n_classes = 2 + IDT IDT self.saver = None + IDT IDT self.encoder = encoder + IDT IDT self.decoder = decoder + IDT IDT self.question_embeddings = None + IDT IDT self.context_embeddings = None + IDT IDT self.a_s_probs = None + IDT IDT self.a_e_probs = None + IDT IDT self.loss = None + IDT IDT self.train_op = None + IDT IDT self.grad_norm = None + IDT IDT self.gradients = None + IDT IDT self.clipped_gradients = None + IDT IDT self.state_size = kwargs['state_size'] + IDT IDT self.embed_path = kwargs['embed_path'] + IDT IDT self.embedding_size = kwargs['embedding_size'] + IDT IDT self.output_size = kwargs['output_size'] + IDT IDT self.optimizer = kwargs['optimizer'] + IDT IDT self.initial_learning_rate = kwargs['learning_rate'] + IDT IDT self.global_step = , trainable= ) IDT IDT self.epochs = kwargs['epochs'] + IDT IDT self.batch_size = kwargs['batch_size'] + IDT IDT self.max_gradient_norm = kwargs['max_gradient_norm'] + IDT IDT self.dropout_keep_prob = kwargs['dropout_keep_prob'] + IDT IDT self.train_dir = kwargs['train_dir'] + IDT IDT self.question_input_placeholder = tf.placeholder(tf.int32, (None, self.max_question_len)) + IDT IDT self.context_input_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + IDT IDT self.question_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_question_len)) + IDT IDT self.context_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_context_len)) + IDT IDT self.start_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + IDT IDT self.end_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + IDT IDT self.dropout_placeholder = tf.placeholder(tf.float32, ()) + IDT IDT self.batch_size_placeholder = tf.placeholder(tf.int32, ()) + IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.setup_training_op() + + IDT def setup_system(self): + IDT IDT """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + IDT IDT W = tf.get_variable('W', (2 * self.state_size, 1)) + IDT IDT W_prime = tf.get_variable('W_prime', (2 * self.state_size, 1)) + IDT IDT U = self.encoder.encode_v2(self.question_embeddings, self.context_embeddings, self.question_mask_placeholder, self.context_mask_placeholder, None, self.dropout_placeholder, self.max_question_len) + IDT IDT self.a_s_probs, self.a_e_probs = self.decoder.decode_v2(U, W, W_prime) + + IDT def setup_loss(self): + IDT IDT """ + Set up your loss computation here + :return: + """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT l1 = , labels= ) IDT IDT IDT l2 = , labels= ) IDT IDT IDT self.loss = tf.reduce_mean(l1 + l2) + + IDT def setup_training_op(self): + IDT IDT """ + Sets up the training ops. + + Creates an optimizer and applies the gradients to all trainable variables. + Clips the global norm of the gradients. + """ + IDT IDT lr = tf.train.exponential_decay(self.initial_learning_rate, self.global_step, 1000, 0.96) + IDT IDT opt = ) IDT IDT self.gradients, params = zip(*opt.compute_gradients(self.loss)) + IDT IDT for param in params: + IDT IDT IDT + IDT IDT self.clipped_gradients, _ = tf.clip_by_global_norm(self.gradients, self.max_gradient_norm) + IDT IDT grads_and_params = zip(self.clipped_gradients, params) + IDT IDT self.grad_norm = tf.global_norm(self.clipped_gradients) + IDT IDT self.train_op = , global_step= ) + IDT def setup_embeddings(self): + IDT IDT """ + Loads distributed word representations based on placeholder tokens + :return: + """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT pretrained_embeddings = np.load(self.embed_path)['glove'] + IDT IDT IDT self.question_embeddings = , name= , dtype= ) IDT IDT IDT self.question_embeddings = tf.nn.embedding_lookup(self.question_embeddings, self.question_input_placeholder) + IDT IDT IDT self.question_embeddings = tf.reshape(self.question_embeddings, [-1, self.max_question_len, self.embedding_size]) + IDT IDT IDT + IDT IDT IDT self.context_embeddings = , name= , dtype= ) IDT IDT IDT self.context_embeddings = tf.nn.embedding_lookup(self.context_embeddings, self.context_input_placeholder) + IDT IDT IDT self.context_embeddings = tf.reshape(self.context_embeddings, [-1, self.max_context_len, self.embedding_size]) + IDT IDT IDT + + IDT def optimize(self, session, train_batch): + IDT IDT """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + IDT IDT question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*train_batch) + IDT IDT feed = {} + IDT IDT feed[self.question_input_placeholder] = question_batch + IDT IDT feed[self.context_input_placeholder] = context_batch + IDT IDT feed[self.question_mask_placeholder] = question_mask_batch + IDT IDT feed[self.context_mask_placeholder] = context_mask_batch + IDT IDT feed[self.start_answer_placeholder] = start_answer_batch + IDT IDT feed[self.end_answer_placeholder] = end_answer_batch + IDT IDT feed[self.dropout_placeholder] = self.dropout_keep_prob + IDT IDT self.batch_size = len(question_batch) + IDT IDT feed[self.batch_size_placeholder] = self.batch_size + IDT IDT _, loss, grad_norm = , self.loss, self.grad_norm], feed_dict= ) IDT IDT return loss, grad_norm + + IDT def test(self, session, valid_x, valid_y): + IDT IDT """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + IDT IDT input_feed = {} + IDT IDT input_feed['valid_x'] = valid_x + IDT IDT if valid_y is not None: + IDT IDT IDT input_feed['valid_y'] = valid_y + IDT IDT output_feed = [] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def decode(self, session, test_x): + IDT IDT """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + IDT IDT question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*test_x) + IDT IDT input_feed = {} + IDT IDT input_feed[self.question_input_placeholder] = question_batch + IDT IDT input_feed[self.context_input_placeholder] = context_batch + IDT IDT input_feed[self.question_mask_placeholder] = question_mask_batch + IDT IDT input_feed[self.context_mask_placeholder] = context_mask_batch + IDT IDT input_feed[self.start_answer_placeholder] = start_answer_batch + IDT IDT input_feed[self.end_answer_placeholder] = end_answer_batch + IDT IDT input_feed[self.dropout_placeholder] = 1.0 + IDT IDT self.batch_size = len(question_batch) + IDT IDT input_feed[self.batch_size_placeholder] = self.batch_size + IDT IDT _, loss, grad_norm = , self.loss, self.grad_norm], feed_dict= ) IDT IDT a_s = , session= ) IDT IDT a_e = , session= ) IDT IDT return a_s, a_e + + IDT def answer(self, session, test_x): + IDT IDT yp, yp2 = self.decode(session, test_x) + IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, session, dataset, model_path, model_name): + IDT IDT """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + IDT IDT new_saver = tf.train.import_meta_graph(model_path + model_name) + IDT IDT new_saver.restore(session, tf.train.latest_checkpoint(model_path)) + IDT IDT f1s = [] + IDT IDT ems = [] + IDT IDT step = 500 + IDT IDT for start_idx in range(0, len(dataset), step): + IDT IDT IDT end_idx = min(start_idx + step, len(dataset)) + IDT IDT IDT f1s_one_batch, ems_one_batch = , dataset[start_idx:end_idx], sample= , log= ) IDT IDT IDT f1s += f1s_one_batch + IDT IDT IDT ems += ems_one_batch + IDT IDT f1_total = sum(f1s) / float(len(f1s)) + IDT IDT em_total = sum(ems) / float(len(ems)) + IDT IDT + IDT IDT + + IDT def evaluate_answer(self, session, dataset, sample= , log= , shuffle= ) IDT IDT """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + IDT IDT if sample is None: + IDT IDT IDT sample = len(dataset) + IDT IDT + IDT IDT if shuffle: + IDT IDT IDT random.shuffle(dataset) + IDT IDT batch = dataset[0:sample] + IDT IDT question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*batch) + IDT IDT a_s, a_e = self.answer(session, batch) + IDT IDT true_a_s = , axis= ) IDT IDT true_a_e = , axis= ) IDT IDT + IDT IDT + IDT IDT + IDT IDT + IDT IDT answers = [context_batch[i][a_s[i]:a_e[i] + 1] for i in range(len(a_s))] + IDT IDT true_answers = [context_batch[i][true_a_s[i]:true_a_e[i] + 1] for i in range(len(true_a_s))] + IDT IDT f1s = [] + IDT IDT ems = [] + IDT IDT for i in range(len(true_answers)): + IDT IDT IDT answer = answers[i] + IDT IDT IDT true_answer = true_answers[i] + IDT IDT IDT f1_one_example = f1_score(answer, true_answer) + IDT IDT IDT f1s.append(f1_one_example) + IDT IDT IDT em_one_example = exact_match_score(answer, true_answer) + IDT IDT IDT ems.append(em_one_example) + IDT IDT f1 = np.sum(f1s) / float(sample) + IDT IDT em = np.sum(ems) / float(sample) + IDT IDT if log: + IDT IDT IDT logging.info('F1: {}, EM: {}, for {} samples'.format(f1, em, sample)) + IDT IDT return f1s, ems + + IDT def train(self, session, dataset, train_dir): + IDT IDT """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious appoarch can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + IDT IDT dataset = self.preprocess_dataset(dataset) + IDT IDT init = tf.global_variables_initializer() + IDT IDT session.run(init) + IDT IDT training_losses = [] + IDT IDT training_metrics = {'train': {'f1': [], 'em': []}, 'val': {'f1': [], 'em': []}} + IDT IDT for epoch in range(100): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.epochs) + IDT IDT IDT self.run_epoch(session, dataset['train'], dataset['val'], epoch, training_losses, training_metrics) + IDT IDT IDT self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch)) + IDT IDT IDT tic = time.time() + IDT IDT IDT params = tf.trainable_variables() + IDT IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT IDT toc = time.time() + IDT IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT IDT plot_losses(training_losses) + IDT IDT IDT plot_metrics(training_metrics) + IDT IDT plot_losses(training_losses) + IDT IDT self.evaluate_answer(session, dataset['val'], sample= , log= ) + IDT def preprocess_dataset(self, dataset): + IDT IDT for data_subset_name in ['train', 'val']: + IDT IDT IDT for i in range(len(dataset[data_subset_name])): + IDT IDT IDT IDT question = dataset[data_subset_name][i][0] + IDT IDT IDT IDT context = dataset[data_subset_name][i][1] + IDT IDT IDT IDT answer = dataset[data_subset_name][i][2] + IDT IDT IDT IDT padded_question, question_mask = self.pad_sequence(question, self.max_question_len) + IDT IDT IDT IDT padded_context, context_mask = self.pad_sequence(context, self.max_context_len) + IDT IDT IDT IDT start_answer = [0] * self.max_context_len + IDT IDT IDT IDT if answer[0] < self.output_size: + IDT IDT IDT IDT IDT start_answer[answer[0]] = 1 + IDT IDT IDT IDT end_answer = [0] * self.max_context_len + IDT IDT IDT IDT if answer[1] < self.output_size: + IDT IDT IDT IDT IDT end_answer[answer[1]] = 1 + IDT IDT IDT IDT dataset[data_subset_name][i] = padded_question, padded_context, question_mask, context_mask, start_answer, end_answer + IDT IDT return dataset + + IDT def pad_sequence(self, sequence, max_length): + IDT IDT new_sequence = [] + IDT IDT mask = [] + IDT IDT if len(sequence) >= max_length: + IDT IDT IDT new_sequence = sequence[0:max_length] + IDT IDT IDT mask = [True] * max_length + IDT IDT elif len(sequence) < max_length: + IDT IDT IDT delta = max_length - len(sequence) + IDT IDT IDT new_sequence = sequence + [0] * delta + IDT IDT IDT mask = [True] * len(sequence) + [False] * delta + IDT IDT return new_sequence, mask + + IDT def run_epoch(self, session, train_examples, val_examples, epoch_no, training_losses, training_metrics): + IDT IDT for i, batch in enumerate(self.minibatches(train_examples, self.batch_size, shuffle= ) IDT IDT IDT ) + IDT IDT IDT loss, grad_norm = self.optimize(session, batch) + IDT IDT IDT + IDT IDT IDT training_losses.append(loss) + IDT IDT IDT if i % 100 == 0: + IDT IDT IDT IDT train_f1s, train_ems = , train_examples, sample= , log= , shuffle= ) IDT IDT IDT IDT val_f1s, val_ems = , val_examples, sample= , log= , shuffle= ) IDT IDT IDT IDT train_f1 = sum(train_f1s) / float(len(train_f1s)) + IDT IDT IDT IDT val_f1 = sum(val_f1s) / float(len(val_f1s)) + IDT IDT IDT IDT train_em = sum(train_ems) / float(len(train_ems)) + IDT IDT IDT IDT val_em = sum(val_ems) / float(len(val_ems)) + IDT IDT IDT IDT training_metrics['train']['f1'].append(train_f1) + IDT IDT IDT IDT training_metrics['val']['f1'].append(val_f1) + IDT IDT IDT IDT training_metrics['train']['em'].append(train_em) + IDT IDT IDT IDT training_metrics['val']['em'].append(val_em) + IDT IDT IDT IDT plot_losses(training_losses) + IDT IDT IDT IDT plot_metrics(training_metrics) + IDT IDT IDT if i % 1000 == 0: + IDT IDT IDT IDT self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch_no) + '_iter_' + str(i)) + + IDT def get_tiny_batches(self, data, sample= ) IDT IDT ret = [] + IDT IDT step = self.batch_size + IDT IDT for start_idx in range(0, sample, step): + IDT IDT IDT ret.append(data[start_idx:start_idx + step]) + IDT IDT return ret + + IDT def minibatches(self, data, batch_size, shuffle= ) IDT IDT if shuffle: + IDT IDT IDT random.shuffle(data) + IDT IDT num_batches = int(math.ceil(len(data) / batch_size)) + IDT IDT q, r = divmod(len(data), num_batches) + IDT IDT indices = [(q * i + min(i, r)) for i in xrange(num_batches + 1)] + IDT IDT return [data[indices[i]:indices[i + 1]] for i in xrange(num_batches)] +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +batch_size = 128 +h1_length = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size ** 2)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size ** 2, h1_length])) + IDT biases1 = tf.Variable(tf.zeros([h1_length])) + IDT logits1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT weights2 = tf.Variable(tf.truncated_normal([h1_length, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits2 = tf.matmul(logits1, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits2, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) + + IDT def accuracy(predictions, labels): + IDT IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) IDT num_steps = 3001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = batch_size * step % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) +import os.path +import tensorflow as tf +import helper +import warnings +from distutils.version import LooseVersion +import project_tests as tests +from matplotlib import pyplot as plt +import time +import shutil +assert LooseVersion(tf.__version__) >= , 'Please use TensorFlow version 1.0 or newer. You are using {}'.format(tf.__version__) )) +if not tf.test.gpu_device_name(): + IDT warnings.warn('No GPU found. Please use a GPU to train your neural network.') +else: + IDT )) +num_classes = 3 +NUMBER_OF_CLASSES = 3 +image_shape = 352, 640 +IMAGE_SHAPE = 352, 640 +EPOCHS = 40 +BATCH_SIZE = 4 +DROPOUT = 0.75 +data_dir = './data' +test_data_dir = '../data/360_raw/test' +runs_dir = '../data/runs' +training_dir = './data/data_road/training' +vgg_path = './data/vgg' +correct_label = tf.placeholder(tf.float32, [None, IMAGE_SHAPE[0], IMAGE_SHAPE[1], NUMBER_OF_CLASSES]) +learning_rate = tf.placeholder(tf.float32) +keep_prob = tf.placeholder(tf.float32) + + +def load_vgg(sess, vgg_path): + IDT """ + IDT Load Pretrained VGG Model into TensorFlow. + IDT :param sess: TensorFlow Session + IDT :param vgg_path: Path to vgg folder, containing "variables/" and "saved_model.pb" + IDT :return: Tuple of Tensors from VGG model (image_input, keep_prob, layer3_out, layer4_out, layer7_out) + IDT """ + IDT model = tf.saved_model.loader.load(sess, ['vgg16'], vgg_path) + IDT graph = tf.get_default_graph() + IDT image_input = graph.get_tensor_by_name('image_input:0') + IDT keep_prob = graph.get_tensor_by_name('keep_prob:0') + IDT layer3 = graph.get_tensor_by_name('layer3_out:0') + IDT layer4 = graph.get_tensor_by_name('layer4_out:0') + IDT parameters = [] + IDT kernel = , 3, 512, 512], dtype= , stddev= , name= ) IDT conv = , kernel, [1, 1, 1, 1], padding= ) IDT biases = , shape= , dtype= , trainable= , name= ) IDT out = tf.nn.bias_add(conv, biases) + IDT conv5_1 = , name= ) IDT parameters += [kernel, biases] + IDT kernel = , 3, 512, 512], dtype= , stddev= , name= ) IDT conv = , kernel, [1, 1, 1, 1], padding= ) IDT biases = , shape= , dtype= , trainable= , name= ) IDT out = tf.nn.bias_add(conv, biases) + IDT conv5_2 = tf.nn.relu(out, 'conv5_2') + IDT parameters += [kernel, biases] + IDT kernel = , 3, 512, 512], dtype= , stddev= , name= ) IDT conv = , kernel, [1, 1, 1, 1], padding= ) IDT biases = , shape= , dtype= , trainable= , name= ) IDT out = tf.nn.bias_add(conv, biases) + IDT conv5_3 = , name= ) IDT parameters += [kernel, biases] + IDT pool5 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= , name= ) IDT kernel = , 20, 512, 32], dtype= , stddev= , name= ) IDT conv = , kernel, [1, 1, 1, 1], padding= ) IDT biases = , shape= , dtype= , trainable= , name= ) IDT out = tf.nn.bias_add(conv, biases) + IDT conv6 = , name= ) IDT parameters += [kernel, biases] + IDT kernel = , 1, 32, 4096], dtype= , stddev= , name= ) IDT conv = , kernel, [1, 1, 1, 1], padding= ) IDT biases = , shape= , dtype= , trainable= , name= ) IDT out = tf.nn.bias_add(conv, biases) + IDT conv7 = , name= ) IDT parameters += [kernel, biases] + IDT layer7 = conv7 + IDT return image_input, keep_prob, layer3, layer4, layer7 + + +def layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes): + IDT """ + IDT Create the layers for a fully convolutional network. Build skip-layers using the vgg layers. + IDT :param vgg_layer3_out: TF Tensor for VGG Layer 3 output + IDT :param vgg_layer4_out: TF Tensor for VGG Layer 4 output + IDT :param vgg_layer7_out: TF Tensor for VGG Layer 7 output + IDT :param num_classes: Number of classes to classify + IDT :return: The Tensor for the last layer of output + IDT """ + IDT layer3, layer4, layer7 = vgg_layer3_out, vgg_layer4_out, vgg_layer7_out + IDT fcn8 = , filters= , kernel_size= , name= ) IDT fcn9 = , filters= , kernel_size= , strides= , 2), padding= , name= ) IDT fcn9_skip_connected = , layer4, name= ) IDT fcn10 = , filters= , kernel_size= , strides= , 2), padding= , name= ) IDT fcn10_skip_connected = , layer3, name= ) IDT fcn11 = , filters= , kernel_size= , strides= , 8), padding= , name= ) IDT + IDT + IDT + IDT + IDT + IDT + IDT + IDT return fcn11 + + +tests.test_layers(layers) + + +def optimize(nn_last_layer, correct_label, learning_rate, num_classes): + IDT """ + IDT Build the TensorFLow loss and optimizer operations. + IDT :param nn_last_layer: TF Tensor of the last layer in the neural network + IDT :param correct_label: TF Placeholder for the correct label image + IDT :param learning_rate: TF Placeholder for the learning rate + IDT :param num_classes: Number of classes to classify + IDT :return: Tuple of (logits, train_op, cross_entropy_loss) + IDT """ + IDT logits = , (-1, num_classes), name= ) IDT correct_label_reshaped = tf.reshape(correct_label, (-1, num_classes)) + IDT cross_entropy = , labels= ) IDT loss_op = , name= ) IDT train_op = , name= ) IDT return logits, correct_label_reshaped, train_op, loss_op + + +tests.test_optimize(optimize) + + +def train_nn(sess, epochs, batch_size, get_batches_fn, train_op, cross_entropy_loss, input_image, correct_label, keep_prob, learning_rate, correct_labels, logits): + IDT """ + IDT Train neural network and print out the loss during training. + IDT :param sess: TF Session + IDT :param epochs: Number of epochs + IDT :param batch_size: Batch size + IDT :param get_batches_fn: Function to get batches of training data. Call using get_batches_fn(batch_size) + IDT :param train_op: TF Operation to train the neural network + IDT :param cross_entropy_loss: TF Tensor for the amount of loss + IDT :param input_image: TF Placeholder for input images + IDT :param correct_label: TF Placeholder for label images + IDT :param keep_prob: TF Placeholder for dropout keep probability + IDT :param learning_rate: TF Placeholder for learning rate + IDT """ + IDT keep_prob_value = 0.5 + IDT learning_rate_value = 0.001 + IDT for epoch in range(epochs): + IDT IDT counter = 0 + IDT IDT total_loss = 0 + IDT IDT for X_batch, gt_batch in get_batches_fn(batch_size): + IDT IDT IDT loss, _, cl, lo = , train_op, correct_labels, logits], feed_dict= , correct_label: gt_batch, keep_prob: keep_prob_value, learning_rate: learning_rate_value}) ) IDT IDT IDT total_loss += loss + IDT IDT IDT counter += 1 + IDT IDT )) + IDT IDT ) + IDT IDT ) + IDT IDT + + +def run(): + IDT )) + IDT helper.maybe_download_pretrained_vgg(data_dir) + IDT + IDT with tf.Session() as sess: + IDT IDT vgg_path = os.path.join(data_dir, 'vgg') + IDT IDT + IDT IDT get_batches_fn = helper.gen_batch_function('../data', image_shape) + IDT IDT + IDT IDT with tf.Session() as session: + IDT IDT IDT image_input, keep_prob, layer3, layer4, layer7 = load_vgg(session, vgg_path) + IDT IDT IDT + IDT IDT IDT model_output = layers(layer3, layer4, layer7, num_classes) + IDT IDT IDT + IDT IDT IDT logits, correct_labels, train_op, cross_entropy_loss = optimize(model_output, correct_label, learning_rate, num_classes) + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT )) + IDT IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT IDT session.run(tf.local_variables_initializer()) + IDT IDT IDT )) + IDT IDT IDT + IDT IDT IDT train_nn(session, EPOCHS, BATCH_SIZE, get_batches_fn, train_op, cross_entropy_loss, image_input, correct_label, keep_prob, learning_rate, correct_labels, logits) + IDT IDT IDT )) + IDT IDT IDT ') + IDT IDT IDT + IDT IDT IDT saver = tf.train.Saver() + IDT IDT IDT output_dir = '../Trained_Model/' + str(time.strftime('%Y_%m_%d-%H_%M_%S', time.gmtime())) + IDT IDT IDT if os.path.exists(output_dir): + IDT IDT IDT IDT shutil.rmtree(output_dir) + IDT IDT IDT os.makedirs(output_dir) + IDT IDT IDT saver.save(session, output_dir + '/model.ckpt') + IDT IDT IDT + + +if __name__ == '__main__': + IDT run() +import models +import tensorflow as tf +import numpy as np +import csv +import random +all_data = [] +data_time = [] +train_data = [] +train_label = [] +test_data = [] +test_label = [] +with open('train_set211.csv', 'r') as f: + IDT rd = csv.reader(f) + IDT for line in rd: + IDT IDT all_data.append(line) +with open('test_set.csv', 'r') as f: + IDT rd = csv.reader(f) + IDT for line in rd: + IDT IDT test_data.append([float(i) for i in line[2:-1]]) + IDT IDT test_label.append([float(line[-1]) - 1]) +random.shuffle(all_data) +for data in all_data: + IDT data_time.append([data[0]]) + IDT train_data.append([float(i) for i in data[2:-1]]) + IDT train_label.append([float(data[-1]) - 1]) +train_data = np.array(train_data) +train_label = np.array(train_label) +test_data = np.array(test_data) +test_label = np.array(test_label) + +""" +hypo = [] +for i in range(len(train_data)): + IDT hypo.append([train_data[i][0] * 0.0779 + train_data[i][1] * 0.1324 + train_data[i][2] * 0.1300 + train_data[i][3] * 0.1194 + train_data[i][4] * 0.0908 - 1.0850]) +hypo = np.array(hypo) + + + + +, train_label, mode=1)) +""" +lr = 0.0015 +nn = , one_hot= , out_one_hot= )nn_train = , None] - 1).astype(float) )nn_test = , None] - 1).astype(float) )X_data = , shape= , 4, 20]) )X = , shape= , 80]) )Y_data = , shape= )Y = , depth= )keep_prob = tf.placeholder(tf.float32) +h_nn = , keep_prob= )h_out = , keep_prob= )cost_nn = , logits= )train = )with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT y_train = train_label.reshape([-1]) + IDT y_test = test_label.reshape([-1]) + IDT for i in range(2000): + IDT IDT if i % 10 == 0: + IDT IDT IDT hypo_test, test_y = , Y], feed_dict= , Y_data: y_test, keep_prob: 1.0}) ) IDT IDT IDT act1 = , test_y, mode= , one_hot= ) IDT IDT IDT act2 = , test_y, mode= , one_hot= ) IDT IDT IDT + IDT IDT sess.run(train, feed_dict= , Y_data: y_train, keep_prob: 0.8}) )""" + IDT + IDT cost_arr = , feed_dict= , Y_data: y_train, keep_prob: 1.0}) ) IDT with open('error.txt', 'w') as f: + IDT IDT for err in cost_arr: + IDT IDT IDT f.write(str(err)) + IDT IDT IDT f.write(' +') + IDT + + +X = , shape= , 4]) )Y = , shape= , 1]) )lr = 0.001 +lin = models.LinReg() +h_lin = lin.forward(X) +cost_lin = tf.reduce_mean(tf.square(h_lin - Y)) +train = ) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + + IDT for i in range(3000): + IDT IDT if i % 10 == 0: + IDT IDT IDT hypo, y_real = , Y], feed_dict= , Y: train_label}) ) IDT IDT IDT hypo_test, y_test = , Y], feed_dict= , Y:test_label}) ) IDT IDT IDT acc1 = , y_real, mode= ) IDT IDT IDT acc2 = , y_real, mode= ) + IDT IDT IDT act1 = , y_test, mode= ) IDT IDT IDT act2 = , y_test, mode= ) IDT IDT IDT + IDT IDT IDT + + IDT IDT sess.run(train, feed_dict= , Y: train_label}) ) + + +lr = 0.001 +logistic = )X = , shape= , 4]) )Y_data = , shape= )Y = , depth= )h_log = logistic.forward(X) +cost_log = , logits= )train = ) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT y_train = train_label.reshape([-1]) + IDT y_test = test_label.reshape([-1]) + IDT for i in range(3000): + IDT IDT if i % 10 == 0: + IDT IDT IDT hypo, y_real = , Y], feed_dict= , Y_data: y_train}) ) IDT IDT IDT hypo_test, test_y = , Y], feed_dict= , Y_data: y_test}) ) IDT IDT IDT acc1 = , y_real, mode= , one_hot= ) IDT IDT IDT acc2 = , y_real, mode= , one_hot= ) + IDT IDT IDT act1 = , test_y, mode= , one_hot= ) IDT IDT IDT act2 = , test_y, mode= , one_hot= ) IDT IDT IDT + IDT IDT IDT + + IDT IDT sess.run(train, feed_dict= , Y_data: y_train}) ) + + +nn = )lr = 0.0015 +X = , shape= , 4]) )Y = , shape= , 1]) )keep_prob = tf.placeholder(tf.float32) +h_nn = , keep_prob= )cost_nn = tf.reduce_mean(tf.square(h_nn - Y)) +train = ) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT for i in range(5000): + IDT IDT if i % 10 == 0: + IDT IDT IDT hypo, y_real = , Y], feed_dict= , Y: train_label, keep_prob: 1.0}) ) IDT IDT IDT hypo_test, y_test = , Y], feed_dict= , Y: test_label, keep_prob: 1.0}) ) IDT IDT IDT acc1 = , y_real, mode= ) IDT IDT IDT acc2 = , y_real, mode= ) + IDT IDT IDT act1 = , y_test, mode= ) IDT IDT IDT act2 = , y_test, mode= ) IDT IDT IDT + IDT IDT IDT + + IDT IDT sess.run(train, feed_dict= , Y: train_label, keep_prob: 0.8}) ) + + +lr = 0.0015 +nn = , out_one_hot= )X = , shape= , 4]) )Y_data = , shape= )Y = , depth= )keep_prob = tf.placeholder(tf.float32) +h_nn = , keep_prob= )cost_nn = , logits= )train = ) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT y_train = train_label.reshape([-1]) + IDT y_test = test_label.reshape([-1]) + IDT for i in range(5000): + IDT IDT if i % 10 == 0: + IDT IDT IDT hypo, y_real = , Y], feed_dict= , Y_data: y_train, keep_prob: 1.0}) ) IDT IDT IDT hypo_test, test_y = , Y], feed_dict= , Y_data: y_test, keep_prob: 1.0}) ) IDT IDT IDT acc1 = , y_real, mode= , one_hot= ) IDT IDT IDT acc2 = , y_real, mode= , one_hot= ) + IDT IDT IDT act1 = , test_y, mode= , one_hot= ) IDT IDT IDT act2 = , test_y, mode= , one_hot= ) IDT IDT IDT + IDT IDT IDT + + IDT IDT sess.run(train, feed_dict= , Y_data: y_train, keep_prob: 0.8}) ) + + +lr = 0.0015 +nn_train = , None] - 1).astype(float) )nn_test = , None] - 1).astype(float) )nn = , one_hot= , out_one_hot= )X_data = , shape= , 4, 20]) )X = , shape= , 80]) )Y = , shape= , 1]) )keep_prob = tf.placeholder(tf.float32) +h_nn = , keep_prob= )cost_nn = tf.reduce_mean(tf.square(h_nn - Y)) +train = ) + +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT for i in range(5000): + IDT IDT if i % 10 == 0: + IDT IDT IDT hypo, y_real = , Y], feed_dict= , Y: train_label, keep_prob: 1.0}) ) IDT IDT IDT hypo_test, y_test = , Y], feed_dict= , Y: test_label, keep_prob: 1.0}) ) IDT IDT IDT acc1 = , y_real, mode= ) IDT IDT IDT acc2 = , y_real, mode= ) + IDT IDT IDT act1 = , y_test, mode= ) IDT IDT IDT act2 = , y_test, mode= ) IDT IDT IDT + IDT IDT IDT + + IDT IDT sess.run(train, feed_dict= , Y: train_label, keep_prob: 0.8}) )""" +nv_rnn = models.NvRNN() +rnn_train = , None] - 1).astype(float) )X1 = , shape= , 21]) )X2 = , shape= , 21]) )X3 = , shape= , 21]) )X4 = , shape= , 21]) )X5 = , shape= , 21]) )Y_data = , shape= )Y = , depth= )keep_prob = tf.placeholder(tf.float32) +h_rnn = , X2, X3, X4, X5, keep_prob= )cost_rnn = , logits= )train = )in_x1 = rnn_train[:, (0)] +in_x2 = rnn_train[:, (1)] +in_x3 = rnn_train[:, (2)] +in_x4 = rnn_train[:, (3)] +in_x5 = rnn_train[:, (4)] +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT y_train = train_label.reshape([-1]) + IDT for i in range(10000): + IDT IDT if i % 10: + IDT IDT IDT hypo, y_real = , Y], feed_dict= , X2: in_x2, X3: in_x3, X4: in_x4, X5: in_x5, Y_data: y_train, keep_prob: 1.0}) ) IDT IDT IDT acc = , y_real, mode= , one_hot= ) IDT IDT IDT + IDT IDT sess.run(train, feed_dict= , X2: in_x2, X3: in_x3, X4: in_x4, X5: in_x5, Y_data: y_train, keep_prob: 0.8}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +import numpy as np +batch = 100 +X = tf.placeholder(tf.float32, [None, 784]) +X_reshaped = tf.reshape(X, [-1, 28, 28, 1]) +Y = tf.placeholder(tf.float32, [None, 10]) +mnist = , one_hot= ) + +def trainown(X, kernel, kernel2, kernel3, kernel4, kernel5, droupout_prob): + IDT ksize = [1, 2, 2, 1] + IDT strides = [1, 1, 1, 1] + IDT strides2 = [1, 2, 2, 1] + IDT filter1 = , stddev= ) IDT layer1_conv = , filter1, strides= , padding= ) IDT layer1_maxpool = , ksize= , strides= , 2, 2, 1], padding= ) IDT filter2 = , stddev= ) IDT layer2_conv = , filter2, strides= , padding= ) IDT layer2_maxpool = , ksize= , strides= , padding= ) IDT layer2 = tf.nn.dropout(layer2_maxpool, droupout_prob) + IDT filter3 = , stddev= ) IDT layer3_conv = , filter3, strides= , padding= ) IDT layer3_maxpool = , ksize= , strides= , padding= ) IDT layer3 = tf.nn.dropout(layer3_maxpool, droupout_prob) + IDT filter4 = , stddev= ) IDT shapestraight = [-1, 4 * 4 * 128] + IDT layer4_shaping = tf.reshape(layer3, shapestraight) + IDT layer4drop = tf.nn.dropout(layer4_shaping, droupout_prob) + IDT layer4 = tf.nn.relu(tf.matmul(layer4drop, filter4)) + IDT filter5 = , stddev= ) IDT layer5drop = tf.nn.dropout(layer4, droupout_prob) + IDT output = tf.nn.relu(tf.matmul(layer5drop, filter5)) + IDT return output + + +kernel = [3, 3, 1, 32] +kernel2 = [3, 3, 32, 64] +kernel3 = [3, 3, 64, 128] +kernel4 = [4 * 4 * 128, 500] +kernel5 = [500, 10] +droupout_prob = tf.placeholder(tf.float32) +train_layer = trainown(X_reshaped, kernel, kernel2, kernel3, kernel4, kernel5, droupout_prob) +Y_ = tf.nn.softmax(train_layer) +correct = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +cost = , labels= )optimize = tf.train.GradientDescentOptimizer(0.005).minimize(cost) +init = tf.initialize_all_variables() +with tf.Session() as session: + IDT session.run(init) + IDT for ep in range(10): + IDT IDT for i in range(int(len(mnist.train.labels) / batch)): + IDT IDT IDT trainimg, trainlbl = ) IDT IDT IDT data = {X: trainimg, Y: trainlbl, droupout_prob: 0.5} + IDT IDT IDT session.run(optimize, feed_dict= ) IDT data = {X: mnist.test.images, Y: mnist.test.labels, droupout_prob: 1} + IDT + IDT ) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from SpeechDataUtils import SpeechDataSet +timit_database = TimitDatabase('G:\\TIMIT') +max_input_sequence_length = timit_database.get_max_mfcc_features_length() +max_output_sequence_length = timit_database.get_max_phonemes_length() +dictionary_size = timit_database.phonemes_dictionary_size +train_data = timit_database.train_dataset +eval_data = timit_database.test_dataset +input_placeholder = , shape= , max_input_sequence_length, 40]) )input_lengths_placeholder = , shape= )output_placeholder = tf.sparse_placeholder(tf.int32) +output_lengths_placeholder = , shape= )cell = tf.nn.rnn_cell.LSTMCell(128) +cell = tf.nn.rnn_cell.MultiRNNCell([cell] * 2) +rnn, _ = , input_placeholder, input_lengths_placeholder, dtype= )rnn = , shape= , max_input_sequence_length * 128]) )weights = , 256], stddev= )biases = )layer = tf.matmul(rnn, weights) + biases +layer = tf.nn.relu(layer) +weights = , max_output_sequence_length * dictionary_size], stddev= )biases = )layer = tf.matmul(layer, weights) + biases +layer = tf.nn.relu(layer) +logits = , shape= , max_output_sequence_length, dictionary_size]) )logits_timemajor = tf.transpose(logits, [1, 0, 2]) +loss = tf.nn.ctc_loss(logits_timemajor, output_placeholder, output_lengths_placeholder) +loss = tf.reduce_mean(loss) +optimizer = tf.train.AdamOptimizer() +train = optimizer.minimize(loss) +session_config = tf.ConfigProto() +session_config.gpu_options.allow_growth = True +with tf.Session(config= ) IDT init = tf.global_variables_initializer() + IDT session.run(init) + IDT for i in tqdm(range(1000)): + IDT IDT batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(11, False) + IDT IDT tmp = [] + IDT IDT for j in range(len(batch_outputs)): + IDT IDT IDT tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + IDT IDT batch_outputs = np.array(tmp) + IDT IDT batch_outputs = SpeechDataSet.tokens_for_sparse(batch_outputs) + IDT IDT feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths, output_placeholder: batch_outputs, output_lengths_placeholder: batch_output_lengths} + IDT IDT session.run(train, feed_dict= ) IDT IDT if i % 100 == 0: + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT + IDT batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(1, False) + IDT feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths} + IDT test = , feed_dict= ) IDT test = session.run(tf.transpose(test, [1, 0, 2])) + IDT decoded, prob = tf.nn.ctc_beam_search_decoder(test, batch_output_lengths, 100, 5) + IDT decoded = session.run(decoded) + IDT prob = session.run(prob) + IDT results = [] + IDT for decoded_path in decoded: + IDT IDT phonemes_ids = decoded_path.values + IDT IDT result_words = '' + IDT IDT for idx in phonemes_ids: + IDT IDT IDT word = timit_database.id_to_phoneme_dictionary[idx] + IDT IDT IDT if word != '': + IDT IDT IDT IDT result_words += word + ' ' + IDT IDT results += [result_words] + IDT target = batch_outputs[0] + IDT target_words = '' + IDT for idx in target: + IDT IDT word = timit_database.id_to_phoneme_dictionary[idx] + IDT IDT if word != '': + IDT IDT IDT target_words += word + ' ' + IDT + IDT + IDT + IDT + IDT + IDT for result_sentence in results: + IDT IDT + IDT + IDT + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import load_policy +import math +import logz +import time + + +class Config(object): + IDT n_features = 17 + IDT n_classes = 6 + IDT dropout = 0.5 + IDT hidden_size_1 = 128 + IDT hidden_size_2 = 256 + IDT hidden_size_3 = 64 + IDT batch_size = 256 + IDT lr = 0.0005 + IDT itera = 20 + IDT train_itera = 20 + IDT envname = 'Walker2d-v1' + IDT max_steps = 1000 + + +class NN(object): + + IDT def add_placeholders(self): + IDT IDT self.input_placeholder = , shape= , Config.n_features), name= ) IDT IDT self.labels_placeholder = , shape= , Config.n_classes), name= ) IDT IDT self.dropout_placeholder = , name= ) IDT IDT self.is_training = tf.placeholder(tf.bool) + + IDT def create_feed_dict(self, inputs_batch, labels_batch= , dropout= , is_training= ) IDT IDT if labels_batch is None: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT else: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT return feed_dict + + IDT def add_prediction_op(self): + IDT IDT self.global_step = tf.Variable(0) + IDT IDT with tf.name_scope('layer1'): + IDT IDT IDT hidden1 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer2'): + IDT IDT IDT hidden2 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer3'): + IDT IDT IDT hidden3 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('output'): + IDT IDT IDT pred = , num_outputs= , activation_fn= ) IDT IDT return pred + + IDT def add_loss_op(self, pred): + IDT IDT loss = , labels= ) IDT IDT tf.summary.scalar('loss', loss) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + IDT IDT with tf.control_dependencies(extra_update_ops): + IDT IDT IDT learning_rate = , self.global_step, 1000, 0.8, staircase= ) IDT IDT IDT train_op = , global_step= ) IDT IDT return train_op + + IDT def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + IDT IDT feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + IDT IDT rs, _, loss = , self.train_op, self.loss], feed_dict= ) IDT IDT train_writer.add_summary(rs, i) + IDT IDT return loss + + IDT def __init__(self, config): + IDT IDT self.config = config + IDT IDT self.build() + + IDT def fit(self, sess, train_x, train_y): + IDT IDT loss = self.train_on_batch(sess, train_x, train_y) + + IDT def build(self): + IDT IDT with tf.name_scope('inputs'): + IDT IDT IDT self.add_placeholders() + IDT IDT with tf.name_scope('predict'): + IDT IDT IDT self.pred = self.add_prediction_op() + IDT IDT with tf.name_scope('loss'): + IDT IDT IDT self.loss = self.add_loss_op(self.pred) + IDT IDT with tf.name_scope('train'): + IDT IDT IDT self.train_op = self.add_training_op(self.loss) + + IDT def get_pred(self, sess, inputs_batch): + IDT IDT feed = , dropout= , is_training= ) IDT IDT p = , feed_dict= ) IDT IDT return p + + +def load(path): + IDT all = np.load(path) + IDT X = all['arr_0'] + IDT y = all['arr_1'] + IDT y1 = y.reshape(y.shape[0], y.shape[2]) + IDT return X, y1 + + +def run_env(env, nn, session): + IDT obs = env.reset() + IDT done = False + IDT totalr = 0.0 + IDT steps = 0 + IDT observations = [] + IDT while not done: + IDT IDT action = nn.get_pred(session, obs[(None), :]) + IDT IDT observations.append(obs) + IDT IDT obs, r, done, _ = env.step(action) + IDT IDT totalr += r + IDT IDT steps += 1 + IDT IDT if steps >= Config.max_steps: + IDT IDT IDT break + IDT return totalr, observations + + +def shuffle(X_train, y_train): + IDT training_data = , y_train), axis= ) IDT np.random.shuffle(training_data) + IDT X = training_data[:, :-Config.n_classes] + IDT y = training_data[:, -Config.n_classes:] + IDT return X, y + + +def main(): + IDT PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + IDT train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + IDT policy_path = os.path.join(PROJECT_ROOT, 'experts/' + Config.envname + '.pkl') + IDT train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + IDT logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_DA_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + IDT X_train, y_train = load(train_path) + IDT + IDT + IDT with tf.Graph().as_default(): + IDT IDT config = Config() + IDT IDT nn = NN(config) + IDT IDT init = tf.global_variables_initializer() + IDT IDT saver = , keep_checkpoint_every_n_hours= ) IDT IDT + IDT IDT policy_fn = load_policy.load_policy(policy_path) + IDT IDT + IDT IDT with tf.Session() as session: + IDT IDT IDT merged = tf.summary.merge_all() + IDT IDT IDT train_writer = tf.summary.FileWriter(train_log_path, session.graph) + IDT IDT IDT session.run(init) + IDT IDT IDT coord = tf.train.Coordinator() + IDT IDT IDT threads = tf.train.start_queue_runners(session, coord) + IDT IDT IDT for j in tqdm.tqdm(range(Config.itera)): + IDT IDT IDT IDT X_train, y_train = shuffle(X_train, y_train) + IDT IDT IDT IDT i = 0 + IDT IDT IDT IDT try: + IDT IDT IDT IDT IDT for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + IDT IDT IDT IDT IDT IDT offset = i * Config.batch_size % X_train.shape[0] + IDT IDT IDT IDT IDT IDT batch_x = X_train[offset:offset + Config.batch_size, :] + IDT IDT IDT IDT IDT IDT batch_y = y_train[offset:offset + Config.batch_size] + IDT IDT IDT IDT IDT IDT loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + IDT IDT IDT IDT IDT IDT i += 1 + IDT IDT IDT IDT IDT + IDT IDT IDT IDT except tf.errors.OutOfRangeError: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT finally: + IDT IDT IDT IDT IDT coord.request_stop() + IDT IDT IDT IDT coord.join(threads) + IDT IDT IDT IDT observations = [] + IDT IDT IDT IDT actions = [] + IDT IDT IDT IDT env = gym.make(Config.envname) + IDT IDT IDT IDT for _ in range(10): + IDT IDT IDT IDT IDT _, o = run_env(env, nn, session) + IDT IDT IDT IDT IDT observations.extend(o) + IDT IDT IDT IDT IDT action = policy_fn(o) + IDT IDT IDT IDT IDT actions.extend(action) + IDT IDT IDT IDT new_x = np.array(observations) + IDT IDT IDT IDT new_y = np.array(actions) + IDT IDT IDT IDT X_train = np.concatenate((X_train, new_x)) + IDT IDT IDT IDT y_train = np.concatenate((y_train, new_y)) + IDT IDT IDT IDT + IDT IDT IDT IDT rollouts = 20 + IDT IDT IDT IDT returns = [] + IDT IDT IDT IDT for _ in range(rollouts): + IDT IDT IDT IDT IDT totalr, _ = run_env(env, nn, session) + IDT IDT IDT IDT IDT returns.append(totalr) + IDT IDT IDT IDT + IDT IDT IDT IDT logz.log_tabular('Iteration', j) + IDT IDT IDT IDT logz.log_tabular('AverageReturn', np.mean(returns)) + IDT IDT IDT IDT logz.log_tabular('StdReturn', np.std(returns)) + IDT IDT IDT IDT logz.dump_tabular() + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +from __future__ import division +import imageio +import matplotlib.pyplot as plt +import numpy as np +import sys +import tensorflow as tf +from six.moves import cPickle as pickle +import random +image_size = 28 +pickle_file = '../Dataset/cleanShuffledMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT a = pickle.load(f) +train_labels = a['train_labels'] +test_labels = a['test_labels'] +validate_labels = a['valid_labels'] +train_dataset = a['train_dataset'] +test_dataset = a['test_dataset'] +validate_dataset = a['valid_dataset'] +lst = ['I', 'C', 'J', 'F', 'B', 'H', 'A', 'E', 'D', 'G'] + + +def flattenAndOneHot(Dataset, labels): + IDT Dataset = Dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT one_hot = np.zeros((len(labels), 10)) + IDT one_hot[np.arange(len(labels)), labels] = 1 + IDT return Dataset, one_hot + + +train_dataset, train_labels = flattenAndOneHot(train_dataset, train_labels) +test_dataset, test_labels = flattenAndOneHot(test_dataset, test_labels) +validate_dataset, validate_labels = flattenAndOneHot(validate_dataset, validate_labels) +num_category = 10 + + +def Accuracy(prediction1, labels1): + IDT pred1 = np.argmax(prediction1, 1) + IDT label1 = np.argmax(labels1, 1) + IDT acc = ) IDT return acc + + +def SGDRegularise(): + IDT batchsize = 200 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_labels = , shape= , num_category)) ) IDT IDT tf_train_data = , shape= , image_size * image_size)) ) IDT IDT beta_regul = tf.placeholder(tf.float32) + IDT IDT tf_validate = tf.constant(validate_dataset) + IDT IDT tf_test = tf.constant(test_dataset) + IDT IDT Weight = tf.Variable(tf.truncated_normal([image_size * image_size, num_category])) + IDT IDT bias = tf.Variable(tf.zeros(num_category)) + IDT IDT mat = tf.matmul(tf_train_data, Weight) + bias + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(mat) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_validate, Weight) + bias) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test, Weight) + bias) + IDT noSteps = 900 + IDT lossArray = np.zeros(1) + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(noSteps): + IDT IDT IDT offset = random.randint(1, train_dataset.shape[0] - batchsize) + IDT IDT IDT batch_data = train_dataset[offset:offset + batchsize, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batchsize, :] + IDT IDT IDT feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT IDT _, predictions, l = , train_prediction, loss], feed_dict= ) IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT lossArray = np.concatenate((lossArray, [l])) + IDT IDT IDT IDT acc = Accuracy(predictions, batch_labels) + IDT IDT IDT IDT + IDT IDT IDT IDT accv = , validate_labels) ) IDT IDT IDT IDT + IDT IDT x = np.arange(300, 1000, 100) + IDT IDT plt.plot(x, lossArray[3:], linewidth= ) IDT IDT plt.show() + IDT IDT , test_labels)) + IDT IDT session.close() + + +def SGDReluRegulariseDemo(): + IDT batchsize = 200 + IDT middle_layerSize = 1024 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_labels = , shape= , num_category)) ) IDT IDT tf_train_data = , shape= , image_size * image_size)) ) IDT IDT beta_regul = tf.placeholder(tf.float32) + IDT IDT tf_validate = tf.constant(validate_dataset) + IDT IDT tf_test = tf.constant(test_dataset) + IDT IDT Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + IDT IDT bias = tf.Variable(tf.zeros(middle_layerSize)) + IDT IDT WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + IDT IDT biasNext = tf.Variable(tf.zeros(num_category)) + IDT IDT layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + IDT IDT mat = tf.matmul(layer, WeightNext) + biasNext + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(mat) + IDT IDT layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + IDT IDT layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + IDT noSteps = 800 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(noSteps): + IDT IDT IDT offset = random.randint(1, train_dataset.shape[0] - batchsize) + IDT IDT IDT batch_data = train_dataset[offset:offset + batchsize, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batchsize, :] + IDT IDT IDT feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT IDT _, predictions, l = , train_prediction, loss], feed_dict= ) IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT acc = Accuracy(predictions, batch_labels) + IDT IDT IDT IDT + IDT IDT IDT IDT acc = , validate_labels) ) IDT IDT IDT IDT + IDT IDT , test_labels)) + IDT IDT session.close() + + +def Dropout(): + IDT batchsize = 200 + IDT middle_layerSize = 1024 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_labels = , shape= , num_category)) ) IDT IDT tf_train_data = , shape= , image_size * image_size)) ) IDT IDT tf_validate = tf.constant(validate_dataset) + IDT IDT tf_test = tf.constant(test_dataset) + IDT IDT Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + IDT IDT bias = tf.Variable(tf.zeros(middle_layerSize)) + IDT IDT WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + IDT IDT biasNext = tf.Variable(tf.zeros(num_category)) + IDT IDT layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + IDT IDT drop = tf.nn.dropout(layer, 0.5) + IDT IDT mat = tf.matmul(drop, WeightNext) + biasNext + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(mat) + IDT IDT layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + IDT IDT layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + IDT noSteps = 5000 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(noSteps): + IDT IDT IDT offset = step * batchsize % (train_dataset.shape[0] - batchsize) + IDT IDT IDT batch_data = train_dataset[offset:offset + batchsize, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batchsize, :] + IDT IDT IDT feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, predictions, l = , train_prediction, loss], feed_dict= ) IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT acc = Accuracy(predictions, batch_labels) + IDT IDT IDT IDT + IDT IDT IDT IDT acc = , validate_labels) ) IDT IDT IDT IDT + IDT IDT , test_labels)) + IDT IDT session.close() + + +Dropout() + + +def DeepDropout(): + IDT batchsize = 200 + IDT middle_layerSize = 1024 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_labels = , shape= , num_category)) ) IDT IDT tf_train_data = , shape= , image_size * image_size)) ) IDT IDT tf_validate = tf.constant(validate_dataset) + IDT IDT tf_test = tf.constant(test_dataset) + IDT IDT Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + IDT IDT bias = tf.Variable(tf.zeros(middle_layerSize)) + IDT IDT WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + IDT IDT biasNext = tf.Variable(tf.zeros(num_category)) + IDT IDT layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + IDT IDT drop = tf.nn.dropout(layer, 0.5) + IDT IDT mat = tf.matmul(drop, WeightNext) + biasNext + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(mat) + IDT IDT layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + IDT IDT layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + IDT noSteps = 5000 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(noSteps): + IDT IDT IDT offset = step * batchsize % (train_dataset.shape[0] - batchsize) + IDT IDT IDT batch_data = train_dataset[offset:offset + batchsize, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batchsize, :] + IDT IDT IDT feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, predictions, l = , train_prediction, loss], feed_dict= ) IDT IDT IDT if step % 100 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT acc = Accuracy(predictions, batch_labels) + IDT IDT IDT IDT + IDT IDT IDT IDT acc = , validate_labels) ) IDT IDT IDT IDT + IDT IDT , test_labels)) + IDT IDT session.close() +import matplotlib.pyplot as plt +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regular = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT loss += beta_regular * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.01} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regular = tf.placeholder(tf.float32) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT hidden_1 = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + IDT loss = , logits= ) IDT loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regular = tf.placeholder(tf.float32) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT hidden_1 = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + IDT loss = , logits= ) IDT loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +num_bacthes = 3 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_bacthes + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regular = tf.placeholder(tf.float32) + IDT keep_prob = tf.placeholder(tf.float32) + IDT weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + IDT biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + IDT weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + IDT biases_2 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT hidden_1 = tf.nn.relu(logits_1) + IDT drop_1 = tf.nn.dropout(hidden_1, keep_prob) + IDT logits_2 = tf.matmul(drop_1, weights_2) + biases_2 + IDT loss = , logits= ) IDT loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +num_bacthes = 3 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_bacthes + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.001, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_layer_nodes_1 = 1024 +hidden_layer_nodes_2 = 512 +hidden_layer_nodes_3 = 256 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regular = tf.placeholder(tf.float32) + IDT keep_prob = tf.placeholder(tf.float32) + IDT global_step = tf.Variable(0) + IDT weights_1 = , hidden_layer_nodes_1], stddev= ) IDT biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes_1])) + IDT weights_2 = , hidden_layer_nodes_2], stddev= ) IDT biases_2 = tf.Variable(tf.zeros([hidden_layer_nodes_2])) + IDT weights_3 = , hidden_layer_nodes_3], stddev= ) IDT biases_3 = tf.Variable(tf.zeros([hidden_layer_nodes_3])) + IDT weights_4 = , num_labels], stddev= ) IDT biases_4 = tf.Variable(tf.zeros([num_labels])) + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT hidden_1 = tf.nn.relu(logits_1) + IDT logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + IDT hidden_2 = tf.nn.relu(logits_2) + IDT logits_3 = tf.matmul(hidden_2, weights_3) + biases_3 + IDT hidden_3 = tf.nn.relu(logits_3) + IDT drop_3 = tf.nn.dropout(hidden_3, keep_prob) + IDT logits_4 = tf.matmul(drop_3, weights_4) + biases_4 + IDT loss = , logits= ) IDT loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_4)) + IDT learning_rate = , global_step, 10000, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits_4) + IDT valid_hidden_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1) + IDT valid_hidden_2 = tf.nn.relu(tf.matmul(valid_hidden_1, weights_2) + biases_2) + IDT valid_hidden_3 = tf.nn.relu(tf.matmul(valid_hidden_2, weights_3) + biases_3) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_3, weights_4) + biases_4) + IDT test_hidden_1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1) + IDT test_hidden_2 = tf.nn.relu(tf.matmul(test_hidden_1, weights_2) + biases_2) + IDT test_hidden_3 = tf.nn.relu(tf.matmul(test_hidden_2, weights_3) + biases_3) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden_3, weights_4) + biases_4) +num_steps = 20001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.002, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape(-1, image_size * image_size) + IDT zero_labels = np.zeros((len(labels), len(set(labels)))) + IDT zero_labels[np.arange(len(labels)), labels] = 1 + IDT return dataset, zero_labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, prediction = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden1 = 500 +num_hidden2 = 500 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_one = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden1])) + IDT biases_one = tf.Variable(tf.zeros([num_hidden1])) + IDT weights_two = tf.Variable(tf.truncated_normal([num_hidden1, num_labels])) + IDT biases_two = tf.Variable(tf.zeros([num_labels])) + IDT logits_one = tf.matmul(tf_train_dataset, weights_one) + biases_one + IDT layer1 = tf.nn.relu(logits_one) + IDT logits = tf.matmul(layer1, weights_two) + biases_two + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_one) + biases_one), weights_two) + biases_two) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_one) + biases_one), weights_two) + biases_two) +num_steps = 1001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_one = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden1])) + IDT biases_one = tf.Variable(tf.zeros([num_hidden1])) + IDT weights_three = tf.Variable(tf.truncated_normal([num_hidden1, num_hidden2])) + IDT biases_three = tf.Variable(tf.zeros([num_hidden2])) + IDT weights_two = tf.Variable(tf.truncated_normal([num_hidden2, num_labels])) + IDT biases_two = tf.Variable(tf.zeros([num_labels])) + IDT logits_one = tf.matmul(tf_train_dataset, weights_one) + biases_one + IDT layer2 = tf.nn.softmax(logits_one) + IDT logits_two = tf.matmul(layer2, weights_three) + biases_three + IDT layer1 = tf.nn.relu(logits_two) + IDT logits = tf.matmul(layer1, weights_two) + biases_two + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf.nn.softmax(tf.matmul(tf_test_dataset, weights_one) + biases_one), weights_three) + biases_three), weights_two) + biases_two) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf.nn.softmax(tf.matmul(tf_valid_dataset, weights_one) + biases_one), weights_three) + biases_three), weights_two) + biases_two) +num_steps = 30001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W, stride): + IDT return tf.nn.conv2d(x, W, strides= , stride, stride, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +def createNetwork(action_count): + IDT W_conv1 = weight_variable([8, 8, 4, 32]) + IDT b_conv1 = bias_variable([32]) + IDT W_conv2 = weight_variable([4, 4, 32, 64]) + IDT b_conv2 = bias_variable([64]) + IDT W_conv3 = weight_variable([3, 3, 64, 64]) + IDT b_conv3 = bias_variable([64]) + IDT W_fc1 = weight_variable([1600, 512]) + IDT b_fc1 = bias_variable([512]) + IDT W_fc2 = weight_variable([512, action_count]) + IDT b_fc2 = bias_variable([action_count]) + IDT s = tf.placeholder('float', [None, 80, 80, 4]) + IDT h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + IDT h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + IDT h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + IDT h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + IDT readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + IDT return s, readout, h_fc1 + + +class DQNAgent(object): + + IDT def __init__(self, action_count): + IDT IDT self.action_count = action_count + IDT IDT self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + IDT IDT self.action_placeholder = tf.placeholder('float', [None, action_count]) + IDT IDT self.reward_placeholder = tf.placeholder('float', [None]) + IDT IDT self.readout_action = , self.action_placeholder), reduction_indices= ) IDT IDT self.cost = tf.reduce_mean(tf.square(self.reward_placeholder - self.readout_action)) + IDT IDT self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.cost) + + IDT def act(self, session, state): + IDT IDT scores = self.score_actions(session, [state])[0] + IDT IDT return np.argmax(scores) + + IDT def score_actions(self, session, state_batch): + IDT IDT return session.run(self.readout, feed_dict= ) + IDT def train(self, session, reward_batch, action_batch, state_batch): + IDT IDT _, loss = , self.cost], feed_dict= , self.action_placeholder: action_batch, self.state_placeholder: state_batch}) ) IDT IDT return loss +from read_data import * +from time import time +import tensorflow as tf +batch_size = 128 +hidden_nodes = 2048 +hidden_nodes_l2 = 512 +starter_learn_rate = 0.5 +reg_beta = 0.002 +num_steps = 5001 + + +def train_three_layer(X, use_dropout): + IDT y1 = tf.nn.relu(tf.matmul(X, W1) + b1) + IDT y2 = None + IDT if use_dropout: + IDT IDT y1d = tf.nn.dropout(y1, input_keep_prob) + IDT IDT y2 = tf.nn.relu(tf.matmul(y1d, W2) + b2) + IDT else: + IDT IDT y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + IDT y3 = None + IDT if use_dropout: + IDT IDT y2d = tf.nn.dropout(y2, train_keep_prob) + IDT IDT y3 = tf.nn.relu(tf.matmul(y2d, W3) + b3) + IDT else: + IDT IDT y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + IDT return y3 + + +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT input_keep_prob = tf.placeholder('float') + IDT train_keep_prob = tf.placeholder('float') + IDT W1 = , hidden_nodes], stddev= ) IDT b1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT W2 = , hidden_nodes_l2], stddev= ) IDT b2 = tf.Variable(tf.zeros([hidden_nodes_l2])) + IDT W3 = , num_labels], stddev= ) IDT b3 = tf.Variable(tf.zeros([num_labels])) + IDT y_train = train_three_layer(tf_train_dataset, True) + IDT y_valid = train_three_layer(tf_valid_dataset, True) + IDT y_test = train_three_layer(tf_test_dataset, True) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_train, tf_train_labels)) + IDT regularizers = tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + IDT loss += reg_beta * regularizers + IDT loss_summary = tf.scalar_summary('loss', loss) + IDT global_step = tf.Variable(0) + IDT learn_rate = tf.train.exponential_decay(starter_learn_rate, global_step, 500, 0.96) + IDT optimizer = , global_step= ) IDT merged = tf.merge_all_summaries() +train_prediction = tf.nn.softmax(y_train) +valid_prediction = tf.nn.softmax(y_valid) +test_prediction = tf.nn.softmax(y_test) +with tf.Session(graph= ) IDT writer = tf.train.SummaryWriter('/tmp/notmnist_logs', session.graph_def) + IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, input_keep_prob: 0.9, train_keep_prob: 0.5} + IDT IDT _, l, predictions, merged_summary, lr = , loss, train_prediction, merged, learn_rate], feed_dict= ) IDT IDT writer.add_summary(merged_summary, step) + IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT , batch_labels)) + IDT IDT IDT + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT - t0)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +from helpers.layer_helpers import weight_variable, conv2d, flatten, linear, normalized_columns_initializer, categorical_sample +from helpers.custom_rnn_cells import GridPredictionLSTMCell, PredictionLSTMStateTuple +""" +The following class it be used when the tasks are (1) A3C and (2) grid pixel intensity change predictions, +and there is a feedback of predictions to the LSTM cell. +""" + + +class GridPredictionLSTMPolicy(object): + + IDT def __init__(self, ob_space, ac_space, replay_size= , grid_size= ) IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.bs = ) IDT IDT self.replay_memory = [] + IDT IDT self.replay_size = replay_size + IDT IDT self.grid_size = grid_size + IDT IDT x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + IDT IDT x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + IDT IDT x = flatten(x) + IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT x = , values= , self.action, self.reward]) ) IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT size = 256 + IDT IDT lstm = , state_is_tuple= , ac_space= , grid_size= ) IDT IDT self.state_size = lstm.state_size + IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT c_init = np.zeros((1, lstm.state_size.c), np.float32) + IDT IDT h_init = np.zeros((1, lstm.state_size.h), np.float32) + IDT IDT pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + IDT IDT self.state_init = [c_init, h_init, pred_init] + IDT IDT c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + IDT IDT h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + IDT IDT pred_in = tf.placeholder(tf.float32, [1, lstm.state_size.pred]) + IDT IDT self.state_in = [c_in, h_in, pred_in] + IDT IDT state_in = PredictionLSTMStateTuple(c_in, h_in, pred_in) + IDT IDT lstm_outputs, lstm_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT lstm_c, lstm_h, lstm_pred = lstm_state + IDT IDT x = tf.reshape(lstm_outputs, [-1, size]) + IDT IDT self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + IDT IDT self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + IDT IDT self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + IDT IDT self.sample = categorical_sample(self.logits, ac_space)[(0), :] + IDT IDT self.predictions = , shape= , grid_size, grid_size, ac_space]) ) IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + IDT IDT self.target_weights = [] + + IDT def get_initial_features(self): + IDT IDT return self.state_init + + IDT def act(self, ob, prev_a, prev_r, c, h, pred): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run([self.sample, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred}) + + IDT def value(self, ob, prev_a, prev_r, c, h, pred): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred})[0] + + IDT def update_replay_memory(self, tuple): + IDT IDT self.replay_memory.append(tuple) + IDT IDT if len(self.replay_memory) > self.replay_size: + IDT IDT IDT self.replay_memory.pop(0) + + IDT def update_target_weights(self): + IDT IDT sess = tf.get_default_session() + IDT IDT self.target_weights = sess.run(self.var_list) + + +""" +The following class it be used when the tasks are (1) A3C and (2) grid pixel intensity change predictions, +and there is NOT a feedback of predictions to the LSTM cell ie this is a replication from the Jaderberg paper. +""" + + +class GridLSTMPolicy(object): + + IDT def __init__(self, ob_space, ac_space, mode= , replay_size= , grid_size= ) IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.replay_memory = [] + IDT IDT self.replay_size = replay_size + IDT IDT self.grid_size = grid_size + IDT IDT self.bs = ) IDT IDT x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + IDT IDT x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + IDT IDT x = flatten(x) + IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT x = , values= , self.action, self.reward]) ) IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT size = 256 + IDT IDT lstm = , state_is_tuple= ) IDT IDT self.state_size = lstm.state_size + IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT c_init = np.zeros((1, lstm.state_size.c), np.float32) + IDT IDT h_init = np.zeros((1, lstm.state_size.h), np.float32) + IDT IDT self.state_init = [c_init, h_init] + IDT IDT c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + IDT IDT h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + IDT IDT self.state_in = [c_in, h_in] + IDT IDT state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) + IDT IDT lstm_outputs, lstm_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT lstm_c, lstm_h = lstm_state + IDT IDT x = tf.reshape(lstm_outputs, [-1, size]) + IDT IDT self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + IDT IDT self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + IDT IDT self.state_out = [lstm_c[:1, :], lstm_h[:1, :]] + IDT IDT self.sample = categorical_sample(self.logits, ac_space)[(0), :] + IDT IDT if mode == 'Grid': + IDT IDT IDT y = linear(x, 32 * (grid_size - 10) * (grid_size - 10), 'auxbranch', normalized_columns_initializer(0.1)) + IDT IDT IDT y = , shape= , grid_size - 10, grid_size - 10, 32]) ) IDT IDT IDT deconv_weights = , 4, ac_space, 32], name= ) IDT IDT IDT self.predictions = , deconv_weights, output_shape= , grid_size, grid_size, ac_space], strides= , 2, 2, 1], padding= ) IDT IDT if mode == 'Features': + IDT IDT IDT pass + IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + IDT IDT self.target_weights = [] + + IDT def get_initial_features(self): + IDT IDT return self.state_init + + IDT def act(self, ob, prev_a, prev_r, c, h): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run([self.sample, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h}) + + IDT def value(self, ob, prev_a, prev_r, c, h): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h})[0] + + IDT def update_replay_memory(self, tuple): + IDT IDT self.replay_memory.append(tuple) + IDT IDT if len(self.replay_memory) > self.replay_size: + IDT IDT IDT self.replay_memory.pop(0) + + IDT def update_target_weights(self): + IDT IDT sess = tf.get_default_session() + IDT IDT self.target_weights = sess.run(self.var_list) + + +""" +Baseline A3C agent policy +""" + + +class A3CLSTMPolicy(object): + + IDT def __init__(self, ob_space, ac_space): + IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + IDT IDT x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + IDT IDT x = flatten(x) + IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT x = , values= , self.action, self.reward]) ) IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT size = 256 + IDT IDT lstm = , state_is_tuple= ) IDT IDT self.state_size = lstm.state_size + IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT c_init = np.zeros((1, lstm.state_size.c), np.float32) + IDT IDT h_init = np.zeros((1, lstm.state_size.h), np.float32) + IDT IDT self.state_init = [c_init, h_init] + IDT IDT c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + IDT IDT h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + IDT IDT self.state_in = [c_in, h_in] + IDT IDT state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) + IDT IDT lstm_outputs, lstm_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT lstm_c, lstm_h = lstm_state + IDT IDT x = tf.reshape(lstm_outputs, [-1, size]) + IDT IDT self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + IDT IDT self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + IDT IDT self.state_out = [lstm_c[:1, :], lstm_h[:1, :]] + IDT IDT self.sample = categorical_sample(self.logits, ac_space)[(0), :] + IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + + IDT def get_initial_features(self): + IDT IDT return self.state_init + + IDT def act(self, ob, prev_a, prev_r, c, h): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run([self.sample, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h}) + + IDT def value(self, ob, prev_a, prev_r, c, h): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h})[0] + + +""" +The following class it be used when the tasks are (1) A3C and (2) convolutional feature predictions, +and there is NOT a feedback of predictions to the LSTM cell ie this is a replication from the Jaderberg paper. +""" + + +class FeatureLSTMPolicy(object): + + IDT def __init__(self, ob_space, ac_space, mode= , replay_size= , grid_size= ) IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.replay_memory = [] + IDT IDT self.replay_size = replay_size + IDT IDT self.grid_size = grid_size + IDT IDT self.bs = ) IDT IDT x = , 16, 'l1', [8, 8], [4, 4], pad= ) IDT IDT x = , 32, 'l2', [4, 4], [2, 2], pad= ) IDT IDT x = flatten(x) + IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT x = , values= , self.action, self.reward]) ) IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT size = 256 + IDT IDT lstm = , state_is_tuple= ) IDT IDT self.state_size = lstm.state_size + IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT c_init = np.zeros((1, lstm.state_size.c), np.float32) + IDT IDT h_init = np.zeros((1, lstm.state_size.h), np.float32) + IDT IDT self.state_init = [c_init, h_init] + IDT IDT c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + IDT IDT h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + IDT IDT self.state_in = [c_in, h_in] + IDT IDT state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) + IDT IDT lstm_outputs, lstm_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT lstm_c, lstm_h = lstm_state + IDT IDT x = tf.reshape(lstm_outputs, [-1, size]) + IDT IDT self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + IDT IDT self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + IDT IDT self.state_out = [lstm_c[:1, :], lstm_h[:1, :]] + IDT IDT self.sample = categorical_sample(self.logits, ac_space)[(0), :] + IDT IDT if mode == 'Grid': + IDT IDT IDT y = linear(x, 32 * (grid_size - 7) * (grid_size - 7), 'auxbranch', normalized_columns_initializer(0.1)) + IDT IDT IDT y = , shape= , grid_size - 7, grid_size - 7, 32]) ) IDT IDT IDT deconv_weights = , 4, ac_space, 32], name= ) IDT IDT IDT self.predictions = , deconv_weights, output_shape= , grid_size, grid_size, ac_space], strides= , 2, 2, 1], padding= ) IDT IDT if mode == 'Features': + IDT IDT IDT y = linear(x, 16 * 9 * 9, 'auxbranch', normalized_columns_initializer(0.1)) + IDT IDT IDT y = , shape= , 9, 9, 16]) ) IDT IDT IDT deconv_weights = , 1, 32 * ac_space, 16], name= ) IDT IDT IDT predictions = , deconv_weights, output_shape= , 9, 9, 32 * ac_space], strides= , 1, 1, 1], padding= ) IDT IDT IDT self.predictions = , shape= , 9, 9, 32, ac_space]) ) IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + IDT IDT self.target_weights = [] + + IDT def get_initial_features(self): + IDT IDT return self.state_init + + IDT def act(self, ob, prev_a, prev_r, c, h): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run([self.sample, self.vf, self.conv_features] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h}) + + IDT def value(self, ob, prev_a, prev_r, c, h): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h})[0] + + IDT def update_replay_memory(self, tuple): + IDT IDT self.replay_memory.append(tuple) + IDT IDT if len(self.replay_memory) > self.replay_size: + IDT IDT IDT self.replay_memory.pop(0) + + IDT def update_target_weights(self): + IDT IDT sess = tf.get_default_session() + IDT IDT self.target_weights = sess.run(self.var_list) +""" +Created on Sun Jul 23 14:33:49 2017 +explore regulation tech + +@author: Yuting Kou +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +import matplotlib.pyplot as plt +""" +1. reload data +""" +data_root = 'D:\\\\data\\\\Yuting\\\\Deep Learning' +pickle_file = os.path.join(data_root, 'notMNIST.pickle') +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 +""" +2. reform data +""" + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +""" +Problem 1: +Introduce and tune L2 regularization for both logistic and neural network models. + + +""" +batch_size = 128 +graph_l2 = tf.Graph() +with graph_l2.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +""" +training model +""" +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT IDT IDT , test_labels)) + + +def tuning_beta(regul_value, graph, name): + IDT for regul in regul_value: + IDT IDT with tf.Session(graph= ) IDT IDT IDT tf.global_variables_initializer().run() + IDT IDT IDT for step in range(num_steps): + IDT IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + IDT IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) + IDT plt.semilogx(regul_value, accuracy_val) + IDT plt.grid(True) + IDT plt.title('Test accuracy by regularization (%s)' % name) + IDT plt.show() + + +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +tuning_beta(regul_val, graph_l2, 'logistic') +""" +neural network reg +""" +num_hidden_nodes = 1024 +batch_size = 128 +graph_nn_l2 = tf.Graph() +with graph_nn_l2.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = [tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])), tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels]))] + IDT biases = [tf.Variable(tf.zeros([num_hidden_nodes])), tf.Variable(tf.zeros([num_labels]))] + IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, weights[0]) + biases[0]) + IDT logits = tf.matmul(hidden_layer, weights[1]) + biases[1] + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights[0]) + biases[0]) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights[1]) + biases[1]) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights[0]) + biases[0]) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights[1]) + biases[1]) +""" +run +""" +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT IDT IDT , test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +tuning_beta(regul_val, graph_nn_l2, '1-layer net') +""" +Problem 2 +Let's demonstrate an extreme case of overfitting. Restrict your training data to just a few batches. What happens? + +logistic +""" +batch_size = 128 +num_hidden_nodes = 1024 +num_steps = 101 +num_batches = 3 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_batches * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT IDT IDT , test_labels)) +""" +Problem 3 +Introduce Dropout on the hidden layer of the neural network. Remember: Dropout should only be introduced during training, not evaluation, otherwise your evaluation results would be stochastic as well. TensorFlow provides nn.dropout() for that, but you have to make sure it's only inserted during training. +What happens to our extreme overfitting case? + +NN +""" +batch_size = 128 +num_hidden_nodes = 1024 +graph_nn = tf.Graph() +with graph_nn.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = [tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])), tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels]))] + IDT biases = [tf.Variable(tf.zeros([num_hidden_nodes])), tf.Variable(tf.zeros([num_labels]))] + IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, weights[0]) + biases[0]) + IDT drop1 = tf.nn.dropout(hidden_layer, 0.5) + IDT logits = tf.matmul(drop1, weights[1]) + biases[1] + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights[0]) + biases[0]) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights[1]) + biases[1]) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights[0]) + biases[0]) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights[1]) + biases[1]) +num_steps = 3001 +batch_size = 128 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +""" +Problem 4: Find Best Performance +""" +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +batch_size = 128 +graph_nn_l2_2 = tf.Graph() +with graph_nn_l2_2.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights = , num_hidden_nodes1], stddev= , tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev= , tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_labels], stddev= ) IDT biases = [tf.Variable(tf.zeros([num_hidden_nodes1])), tf.Variable(tf.zeros([num_hidden_nodes2])), tf.Variable(tf.zeros([num_labels]))] + IDT hidden_layer1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights[0]) + biases[0]) + IDT hidden_layer2 = tf.nn.relu(tf.matmul(hidden_layer1, weights[1]) + biases[1]) + IDT logits = tf.matmul(hidden_layer2, weights[2]) + biases[2] + IDT loss = , logits= ) IDT learning_rate = , global_step, 1000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights[0]) + biases[0]) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights[1]) + biases[1]) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights[2]) + biases[2]) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights[0]) + biases[0]) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights[1]) + biases[1]) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights[2]) + biases[2]) +""" +run +""" +num_steps = 9001 +import random + + +def disp_sample_dataset(dataset, labels): + IDT items = random.sample(range(len(labels)), 8) + IDT for i, item in enumerate(items): + IDT IDT plt.subplot(2, 4, i + 1) + IDT IDT plt.axis('off') + IDT IDT plt.title(pretty_labels[labels[item]]) + IDT IDT plt.imshow(dataset[item]) + + +pretty_labels = {(0): 'A', (1): 'B', (2): 'C', (3): 'D', (4): 'E', (5): 'F', (6): 'G', (7): 'H', (8): 'I', (9): 'J'} +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT IDT IDT , test_labels)) + IDT disp_sample_dataset(test_dataset.reshape(-1, image_size, image_size), np.argmax(test_prediction.eval(), 1)) + IDT plt.show() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import sys +import time +import tensorflow as tf +import nn_datasource as ds +from checkpoint_manager import CheckpointManager +IMAGE_WIDTH = 72 +IMAGE_HEIGHT = 170 +IMAGE_CHANNEL = 1 +EPOCH_LENGTH = 300 +BATCH_SIZE = 100 +LEARNING_RATE = 0.001 +RANDOM_SEED = 2 +WEIGHT_COUNTER = 0 +BIAS_COUNTER = 0 +CONVOLUTION_COUNTER = 0 +POOLING_COUNTER = 0 +sess = None + + +def new_weights(shape): + IDT global WEIGHT_COUNTER + IDT weight = , seed= , name= ) IDT WEIGHT_COUNTER += 1 + IDT return weight + + +def new_biases(length): + IDT global BIAS_COUNTER + IDT bias = , name= ) IDT BIAS_COUNTER += 1 + IDT return bias + + +def new_conv_layer(input, num_input_channels, filter_size, num_filters, pooling= ) IDT global CONVOLUTION_COUNTER + IDT global POOLING_COUNTER + IDT shape = [filter_size, filter_size, num_input_channels, num_filters] + IDT weights = ) IDT biases = ) IDT layer = , filter= , strides= , 1, 1, 1], padding= , name= ) IDT CONVOLUTION_COUNTER += 1 + IDT layer = tf.add(layer, biases) + IDT layer = tf.nn.relu(layer) + IDT if pooling is not None and pooling > 1: + IDT IDT layer = , ksize= , pooling, pooling, 1], strides= , pooling, pooling, 1], padding= , name= ) IDT POOLING_COUNTER += 1 + IDT return layer, weights + + +def flatten_layer(layer): + IDT layer_shape = layer.get_shape() + IDT num_features = layer_shape[1:4].num_elements() + IDT layer_flat = tf.reshape(layer, [-1, num_features]) + IDT return layer_flat, num_features + + +def new_fc_layer(input, num_inputs, num_outputs): + IDT weights = , num_outputs]) ) IDT biases = ) IDT layer = tf.add(tf.matmul(input, weights), biases) + IDT return layer + + +tf.reset_default_graph() +TEST = True +NETWORK_NUMBER = 4 + +input_placeholder = , shape= , IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNEL], name= )output_placeholder = , shape= , 2], name= )layer_conv_1, weights_conv_1 = , num_input_channels= , filter_size= , num_filters= , pooling= )layer_conv_2, weights_conv_2 = , num_input_channels= , filter_size= , num_filters= , pooling= )layer_conv_3, weights_conv_3 = , num_input_channels= , filter_size= , num_filters= , pooling= )layer_conv_4, weights_conv_4 = , num_input_channels= , filter_size= , num_filters= , pooling= )layer_conv_5, weights_conv_5 = , num_input_channels= , filter_size= , num_filters= , pooling= )layer_flat, num_features = flatten_layer(layer_conv_5) +layer_fc_1 = , num_inputs= , num_outputs= )layer_fc_1 = tf.nn.softmax(layer_fc_1) +if TEST is not True: + IDT layer_fc_1 = tf.nn.dropout(layer_fc_1, 0.5) +layer_fc_2 = , num_inputs= , num_outputs= )layer_fc_2 = tf.nn.softmax(layer_fc_2) +if TEST is not True: + IDT layer_fc_2 = tf.nn.dropout(layer_fc_2, 0.5) +layer_output = , num_inputs= , num_outputs= )cross_entropy = , logits= )cost = tf.reduce_mean(cross_entropy) +optimizer = tf.train.AdamOptimizer(LEARNING_RATE).minimize(cost) +predictions = , dimension= )prediction_equalities = , tf.argmax(output_placeholder, dimension= )accuracy = tf.reduce_mean(tf.cast(prediction_equalities, tf.float32)) + + +def train_nn(number, input_placeholder, output_placeholder, accuracy, cost, optimizer): + IDT global TEST + IDT checkpoint_manager = CheckpointManager(number) + IDT init_g = tf.global_variables_initializer() + IDT init_l = tf.local_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init_g) + IDT IDT sess.run(init_l) + IDT IDT checkpoint_manager.on_training_start(ds.DATASET_FOLDER, EPOCH_LENGTH, BATCH_SIZE, LEARNING_RATE, 'AdamOptimizer', True) + IDT IDT for batch_index, batch_images, batch_labels in ds.training_batch_generator(BATCH_SIZE, grayscale= ) IDT IDT IDT ) + IDT IDT IDT for current_epoch in range(EPOCH_LENGTH): + IDT IDT IDT IDT feed = {input_placeholder: batch_images, output_placeholder: batch_labels} + IDT IDT IDT IDT epoch_accuracy, epoch_cost, _ = , cost, optimizer], feed_dict= ) IDT IDT IDT IDT ) + IDT IDT IDT IDT checkpoint_manager.on_epoch_completed() + IDT IDT IDT TEST = True + IDT IDT IDT batch_accuracy_training, batch_cost_training = , cost], feed_dict= ) IDT IDT IDT TEST = False + IDT IDT IDT ) + IDT IDT IDT checkpoint_manager.on_batch_completed(batch_cost_training, batch_accuracy_training) + IDT IDT IDT checkpoint_manager.save_model(sess) + IDT IDT )) + IDT IDT checkpoint_manager.on_training_completed(None) + + +def test_frame(frame): + IDT prediction = tf.argmax(tf.nn.softmax(layer_output), 1) + IDT ) + + +def test_nn(number, input_placeholder, output_placeholder, accuracy, cost): + IDT checkpoint_manager = CheckpointManager(number) + IDT init_g = tf.global_variables_initializer() + IDT init_l = tf.local_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init_g) + IDT IDT sess.run(init_l) + IDT IDT checkpoint_manager.restore_model(sess) + IDT IDT total_accuracy = 0 + IDT IDT total_cost = 0 + IDT IDT batches = None + IDT IDT for batch_index, test_images, test_labels in ds.test_batch_generator(100, grayscale= ) IDT IDT IDT feed = {input_placeholder: test_images, output_placeholder: test_labels} + IDT IDT IDT test_accuracy, test_cost = , cost], feed_dict= ) IDT IDT IDT ) + IDT IDT IDT total_accuracy += test_accuracy + IDT IDT IDT total_cost += test_cost + IDT IDT IDT batches = batch_index + IDT IDT overall_accuracy = total_accuracy / (batches + 1) + IDT IDT overall_cost = total_cost / (batches + 1) + IDT IDT ) + IDT IDT return overall_accuracy, overall_cost + + +def main(): + IDT pass + + +def init(): + IDT checkpoint_manager = CheckpointManager(NETWORK_NUMBER) + IDT init_g = tf.global_variables_initializer() + IDT init_l = tf.local_variables_initializer() + IDT with tf.Session() as ses: + IDT IDT ses.run(init_g) + IDT IDT ses.run(init_l) + IDT IDT checkpoint_manager.restore_model(ses) + IDT IDT sess = ses + + +if __name__ == '__main__': + IDT main() +import tensorflow as tf +from ntmnist import dataset +import numpy as np +pickle_file = 'notMNIST.pickle' +data = dataset(pickle_file) + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +train_subset = 200000 +image_size = 28 +num_labels = 10 +hn1_num = 1024 +hn2_num = 512 +batch_size = 128 +reg_beta = 0.005 +data.set_length(train_subset) +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT keep_prob = tf.placeholder('float') + IDT with tf.name_scope('hidden1') as scope_1: + IDT IDT weights_1 = , hn1_num]), name= ) IDT IDT biases_1 = , name= ) IDT IDT ) + IDT IDT hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + IDT keep_prob = tf.placeholder('float') + IDT hidden_layer_drop1 = tf.nn.dropout(hidden1, keep_prob) + IDT with tf.name_scope('hidden2') as scope_2: + IDT IDT weights_2 = , hn2_num]), name= ) IDT IDT biases_2 = , name= ) IDT IDT ) + IDT IDT hidden2 = tf.nn.relu(tf.matmul(hidden_layer_drop1, weights_2) + biases_2) + IDT hidden_layer_drop2 = tf.nn.dropout(hidden2, keep_prob) + IDT with tf.name_scope('out') as scope_3: + IDT IDT weights = , num_labels]), name= ) IDT IDT biases = , name= ) IDT IDT ) + IDT IDT logits = tf.matmul(hidden_layer_drop2, weights) + biases + IDT loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss1 + reg_beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_1)) + IDT optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) +num_steps = 30001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT batch_data, batch_labels = data.next_batch(batch_size) + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, loss_o, l, predictions = , loss1, loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT , data.valid_labels)) + IDT IDT IDT + IDT IDT IDT .eval(), tf.nn.l2_loss(weights_2).eval()) + IDT IDT IDT feed_dict = {tf_train_dataset: data.test_dataset, keep_prob: 1.0} + IDT IDT IDT predictions = , feed_dict= ) IDT IDT IDT ) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import matplotlib.pyplot as plt +import tensorflow as tf +import os +from tensorflow.python.ops.parallel_for.gradients import jacobian +np.random.seed(10) +file_path = os.path.dirname(os.getcwd()) + '/Data/SINDY_Data.npy' +states = np.load(file_path)[:-1, :] +data_tsteps = np.shape(states)[0] +state_len = np.shape(states)[1] +cft_max = , axis= )cft_min = , axis= )states = (-1.0 + 2.0 * (states[:, :] - cft_min[(None), :]) / (cft_max[(None), :] - cft_min[(None), :])) * (1.0 + np.random.randn() * 0.02) +seq_num = 20 +num_batch = 1000 +total_size = np.shape(states)[0] - int(seq_num) +input_seq = , seq_num, state_len)) )output_seq = , seq_num - 1, state_len)) )for bnum in range(num_batch): + IDT t = , high= ) IDT input_seq[(bnum), :, :] = ) IDT output_seq[(bnum), :, :] = )lstm_session = tf.Session() +lstm_neurons = 32 +weights = , lstm_neurons], seed= , mean= , stddev= , 'fg': tf.Variable(tf.truncated_normal(shape= , lstm_neurons], seed= , mean= , stddev= , 'og': tf.Variable(tf.truncated_normal(shape= , lstm_neurons], seed= , mean= , stddev= ) IDT 4, mean= , stddev= , 'op': tf.Variable(tf.truncated_normal(shape= , state_len], seed= , mean= , stddev= )biases = , seed= , mean= , stddev= , 'fg': tf.Variable(tf.truncated_normal(shape= , seed= , mean= , stddev= , 'og': tf.Variable(tf.truncated_normal(shape= , seed= , mean= , stddev= , 'iz': tf.Variable(tf.truncated_normal(shape= , seed= , mean= , stddev= ) IDT tf.truncated_normal(shape= , seed= , mean= , stddev= )placeholder_batch_size = num_batch // seq_num +ph_input = tf.placeholder(tf.float32, [placeholder_batch_size, seq_num, state_len]) +ph_output = tf.placeholder(tf.float32, [placeholder_batch_size, seq_num - 1, state_len]) +ig = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['ig']), biases['ig'])) +fg = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['fg']), biases['fg'])) +og = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['og']), biases['og'])) +iz = tf.nn.tanh(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['iz']), biases['iz'])) +internal_state_temp = tf.multiply(ig, iz) +internal_state_curr = tf.slice(internal_state_temp, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state_prev = tf.slice(internal_state_temp, [0, 0, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +fg_c = tf.slice(fg, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +og_c = tf.slice(og, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state = tf.add(tf.multiply(fg_c, internal_state_prev), internal_state_curr) +ht = tf.multiply(og_c, tf.nn.tanh(internal_state)) +output_seq_pred = tf.add(tf.einsum('ijk,kl->ijl', ht, weights['op']), biases['op']) +loss_temp = tf.reduce_mean(tf.squared_difference(output_seq_pred, ph_output)) +J = jacobian(loss_temp, ph_input) +J = , shape= , seq_num * state_len]) )loss = loss_temp +optimizer = )grads_and_vars = optimizer.compute_gradients(loss) +backprop = optimizer.apply_gradients(grads_and_vars) +init_op = tf.global_variables_initializer() +lstm_session.run(init_op) +saver = tf.train.Saver() +current_pwd = os.getcwd() + '/' +num_epochs = int(10 * num_batch) +best_loss = np.Inf +with lstm_session.as_default(): + IDT bnum = 0 + IDT for i in range(num_epochs): + IDT IDT start_id = bnum * placeholder_batch_size + IDT IDT end_id = (bnum + 1) * placeholder_batch_size + IDT IDT input_batch = input_seq[start_id:end_id, :, :] + IDT IDT output_batch = output_seq[start_id:end_id, :, :] + IDT IDT bnum = bnum + 1 + IDT IDT val_num = , high= ) IDT IDT val_start_id = val_num * placeholder_batch_size + IDT IDT val_end_id = (val_num + 1) * placeholder_batch_size + IDT IDT val_input_batch = input_seq[val_start_id:val_end_id, :, :] + IDT IDT val_output_batch = output_seq[val_start_id:val_end_id, :, :] + IDT IDT if bnum == seq_num: + IDT IDT IDT bnum = 0 + IDT IDT backprop.run(feed_dict= , ph_output: output_batch}) ) IDT IDT mse_batch = , ph_output: output_batch})) ) IDT IDT mse_batch_val = , ph_output: val_output_batch})) ) IDT IDT + IDT IDT if mse_batch_val < best_loss: + IDT IDT IDT save_path = saver.save(lstm_session, current_pwd) + IDT IDT IDT + IDT IDT IDT best_loss = mse_batch_val +eval_session = tf.Session() +placeholder_batch_size = 1 +ph_input = tf.placeholder(tf.float32, [placeholder_batch_size, seq_num, state_len]) +ig = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['ig']), biases['ig'])) +fg = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['fg']), biases['fg'])) +og = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['og']), biases['og'])) +iz = tf.nn.tanh(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['iz']), biases['iz'])) +internal_state_temp = tf.multiply(ig, iz) +internal_state_curr = tf.slice(internal_state_temp, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state_prev = tf.slice(internal_state_temp, [0, 0, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +fg_c = tf.slice(fg, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +og_c = tf.slice(og, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state = tf.add(tf.multiply(fg_c, internal_state_prev), internal_state_curr) +ht = tf.multiply(og_c, tf.nn.tanh(internal_state)) +output_seq_pred = tf.add(tf.einsum('ijk,kl->ijl', ht, weights['op']), biases['op']) +init_op = tf.global_variables_initializer() +eval_session.run(init_op) +saver = tf.train.Saver() +save_path = os.getcwd() + '/' +saver.restore(eval_session, save_path) + +state_tracker = , data_tsteps, state_len), dtype= )state_tracker[(0), 0:seq_num, :] = states[0:seq_num, :] +with eval_session.as_default(): + IDT for t in range(seq_num, data_tsteps): + IDT IDT lstm_input = state_tracker[:, t - seq_num:t, :] + IDT IDT output_state = ) IDT IDT state_tracker[(0), (t), :] = , (-1) )fig, ax = , ncols= )mnum = 0 +for j in range(2): + IDT for i in range(2): + IDT IDT ax[i, j].plot(states[:, (mnum)], label= , color= ) IDT IDT ax[i, j].plot(state_tracker[(0), :, (mnum)], label= , color= ) IDT IDT mnum = mnum + 1 +plt.legend() +plt.show() +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +from time import time +from read_data import * +train_subset = 100000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +""" +with tf.Session(graph= ) + + + tf.initialize_all_variables().run() + + t0 = time() + for step in range(num_steps): + IDT + IDT + IDT + IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT if (step % 100 = ) IDT ) + IDT print('Training accuracy: %.1f%%' % accuracy( + IDT IDT predictions, train_labels[:train_subset, :])) + IDT + IDT + IDT + IDT print('Validation accuracy: %.1f%%' % accuracy( + IDT IDT valid_prediction.eval(), valid_labels)) + print(' +') + - t0, 3))) + , test_labels)) +""" + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT - t0, 3))) + IDT , test_labels)) + + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT output1 = tf.matmul(data, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +from __future__ import print_function, division +import tensorflow as tf +import numpy as np +from tf_utils import * + + +class Network(object): + + IDT def __init__(self, config): + IDT IDT self.state_dim = config.state_dim + IDT IDT self.state_shape = config.state_shape + IDT IDT self.state_channels = self.state_shape[-1] + IDT IDT self.action_num = config.action_num + IDT IDT self.weight_decay = config.weight_decay + IDT IDT self.lr = config.lr + IDT IDT self.state = , shape= , self.state_dim]) ) IDT IDT self.q_target = , shape= ) IDT IDT self.actions = , shape= , self.action_num]) ) IDT IDT self.q_difference = , shape= , self.action_num]) ) IDT IDT self.state2 = , shape= , self.state_dim]) ) IDT IDT self.q_predict, self.action_predict = self.build_network() + IDT IDT self.q_predict_with_action = , axis= ) IDT IDT losses = tf.square(self.q_predict_with_action - self.q_target) + IDT IDT self.l2_loss = tf.reduce_mean(losses) + IDT IDT self.w_norm = tf.add_n(tf.get_collection('weight_norms')) + IDT IDT self.a_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.action_predict, self.actions)) + IDT IDT self.objective = self.l2_loss + self.a_loss + self.weight_decay * self.w_norm + IDT IDT self.train_step = tf.train.AdamOptimizer(self.lr).minimize(self.objective) + + IDT def build_network(self): + IDT IDT h_num = 400 + IDT IDT w = weight_variable([self.state_dim, h_num]) + IDT IDT b = bias_variable([h_num]) + IDT IDT tf.add_to_collection('weight_norms', tf.nn.l2_loss(w)) + IDT IDT h_s = tf.nn.relu(tf.nn.bias_add(tf.matmul(self.state, w), b)) + IDT IDT h_s2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(self.state2, w), b)) + IDT IDT h2_num = 100 + IDT IDT w = weight_variable([h_num, h2_num]) + IDT IDT b = bias_variable([h2_num]) + IDT IDT tf.add_to_collection('weight_norms', tf.nn.l2_loss(w)) + IDT IDT h_s = tf.nn.relu(tf.nn.bias_add(tf.matmul(h_s, w), b)) + IDT IDT h_s2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(h_s2, w), b)) + IDT IDT w = weight_variable([h2_num, self.action_num]) + IDT IDT b = bias_variable([self.action_num]) + IDT IDT tf.add_to_collection('weight_norms', tf.nn.l2_loss(w)) + IDT IDT q = tf.nn.bias_add(tf.matmul(h_s, w), b) + IDT IDT h_a = tf.concat(1, [h_s, h_s2]) + IDT IDT w = weight_variable([h2_num * 2, self.action_num]) + IDT IDT b = bias_variable([self.action_num]) + IDT IDT a = tf.nn.bias_add(tf.matmul(h_a, w), b) + IDT IDT return q, a + + IDT def get_q_output(self, sess, s): + IDT IDT return self.q_predict.eval(session= , feed_dict= ) + IDT def update_with_targets(self, sess, s, a, targets): + IDT IDT self.train_step.run(session= , feed_dict= , self.q_target: targets, self.actions: a}) ) + IDT def update_to_predict_action(self, sess, s, s2, a): + IDT IDT self.train_step_a.run(session= , feed_dict= , self.state2: s2, self.actions: a}) ) + IDT def update_step(self, sess, s, s2, a, targets): + IDT IDT self.train_step.run(session= , feed_dict= , self.state2: s2, self.actions: a, self.q_target: targets}) ) + IDT def get_loss(self, sess, s, a, targets): + IDT IDT return self.l2_loss.eval(session= , feed_dict= , self.q_target: targets, self.actions: a}) ) + IDT def get_action_loss(self, sess, s, s2, a): + IDT IDT return self.a_loss.eval(session= , feed_dict= , self.state2: s2, self.actions: a}) ) + IDT def get_weight_norm(self, sess): + IDT IDT return self.w_norm.eval(session= )from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import time +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +train_subset = 10000 +batch_size = 128 +beta = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + IDT + IDT + IDT +batch_size = 128 +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 128 +num_nodes = 1024 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(relu1, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 12 +num_nodes = 1024 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(relu1, weights) + biases + IDT loss = , labels= ) IDT l2_regularizer = tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_relu1) + IDT loss += 0.0005 * l2_regularizer + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 12 +num_nodes = 1024 +beta = 0.001 +prob_limit = 0.75 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT keep_prob = tf.placeholder('float') + IDT hidden_layer_drop = tf.nn.dropout(relu1, keep_prob) + IDT logits = tf.matmul(hidden_layer_drop, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: prob_limit} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 128 +h1_num_nodes = 1024 +h2_num_nodes = 768 +h3_num_nodes = 512 +h1_stddev = np.sqrt(2.0 / 784) +h2_stddev = np.sqrt(2.0 / h1_num_nodes) +h3_stddev = np.sqrt(2.0 / h2_num_nodes) +logits_stddev = np.sqrt(2.0 / h3_num_nodes) +beta = 0.001 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT h1_weights = , h1_num_nodes], stddev= ) IDT h1_biases = tf.Variable(tf.zeros([h1_num_nodes])) + IDT h1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, h1_weights) + h1_biases) + IDT + IDT h2_weights = , h2_num_nodes], stddev= ) IDT h2_biases = tf.Variable(tf.zeros([h2_num_nodes])) + IDT h2_layer = tf.nn.relu(tf.matmul(h1_layer, h2_weights) + h2_biases) + IDT + IDT h3_weights = , h3_num_nodes], stddev= ) IDT h3_biases = tf.Variable(tf.zeros([h3_num_nodes])) + IDT h3_layer = tf.nn.relu(tf.matmul(h2_layer, h3_weights) + h3_biases) + IDT + IDT weights = , num_labels], stddev= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(h3_layer, weights) + biases + IDT loss = , labels= ) IDT global_step = tf.Variable(0) + IDT learning_rate = , global_step, 100000, 0.95, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT h1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, h1_weights) + h1_biases) + IDT h2_valid = tf.nn.relu(tf.matmul(h1_valid, h2_weights) + h2_biases) + IDT h3_valid = tf.nn.relu(tf.matmul(h2_valid, h3_weights) + h3_biases) + IDT valid_logits = tf.matmul(h3_valid, weights) + biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT ->', valid_prediction) + IDT h1_test = tf.nn.relu(tf.matmul(tf_test_dataset, h1_weights) + h1_biases) + IDT h2_test = tf.nn.relu(tf.matmul(h1_test, h2_weights) + h2_biases) + IDT h3_test = tf.nn.relu(tf.matmul(h2_test, h3_weights) + h3_biases) + IDT test_logits = tf.matmul(h3_test, weights) + biases + IDT test_prediction = tf.nn.softmax(test_logits) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT ->', valid_prediction.eval()) + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 1 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT IDT if step % 100 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 1 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +n_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_01 = tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_nodes])) + IDT weights_12 = tf.Variable(tf.truncated_normal([n_hidden_nodes, num_labels])) + IDT biases_01 = tf.Variable(tf.zeros([n_hidden_nodes])) + IDT biases_12 = tf.Variable(tf.zeros([num_labels])) + IDT z_01 = tf.matmul(tf_train_dataset, weights_01) + biases_01 + IDT h1 = tf.nn.relu(z_01) + IDT z_12 = tf.matmul(h1, weights_12) + biases_12 + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(z_12) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_01) + biases_01), weights_12) + biases_12) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_01) + biases_01), weights_12) + biases_12) +num_steps = 1 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT +batch_size = 18000 +n_hidden_nodes = 1024 +num_steps = 32 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_01 = tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_nodes])) + IDT weights_12 = tf.Variable(tf.truncated_normal([n_hidden_nodes, num_labels])) + IDT biases_01 = tf.Variable(tf.zeros([n_hidden_nodes])) + IDT biases_12 = tf.Variable(tf.zeros([num_labels])) + IDT z_01 = tf.matmul(tf_train_dataset, weights_01) + biases_01 + IDT h1 = tf.nn.relu(z_01) + IDT z_12 = tf.matmul(h1, weights_12) + biases_12 + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(z_12) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_01) + biases_01), weights_12) + biases_12) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_01) + biases_01), weights_12) + biases_12) +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = 0 + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +from tensorflow.contrib import layers as L +from sklearn.ensemble import RandomForestRegressor +import matplotlib.pyplot + + +def euclidean_loss(a, b): + IDT return tf.reduce_mean(tf.reduce_sum(tf.pow(a - b, 2))) / tf.cast(tf.shape(a)[0], 'float') + + +def MSE(a, b): + IDT return np.mean((a - b) ** 2) + + +def scaled_sigmoid(x): + IDT return 2 * tf.sigmoid(x) - 1 + + +class CATE_estimator: + + IDT def evaluate(self, X, TAU): + IDT IDT t_hat = self.predict(X) + IDT IDT t_dist = MSE(t_hat, TAU) + IDT IDT return t_dist + + +class LinearTlearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, catagorial= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT self.nn_input_X1 = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT self.nn_input_X0 = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT if catagorial: + IDT IDT IDT self.nn_input_Y1 = tf.placeholder('int64', [None]) + IDT IDT IDT self.nn_input_Y0 = tf.placeholder('int64', [None]) + IDT IDT else: + IDT IDT IDT self.nn_input_Y1 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT IDT self.nn_input_Y0 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT Y_hat_0 = , YS.shape[1], activation_fn= , weights_initializer= ) IDT IDT Y_hat_1 = , YS.shape[1], activation_fn= , weights_initializer= ) IDT IDT self.loss0 = euclidean_loss(Y_hat_0, self.nn_input_Y0) + IDT IDT self.loss1 = euclidean_loss(Y_hat_1, self.nn_input_Y1) + IDT IDT self.tau_estimate = Y_hat_1 - Y_hat_0 + IDT IDT self.trainer_0 = tf.train.AdamOptimizer().minimize(self.loss0) + IDT IDT self.trainer_1 = tf.train.AdamOptimizer().minimize(self.loss1) + IDT IDT self.name = 't_lm' + IDT IDT self.catagorial = catagorial + + IDT def train(self, X0, Y0, X1, Y1): + IDT IDT if len(Y1.shape) == 1: + IDT IDT IDT Y1 = , axis= ) IDT IDT if len(Y0.shape) == 1: + IDT IDT IDT Y0 = , axis= ) IDT IDT fd = {self.nn_input_X0: X0, self.nn_input_X1: X1, self.nn_input_Y0: Y0, self.nn_input_Y1: Y1} + IDT IDT sess = tf.get_default_session() + IDT IDT _, _, l0, l1 = , self.trainer_1, self.loss0, self.loss1], feed_dict= ) IDT IDT return l0 + l1 + + IDT def predict(self, X): + IDT IDT fd = , self.nn_input_X1: X, self.nn_input_Y0: np.expand_dims(np.zeros(len(X)), axis= , self.nn_input_Y1: np.expand_dims(np.zeros(len(X)), axis= ) IDT IDT sess = tf.get_default_session() + IDT IDT t_hat = , feed_dict= ) IDT IDT return np.squeeze(t_hat.flatten()) + + +class Tlearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, activation= , link_function= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT self.nn_input_X1 = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT self.nn_input_X0 = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT self.nn_input_Y1 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT self.nn_input_Y0 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT n_hid = 64 + IDT IDT h0_0 = , n_hid, activation_fn= , weights_initializer= ) IDT IDT h1_0 = , n_hid, activation_fn= , weights_initializer= ) IDT IDT Y_hat_0 = , YS.shape[1], activation_fn= , weights_initializer= ) IDT IDT h0_1 = , n_hid, activation_fn= , weights_initializer= ) IDT IDT h1_1 = , n_hid, activation_fn= , weights_initializer= ) IDT IDT Y_hat_1 = , YS.shape[1], activation_fn= , weights_initializer= ) IDT IDT if link_function: + IDT IDT IDT Y_hat_1 = tf.sigmoid(Y_hat_1) + IDT IDT IDT Y_hat_0 = tf.sigmoid(Y_hat_0) + IDT IDT self.loss0 = euclidean_loss(Y_hat_0, self.nn_input_Y0) + IDT IDT self.loss1 = euclidean_loss(Y_hat_1, self.nn_input_Y1) + IDT IDT self.tau_estimate = Y_hat_1 - Y_hat_0 + IDT IDT self.trainer_0 = tf.train.AdamOptimizer().minimize(self.loss0) + IDT IDT self.trainer_1 = tf.train.AdamOptimizer().minimize(self.loss1) + IDT IDT if activation == tf.nn.relu: + IDT IDT IDT self.name = 't_relu' + IDT IDT elif activation == tf.nn.tanh: + IDT IDT IDT self.name = 't_tanh' + IDT IDT elif activation == tf.nn.sigmoid: + IDT IDT IDT self.name = 't_sigmoid' + + IDT def train(self, X0, Y0, X1, Y1): + IDT IDT if len(Y1.shape) == 1: + IDT IDT IDT Y1 = , axis= ) IDT IDT if len(Y0.shape) == 1: + IDT IDT IDT Y0 = , axis= ) IDT IDT fd = {self.nn_input_X0: X0, self.nn_input_X1: X1, self.nn_input_Y0: Y0, self.nn_input_Y1: Y1} + IDT IDT sess = tf.get_default_session() + IDT IDT _, _, l0, l1 = , self.trainer_1, self.loss0, self.loss1], feed_dict= ) IDT IDT return (l0 + l1) / 2 + + IDT def predict(self, X, clip_range= , 1)) ) IDT IDT fd = , self.nn_input_X1: X, self.nn_input_Y0: np.expand_dims(np.zeros(len(X)), axis= , self.nn_input_Y1: np.expand_dims(np.zeros(len(X)), axis= ) IDT IDT sess = tf.get_default_session() + IDT IDT t_hat = , feed_dict= ) IDT IDT return t_hat.flatten() + + +class Slearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, activation= , link_function= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT self.nn_input_X = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT self.nn_input_W = tf.placeholder('float', [None]) + IDT IDT self.nn_input_XW = , tf.expand_dims(self.nn_input_W, axis= , axis= ) IDT IDT self.nn_input_Y = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT n_hid = 64 + IDT IDT h0_0 = , n_hid, activation_fn= , weights_initializer= ) IDT IDT h1_0 = , n_hid, activation_fn= , weights_initializer= ) IDT IDT self.Y_hat = , YS.shape[1], activation_fn= ) IDT IDT if link_function: + IDT IDT IDT self.Y_hat = tf.sigmoid(self.Y_hat) + IDT IDT self.loss = euclidean_loss(self.Y_hat, self.nn_input_Y) + IDT IDT self.trainer = tf.train.AdamOptimizer().minimize(self.loss) + IDT IDT if activation == tf.nn.relu: + IDT IDT IDT self.name = 's_relu' + IDT IDT elif activation == tf.nn.tanh: + IDT IDT IDT self.name = 's_tanh' + + IDT def train(self, X_mb, Y_mb, W_mb): + IDT IDT if len(Y_mb.shape) == 1: + IDT IDT IDT Y_mb = , axis= ) IDT IDT fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb} + IDT IDT sess = tf.get_default_session() + IDT IDT _, l = , self.loss], feed_dict= ) IDT IDT return l + + IDT def predict(self, X, clip_range= , 1)) ) IDT IDT sess = tf.get_default_session() + IDT IDT fd = , self.nn_input_Y: np.expand_dims(np.zeros(len(X)), axis= , self.nn_input_W: np.expand_dims(np.zeros(len(X)), axis= ) IDT IDT W0 = np.zeros(X.shape[0]) + IDT IDT W1 = np.ones(X.shape[0]) + IDT IDT fd[self.nn_input_W] = W0 + IDT IDT mu_hat_x_0 = , feed_dict= ) IDT IDT fd[self.nn_input_W] = W1 + IDT IDT mu_hat_x_1 = , feed_dict= ) IDT IDT tau_hat = mu_hat_x_1 - mu_hat_x_0 + IDT IDT return tau_hat.flatten() + + +class YLearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, activation= , link_function= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT self.nn_input_X = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT self.nn_input_sigma_0_ground_truth = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT self.nn_input_sigma_1_grond_truth = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT self.nn_input_sigma_tau_ground_truth = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT n_hid = 64 + IDT IDT if activation == tf.nn.relu: + IDT IDT IDT self.name = 'y_relu' + IDT IDT elif activation == tf.nn.tanh: + IDT IDT IDT self.name = 'y_tanh' + IDT IDT h0_sigma_0 = , n_hid, activation_fn= ) IDT IDT h1_sigma_0 = , n_hid, activation_fn= ) IDT IDT h0_sigma_1 = , n_hid, activation_fn= ) IDT IDT h1_sigma_1 = , n_hid, activation_fn= ) IDT IDT h0_sigma_tau = , n_hid, activation_fn= ) IDT IDT h1_sigma_tau = , n_hid, activation_fn= ) IDT IDT self.sigma_0_hat = , YS.shape[1], activation_fn= ) IDT IDT self.sigma_1_hat = , YS.shape[1], activation_fn= ) IDT IDT self.sigma_tau_hat = , YS.shape[1], activation_fn= ) IDT IDT if link_function: + IDT IDT IDT self.sigma_0_hat = tf.sigmoid(self.sigma_0_hat) + IDT IDT IDT self.sigma_1_hat = tf.sigmoid(self.sigma_1_hat) + IDT IDT IDT self.sigma_tau_hat = tf.nn.tanh(self.sigma_tau_hat) + IDT IDT loss_one = euclidean_loss(self.sigma_0_hat, self.nn_input_sigma_0_ground_truth) + IDT IDT loss_two = euclidean_loss(self.sigma_1_hat, self.nn_input_sigma_1_grond_truth) + IDT IDT loss_three = euclidean_loss(self.sigma_tau_hat, self.nn_input_sigma_tau_ground_truth) + IDT IDT self.loss = loss_one + loss_two + loss_three + IDT IDT self.trainer = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, X_mb, Y_mb, W_mb): + IDT IDT if len(Y_mb.shape) == 1: + IDT IDT IDT Y_mb = , axis= ) IDT IDT sigma_0, sigma_1, sigma_tau = self.generate_labels(X_mb, Y_mb, W_mb) + IDT IDT fd = {self.nn_input_X: X_mb, self.nn_input_sigma_0_ground_truth: sigma_0, self.nn_input_sigma_1_grond_truth: sigma_1, self.nn_input_sigma_tau_ground_truth: sigma_tau} + IDT IDT sess = tf.get_default_session() + IDT IDT _, l = , self.loss], feed_dict= ) IDT IDT return l + + IDT def predict(self, X, clip_range= , 1)) ) IDT IDT tau_hat = self.get_sigma(self.sigma_tau_hat, X) + IDT IDT return tau_hat + + IDT def get_sigma(self, sigma, X): + IDT IDT sess = tf.get_default_session() + IDT IDT fd = {self.nn_input_X: X} + IDT IDT out = , feed_dict= ) IDT IDT out = out.flatten() + IDT IDT return out + + IDT def generate_labels(self, X, Y, W): + IDT IDT Y = Y.flatten() + IDT IDT W = W.flatten() + IDT IDT sigma_0 = np.zeros_like(Y) + IDT IDT sigma_1 = np.zeros_like(Y) + IDT IDT sigma_tau = np.zeros_like(Y) + IDT IDT W_0 = ) IDT IDT W_1 = ) IDT IDT sigma_0[W_0] = Y[W_0] + IDT IDT sigma_0[W_1] = Y[W_1] - self.get_sigma(self.sigma_tau_hat, X[W_1]) + IDT IDT sigma_1[W_1] = Y[W_1] + IDT IDT sigma_1[W_0] = Y[W_0] + self.get_sigma(self.sigma_tau_hat, X[W_0]) + IDT IDT sigma_tau[W_1] = Y[W_1] - self.get_sigma(self.sigma_0_hat, X[W_1]) + IDT IDT sigma_tau[W_0] = self.get_sigma(self.sigma_1_hat, X[W_0]) - Y[W_0] + IDT IDT sigma_0 = np.expand_dims(sigma_0, -1) + IDT IDT sigma_1 = np.expand_dims(sigma_1, -1) + IDT IDT sigma_tau = np.expand_dims(sigma_tau, -1) + IDT IDT return sigma_0, sigma_1, sigma_tau + + +class Xlearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, activation= , link_function= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT self.nn_input_X1 = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT self.nn_input_Y1 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT self.nn_input_X0 = tf.placeholder('float', [None, XS.shape[1]]) + IDT IDT self.nn_input_Y0 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT self.nn_input_D_hat_0 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT self.nn_input_D_hat_1 = tf.placeholder('float', [None, YS.shape[1]]) + IDT IDT n_hid = 64 + IDT IDT if activation == tf.nn.relu: + IDT IDT IDT self.name = 'x_relu' + IDT IDT elif activation == tf.nn.tanh: + IDT IDT IDT self.name = 'x_tanh' + + IDT IDT def get_weights_and_biases(n_layers, in_shape, out_shape): + IDT IDT IDT w_dict = dict() + IDT IDT IDT b_dict = dict() + IDT IDT IDT n_units = n_hid + IDT IDT IDT for i in range(n_layers): + IDT IDT IDT IDT if i == n_layers - 1: + IDT IDT IDT IDT IDT n_units = out_shape + IDT IDT IDT IDT w_dict['h_' + str(i)] = , n_units])) ) IDT IDT IDT IDT b_dict['b_' + str(i)] = ) IDT IDT IDT IDT in_shape = n_hid + IDT IDT IDT return w_dict, b_dict + + IDT IDT def MLP(nn_input, weights, biases): + IDT IDT IDT n_layers = len(weights) + IDT IDT IDT l = nn_input + IDT IDT IDT for i in range(n_layers): + IDT IDT IDT IDT if i == n_layers - 1: + IDT IDT IDT IDT IDT Y_hat = tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)] + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT l = activation(tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)]) + IDT IDT IDT return Y_hat + IDT IDT weights_0, biases_0 = get_weights_and_biases(3, XS.shape[1], YS.shape[1]) + IDT IDT weights_1, biases_1 = get_weights_and_biases(3, XS.shape[1], YS.shape[1]) + IDT IDT mu_hat_0 = MLP(self.nn_input_X0, weights_0, biases_0) + IDT IDT mu_hat_1 = MLP(self.nn_input_X1, weights_1, biases_1) + IDT IDT if link_function: + IDT IDT IDT mu_hat_0 = tf.sigmoid(mu_hat_0) + IDT IDT IDT mu_hat_1 = tf.sigmoid(mu_hat_1) + IDT IDT self.D_hat_1 = self.nn_input_Y1 - MLP(self.nn_input_X1, weights_0, biases_0) + IDT IDT self.D_hat_0 = MLP(self.nn_input_X0, weights_1, biases_1) - self.nn_input_Y0 + IDT IDT tau_hat_0 = , n_hid, activation_fn= ) IDT IDT tau_hat_0 = , n_hid, activation_fn= ) IDT IDT tau_hat_0 = , YS.shape[1], activation_fn= ) IDT IDT tau_hat_1 = , n_hid, activation_fn= ) IDT IDT tau_hat_1 = , n_hid, activation_fn= ) IDT IDT tau_hat_1 = , YS.shape[1], activation_fn= ) IDT IDT if link_function: + IDT IDT IDT tau_hat_0 = tf.nn.tanh(tau_hat_0) + IDT IDT IDT tau_hat_1 = tf.nn.tanh(tau_hat_1) + IDT IDT self.tau_hat_0 = tau_hat_0 + IDT IDT self.tau_hat_1 = tau_hat_1 + IDT IDT self.loss_tau_0 = euclidean_loss(tau_hat_0, self.nn_input_D_hat_0) + IDT IDT self.loss_tau_1 = euclidean_loss(tau_hat_1, self.nn_input_D_hat_1) + IDT IDT self.loss_mu_0 = euclidean_loss(mu_hat_0, self.nn_input_Y0) + IDT IDT self.loss_mu_1 = euclidean_loss(mu_hat_1, self.nn_input_Y1) + IDT IDT self.trainer_0 = tf.train.AdamOptimizer().minimize(self.loss_mu_0) + IDT IDT self.trainer_1 = tf.train.AdamOptimizer().minimize(self.loss_mu_1) + IDT IDT self.trainer_tau_0 = tf.train.AdamOptimizer().minimize(self.loss_tau_0) + IDT IDT self.trainer_tau_1 = tf.train.AdamOptimizer().minimize(self.loss_tau_1) + + IDT def train(self, X0, Y0, X1, Y1): + IDT IDT if len(Y1.shape) == 1: + IDT IDT IDT Y1 = , axis= ) IDT IDT if len(Y0.shape) == 1: + IDT IDT IDT Y0 = , axis= ) IDT IDT fd = {self.nn_input_X0: X0, self.nn_input_X1: X1, self.nn_input_Y0: Y0, self.nn_input_Y1: Y1} + IDT IDT sess = tf.get_default_session() + IDT IDT sess.run([self.trainer_0, self.trainer_1], feed_dict= ) IDT IDT D_0, D_1 = , self.D_hat_1], feed_dict= ) IDT IDT fd[self.nn_input_D_hat_1] = D_1 + IDT IDT fd[self.nn_input_D_hat_0] = D_0 + IDT IDT _, _, l0t, l1t = , self.trainer_tau_1, self.loss_tau_0, self.loss_tau_1], feed_dict= ) IDT IDT return (l0t + l1t) / 2 + + IDT def predict(self, X, clip_range= , 1)) ) IDT IDT fd = , self.nn_input_X1: X, self.nn_input_Y0: np.expand_dims(np.zeros(len(X)), axis= , self.nn_input_Y1: np.expand_dims(np.zeros(len(X)), axis= ) IDT IDT sess = tf.get_default_session() + IDT IDT D_0, D_1 = , self.D_hat_1], feed_dict= ) IDT IDT fd[self.nn_input_D_hat_1] = D_1 + IDT IDT fd[self.nn_input_D_hat_0] = D_0 + IDT IDT t_0, t_1 = , self.tau_hat_1], feed_dict= ) IDT IDT t_hat = 0.5 * t_0 + 0.5 * t_1 + IDT IDT return t_hat.flatten() + + +class Ulearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, activation= , link_function= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT WS = WS.reshape([-1, 1]) + IDT IDT self.nn_input_X = tf.placeholder('float64', [None, XS.shape[1]]) + IDT IDT self.nn_input_W = tf.placeholder('float64', [None, 1]) + IDT IDT self.nn_input_Y = tf.placeholder('float64', [None, YS.shape[1]]) + IDT IDT self.slice = tf.placeholder('int32') + IDT IDT self.batch_size = tf.placeholder('int32') + IDT IDT n_hid = 64 + IDT IDT if activation == tf.nn.relu: + IDT IDT IDT self.name = 'u_relu' + IDT IDT elif activation == tf.nn.tanh: + IDT IDT IDT self.name = 'u_tanh' + + IDT IDT def get_weights_and_biases(n_layers, in_shape, out_shape): + IDT IDT IDT w_dict = dict() + IDT IDT IDT b_dict = dict() + IDT IDT IDT n_units = n_hid + IDT IDT IDT for i in range(n_layers): + IDT IDT IDT IDT if i == n_layers - 1: + IDT IDT IDT IDT IDT n_units = out_shape + IDT IDT IDT IDT w_dict['h_' + str(i)] = , n_units], dtype= ) IDT IDT IDT IDT b_dict['b_' + str(i)] = , dtype= ) IDT IDT IDT IDT in_shape = n_hid + IDT IDT IDT return w_dict, b_dict + + IDT IDT def MLP(nn_input, weights, biases): + IDT IDT IDT n_layers = len(weights) + IDT IDT IDT l = nn_input + IDT IDT IDT for i in range(n_layers): + IDT IDT IDT IDT if i == n_layers - 1: + IDT IDT IDT IDT IDT Y_hat = tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)] + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT l = activation(tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)]) + IDT IDT IDT return Y_hat + IDT IDT M_obs_weights, M_obs_biases = get_weights_and_biases(3, XS.shape[1], YS.shape[1]) + IDT IDT self.mu_hat_obs = MLP(self.nn_input_X, M_obs_weights, M_obs_biases) + IDT IDT mu_hat_obs_stopped = tf.stop_gradient(self.mu_hat_obs) + IDT IDT self.rf = , oob_score= , verbose= ) IDT IDT self.rf.fit(XS, np.ravel(WS)) + IDT IDT self.ehat_oob = self.rf.oob_prediction_ + IDT IDT self.e_hat_x = tf.reshape(tf.gather(self.ehat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + IDT IDT self.R = (self.nn_input_Y - self.mu_hat_obs) / tf.clip_by_value(self.nn_input_W - self.e_hat_x, 1e-06, 1 - 1e-06) + IDT IDT h0_1 = , n_hid, activation_fn= ) IDT IDT h1_1 = , n_hid, activation_fn= ) IDT IDT self.tau_hat = , int(self.R.shape[1]), activation_fn= ) IDT IDT if link_function: + IDT IDT IDT self.tau_hat = tf.nn.tanh(self.tau_hat) + IDT IDT self.loss_mu_obs = tf.reduce_mean(tf.reduce_sum(tf.pow(self.mu_hat_obs - self.nn_input_Y, 2))) + IDT IDT self.mu_obs_trainer = tf.train.AdamOptimizer().minimize(self.loss_mu_obs) + IDT IDT self.tau_loss = tf.reduce_mean(tf.reduce_sum(tf.pow(self.tau_hat - self.R, 2))) + IDT IDT self.tau_trainer = tf.train.AdamOptimizer().minimize(self.tau_loss) + + IDT def train(self, X_mb, Y_mb, W_mb, slice_num, batch_size): + IDT IDT if len(Y_mb.shape) == 1: + IDT IDT IDT Y_mb = , axis= ) IDT IDT if len(W_mb.shape) == 1: + IDT IDT IDT W_mb = , axis= ) IDT IDT fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb, self.slice: slice_num, self.batch_size: batch_size} + IDT IDT sess = tf.get_default_session() + IDT IDT _, _, t_l = , self.tau_trainer, self.tau_loss], feed_dict= ) IDT IDT return t_l + + IDT def predict(self, X): + IDT IDT fd = {self.nn_input_X: X} + IDT IDT sess = tf.get_default_session() + IDT IDT t_hat = , feed_dict= ) IDT IDT return t_hat.flatten() + + +class Flearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, activation= , link_function= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT WS = WS.reshape([-1, 1]) + IDT IDT self.nn_input_X = , [None, XS.shape[1]], name= ) IDT IDT self.nn_input_W = , [None, 1], name= ) IDT IDT self.nn_input_Y = , [None, YS.shape[1]], name= ) IDT IDT self.slice = , name= ) IDT IDT self.batch_size = , name= ) IDT IDT self.rf = , oob_score= , verbose= ) IDT IDT self.rf.fit(XS, np.ravel(WS)) + IDT IDT self.ehat_oob = self.rf.oob_prediction_ + IDT IDT self.e_hat_x = tf.reshape(tf.gather(self.ehat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + IDT IDT self.e_hat_x = tf.clip_by_value(self.e_hat_x, 1e-06, 1 - 1e-06) + IDT IDT Y_star = self.nn_input_Y * ((self.nn_input_W - self.e_hat_x) / (self.e_hat_x * (1 - self.e_hat_x))) + IDT IDT n_hid = 64 + IDT IDT h0_1 = , n_hid, activation_fn= ) IDT IDT h1_1 = , n_hid, activation_fn= ) IDT IDT self.tau_hat = , int(Y_star.shape[1]), activation_fn= ) IDT IDT if link_function: + IDT IDT IDT self.tau_hat = tf.nn.tanh(self.tau_hat) + IDT IDT self.tau_loss = tf.reduce_mean(tf.reduce_sum(tf.pow(self.tau_hat - Y_star, 2))) + IDT IDT self.tau_trainer = tf.train.AdamOptimizer().minimize(self.tau_loss) + IDT IDT if activation == tf.nn.relu: + IDT IDT IDT self.name = 'f_relu' + IDT IDT elif activation == tf.nn.tanh: + IDT IDT IDT self.name = 'f_tanh' + + IDT def train(self, X_mb, Y_mb, W_mb, slice_num, batch_size): + IDT IDT if len(Y_mb.shape) == 1: + IDT IDT IDT Y_mb = , axis= ) IDT IDT if len(W_mb.shape) == 1: + IDT IDT IDT W_mb = , axis= ) IDT IDT fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb, self.slice: slice_num, self.batch_size: batch_size} + IDT IDT sess = tf.get_default_session() + IDT IDT _, t_l = , self.tau_loss], feed_dict= ) IDT IDT return t_l + + IDT def predict(self, X): + IDT IDT fd = {self.nn_input_X: X} + IDT IDT sess = tf.get_default_session() + IDT IDT t_hat = , feed_dict= ) IDT IDT return t_hat.flatten() + + +class RLearner(CATE_estimator): + IDT tf_trainable = True + + IDT def __init__(self, XS, YS, WS, activation= , link_function= ) IDT IDT if len(YS.shape) == 1: + IDT IDT IDT YS = , axis= ) IDT IDT WS = WS.reshape([-1, 1]) + IDT IDT self.nn_input_X = tf.placeholder('float64', [None, XS.shape[1]]) + IDT IDT self.nn_input_W = tf.placeholder('float64', [None, 1]) + IDT IDT self.nn_input_Y = tf.placeholder('float64', [None, YS.shape[1]]) + IDT IDT self.slice = tf.placeholder(tf.int32) + IDT IDT self.batch_size = tf.placeholder(tf.int32) + IDT IDT self.rf = , oob_score= , verbose= ) IDT IDT self.rf.fit(XS, np.ravel(WS)) + IDT IDT self.ehat_oob = self.rf.oob_prediction_ + IDT IDT self.e_hat_x = tf.reshape(tf.gather(self.ehat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + IDT IDT self.rf_m = , oob_score= , verbose= ) IDT IDT self.rf_m.fit(XS, np.ravel(YS)) + IDT IDT self.m_hat_oob = self.rf_m.oob_prediction_ + IDT IDT self.m_hat_x = tf.reshape(tf.gather(self.m_hat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + IDT IDT n_hid = 64 + IDT IDT h0_1 = , n_hid, activation_fn= ) IDT IDT h1_1 = , n_hid, activation_fn= ) IDT IDT self.tau_hat = , int(self.nn_input_Y.shape[1]), activation_fn= ) IDT IDT if link_function: + IDT IDT IDT self.tau_hat = tf.nn.tanh(self.tau_hat) + IDT IDT self.tau_loss = tf.reduce_mean((self.nn_input_Y - self.m_hat_x - (self.nn_input_W - self.e_hat_x) * self.tau_hat) ** 2) + IDT IDT self.tau_trainer = tf.train.AdamOptimizer().minimize(self.tau_loss) + IDT IDT if activation == tf.nn.relu: + IDT IDT IDT self.name = 'r_relu' + IDT IDT elif activation == tf.nn.tanh: + IDT IDT IDT self.name = 'r_tanh' + + IDT def train(self, X_mb, Y_mb, W_mb, slice_num, batch_size): + IDT IDT if len(Y_mb.shape) == 1: + IDT IDT IDT Y_mb = , axis= ) IDT IDT if len(W_mb.shape) == 1: + IDT IDT IDT W_mb = , axis= ) IDT IDT fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb, self.slice: slice_num, self.batch_size: batch_size} + IDT IDT sess = tf.get_default_session() + IDT IDT _, t_l = , self.tau_loss], feed_dict= ) IDT IDT return t_l + + IDT def predict(self, X): + IDT IDT fd = {self.nn_input_X: X} + IDT IDT sess = tf.get_default_session() + IDT IDT t_hat = , feed_dict= ) IDT IDT return t_hat.flatten() + + +if __name__ == '__main__': + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +@author sourabhxiii + +""" +import numpy as np +import tensorflow as tf +from tensorflow import layers +from collections import namedtuple + + +class Critic: + + IDT def __init__(self, state_dim, axn_dim, output_dim, np_index, seed): + IDT IDT tf.set_random_seed(seed) + IDT IDT self.lr = 0.03 + IDT IDT with tf.name_scope('input'): + IDT IDT IDT self.features = , shape= , state_dim), name= ) IDT IDT IDT self.action = , shape= , axn_dim), name= ) IDT IDT IDT self.target_q = , shape= , 1), name= ) IDT IDT with tf.name_scope('output'): + IDT IDT IDT self.y = , shape= , 1), name= ) IDT IDT with tf.name_scope('critic_net'): + IDT IDT IDT nn = , kernel_initializer= , bias_initializer= , activation= , name= ) IDT IDT IDT nn = tf.concat([layers.flatten(nn), self.action], 1) + IDT IDT IDT nn = , kernel_initializer= , bias_initializer= , activation= , name= ) IDT IDT IDT nn = , kernel_initializer= , bias_initializer= , activation= , name= ) IDT IDT IDT self.predict_op = , kernel_initializer= , bias_initializer= , activation= , name= ) IDT IDT with tf.name_scope('critic_gradient'): + IDT IDT IDT with tf.name_scope('critic_loss'): + IDT IDT IDT IDT self.loss = , predictions= ) IDT IDT IDT with tf.name_scope('critic_train'): + IDT IDT IDT IDT self.train_op = tf.train.AdamOptimizer(self.lr).minimize(self.loss) + IDT IDT """ + IDT IDT https://pemami4911.github.io/blog/2016/08/21/ddpg-rl.html + IDT IDT https://github.com/pemami4911/deep-rl/tree/master/ddpg + IDT IDT """ + IDT IDT self.action_grads = tf.gradients(self.predict_op, self.action) + IDT IDT with tf.name_scope('init'): + IDT IDT IDT self.init = tf.global_variables_initializer() + IDT IDT self.network_params = tf.trainable_variables()[np_index:] + IDT IDT return + + IDT def set_session(self, sess): + IDT IDT self.session = sess + IDT IDT self.session.run(self.init) + + IDT def model_session(self): + IDT IDT return self.session + + IDT def update_weight(self, obs, axn, target): + IDT IDT self.session.run(self.train_op, feed_dict= , self.action: axn, self.target_q: target}) ) + IDT def get_q(self, obs, axns): + IDT IDT return self.session.run(self.predict_op, feed_dict= , self.action: np.squeeze(axns)}) ) + IDT def get_action_gradients(self, obs, axns): + IDT IDT return self.session.run(self.action_grads, feed_dict= , self.action: np.squeeze(axns)}) ) + IDT def get_weights(self): + IDT IDT return self.network_params +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import json +import numpy as np +import tensorflow as tf +from skimage import io +from matplotlib.patches import Rectangle +from PIL import Image +import progressbar +import time +import warnings +warnings.filterwarnings('ignore') +pic_dir_path = '../data/img2' +json_dir_path = '../data/ann2' +MODEL_PATH = '../data/model_artif/' +PIXEL_COUNT = 480 * 640 +LABEL_COUNT = 4 +EPOCH = 30 +if not os.path.exists(MODEL_PATH): + IDT os.makedirs(MODEL_PATH) + + +def read_img(img_path, height= , width= , dim= ) IDT return io.imread(img_path)[:, :, :3] / 255 + + +def read_collection(img_paths, height= , width= , dim= ) IDT n_imgs = img_paths.shape[0] + IDT output = , height, width, dim), dtype= ) IDT for i, path in enumerate(img_paths): + IDT IDT output[(i), :, :, :] = ) IDT return output.reshape(n_imgs, height * width, dim) + + +class Dataset: + + IDT def __init__(self, X, Y): + IDT IDT self.X = X + IDT IDT self.Y = Y + IDT IDT self._epochs_completed = 0 + IDT IDT self._index_in_epoch = 0 + IDT IDT self._num_examples = X.shape[0] + + IDT def next_batch(self, batch_size= ) IDT IDT start = self._index_in_epoch + IDT IDT self._index_in_epoch += batch_size + IDT IDT if self._index_in_epoch > self._num_examples: + IDT IDT IDT self._epochs_completed += 1 + IDT IDT IDT perm = np.arange(self._num_examples) + IDT IDT IDT np.random.shuffle(perm) + IDT IDT IDT self.X = self.X[perm] + IDT IDT IDT self.Y = self.Y[perm] + IDT IDT IDT start = 0 + IDT IDT IDT self._index_in_epoch = batch_size + IDT IDT IDT assert batch_size <= self._num_examples + IDT IDT end = self._index_in_epoch + IDT IDT return read_collection(self.X[start:end]), self.Y[start:end] + + IDT def epoch_completed(self): + IDT IDT return self._epochs_completed + + +def mse(expected, predicted): + IDT se = tf.square(expected - predicted) + IDT return tf.reduce_mean(se) + + +def weight_variable(name, shape): + IDT initial = , stddev= ) IDT return tf.get_variable(name, initializer= ) + +def bias_variable(name, shape): + IDT initial = , shape= ) IDT return tf.get_variable(name, initializer= ) + +def conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +class Model(object): + IDT xxx = 0 + + +def build_model(): + IDT x_placeholder = , shape= , PIXEL_COUNT, 3]) ) IDT y_placeholder = , shape= , LABEL_COUNT]) ) IDT x_image = tf.reshape(x_placeholder, [-1, 480, 640, 3]) + IDT W_conv1 = weight_variable('w1', [3, 3, 3, 32]) + IDT b_conv1 = bias_variable('b1', [32]) + IDT h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT W_conv2 = weight_variable('w2', [3, 3, 32, 64]) + IDT b_conv2 = bias_variable('b2', [64]) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + IDT h_pool2 = max_pool_2x2(h_conv2) + IDT W_conv3 = weight_variable('w3', [3, 3, 64, 128]) + IDT b_conv3 = bias_variable('b3', [128]) + IDT h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) + IDT h_pool3 = max_pool_2x2(h_conv3) + IDT h_pool3_flat = tf.reshape(h_pool3, [-1, 60 * 80 * 128]) + IDT W_fc1 = weight_variable('w4', [60 * 80 * 128, 500]) + IDT b_fc1 = bias_variable('b4', [500]) + IDT h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1) + IDT W_fc2 = weight_variable('w5', [500, 500]) + IDT b_fc2 = bias_variable('b5', [500]) + IDT h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2) + IDT W_out = weight_variable('w6', [500, LABEL_COUNT]) + IDT b_out = bias_variable('b6', [LABEL_COUNT]) + IDT output = tf.matmul(h_fc2, W_out) + b_out + IDT model = Model() + IDT model.x_placeholder = x_placeholder + IDT model.y_placeholder = y_placeholder + IDT model.output = output + IDT return model + + +img_names = np.array(os.listdir(pic_dir_path)) +json_names = os.listdir(json_dir_path) +img = io.imread('../data/img2/' + img_names[10]) +for i, name in enumerate(json_names): + IDT json_names[i] = os.path.join(json_dir_path, name) +info = [] +for name in json_names: + IDT f = open(name, 'r') + IDT f_json = json.load(f) + IDT info += f_json[0]['imgs'] +info = np.array(info) +X_paths = [] +Y = , 4), dtype= )info1001 = info[:1001] +bar = )for i, inf in enumerate(info1001): + IDT path = os.path.join(pic_dir_path, inf['car_number_pic']) + IDT X_paths.append(path) + IDT Y[(i), :] = inf['coordinates'] + IDT bar.update(i) +X_paths = np.array(X_paths) +X2_train = X_paths[:901] +X2_test = X_paths[901:] +Y_train = Y[:901, :] +Y_test = Y[901:, :] +Y2_train = Y_train / (64.0, 32.0, 64.0, 32.0) - 1.0 +Y2_test = Y_test / (64.0, 32.0, 64.0, 32.0) - 1 +dataset = Dataset(X2_train, Y2_train) +g = tf.Graph() +with g.as_default(): + IDT session = tf.InteractiveSession() + IDT model = build_model() + IDT loss = mse(model.y_placeholder, model.output) + IDT saver = tf.train.Saver() + IDT start_time = time.time() + IDT best_score = 1 + IDT train_step = ) IDT session = tf.InteractiveSession() + IDT session.run(tf.global_variables_initializer()) + IDT last_epoch = -1 + IDT while dataset.epoch_completed() < EPOCH: + IDT IDT batch_x, batch_y = dataset.next_batch(4) + IDT IDT train_step.run(feed_dict= , model.y_placeholder: batch_y}) ) IDT IDT if dataset.epoch_completed() > last_epoch: + IDT IDT IDT last_epoch = dataset.epoch_completed() + IDT IDT IDT score_test = , model.y_placeholder: Y2_test}) ) IDT IDT IDT if score_test < best_score: + IDT IDT IDT IDT best_score = score_test + IDT IDT IDT IDT saver.save(session, os.path.join(MODEL_PATH, 'model')) + IDT IDT IDT if dataset.epoch_completed() % 1 == 0: + IDT IDT IDT IDT epm = 60 * dataset.epoch_completed() / (time.time() - start_time) + IDT IDT IDT IDT , score_test, epm)) + IDT - start_time)) + IDT session.close() +import tensorflow as tf +import numpy as np + + +class Model: + IDT HIDDEN_UNIT_SIZE = 32 + IDT HIDDEN_UNIT_SIZE2 = 16 + IDT COLUMN_SIZE = 9 + + IDT def __init__(self): + IDT IDT self.__setup_placeholder() + IDT IDT self.__setup_model() + IDT IDT self.__setup_ops() + + IDT def __enter__(self): + IDT IDT self.session = tf.Session() + IDT IDT return self + + IDT def __exit__(self, exc_type, exc_value, traceback): + IDT IDT self.session.close() + IDT IDT return False + + IDT def save(self, path): + IDT IDT saver = tf.train.Saver() + IDT IDT saver.save(self.session, path) + + IDT def restore(self, path): + IDT IDT saver = tf.train.Saver() + IDT IDT saver.restore(self.session, path) + + IDT def __setup_placeholder(self): + IDT IDT column_size = Model.COLUMN_SIZE + IDT IDT self.trade_data = tf.placeholder('float', [None, column_size]) + IDT IDT self.actual_class = tf.placeholder('float', [None, 2]) + IDT IDT self.keep_prob = tf.placeholder('float') + IDT IDT self.label = tf.placeholder('string') + + IDT def __setup_model(self): + IDT IDT column_size = Model.COLUMN_SIZE + IDT IDT w1 = , Estimator.HIDDEN_UNIT_SIZE], stddev= ) IDT IDT b1 = , shape= ) IDT IDT h1 = tf.nn.relu(tf.matmul(self.trade_data, w1) + b1) + IDT IDT w2 = , Estimator.HIDDEN_UNIT_SIZE2], stddev= ) IDT IDT b2 = , shape= ) IDT IDT h2 = tf.nn.relu(tf.matmul(h1, w2) + b2) + IDT IDT h2_drop = tf.nn.dropout(h2, self.keep_prob) + IDT IDT w2 = , 2], stddev= ) IDT IDT b2 = , shape= ) IDT IDT self.output = tf.nn.softmax(tf.matmul(h2_drop, w2) + b2) + + IDT def __setup_ops(self): + IDT IDT cross_entropy = -tf.reduce_sum(self.actual_class * tf.log(self.output)) + IDT IDT self.summary = tf.scalar_summary(self.label, cross_entropy) + IDT IDT self.train_op = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + IDT IDT self.merge_summaries = tf.merge_summary([self.summary]) + IDT IDT correct_prediction = tf.equal(tf.argmax(self.output, 1), tf.argmax(self.actual_class, 1)) + IDT IDT self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + + +class Trainer(Model): + + IDT def train(self, steps, data): + IDT IDT self.__prepare_train(self.session) + IDT IDT for i in range(steps): + IDT IDT IDT self.__do_train(self.session, i, data) + IDT IDT IDT if i % 100 == 0: + IDT IDT IDT IDT self.__add_summary(self.session, i, data) + IDT IDT IDT IDT self.__print_status(self.session, i, data) + + IDT def __prepare_train(self, session): + IDT IDT self.summary_writer = , graph_def= ) IDT IDT session.run(tf.initialize_all_variables()) + + IDT def __do_train(self, session, i, data): + IDT IDT session.run(self.train_op, feed_dict= ) + IDT def __add_summary(self, session, i, data): + IDT IDT summary_str = , feed_dict= ) IDT IDT summary_str += , feed_dict= ) IDT IDT self.summary_writer.add_summary(summary_str, i) + + IDT def __print_status(self, session, i, data): + IDT IDT train_accuracy = , feed_dict= ) IDT IDT test_accuracy = , feed_dict= ) IDT IDT ) + + IDT def train_feed_dict(self, data): + IDT IDT return {self.trade_data: data.train_data(), self.actual_class: data.train_up_down(), self.keep_prob: 0.8, self.label: 'train'} + + IDT def test_feed_dict(self, data): + IDT IDT return {self.trade_data: data.test_data(), self.actual_class: data.test_up_down(), self.keep_prob: 1, self.label: 'test'} + + IDT def __reshape(self, profit_or_loss): + IDT IDT return profit_or_loss.values.reshape(len(profit_or_loss.values), 1) + + +class Estimator(Model): + + IDT def estimate(self, data): + IDT IDT return self.session.run(tf.argmax(self.output, 1), feed_dict= ) + IDT def estimate_feed_dict(self, data): + IDT IDT return {self.trade_data: data, self.keep_prob: 1} + + +def main(): + IDT loader = TradeResultsLoader() + IDT data = TradeResults(loader.retrieve_trade_data()) + IDT with Trainer() as trainer: + IDT IDT trainer.train(10001, data) + IDT IDT trainer.save('./model.ckpt') + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Sat Apr 21 19:59:10 2018 + +transfer learning conv const fully init + +@author: latlab +""" +import numpy as np +import tensorflow as tf +import random as rn +from sklearn.cross_validation import KFold + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +num_class = 2 +labels = np.asarray(range(num_class)) +img_size_x = 64 +img_size_y = 64 +img_size_z = 28 +num_channels = 1 +num_labels = 2 +batch_size1 = 4 +num_samples1 = 92 +kernel_size_1 = 5 +depth_1 = 5 +num_hid_1 = 64 +beta = 0.01 +import time +start = time.time() +num_steps1 = 1501 +keep_prob1 = 1 +file = open('D:\\Domonkos\\transfer_learning\\' + str(num_steps1) + '_' + str(keep_prob1) + '_' + str(batch_size1) + '_transferConstInitprior_adam.txt', 'w') +file.write('execution_time' + ' ' + 'minibatch_loss' + ' ' + 'minibatch_accuracy' + ' ' + ' ' + 'num_step' + ' ' + '\n') +num_kmeans = 10 +num_steps2 = 151 +keep_prob2 = 1 +batch_size2 = 4 +file2 = open('D:\\Domonkos\\transfer_learning\\' + str(num_kmeans) + '_' + str(num_steps2) + '_' + str(keep_prob2) + '_' + str(batch_size2) + '_transferConstInitposterior_adam.txt', 'w') +file2.write('execution_time' + ' ' + 'minibatch_loss' + ' ' + 'minibatch_accuracy' + ' ' + 'validation_accuracy' + ' ' + 'num_kmeans' + ' ' + 'num_step' + ' ' + '\n') +metadata = np.loadtxt('D:\\Domonkos\\transfer_learning\\labels_tf.txt').astype(int) +metadata2 = np.loadtxt('D:\\Domonkos\\resting_raw\\metadata\\labels.txt') +for k in range(10): + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , name= , shape= , img_size_x, img_size_y, img_size_z, num_channels)) ) IDT IDT tf_train_labels = , name= , shape= , num_labels)) ) IDT IDT tf_keep_prob = , name= ) IDT IDT layer1_weights = , kernel_size_1, kernel_size_1, num_channels, depth_1], stddev= , name= ) IDT IDT layer1_biases = , name= ) IDT IDT layer2_weights = , kernel_size_1, kernel_size_1, depth_1, depth_1], stddev= , name= ) IDT IDT layer2_biases = , shape= , name= ) IDT IDT layer3_weights = , num_hid_1], stddev= , name= ) IDT IDT layer3_biases = , shape= , name= ) IDT IDT layer4_weights = , num_labels], stddev= , name= ) IDT IDT layer4_biases = , shape= , name= ) + IDT IDT def model(data): + IDT IDT IDT conv = , layer1_weights, [1, 1, 1, 1, 1], padding= ) IDT IDT IDT hidden = tf.nn.relu(conv + layer1_biases) + IDT IDT IDT pool_pre = , [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding= ) IDT IDT IDT pool = tf.nn.dropout(pool_pre, tf_keep_prob) + IDT IDT IDT conv = , layer2_weights, [1, 1, 1, 1, 1], padding= ) IDT IDT IDT hidden = tf.nn.relu(conv + layer2_biases) + IDT IDT IDT pool_pre = , [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding= ) IDT IDT IDT pool = tf.nn.dropout(pool_pre, tf_keep_prob) + IDT IDT IDT shape = pool.get_shape().as_list() + IDT IDT IDT reshape = tf.reshape(pool, [shape[0], shape[1] * shape[2] * shape[3] * shape[4]]) + IDT IDT IDT fully_connect_1 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases) + IDT IDT IDT fully_connect_1_drop = tf.nn.dropout(fully_connect_1, 1) + IDT IDT IDT return tf.matmul(fully_connect_1_drop, layer4_weights) + layer4_biases + IDT IDT logits = model(tf_train_dataset) + IDT IDT loss = , logits= ) IDT IDT optimizer = tf.train.AdamOptimizer(0.001).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT train_index = np.arange(92) + IDT IDT np.random.shuffle(train_index) + IDT IDT for step in range(num_steps1): + IDT IDT IDT offset = step * batch_size1 % (num_samples1 - batch_size1) + IDT IDT IDT batch_data = [] + IDT IDT IDT batch_labels = [] + IDT IDT IDT for i in range(batch_size1): + IDT IDT IDT IDT ind = train_index[(offset + i) % train_index.shape[0]] + IDT IDT IDT IDT batch_data.append(np.load('D:\\Domonkos\\transfer_learning\\numpys_tf_use2\\' + str(ind) + '_' + str(int(np.floor(rn.random() * metadata[ind, 6]))) + '.npy')) + IDT IDT IDT IDT batch_labels.append(metadata[ind, 2]) + IDT IDT IDT batch_data = np.asarray(batch_data) + IDT IDT IDT batch_labels = np.asarray(batch_labels) + IDT IDT IDT batch_labels = , (None)]).astype(np.float32) ) IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_keep_prob: keep_prob1} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT file.write(str(time.time() - start) + ' ' + str(l) + ' ' + str(accuracy(predictions, batch_labels)) + ' ' + ' ' + str(step) + ' ' + '\n') + IDT IDT layer1_weights_tf = session.run('layer1_weights:0') + IDT IDT layer1_biases_tf = session.run('layer1_biases:0') + IDT IDT layer2_weights_tf = session.run('layer2_weights:0') + IDT IDT layer2_biases_tf = session.run('layer2_biases:0') + IDT IDT layer3_weights_tf = session.run('layer3_weights:0') + IDT IDT layer3_biases_tf = session.run('layer3_biases:0') + IDT IDT layer4_weights_tf = session.run('layer4_weights:0') + IDT IDT layer4_biases_tf = session.run('layer4_biases:0') + IDT IDT + '\n') + IDT labels = np.asarray(range(num_class)) + IDT batch_size2 = 4 + IDT num_samples2 = 60 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , name= , shape= , img_size_x, img_size_y, img_size_z, num_channels)) ) IDT IDT tf_train_labels = , name= , shape= , num_labels)) ) IDT IDT tf_keep_prob = , name= ) IDT IDT tf_valid_dataset = , name= , shape= , img_size_x, img_size_y, img_size_z, num_channels)) ) IDT IDT layer1_weights = , name= , shape= , kernel_size_1, kernel_size_1, num_channels, depth_1)) ) IDT IDT layer1_biases = , name= , shape= ) IDT IDT layer2_weights = , name= , shape= , kernel_size_1, kernel_size_1, depth_1, depth_1)) ) IDT IDT layer2_biases = , name= , shape= ) IDT IDT layer3_weights = , num_hid_1], stddev= , name= ) IDT IDT layer3_biases = , shape= , name= ) IDT IDT layer4_weights = , num_labels], stddev= , name= ) IDT IDT layer4_biases = , shape= , name= ) + IDT IDT def model(data): + IDT IDT IDT conv = , layer1_weights, [1, 1, 1, 1, 1], padding= ) IDT IDT IDT hidden = tf.nn.relu(conv + layer1_biases) + IDT IDT IDT pool_pre = , [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding= ) IDT IDT IDT pool = tf.nn.dropout(pool_pre, tf_keep_prob) + IDT IDT IDT conv = , layer2_weights, [1, 1, 1, 1, 1], padding= ) IDT IDT IDT hidden = tf.nn.relu(conv + layer2_biases) + IDT IDT IDT pool_pre = , [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding= ) IDT IDT IDT pool = tf.nn.dropout(pool_pre, tf_keep_prob) + IDT IDT IDT shape = pool.get_shape().as_list() + IDT IDT IDT reshape = tf.reshape(pool, [shape[0], shape[1] * shape[2] * shape[3] * shape[4]]) + IDT IDT IDT fully_connect_1 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases) + IDT IDT IDT fully_connect_1_drop = tf.nn.dropout(fully_connect_1, 1) + IDT IDT IDT return tf.matmul(fully_connect_1_drop, layer4_weights) + layer4_biases + IDT IDT logits = model(tf_train_dataset) + IDT IDT loss = , logits= ) IDT IDT optimizer = tf.train.AdamOptimizer(0.001).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + IDT kfold = KFold(num_samples2, num_kmeans, True, None) + IDT ind1 = 0 + IDT for train_index, test_index in kfold: + IDT IDT np.random.shuffle(train_index) + IDT IDT valid_dataset = [] + IDT IDT valid_labels = [] + IDT IDT for j in range(test_index.shape[0]): + IDT IDT IDT valid_dataset.append(np.load('D:\\Domonkos\\resting_raw\\numpys_resamp_ss\\rss_' + str(test_index[-(j + 1)]) + '_' + str(int(np.floor(rn.random() * 100))) + '.npy')) + IDT IDT IDT valid_labels.append(metadata2[test_index[-(j + 1)], 2]) + IDT IDT valid_dataset = np.asarray(valid_dataset) + IDT IDT valid_labels = np.asarray(valid_labels) + IDT IDT valid_labels = , (None)]).astype(np.float32) ) IDT IDT with tf.Session(graph= ) IDT IDT IDT tf.global_variables_initializer().run() + IDT IDT IDT + IDT IDT IDT session.run(layer3_weights, feed_dict= ) IDT IDT IDT session.run(layer3_biases, feed_dict= ) IDT IDT IDT session.run(layer4_weights, feed_dict= ) IDT IDT IDT session.run(layer4_biases, feed_dict= ) IDT IDT IDT + '\n') + IDT IDT IDT for step in range(num_steps2): + IDT IDT IDT IDT offset = step * batch_size2 % (num_samples2 - num_samples2 / num_kmeans - batch_size2) + IDT IDT IDT IDT batch_data = [] + IDT IDT IDT IDT batch_labels = [] + IDT IDT IDT IDT for i in range(batch_size2): + IDT IDT IDT IDT IDT ind = train_index[(offset + i) % train_index.shape[0]] + IDT IDT IDT IDT IDT batch_data.append(np.load('D:\\Domonkos\\resting_raw\\numpys_resamp_ss\\rss_' + str(ind) + '_' + str(int(np.floor(rn.random() * 100))) + '.npy')) + IDT IDT IDT IDT IDT batch_labels.append(metadata2[ind, 2]) + IDT IDT IDT IDT batch_data = np.asarray(batch_data) + IDT IDT IDT IDT batch_labels = np.asarray(batch_labels) + IDT IDT IDT IDT batch_labels = , (None)]).astype(np.float32) ) IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_valid_dataset: valid_dataset, tf_keep_prob: keep_prob2, layer1_weights: layer1_weights_tf, layer1_biases: layer1_biases_tf, layer2_weights: layer2_weights_tf, layer2_biases: layer2_biases_tf} + IDT IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT IDT file2.write(str(time.time() - start) + ' ' + str(l) + ' ' + str(accuracy(predictions, batch_labels)) + ' ' + str(accuracy(valid_prediction.eval(feed_dict= , valid_labels)) + ' ' + str(ind1) + ' ' + str(step) + ' ' + '\n') ) IDT IDT ind1 += 1 +end = time.time() +file.close() +file2.write(str(end - start)) +file2.close() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT data_d = , keep_prob= ) IDT output1 = tf.matmul(data_d, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 100 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() + +from __future__ import print_function +import numpy as np +import tensorflow as tf +import matplotlib.pyplot as plt +from tensorflow import contrib +from tensorflow.contrib import layers + + +class Gan(object): + IDT """Adversary based generator network. + IDT """ + + IDT def __init__(self, batch_size= , ndims= , cdim= , nlatent= , eta_d= , eta_g= ) IDT IDT """Initializes a GAN + + IDT IDT Args: + IDT IDT IDT ndims(int): Number of dimensions in the feature. + IDT IDT IDT nlatent(int): Number of dimensions in the latent space. + IDT IDT """ + IDT IDT self.batch_size = batch_size + IDT IDT self.ndims = ndims + IDT IDT self.in_shape = int(ndims ** 0.5), int(ndims ** 0.5) + IDT IDT self.n_latent = nlatent + IDT IDT self.colorspace_dim = cdim + IDT IDT self.x_placeholder = tf.placeholder(tf.float32, [None, ndims * cdim]) + IDT IDT self.z_placeholder = tf.placeholder(tf.float32, [None, nlatent]) + IDT IDT self.x_hat = self._generator(self.z_placeholder) + IDT IDT y_hat = self._discriminator(self.x_hat) + IDT IDT y = , reuse= ) IDT IDT self.d_loss = self._discriminator_loss(y, y_hat) + IDT IDT self.g_loss = self._generator_loss(y_hat) + IDT IDT discrim_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'discriminator') + IDT IDT + IDT IDT self.d_optimizer = , var_list= ) IDT IDT gen_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'generator') + IDT IDT + IDT IDT self.g_optimizer = , var_list= ) IDT IDT self.sample_gen = self.x_hat + IDT IDT self.session = tf.InteractiveSession() + IDT IDT self.session.run(tf.global_variables_initializer()) + + IDT def _discriminator(self, x, reuse= ) IDT IDT """Discriminator block of the network. + + IDT IDT Args: + IDT IDT IDT x (tf.Tensor): The input tensor of dimension (None, 784). + IDT IDT IDT reuse (Boolean): re use variables with same name in scope instead of creating + IDT IDT IDT new ones, check Tensorflow documentation + IDT IDT Returns: + IDT IDT IDT y (tf.Tensor): Scalar output prediction D(x) for true vs fake image(None, 1). + IDT IDT IDT DO NOT USE AN ACTIVATION FUNCTION AT THE OUTPUT LAYER HERE. + + IDT IDT """ + IDT IDT with tf.variable_scope('discriminator', reuse= ) IDT IDT IDT h1 = , self.ndims * self.colorspace_dim, activation_fn= ) IDT IDT IDT h2 = , self.ndims * self.colorspace_dim, activation_fn= ) IDT IDT IDT h3 = , self.ndims * self.colorspace_dim, activation_fn= ) IDT IDT IDT y = , 1, activation_fn= , scope= ) IDT IDT IDT return y + + IDT def _discriminator_loss(self, y, y_hat): + IDT IDT """Loss for the discriminator. + + IDT IDT Args: + IDT IDT IDT y (tf.Tensor): The output tensor of the discriminator for true images of dimension (None, 1). + IDT IDT IDT y_hat (tf.Tensor): The output tensor of the discriminator for fake images of dimension (None, 1). + IDT IDT Returns: + IDT IDT IDT l (tf.Scalar): average batch loss for the discriminator. + + IDT IDT """ + IDT IDT loss_y = , labels= ) IDT IDT loss_yhat = , labels= ) IDT IDT return loss_y + loss_yhat + + IDT def _generator(self, z, reuse= ) IDT IDT """From a sampled z, generate an image. + + IDT IDT Args: + IDT IDT IDT z(tf.Tensor): z from _sample_z of dimension (None, 2). + IDT IDT IDT reuse (Boolean): re use variables with same name in scope instead of creating + IDT IDT IDT new ones, check Tensorflow documentation + IDT IDT Returns: + IDT IDT IDT x_hat(tf.Tensor): Fake image G(z) (None, 784). + IDT IDT """ + IDT IDT with tf.variable_scope('generator', reuse= ) IDT IDT IDT h1 = , self.ndims * self.colorspace_dim, activation_fn= ) IDT IDT IDT h2 = , self.ndims * self.colorspace_dim, activation_fn= ) IDT IDT IDT h3 = , self.ndims * self.colorspace_dim, activation_fn= ) IDT IDT IDT raw = , self.ndims * self.colorspace_dim, activation_fn= , scope= ) IDT IDT IDT return raw + + IDT def _generator_loss(self, y_hat): + IDT IDT """Loss for the discriminator. + + IDT IDT Args: + IDT IDT IDT y_hat (tf.Tensor): The output tensor of the discriminator for fake images of dimension (None, 1). + IDT IDT Returns: + IDT IDT IDT l (tf.Scalar): average batch loss for the discriminator. + + IDT IDT """ + IDT IDT return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits= , labels= ) + IDT def sample(self, z_mu): + IDT IDT img = , feed_dict= ) IDT IDT return img + + IDT def generate_file(self, z_mus, step, n= , img_dir= ) IDT IDT out = np.empty((self.in_shape[0] * n, self.in_shape[1] * n, self.colorspace_dim)) + IDT IDT if self.colorspace_dim == 1: + IDT IDT IDT out = np.empty((self.in_shape[0] * n, self.in_shape[1] * n)) + IDT IDT for x_idx in range(n): + IDT IDT IDT for y_idx in range(n): + IDT IDT IDT IDT img = self.sample(z_mus[x_idx + y_idx * n]) + IDT IDT IDT IDT if self.colorspace_dim == 1: + IDT IDT IDT IDT IDT out[x_idx * self.in_shape[0]:(x_idx + 1) * self.in_shape[0], y_idx * self.in_shape[1]:(y_idx + 1) * self.in_shape[1]] = ) IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT out[x_idx * self.in_shape[0]:(x_idx + 1) * self.in_shape[0], y_idx * self.in_shape[1]:(y_idx + 1) * self.in_shape[1]] = , self.in_shape[1], self.colorspace_dim]) ) IDT IDT plt.imsave(img_dir + str(step) + '.png', out, cmap= ) + IDT def train(self, dataset, img_size= , num_steps= , d_steps= ) IDT IDT z_mus = [np.random.uniform(-1, 1, [1, self.n_latent]) for _ in range(400)] + IDT IDT + IDT IDT num_epochs = 0 + IDT IDT for step in range(num_steps): + IDT IDT IDT if step % 10 == 0: + IDT IDT IDT IDT + IDT IDT IDT batch_x, new_epoch = dataset.next_batch(self.batch_size) + IDT IDT IDT batch_x = batch_x.reshape((self.batch_size, self.ndims * self.colorspace_dim)) + IDT IDT IDT batch_x = (batch_x - 0.5) / 0.5 + IDT IDT IDT for _ in range(d_steps): + IDT IDT IDT IDT batch_z = np.random.uniform(-1, 1, [self.batch_size, self.n_latent]) + IDT IDT IDT IDT self.session.run(self.d_optimizer, feed_dict= , self.z_placeholder: batch_z}) ) IDT IDT IDT batch_z = np.random.uniform(-1, 1, [self.batch_size, self.n_latent]) + IDT IDT IDT self.session.run(self.g_optimizer, feed_dict= ) IDT IDT IDT if new_epoch: + IDT IDT IDT IDT num_epochs += 1 + IDT IDT IDT IDT if num_epochs % 5 == 0: + IDT IDT IDT IDT IDT self.generate_file(z_mus, step) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W, stride): + IDT return tf.nn.conv2d(x, W, strides= , stride, stride, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +def createNetwork(action_count): + IDT W_conv1 = weight_variable([8, 8, 4, 32]) + IDT b_conv1 = bias_variable([32]) + IDT W_conv2 = weight_variable([4, 4, 32, 64]) + IDT b_conv2 = bias_variable([64]) + IDT W_conv3 = weight_variable([3, 3, 64, 64]) + IDT b_conv3 = bias_variable([64]) + IDT W_fc1 = weight_variable([1600, 512]) + IDT b_fc1 = bias_variable([512]) + IDT W_fc2 = weight_variable([512, action_count]) + IDT b_fc2 = bias_variable([action_count]) + IDT s = tf.placeholder('float', [None, 80, 80, 4]) + IDT h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + IDT h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + IDT h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + IDT h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + IDT readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + IDT return s, readout, h_fc1 + + +class DQNAgent(object): + + IDT def __init__(self, action_count): + IDT IDT self.action_count = action_count + IDT IDT self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + IDT IDT self.action_placeholder = tf.placeholder('float', [None, action_count]) + IDT IDT self.reward_placeholder = tf.placeholder('float', [None]) + IDT IDT self.readout_action = , self.action_placeholder), reduction_indices= ) IDT IDT self.cost = tf.reduce_mean(tf.square(self.reward_placeholder - self.readout_action)) + IDT IDT self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.cost) + + IDT def act(self, session, state): + IDT IDT scores = self.score_actions(session, [state])[0] + IDT IDT return np.argmax(scores) + + IDT def score_actions(self, session, state_batch): + IDT IDT return session.run(self.readout, feed_dict= ) + IDT def train(self, session, reward_batch, action_batch, state_batch): + IDT IDT _, loss = , self.cost], feed_dict= , self.action_placeholder: action_batch, self.state_placeholder: state_batch}) ) IDT IDT return loss +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data + + +def constantTest(): + IDT m1 = tf.constant([[3, 3]]) + IDT m2 = tf.constant([[2], [3]]) + IDT predect = tf.matmul(m1, m2) + IDT + IDT with tf.Session() as sess: + IDT IDT ) + + +def variableTest(): + IDT a = tf.Variable(1.0) + IDT newValue = tf.add(a, 1) + IDT update = tf.assign(a, newValue) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT ) + IDT IDT sess.run(update) + IDT IDT ) + + +def fetchTest(): + IDT input1 = tf.constant(3.0) + IDT input2 = tf.constant(2.0) + IDT input3 = tf.constant(5.0) + IDT intermed = tf.add(input2, input3) + IDT with tf.Session() as sess: + IDT IDT result = sess.run([input1, intermed]) + IDT IDT + + +def feedTest(): + IDT input1 = tf.placeholder(tf.float32) + IDT input2 = tf.placeholder(tf.float32) + IDT output = tf.add(input1, input2) + IDT with tf.Session() as sess: + IDT IDT ) + + +def test4liner(): + IDT x_data = np.random.rand(100) + IDT y_data = x_data * 0.1 + 0.2 + IDT b = tf.Variable(0.0) + IDT k = tf.Variable(0.0) + IDT y = k * x_data + b + IDT with tf.name_scope('loss'): + IDT IDT loss = tf.reduce_mean(tf.square(y - y_data)) + IDT with tf.name_scope('train'): + IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.2) + IDT IDT train = optimizer.minimize(loss) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT writer = tf.summary.FileWriter('logs', sess.graph) + IDT IDT sess.run(init) + IDT IDT for i in range(100): + IDT IDT IDT sess.run(train) + IDT IDT IDT if i % 10 == 0: + IDT IDT IDT IDT ) + + +def test4regression(): + IDT x_data = np.linspace(-0.5, 0.5, 200)[:, (np.newaxis)] + IDT noise = np.random.normal(0, 0.02, x_data.shape) + IDT y_data = np.square(x_data) + noise + IDT x = tf.placeholder(tf.float32, [None, 1]) + IDT y = tf.placeholder(tf.float32, [None, 1]) + IDT Weights_L1 = tf.Variable(tf.random_normal([1, 10])) + IDT biases_L1 = tf.Variable(0.0) + IDT Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + IDT L1 = tf.nn.tanh(Wx_plus_b_L1) + IDT Weights_L2 = tf.Variable(tf.random_normal([10, 1])) + IDT biases_L2 = tf.Variable(tf.zeros([1, 1])) + IDT Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + IDT prediction = tf.nn.tanh(Wx_plus_b_L2) + IDT loss = tf.reduce_mean(tf.square(y - prediction)) + IDT train = tf.train.GradientDescentOptimizer(0.2).minimize(loss) + IDT with tf.Session() as sess: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT for i in range(2000): + IDT IDT IDT sess.run(train, feed_dict= , y: y_data}) ) IDT IDT prediction_value = , feed_dict= ) IDT IDT plt.figure() + IDT IDT plt.scatter(x_data, y_data) + IDT IDT plt.plot(x_data, prediction_value, 'r-') + IDT IDT plt.show() + + +def test4classifyMnist(): + IDT mnist = , one_hot= ) IDT batch_size = 100 + IDT n_batch = mnist.train.num_examples // batch_size + IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT y = tf.placeholder(tf.float32, [None, 10]) + IDT Weights_L1 = tf.Variable(tf.random_normal([784, 100])) + IDT biases_L1 = tf.Variable(tf.zeros([100]) + 0.1) + IDT Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + IDT L1 = tf.nn.sigmoid(Wx_plus_b_L1) + IDT Weights_L2 = tf.Variable(tf.random_normal([100, 100])) + IDT biases_L2 = tf.Variable(tf.zeros([100])) + IDT Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + IDT L2 = tf.nn.sigmoid(Wx_plus_b_L2) + IDT Weights_L3 = tf.Variable(tf.random_normal([100, 10])) + IDT biases_L3 = tf.Variable(tf.zeros([10])) + IDT Wx_plus_b_L3 = tf.matmul(L2, Weights_L3) + biases_L3 + IDT prediction = tf.nn.softmax(Wx_plus_b_L3) + IDT loss = , logits= ) IDT train = tf.train.GradientDescentOptimizer(0.2).minimize(loss) + IDT correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT with tf.Session() as sess: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT for i in range(20): + IDT IDT IDT for batch in range(n_batch): + IDT IDT IDT IDT batch_xs, batch_ys = mnist.train.next_batch(batch_size) + IDT IDT IDT IDT sess.run(train, feed_dict= , y: batch_ys}) ) IDT IDT IDT acc = , feed_dict= , y: mnist.test.labels}) ) IDT IDT IDT + ',Testing Accuracy ' + str(acc)) + + +def test4fitness2dropout(): + IDT mnist = , one_hot= ) IDT batch_size = 100 + IDT n_batch = mnist.train.num_examples // batch_size + IDT keep_prob = tf.placeholder(tf.float32) + IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT y = tf.placeholder(tf.float32, [None, 10]) + IDT Weights_L1 = tf.Variable(tf.zeros([784, 100])) + IDT biases_L1 = tf.Variable(tf.zeros([100]) + 0.1) + IDT Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + IDT L1 = tf.nn.sigmoid(Wx_plus_b_L1) + IDT L1 = tf.nn.dropout(L1, keep_prob) + IDT Weights_L2 = tf.Variable(tf.zeros([100, 100])) + IDT biases_L2 = tf.Variable(tf.zeros([100])) + IDT Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + IDT L2 = tf.nn.sigmoid(Wx_plus_b_L2) + IDT L2 = tf.nn.dropout(L2, keep_prob) + IDT Weights_L3 = tf.Variable(tf.zeros([100, 10])) + IDT biases_L3 = tf.Variable(tf.zeros([10])) + IDT Wx_plus_b_L3 = tf.matmul(L2, Weights_L3) + biases_L3 + IDT prediction = tf.nn.softmax(Wx_plus_b_L3) + IDT loss = , logits= ) IDT train = tf.train.AdamOptimizer().minimize(loss) + IDT correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT with tf.Session() as sess: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT for i in range(20): + IDT IDT IDT for batch in range(n_batch): + IDT IDT IDT IDT batch_xs, batch_ys = mnist.train.next_batch(batch_size) + IDT IDT IDT IDT sess.run(train, feed_dict= , y: batch_ys, keep_prob: 0.7}) ) IDT IDT IDT acc_test = , feed_dict= , y: mnist.test.labels, keep_prob: 1.0}) ) IDT IDT IDT acc_train = , feed_dict= , y: mnist.train.labels, keep_prob: 1.0}) ) IDT IDT IDT + ',Testing Accuracy ' + str(acc_test) + ',Training Accuracy ' + str(acc_train)) + + +def test4tensorboard(): + + IDT def variable_summaries(var): + IDT IDT with tf.name_scope('summaries'): + IDT IDT IDT mean = tf.reduce_mean(var) + IDT IDT IDT tf.summary.scalar('mean', mean) + IDT IDT IDT with tf.name_scope('stddex'): + IDT IDT IDT IDT stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) + IDT IDT IDT tf.summary.scalar('stddev', stddev) + IDT IDT IDT tf.summary.scalar('max', tf.reduce_max(var)) + IDT IDT IDT tf.summary.scalar('max', tf.reduce_min(var)) + IDT IDT IDT tf.summary.histogram('histogram', var) + IDT mnist = , one_hot= ) IDT batch_size = 100 + IDT n_batch = mnist.train.num_examples // batch_size + IDT keep_prob = tf.placeholder(tf.float32) + IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT y = tf.placeholder(tf.float32, [None, 10]) + IDT Weights_L1 = tf.Variable(tf.zeros([784, 100])) + IDT biases_L1 = tf.Variable(tf.zeros([100]) + 0.1) + IDT variable_summaries(Weights_L1) + IDT variable_summaries(biases_L1) + IDT Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + IDT L1 = tf.nn.sigmoid(Wx_plus_b_L1) + IDT L1 = tf.nn.dropout(L1, keep_prob) + IDT Weights_L2 = tf.Variable(tf.zeros([100, 100])) + IDT biases_L2 = tf.Variable(tf.zeros([100])) + IDT Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + IDT L2 = tf.nn.sigmoid(Wx_plus_b_L2) + IDT L2 = tf.nn.dropout(L2, keep_prob) + IDT Weights_L3 = tf.Variable(tf.zeros([100, 10])) + IDT biases_L3 = tf.Variable(tf.zeros([10])) + IDT Wx_plus_b_L3 = tf.matmul(L2, Weights_L3) + biases_L3 + IDT prediction = tf.nn.softmax(Wx_plus_b_L3) + IDT loss = , logits= ) IDT tf.summary.scalar('loss', tf.reduce_mean(loss)) + IDT train = tf.train.AdamOptimizer().minimize(loss) + IDT correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT tf.summary.scalar('accuracy', tf.reduce_mean(accuracy)) + IDT merged = tf.summary.merge_all() + IDT with tf.Session() as sess: + IDT IDT writer = tf.summary.FileWriter('logs', sess.graph) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT for i in range(20): + IDT IDT IDT for batch in range(n_batch): + IDT IDT IDT IDT batch_xs, batch_ys = mnist.train.next_batch(batch_size) + IDT IDT IDT IDT sess.run(train, feed_dict= , y: batch_ys, keep_prob: 0.7}) ) IDT IDT IDT summary, acc_test = , accuracy], feed_dict= , y: mnist.test.labels, keep_prob: 1.0}) ) IDT IDT IDT writer.add_summary(summary, i) + IDT IDT IDT acc_train = , feed_dict= , y: mnist.train.labels, keep_prob: 1.0}) ) IDT IDT IDT + ',Testing Accuracy ' + str(acc_test) + ',Training Accuracy ' + str(acc_train)) + + +def test4cnn(): + IDT + IDT mnist = , one_hot= ) IDT batch_size = 100 + IDT n_batch = mnist.train.num_examples // batch_size + + IDT def weight_variable(shape): + IDT IDT initial = , stddev= ) IDT IDT return tf.Variable(initial) + + IDT def bias_variable(shape): + IDT IDT initial = , shape= ) IDT IDT return tf.Variable(initial) + + IDT def conv2d(x, W): + IDT IDT """ + IDT IDT padding "SAME", "VALID" + IDT IDT strides + IDT IDT strides[0] = 1 batch 1input + IDT IDT strides[3] = 1 channels 113 + IDT IDT strides[1]strides[2] xy + IDT IDT """ + IDT IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + IDT def max_pool_2x2(x): + IDT IDT """ + IDT IDT max_pool: + IDT IDT ksize + IDT IDT ksize[0] = 1 batch 1input + IDT IDT ksize[3] = 1 channels 1 + IDT IDT ksize[1]ksize[2] xy + IDT IDT """ + IDT IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT y = tf.placeholder(tf.float32, [None, 10]) + IDT x_image = tf.reshape(x, [-1, 28, 28, 1]) + IDT W_convl = weight_variable([5, 5, 1, 32]) + IDT b_convl = bias_variable([32]) + IDT h_conv1 = tf.nn.relu(conv2d(x_image, W_convl) + b_convl) + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT W_conv2 = weight_variable([5, 5, 32, 64]) + IDT b_conv2 = bias_variable([64]) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + IDT h_pool2 = max_pool_2x2(h_conv2) + IDT W_fcl = weight_variable([7 * 7 * 64, 1024]) + IDT b_fcl = bias_variable([1024]) + IDT h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + IDT h_fcl = tf.nn.relu(tf.matmul(h_pool2_flat, W_fcl) + b_fcl) + IDT keep_prob = tf.placeholder(tf.float32) + IDT h_fcl_drop = tf.nn.dropout(h_fcl, keep_prob) + IDT W_fc2 = weight_variable([1024, 10]) + IDT b_fc2 = bias_variable([10]) + IDT prediction = tf.nn.softmax(tf.matmul(h_fcl_drop, W_fc2) + b_fc2) + IDT cross_entropy = , logits= ) IDT train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + IDT correct_prediction = tf.equal(tf.arg_max(prediction, 1), tf.arg_max(y, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT with tf.Session() as sess: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT for i in range(20): + IDT IDT IDT for batch in range(n_batch): + IDT IDT IDT IDT batch_xs, batch_ys = mnist.train.next_batch(batch_size) + IDT IDT IDT IDT sess.run(train_step, feed_dict= , y: batch_ys, keep_prob: 0.7}) ) IDT IDT IDT acc = , feed_dict= , y: mnist.test.labels, keep_prob: 1.0}) ) IDT IDT IDT + ',Testing Accuracy= ' + str(acc)) + + +def test4rnnLstm(): + IDT mnist = , one_hot= ) IDT n_inputs = 28 + IDT max_time = 28 + IDT lstm_size = 100 + IDT n_classes = 10 + IDT batch_size = 50 + IDT n_batch = mnist.train.num_examples // batch_size + IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT y = tf.placeholder(tf.float32, [None, 10]) + IDT weights = , n_classes], stddev= ) IDT biases = , shape= ) + IDT def RNN(X, weights, biases): + IDT IDT inputs = tf.reshape(X, [-1, max_time, n_inputs]) + IDT IDT lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size) + IDT IDT outputs, final_state = , inputs, dtype= ) IDT IDT results = tf.nn.softmax(tf.matmul(final_state[1], weights) + biases) + IDT IDT return results + IDT prediction = RNN(x, weights, biases) + IDT cross_entropy = , labels= ) IDT train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + IDT correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT init = tf.global_variables_initializer() + IDT with tf.Session() as sess: + IDT IDT sess.run(init) + IDT IDT for epoch in range(6): + IDT IDT IDT for batch in range(n_batch): + IDT IDT IDT IDT batch_xs, batch_ys = mnist.train.next_batch(batch_size) + IDT IDT IDT IDT sess.run(train_step, feed_dict= , y: batch_ys}) ) IDT IDT IDT acc = , feed_dict= , y: mnist.test.labels}) ) IDT IDT IDT + ',Testing Accuracy= ' + str(acc)) + + +if __name__ == '__main__': + IDT test4tensorboard() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +from time import time +from read_data import * +train_subset = 100000 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +""" +with tf.Session(graph= ) + + + tf.initialize_all_variables().run() + + t0 = time() + for step in range(num_steps): + IDT + IDT + IDT + IDT _, l, predictions = session.run([optimizer, loss, train_prediction]) + IDT if (step % 100 = ) IDT ) + IDT print('Training accuracy: %.1f%%' % accuracy( + IDT IDT predictions, train_labels[:train_subset, :])) + IDT + IDT + IDT + IDT print('Validation accuracy: %.1f%%' % accuracy( + IDT IDT valid_prediction.eval(), valid_labels)) + print(' +') + - t0, 3))) + , test_labels)) +""" + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT - t0, 3))) + IDT , test_labels)) + + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT output1 = tf.matmul(data, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +""" +code for running tests of each model + +import numpy as np +X = np.random.randn(10000, 5) +Y = np.random.randn(10000, 1) + 0.5 * X[:, 0:1] - 0.25 * X[:, 1:2] +m = linReg(X[0::2, :], Y[0::2], X[1::2, :], Y[1::2]) + +""" + + +def linReg(Xtrain, Ytrain, Xtest, Ytest): + IDT """ + IDT Linear regression built in tensor flow + IDT """ + IDT numIter = 5000 + IDT dim = Xtrain.shape[1] + IDT totalTrainVar = np.mean(Ytrain ** 2) + IDT totalTestVar = np.mean(Ytest ** 2) + IDT with tf.Session() as sess: + IDT IDT x = , shape= , dim]) ) IDT IDT y_ = , shape= , 1]) ) IDT IDT W = tf.Variable(tf.random_uniform([dim, 1])) + IDT IDT b = tf.Variable(tf.random_uniform([1, 1])) + IDT IDT y = tf.matmul(x, W) + b + IDT IDT mse = tf.reduce_mean(tf.square(y_ - y)) + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT train_step = tf.train.AdamOptimizer().minimize(mse) + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT for i in range(numIter): + IDT IDT IDT train_step.run(feed_dict= , y_: Ytrain}, session= ) IDT IDT IDT if i % 500 == 0: + IDT IDT IDT IDT trainError = , y_: Ytrain}, session= ) IDT IDT IDT IDT trainR2 = (totalTrainVar - trainError) / totalTrainVar + IDT IDT IDT IDT ) + IDT IDT testError = , y_: Ytest}, session= ) IDT IDT testR2 = (totalTestVar - testError) / totalTestVar + IDT IDT ) + IDT IDT statModel = , b.eval(session= ) IDT return statModel + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +def convNet(Xtrain, Ytrain, Xtest, Ytest): + IDT """ + IDT Convolutional netowrk built in tensor flow + IDT """ + IDT numIter = 50000 + IDT dim = Xtrain.shape[1] + IDT imageDim = int(np.sqrt(dim)) + IDT totalTrainVar = np.mean(Ytrain ** 2) + IDT totalTestVar = np.mean(Ytest ** 2) + IDT with tf.Session() as sess: + IDT IDT x = , shape= , dim]) ) IDT IDT y_ = , shape= , 1]) ) IDT IDT x_image = tf.reshape(x, [-1, imageDim, imageDim, 1]) + IDT IDT W_conv1 = weight_variable([10, 10, 1, 32]) + IDT IDT b_conv1 = bias_variable([32]) + IDT IDT h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + IDT IDT h_pool1 = max_pool_2x2(h_conv1) + IDT IDT W_conv2 = weight_variable([10, 10, 32, 64]) + IDT IDT b_conv2 = bias_variable([64]) + IDT IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + IDT IDT h_pool2 = max_pool_2x2(h_conv2) + IDT IDT W_fc1 = weight_variable([7 * 7 * 64, 1024]) + IDT IDT b_fc1 = bias_variable([1024]) + IDT IDT h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + IDT IDT h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + IDT IDT keep_prob = tf.placeholder('float') + IDT IDT h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + IDT IDT W_fc2 = weight_variable([1024, 1]) + IDT IDT b_fc2 = bias_variable([1]) + IDT IDT y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) + IDT IDT mse = tf.reduce_mean(tf.square(y_ - y_conv)) + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT train_step = tf.train.AdamOptimizer(0.0001).minimize(mse) + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT dataDict = {x: Xtrain, y_: Ytrain, keep_prob: 0.8} + IDT IDT for i in range(numIter): + IDT IDT IDT train_step.run(feed_dict= , session= ) IDT IDT IDT if i % 1000 == 0: + IDT IDT IDT IDT trainError = , session= ) IDT IDT IDT IDT trainR2 = (totalTrainVar - trainError) / totalTrainVar + IDT IDT IDT IDT ) + IDT IDT testError = , y_: Ytest, keep_prob: 1.0}, session= ) IDT IDT testR2 = (totalTestVar - testError) / totalTestVar + IDT IDT ) + IDT return testR2 +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +from __future__ import absolute_import +from __future__ import division +import sys +if sys.version[0] == '2': + IDT import cPickle as pkl +else: + IDT import pickle as pkl +import numpy as np +import tensorflow as tf +import utils +dtype = utils.DTYPE + + +class Model: + + IDT def __init__(self): + IDT IDT self.sess = None + IDT IDT self.X = None + IDT IDT self.y = None + IDT IDT self.layer_keeps = None + IDT IDT self.vars = None + IDT IDT self.keep_prob_train = None + IDT IDT self.keep_prob_test = None + + IDT def run(self, fetches, X= , y= , mode= ) IDT IDT feed_dict = {} + IDT IDT if type(self.X) is list: + IDT IDT IDT for i in range(len(X)): + IDT IDT IDT IDT feed_dict[self.X[i]] = X[i] + IDT IDT else: + IDT IDT IDT feed_dict[self.X] = X + IDT IDT if y is not None: + IDT IDT IDT feed_dict[self.y] = y + IDT IDT if self.layer_keeps is not None: + IDT IDT IDT if mode == 'train': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_train + IDT IDT IDT elif mode == 'test': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_test + IDT IDT return self.sess.run(fetches, feed_dict) + + IDT def dump(self, model_path): + IDT IDT var_map = {} + IDT IDT for name, var in self.vars.iteritems(): + IDT IDT IDT var_map[name] = self.run(var) + IDT IDT pkl.dump(var_map, open(model_path, 'wb')) + IDT IDT + + +class LR(Model): + + IDT def __init__(self, input_dim= , output_dim= , init_path= , opt_algo= , learning_rate= , l2_weight= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , logits= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FM(Model): + + IDT def __init__(self, input_dim= , output_dim= , factor_order= , init_path= , opt_algo= , learning_rate= , l2_w= , l2_v= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT v = self.vars['v'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + IDT IDT IDT xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + IDT IDT IDT p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FNN(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT ) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT node_in = num_inputs * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT , l.shape, wi.shape, bi.shape) + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class CCPM(Model): + + IDT def __init__(self, field_sizes= , embed_size= , filter_sizes= , layer_acts= , drop_out= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + IDT IDT init_vars.append(('b1', [1], 'zero', dtype)) + IDT IDT + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + IDT IDT IDT f1 = self.vars['f1'] + IDT IDT IDT l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + IDT IDT IDT f2 = self.vars['f2'] + IDT IDT IDT l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT l = tf.matmul(l, w1) + b1 + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN1(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + IDT IDT IDT l = tf.concat([xw, ip], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN2(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= , layer_norm= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT node_in = num_inputs * embed_size + embed_size * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT z = tf.reduce_sum(tf.reshape(xw, [-1, num_inputs, embed_size]), 1) + IDT IDT IDT op = tf.reshape(tf.matmul(tf.reshape(z, [-1, embed_size, 1]), tf.reshape(z, [-1, 1, embed_size])), [-1, embed_size * embed_size]) + IDT IDT IDT if layer_norm: + IDT IDT IDT IDT p_mean, p_var = , [1], keep_dims= ) IDT IDT IDT IDT op = (op - p_mean) / tf.sqrt(p_var) + IDT IDT IDT IDT p_g = , name= ) IDT IDT IDT IDT p_b = , name= ) IDT IDT IDT IDT op = op * p_g + p_b + IDT IDT IDT l = tf.concat([xw, op], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(tf.concat(w0, 0)) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + IDT IDT IDT z = tf.reduce_sum(tf.reshape(xw, [-1, num_inputs, embed_size]), 1) + IDT IDT IDT op = tf.reshape(tf.matmul(tf.reshape(z, [-1, embed_size, 1]), tf.reshape(z, [-1, 1, embed_size])), [-1, embed_size * embed_size]) + IDT IDT IDT l = tf.concat([xw, ip, op], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import layers_normal as nn + + +def dihedral(x, i): + IDT if len(x.shape) == 3: + IDT IDT if i & 4: + IDT IDT IDT y = np.transpose(x, (1, 0, 2)) + IDT IDT else: + IDT IDT IDT y = x.copy() + IDT IDT if i & 3 == 0: + IDT IDT IDT return y + IDT IDT if i & 3 == 1: + IDT IDT IDT return y[:, ::-1] + IDT IDT if i & 3 == 2: + IDT IDT IDT return y[::-1, :] + IDT IDT if i & 3 == 3: + IDT IDT IDT return y[::-1, ::-1] + IDT if len(x.shape) == 4: + IDT IDT if i & 4: + IDT IDT IDT y = np.transpose(x, (0, 2, 1, 3)) + IDT IDT else: + IDT IDT IDT y = x.copy() + IDT IDT if i & 3 == 0: + IDT IDT IDT return y + IDT IDT if i & 3 == 1: + IDT IDT IDT return y[:, :, ::-1] + IDT IDT if i & 3 == 2: + IDT IDT IDT return y[:, ::-1, :] + IDT IDT if i & 3 == 3: + IDT IDT IDT return y[:, ::-1, ::-1] + + +def summary_images(x, name): + IDT for i in range(min(4, x.get_shape().as_list()[3])): + IDT IDT tf.summary.image('{}-{}'.format(name, i), x[:, :, :, i:i + 1]) + + +class CNN: + + IDT def __init__(self): + IDT IDT self.tfx = None + IDT IDT self.tfy = None + IDT IDT self.tfp = None + IDT IDT self.xent = None + IDT IDT self.tftrain1 = None + IDT IDT self.tftrain2 = None + IDT IDT self.tftrain_all = None + IDT IDT self.tfkp = None + IDT IDT self.tfacc = None + IDT IDT self.train_counter = 0 + IDT IDT self.test = None + IDT IDT self.embedding_input = None + + IDT def NN1(self, x): + IDT IDT assert x.get_shape().as_list()[:3] == [None, 101, 101] + IDT IDT x = , 16, w= ) IDT IDT x = nn.convolution(x) + IDT IDT x = nn.max_pool(x) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 48, 48, 16] + IDT IDT x = nn.convolution(x, 32) + IDT IDT x = nn.convolution(x) + IDT IDT x = nn.max_pool(x) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 22, 22, 32] + IDT IDT x = nn.convolution(x, 64) + IDT IDT x = nn.convolution(x) + IDT IDT x = nn.max_pool(x) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT assert x.get_shape().as_list() == [None, 9, 9, 64] + IDT IDT x = nn.convolution(x, 128) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.convolution(x) + IDT IDT summary_images(x, 'nn1') + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT assert x.get_shape().as_list() == [None, 5, 5, 128] + IDT IDT x = , 1024, w= ) IDT IDT assert x.get_shape().as_list() == [None, 1, 1, 1024] + IDT IDT x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.fullyconnected(x, 1024) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.fullyconnected(x, 1024) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT self.test = x + IDT IDT x = , 2, activation= ) IDT IDT return x + + IDT def NN2(self, x): + IDT IDT assert x.get_shape().as_list()[:3] == [None, 101, 101] + IDT IDT x = x[:, 28:73, 28:73, :] + IDT IDT assert x.get_shape().as_list()[:3] == [None, 45, 45] + IDT IDT x = , 16, w= ) IDT IDT x = , 19, w= ) IDT IDT x = , 23, w= ) IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = , 27, w= ) IDT IDT x = , 33, w= ) IDT IDT x = , 39, w= ) IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = , 47, w= ) IDT IDT x = , 57, w= ) IDT IDT x = , 68, w= ) IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = , 82, w= ) IDT IDT summary_images(x, 'nn2') + IDT IDT assert x.get_shape().as_list() == [None, 5, 5, 82] + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = , 256, w= ) IDT IDT assert x.get_shape().as_list() == [None, 1, 1, 256] + IDT IDT x = tf.reshape(x, [-1, 256]) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.fullyconnected(x, 512) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.fullyconnected(x, 512) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = , 2, activation= ) IDT IDT return x + + IDT def create_architecture(self, bands): + IDT IDT self.tfkp = , tf.float32), [], name= ) IDT IDT self.tfacc = , tf.float32), [], name= ) IDT IDT x = , [None, 101, 101, bands], name= ) IDT IDT summary_images(x, 'input') + IDT IDT with tf.name_scope('nn1'): + IDT IDT IDT x1 = self.NN1(x) + IDT IDT tv1 = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) + IDT IDT with tf.name_scope('nn2'): + IDT IDT IDT x2 = self.NN2(x) + IDT IDT tv2 = [x for x in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) if not x in tv1] + IDT IDT with tf.name_scope('nn3'): + IDT IDT IDT x = tf.concat([x1, x2], 1) + IDT IDT IDT self.embedding_input = x + IDT IDT IDT x = nn.fullyconnected(x, 8) + IDT IDT IDT x = nn.fullyconnected(x, 8) + IDT IDT IDT x = , 1, activation= ) IDT IDT tv_join = [x for x in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) if not x in tv1 and not x in tv2] + IDT IDT assert x.get_shape().as_list() == [None, 1] + IDT IDT self.tfp = tf.nn.sigmoid(tf.reshape(x, [-1])) + IDT IDT with tf.name_scope('xent'): + IDT IDT IDT self.tfy = tf.placeholder(tf.float32, [None]) + IDT IDT IDT xent = , labels= , [-1, 1])) ) IDT IDT IDT self.xent = tf.reduce_mean(xent) + IDT IDT with tf.name_scope('train'): + IDT IDT IDT self.tftrain1 = , var_list= ) IDT IDT IDT self.tftrain2 = , var_list= ) IDT IDT IDT self.tftrain_all = , var_list= ) + IDT @staticmethod + IDT def split_test_train(path): + IDT IDT import os + IDT IDT files = ['{}/{}'.format(path, f) for f in sorted(os.listdir(path))] + IDT IDT return files[:3000], files[3000:] + + IDT @staticmethod + IDT def load(files): + IDT IDT xs = np.stack([np.load(f)['image'] for f in files]) + IDT IDT return CNN.prepare(xs) + + IDT @staticmethod + IDT def prepare(images): + IDT IDT images[images == 100] = 0.0 + IDT IDT if images.shape[-1] == 1: + IDT IDT IDT images = (images - 4.337e-13) / 5.504e-12 + IDT IDT elif images.shape[-1] == 4: + IDT IDT IDT images = (images - 1.685e-12) / 5.122e-11 + IDT IDT else: + IDT IDT IDT + IDT IDT return images + + IDT @staticmethod + IDT def batch(files, labels): + IDT IDT id0 = ) IDT IDT id1 = ) IDT IDT k = 15 + IDT IDT idn = , k, replace= ) IDT IDT idp = , k, replace= ) IDT IDT xs = CNN.load([files[i] for i in idp] + [files[i] for i in idn]) + IDT IDT ys = np.concatenate((labels[idp], labels[idn])) + IDT IDT for i in range(len(xs)): + IDT IDT IDT s = np.random.uniform(0.8, 1.2) + IDT IDT IDT u = np.random.uniform(-0.1, 0.1) + IDT IDT IDT xs[i] = dihedral(xs[i], np.random.randint(8)) * s + u + IDT IDT return xs, ys + + IDT def train(self, session, xs, ys, options= , run_metadata= , tensors= ) IDT IDT if tensors is None: + IDT IDT IDT tensors = [] + IDT IDT if self.train_counter < 10000: + IDT IDT IDT train = self.tftrain1 + IDT IDT IDT x = self.train_counter + IDT IDT elif self.train_counter < 20000: + IDT IDT IDT train = self.tftrain2 + IDT IDT IDT x = self.train_counter - 10000 + IDT IDT else: + IDT IDT IDT train = self.tftrain_all + IDT IDT IDT x = self.train_counter - 20000 + IDT IDT acc = 0.6 ** (x / 1000.0) + IDT IDT kp = 0.5 + 0.5 * 0.5 ** (x / 2000.0) + IDT IDT output = , self.xent] + tensors, feed_dict= , self.tfy: ys, self.tfkp: kp, self.tfacc: acc}, options= , run_metadata= ) IDT IDT self.train_counter += 1 + IDT IDT return output[1], output[2:] + + IDT def predict_naive(self, session, images): + IDT IDT return session.run(self.tfp, feed_dict= ) + IDT def predict_naive_xentropy(self, session, images, labels): + IDT IDT return session.run([self.tfp, self.xent], feed_dict= , self.tfy: labels}) ) + IDT def predict(self, session, images): + IDT IDT ps = self.predict_naive(session, images) + IDT IDT for i in range(1, 8): + IDT IDT IDT ps *= self.predict_naive(session, dihedral(images, i)) + IDT IDT return ps + + IDT def predict_xentropy(self, session, images, labels): + IDT IDT ps, xent = self.predict_naive_xentropy(session, images, labels) + IDT IDT for i in range(1, 8): + IDT IDT IDT ps *= self.predict_naive(session, dihedral(images, i)) + IDT IDT return ps, xent +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT + IDT image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +batch_size = 128 +hidden_nodes_1 = 1024 +hidden_nodes_2 = 512 +hidden_nodes_3 = 1024 +learning_rate = 0.0001 +beta = 0.005 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT keep_prob = tf.placeholder(tf.float32) + IDT weights_1 = tf.Variable(tf.random_normal([image_size * image_size, hidden_nodes_1])) + IDT biases_1 = tf.Variable(tf.zeros([hidden_nodes_1])) + IDT weights_2 = tf.Variable(tf.random_normal([hidden_nodes_1, hidden_nodes_2])) + IDT biases_2 = tf.Variable(tf.zeros([hidden_nodes_2])) + IDT weights_3 = tf.Variable(tf.random_normal([hidden_nodes_2, hidden_nodes_3])) + IDT biases_3 = tf.Variable(tf.zeros([hidden_nodes_3])) + IDT weights_out = tf.Variable(tf.random_normal([hidden_nodes_3, num_labels])) + IDT biases_out = tf.Variable(tf.zeros([num_labels])) + + IDT def forward_prop(input): + IDT IDT h1 = tf.nn.dropout(tf.nn.relu(tf.matmul(input, weights_1) + biases_1), keep_prob) + IDT IDT h2 = tf.nn.dropout(tf.nn.relu(tf.matmul(h1, weights_2) + biases_2), keep_prob) + IDT IDT h3 = tf.nn.dropout(tf.nn.relu(tf.matmul(h2, weights_3) + biases_3), keep_prob) + IDT IDT return tf.matmul(h3, weights_out) + biases_out + IDT logits = forward_prop(tf_train_dataset) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss += beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_out)) + IDT optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(forward_prop(tf_valid_dataset)) + IDT test_prediction = tf.nn.softmax(forward_prop(tf_test_dataset)) +num_steps = 10001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 1.0} + IDT IDT feed_dict_w_drop = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +def logic_L2_reg(num_steps= , batch_size= , beta= ) IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT IDT loss = , logits= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +def nn_L2_reg(num_steps= , batch_size= , beta= ) IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT weights_input = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + IDT IDT biases_input = tf.Variable(tf.zeros([1024])) + IDT IDT weights_layer1 = tf.Variable(tf.truncated_normal([1024, num_labels])) + IDT IDT biases_layer2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT layer1 = tf.matmul(tf_train_dataset, weights_input) + biases_input + IDT IDT layer1_relu = tf.nn.relu(layer1) + IDT IDT layer2 = tf.matmul(layer1_relu, weights_layer1) + biases_layer2 + IDT IDT loss = , logits= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(layer2) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + IDT with tf.Session(graph= ) IDT IDT + IDT IDT NN') + IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +def nn_L2_dropout(num_steps= , batch_size= , keep_prob= , beta= ) IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT weights_input = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + IDT IDT biases_input = tf.Variable(tf.zeros([1024])) + IDT IDT weights_layer1 = tf.Variable(tf.truncated_normal([1024, num_labels])) + IDT IDT biases_layer2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT layer1 = tf.matmul(tf_train_dataset, weights_input) + biases_input + IDT IDT layer1_relu = tf.nn.relu(layer1) + IDT IDT layer1_dropout = tf.nn.dropout(layer1_relu, keep_prob) + IDT IDT layer2 = tf.matmul(layer1_dropout, weights_layer1) + biases_layer2 + IDT IDT loss = , logits= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + IDT with tf.Session(graph= ) IDT IDT + IDT IDT NN---With Dropout') + IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +nn_L2_dropout() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +from tqdm import tqdm +from utils import BatchGenerator, make_dir +import os +import pdb + + +class NNClassifier: + + IDT def _inference(self, X, is_train): + IDT IDT hidden_size = 30 + IDT IDT word_embedding = tf.constant(self._embedding) + IDT IDT embedding_b = tf.nn.embedding_lookup(word_embedding, X[:, (0), :]) + IDT IDT embedding_q = tf.nn.embedding_lookup(word_embedding, X[:, (1), :]) + IDT IDT with tf.variable_scope('Body_GRU'): + IDT IDT IDT with tf.variable_scope('Body_GRU_fw'): + IDT IDT IDT IDT cell_b_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT with tf.variable_scope('Body_GRU_bw'): + IDT IDT IDT IDT cell_b_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT lengths_b = , (0), :]), axis= ) IDT IDT IDT output_b, out_state_b = , cell_b_fw, embedding_b, sequence_length= , dtype= ) IDT IDT IDT output_b = , output_b[1]], axis= , name= ) IDT IDT IDT out_state_b = , out_state_b[1]], axis= ) IDT IDT with tf.variable_scope('Question_GRU'): + IDT IDT IDT with tf.variable_scope('Question_GRU_fw'): + IDT IDT IDT IDT cell_q_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT with tf.variable_scope('Question_GRU_bw'): + IDT IDT IDT IDT cell_q_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT lengths_q = , (1), :]), axis= ) IDT IDT IDT output_q, out_state_q = , cell_q_fw, embedding_q, sequence_length= , dtype= ) IDT IDT IDT output_q = , output_q[1]], axis= ) IDT IDT IDT out_state_q = , out_state_q[1]], axis= , name= ) IDT IDT similarity = tf.matmul(output_b, tf.reshape(out_state_q, [tf.shape(X)[0], -1, 1])) + IDT IDT mask = , (0), :]), dtype= , [tf.shape(X)[0], -1, 1], name= ) IDT IDT attention_weight = , tf.reshape(tf.reduce_sum(tf.exp(similarity) * mask, axis= , [tf.shape(X)[0], -1, 1]), name= ) IDT IDT attention_b = , axis= ) IDT IDT output = , attention_b], axis= ) IDT IDT output = , units= ) IDT IDT output = tf.nn.elu(output) + IDT IDT output = , units= ) IDT IDT output = tf.nn.elu(output) + IDT IDT output = , units= ) IDT IDT output = tf.nn.elu(output) + IDT IDT dense = , units= ) IDT IDT return dense + + IDT def _iter(self, X, y, tensor_loss, train_op, placeholder, metric_tensors): + IDT IDT self._session.run(tf.local_variables_initializer()) + IDT IDT metric_scores = {} + IDT IDT for metric in self._metrics: + IDT IDT IDT metric_scores[metric] = 0 + IDT IDT batch_generator = BatchGenerator(X, y, self._batch_size) + IDT IDT for b in tqdm(range(X.shape[0] // self._batch_size + 1)): + IDT IDT IDT batch = next(batch_generator) + IDT IDT IDT feed_dict = {placeholder['x']: batch['x'], placeholder['y']: batch['y']} + IDT IDT IDT if train_op is not None: + IDT IDT IDT IDT loss, _, metrics = , train_op, metric_tensors], feed_dict= ) IDT IDT IDT else: + IDT IDT IDT IDT loss, metrics = , metric_tensors], feed_dict= ) IDT IDT if train_op == None: + IDT IDT IDT self._history.append(metrics['accuracy'][0]) + IDT IDT IDT if self._early_stop != None: + IDT IDT IDT IDT self._history[-1] = max(self._history[-1:-self._early_stop - 1:-1]) + IDT IDT summary = tf.Summary() + IDT IDT summary.value.add(tag= , simple_value= ) IDT IDT + IDT IDT for metric in self._metrics: + IDT IDT IDT score = float(metrics[metric][0]) + IDT IDT IDT summary.value.add(tag= , simple_value= ) IDT IDT IDT , end='') + IDT IDT + IDT IDT return summary + + IDT def __init__(self, learning_rate= , batch_size= , n_iters= , name= , valid= , embedding= , early_stop= ) IDT IDT self._batch_size = batch_size + IDT IDT self._n_iters = n_iters + IDT IDT self._metrics = {'accuracy': tf.metrics.accuracy} + IDT IDT self._global_step = , dtype= , trainable= , name= ) IDT IDT self._optimizer = tf.train.AdamOptimizer(learning_rate) + IDT IDT self._loss = tf.losses.softmax_cross_entropy + IDT IDT self._name = name + IDT IDT self._valid = valid + IDT IDT self._embedding = embedding + IDT IDT self._history = [] + IDT IDT self._early_stop = early_stop + + IDT def fit(self, X, y): + IDT IDT self._n_classes = y.shape[1] + IDT IDT make_dir(self._name) + IDT IDT make_dir(os.path.join(self._name, 'train')) + IDT IDT make_dir(os.path.join(self._name, 'valid')) + IDT IDT placeholder = , shape= , X.shape[1], X.shape[2])), 'y': tf.placeholder(tf.int32, shape= , y.shape[1])) ) IDT IDT with tf.variable_scope('nn') as scope: + IDT IDT IDT y_prob = , is_train= ) IDT IDT IDT loss = self._loss(placeholder['y'], y_prob) + IDT IDT IDT reg_const = 0.0005 + IDT IDT IDT l2 = reg_const * sum([tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()]) + IDT IDT IDT loss += l2 + IDT IDT IDT yp = tf.cast(placeholder['y'], tf.float32) + IDT IDT IDT train_op = , global_step= ) IDT IDT IDT metric_tensors = {} + IDT IDT IDT for metric in self._metrics: + IDT IDT IDT IDT y_pred_argmax = , axis= ) IDT IDT IDT IDT y_true_argmax = , axis= ) IDT IDT IDT IDT metric_tensors[metric] = self._metrics[metric](y_true_argmax, y_pred_argmax) + IDT IDT gpu_options = ) IDT IDT self._session = ) IDT IDT summary_writer = {'train': tf.summary.FileWriter(os.path.join(self._name, 'train'), self._session.graph), 'valid': tf.summary.FileWriter(os.path.join(self._name, 'valid'), self._session.graph)} + IDT IDT self._session.run(tf.global_variables_initializer()) + IDT IDT saver = tf.train.Saver() + IDT IDT ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoint/')) + IDT IDT if ckpt and ckpt.model_checkpoint_path: + IDT IDT IDT saver.restore(self._session, ckpt.model_checkpoint_path) + IDT IDT initial_step = ) IDT IDT for i in range(initial_step, self._n_iters): + IDT IDT IDT + IDT IDT IDT summary = self._iter(X, y, loss, train_op, placeholder, metric_tensors) + IDT IDT IDT summary_writer['train'].add_summary(summary, i) + IDT IDT IDT summary_writer['train'].flush() + IDT IDT IDT saver.save(self._session, 'checkpoint/session', i) + IDT IDT IDT if self._valid is not None: + IDT IDT IDT IDT + IDT IDT IDT IDT summary = self._iter(self._valid['x'], self._valid['y'], loss, None, placeholder, metric_tensors) + IDT IDT IDT IDT summary_writer['valid'].add_summary(summary, i) + IDT IDT IDT IDT summary_writer['valid'].flush() + IDT IDT IDT IDT if self._early_stop is not None: + IDT IDT IDT IDT IDT ) + IDT IDT IDT IDT IDT if len(self._history) > self._early_stop and self._history[-1] == self._history[-1 - self._early_stop]: + IDT IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT IDT return + + IDT def predict(self, X, prob= , remove_s= ) IDT IDT with tf.variable_scope('nn', reuse= ) IDT IDT IDT X_placeholder = , shape= , X.shape[1], X.shape[2])) ) IDT IDT IDT y_prob = , is_train= ) IDT IDT IDT if remove_s: + IDT IDT IDT IDT y_prob = tf.slice(y_prob, [0, 0], [X.shape[0], 5]) + IDT IDT IDT y_prob = tf.nn.softmax(y_prob) + IDT IDT IDT y_max = , axis= ) IDT IDT IDT y_pred = , tf.reshape(y_max, (-1, 1))), dtype= ) IDT IDT IDT y_, y_prob = , y_prob], feed_dict= ) IDT IDT if not prob: + IDT IDT IDT return y_ + IDT IDT else: + IDT IDT IDT return y_, y_prob +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import random +import tensorflow as tf +import numpy as np + + +def sort_by_label(image, label): + IDT tmp = {} + IDT for i in range(10): + IDT IDT tmp[i] = [] + IDT for x, y in zip(image, label): + IDT IDT tmp[y].append(x.reshape([28 * 28])) + IDT result = {} + IDT for k, v in tmp.items(): + IDT IDT result[k] = , dtype= ) IDT return result + + +def load_mnist(): + IDT mnist = tf.keras.datasets.mnist + IDT (x_train, y_train), (x_test, y_test) = ) IDT x_train, x_test = x_train / 255.0, x_test / 255.0 + IDT train = sort_by_label(x_train, y_train) + IDT test = sort_by_label(x_test, y_test) + IDT return train, test + + +MARGIN = 1.0 +anchor_placeholder = , 28 * 28], dtype= )positive_placeholder = , 28 * 28], dtype= )negative_placeholder = , 28 * 28], dtype= )center_placeholder = , 64], dtype= )w0 = , 64], stddev= )b0 = )w1 = , 64], stddev= )b1 = )w2 = , 64], stddev= )b2 = ) + +def encode_image(input_tensor): + IDT h0 = tf.nn.relu(tf.matmul(input_tensor, w0) + b0) + IDT h1 = tf.nn.relu(tf.matmul(h0, w1) + b1) + IDT h2 = tf.matmul(h1, w2) + b2 + IDT return tf.nn.l2_normalize(h2, axis= ) + +def sample_triplet(dataset, size): + IDT labels = list(range(9)) + IDT anchor_image = [] + IDT positive_image = [] + IDT negative_image = [] + IDT for i in range(size): + IDT IDT positive_label, negative_label = , k= ) IDT IDT a, p = , k= ) IDT IDT n = random.choice(dataset[negative_label]) + IDT IDT anchor_image.append(a) + IDT IDT positive_image.append(p) + IDT IDT negative_image.append(n) + IDT return np.array(anchor_image), np.array(positive_image), np.array(negative_image) + + +anchor_output = encode_image(anchor_placeholder) +positive_output = encode_image(positive_placeholder) +negative_output = encode_image(negative_placeholder) +d_pos = , axis= )d_neg = , axis= )triplet_loss = tf.maximum(0.0, MARGIN + d_pos - d_neg) +regularizer = tf.nn.l2_loss(w0) + tf.nn.l2_loss(w1) + tf.nn.l2_loss(w2) +loss = tf.reduce_mean(triplet_loss) + 1e-05 * regularizer +optimizer = )train_ops = optimizer.minimize(loss) +init = tf.global_variables_initializer() + + +def calc_center(dataset, session): + IDT result = [] + IDT for i in range(10): + IDT IDT tmp = , feed_dict= ) IDT IDT result.append(np.mean(tmp, axis= ) IDT return np.array(result) + + +def calc_encoding(dataset, session): + IDT result = [] + IDT for i in range(10): + IDT IDT tmp = , feed_dict= ) IDT IDT result.append(tmp) + IDT return np.array(result) + + +def get_predict(embedding, center): + IDT min_dist = None + IDT result = 0 + IDT for i in range(10): + IDT IDT dist = np.sum(np.square(embedding - center[i])) + IDT IDT if min_dist is None or dist < min_dist: + IDT IDT IDT min_dist = dist + IDT IDT IDT result = i + IDT return result + + +def main(): + IDT session = tf.Session() + IDT session.run(init) + IDT training_set, test_set = load_mnist() + IDT batch_size = 100 + IDT cost = 0.0 + IDT for i in range(5): + IDT IDT a, p, n = sample_triplet(training_set, batch_size) + IDT IDT _, c = , loss], feed_dict= , positive_placeholder: p, negative_placeholder: n}) ) IDT IDT cost += c / batch_size + IDT IDT if i % 1000 == 0: + IDT IDT IDT ) + IDT IDT IDT cost = 0.0 + IDT center = calc_center(training_set, session) + IDT ) + IDT error = 0 + IDT test_encoding = calc_encoding(test_set, session) + IDT for embedding in test_encoding[9]: + IDT IDT if get_predict(embedding, center) != 9: + IDT IDT IDT error += 1 + IDT )) + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +import neural_network as nn + + +class Generator(nn.Generator): + IDT """ + IDT Example OpenAI-Gym Generator architecture. + IDT """ + + IDT def __init__(self, sess): + IDT IDT """ + IDT IDT Args + IDT IDT ---- + IDT IDT IDT sess : the tensorflow session to be used + IDT IDT """ + IDT IDT self.sess_ = sess + IDT IDT with tf.variable_scope('gen'): + IDT IDT IDT self.input_state_ = , shape= , 4], name= ) IDT IDT IDT self.input_seed_ = , shape= , 1], name= ) IDT IDT IDT self.concat = , self.input_seed_], 1, name= ) IDT IDT IDT self.hidden = , 8, activation= , name= ) IDT IDT IDT self.output_ = , 2, name= ) IDT IDT self.sess.run(tf.global_variables_initializer()) + + IDT @property + IDT def input_state(self): + IDT IDT """ + IDT IDT The input state of shape [None, 4] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input state's placeholder tensor + IDT IDT """ + IDT IDT return self.input_state_ + + IDT @property + IDT def output(self): + IDT IDT """ + IDT IDT The outputted action distribution of shape [None, 2] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A tensor: the output tensor + IDT IDT """ + IDT IDT return self.output_ + + IDT @property + IDT def sess(self): + IDT IDT """ + IDT IDT The session used to create the graph + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A session: the graph's session + IDT IDT """ + IDT IDT return self.sess_ + + IDT @property + IDT def input_seed(self): + IDT IDT """ + IDT IDT The input random seed + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder: the input seed's placeholder tensor + IDT IDT """ + IDT IDT return self.input_seed_ + + IDT @property + IDT def trainable_variables(self): + IDT IDT """ + IDT IDT A list of the trainable variables in our generator + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A list of tensors: the trainable variables in this graph + IDT IDT """ + IDT IDT return tf.trainable_variables('gen') + + +class Discriminator(nn.Discriminator): + IDT """ + IDT Example OpenAI-Gym Discriminator Architecture + IDT """ + + IDT def __init__(self, sess): + IDT IDT """ + IDT IDT Args + IDT IDT ---- + IDT IDT IDT sess : the tensorflow session to be used + IDT IDT """ + IDT IDT self.sess_ = sess + IDT IDT with tf.variable_scope('dis'): + IDT IDT IDT self.input_state_ = , shape= , 4], name= ) IDT IDT IDT self.input_reward_ = , shape= , name= ) IDT IDT IDT self.input_action_ = , shape= , 1], name= ) IDT IDT IDT self.input_reward_exp = , axis= , name= ) IDT IDT IDT self.concat = , self.input_reward_exp, self.input_action_], axis= , name= ) IDT IDT IDT self.hidden = , 8, activation= , name= ) IDT IDT IDT self.output_ = , 1, activation= , name= ) IDT IDT self.sess.run(tf.global_variables_initializer()) + + IDT @property + IDT def input_state(self): + IDT IDT """ + IDT IDT The input state of shape [None, 4] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input state's placeholder tensor + IDT IDT """ + IDT IDT return self.input_state_ + + IDT @property + IDT def input_action(self): + IDT IDT """ + IDT IDT The input action of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input action's placeholder tensor + IDT IDT """ + IDT IDT return self.input_action_ + + IDT @property + IDT def output(self): + IDT IDT """ + IDT IDT The probability output of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A tensor: the output's tensor + IDT IDT """ + IDT IDT return self.output_ + + IDT @property + IDT def sess(self): + IDT IDT """ + IDT IDT The session used to create a graph + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A session: the graph's session + IDT IDT """ + IDT IDT return self.sess_ + + IDT @property + IDT def input_reward(self): + IDT IDT """ + IDT IDT The input reward + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input reward's tensor + IDT IDT """ + IDT IDT return self.input_reward_ + + IDT @property + IDT def trainable_variables(self): + IDT IDT """ + IDT IDT A list of the trainable variables in our generator + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A list of tensors: the trainable variables in this graph + IDT IDT """ + IDT IDT return tf.trainable_variables('dis') + + +class Discriminator_copy(nn.Discriminator_copy): + IDT """ + IDT Example OpenAI-Gym Discriminator Copying method + IDT """ + + IDT def __init__(self, dis, new_rew_input): + IDT IDT """ + IDT IDT Initializes a discriminator_copy object + + IDT IDT Args + IDT IDT ---- + IDT IDT IDT dis (Discriminator) : The discriminator to copy + IDT IDT IDT new_rew_input (tf.placeholder) : a new reward input. + IDT IDT """ + IDT IDT self.sess_ = dis.sess + IDT IDT with tf.variable_scope('dis', reuse= ) IDT IDT IDT self.input_state_ = , shape= , 4], name= ) IDT IDT IDT self.input_reward_ = new_rew_input + IDT IDT IDT self.input_action_ = , shape= , 1], name= ) IDT IDT IDT self.input_reward_exp = , axis= , name= ) IDT IDT IDT self.concat = , self.input_reward_exp, self.input_action_], axis= , name= ) IDT IDT IDT self.hidden_ker = tf.get_variable('hidden/kernel') + IDT IDT IDT self.hidden_bias = tf.get_variable('hidden/bias') + IDT IDT IDT self.output_ker = tf.get_variable('output/kernel') + IDT IDT IDT self.output_bias = tf.get_variable('output/bias') + IDT IDT self.hidden = tf.matmul(self.concat, self.hidden_ker) + self.hidden_bias + IDT IDT self.output_ = tf.sigmoid(tf.matmul(self.hidden, self.output_ker) + self.output_bias) + + IDT @property + IDT def input_state(self): + IDT IDT """ + IDT IDT The input state of shape [None, 4] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input state's placeholder tensor + IDT IDT """ + IDT IDT return self.input_state_ + + IDT @property + IDT def input_action(self): + IDT IDT """ + IDT IDT The input action of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input action's placeholder tensor + IDT IDT """ + IDT IDT return self.input_action_ + + IDT @property + IDT def output(self): + IDT IDT """ + IDT IDT The probability output of shape [None, 1] + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A tensor: the output's tensor + IDT IDT """ + IDT IDT return self.output_ + + IDT @property + IDT def sess(self): + IDT IDT """ + IDT IDT The session used to create a graph + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A session: the graph's session + IDT IDT """ + IDT IDT return self.sess_ + + IDT @property + IDT def input_reward(self): + IDT IDT """ + IDT IDT The input reward + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A placeholder tensor: the input reward's tensor + IDT IDT """ + IDT IDT return self.input_reward_ + + IDT @property + IDT def trainable_variables(self): + IDT IDT """ + IDT IDT A list of the trainable variables in our generator + + IDT IDT Returns + IDT IDT ------- + IDT IDT IDT A list of tensors: the trainable variables in this graph + IDT IDT """ + IDT IDT return tf.trainable_variables('dis') +import numpy as np +import tensorflow as tf +import config + + +class NN: + + IDT def __init__(self, session): + IDT IDT self.session = session + + IDT def f_batch(self, state_batch): + IDT IDT return self.session.run([p_op, v_op], feed_dict= , training: False}) ) + IDT def train(self, state_batch, pi_batch, z_batch): + IDT IDT p_loss, v_loss, _ = , v_loss_op, train_step_op], feed_dict= , pi: pi_batch, z: z_batch, learning_rate: config.learning_rate, training: True}) ) IDT IDT return p_loss, v_loss + + +def init_model(): + IDT x = single_convolutional_block(state) + IDT for i in range(config.residual_blocks_num): + IDT IDT x = residual_block(x) + IDT p_op = policy_head(x) + IDT v_op = value_head(x) + IDT with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): + IDT IDT v_loss_op, p_loss_op, combined_loss_op = loss(pi, z, p_op, v_op) + IDT IDT train_step_op = , momentum= ) IDT return p_op, v_op, p_loss_op, v_loss_op, train_step_op + + +def weight_variable(shape): + IDT return tf.Variable(tf.truncated_normal(shape, stddev= ) + +def bias_variable(shape): + IDT return tf.Variable(tf.constant(0.1, shape= ) + +def conv2d(x, kernel_size, filter_num): + IDT W = weight_variable([kernel_size, kernel_size, x.shape.dims[3].value, filter_num]) + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def batch_normalization(x): + IDT return tf.layers.batch_normalization(x, training= ) + +def rectifier_nonlinearity(x): + IDT return tf.nn.relu(x) + + +def linear_layer(x, size): + IDT W = weight_variable([x.shape.dims[1].value, size]) + IDT b = bias_variable([size]) + IDT return tf.matmul(x, W) + b + + +def single_convolutional_block(x): + IDT x = conv2d(x, 3, 64) + IDT x = batch_normalization(x) + IDT return rectifier_nonlinearity(x) + + +def residual_block(x): + IDT original_x = x + IDT x = conv2d(x, 3, 64) + IDT x = batch_normalization(x) + IDT x = rectifier_nonlinearity(x) + IDT x = conv2d(x, 3, 64) + IDT x = batch_normalization(x) + IDT x += original_x + IDT return rectifier_nonlinearity(x) + + +def policy_head(x): + IDT x = conv2d(x, 1, 2) + IDT x = batch_normalization(x) + IDT x = rectifier_nonlinearity(x) + IDT x = tf.reshape(x, [-1, config.board_length * 2]) + IDT return linear_layer(x, config.all_moves_num) + + +def value_head(x): + IDT x = conv2d(x, 1, 1) + IDT x = batch_normalization(x) + IDT x = rectifier_nonlinearity(x) + IDT x = tf.reshape(x, [-1, config.board_length]) + IDT x = linear_layer(x, 64) + IDT x = rectifier_nonlinearity(x) + IDT x = linear_layer(x, 1) + IDT return tf.nn.tanh(x) + + +def loss(pi, z, p, v): + IDT v_loss = tf.reduce_mean(tf.square(z - v)) + IDT p_loss = , labels= ) IDT variables = [v for v in tf.trainable_variables() if 'bias' not in v.name and 'beta' not in v.name] + IDT l2 = tf.add_n([tf.nn.l2_loss(variable) for variable in variables]) + IDT return v_loss, p_loss, v_loss + p_loss + config.l2_weight * l2 + + +state = , [None, config.N, config.N, config.history_num * 2 + 1], name= )pi = , [None, config.all_moves_num], name= )z = , [None, 1], name= )training = , name= )learning_rate = , name= )p_op, v_op, p_loss_op, v_loss_op, train_step_op = init_model() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(LSTMAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.tanh(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, lstm_state + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT """ + + IDT def __init__(self, hidden_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.dropout = dropout + + IDT def encode(self, inputs, masks, attention_inputs= , initial_state= , None), model_type= , name= , reuse= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(name, reuse= ) IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT else: + IDT IDT IDT IDT assert model_type == 'lstm' + IDT IDT IDT IDT cell = LSTMAttnCell(self.hidden_size, attention_inputs) + IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT outputs, final_state = , cell, inputs, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT final_outputs = outputs[0] + outputs[1] + IDT IDT IDT return final_outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, hidden_size, output_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.output_size = output_size + IDT IDT self.dropout = dropout + + IDT def decode(self, knowledge_rep, masks, initial_state= , None)) ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT IDT and output a probability estimation over + IDT IDT all paragraph tokens on which token should be + IDT IDT the start of the answer span, and which should be + IDT IDT the end of the answer span. + + IDT IDT :param knowledge_rep: it is a representation of the paragraph and question, + IDT IDT IDT IDT IDT IDT IDT decided by how you choose to implement the encoder + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('decoder'): + IDT IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT IDT start_states, start_final_state = , cell, knowledge_rep, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT IDT start_states = start_states[0] + start_states[1] + IDT IDT IDT IDT start_states_reshaped = tf.reshape(start_states, [-1, self.hidden_size]) + IDT IDT IDT IDT start_probs = , output_size= , bias= ) IDT IDT IDT IDT start_probs = tf.reshape(start_probs, [-1, self.output_size]) + IDT IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT IDT end_states, end_final_state = , cell, knowledge_rep, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT IDT end_states = end_states[0] + end_states[1] + IDT IDT IDT IDT end_states_reshaped = tf.reshape(end_states, [-1, self.hidden_size]) + IDT IDT IDT IDT end_probs = , output_size= , bias= ) IDT IDT IDT IDT end_probs = tf.reshape(end_probs, [-1, self.output_size]) + IDT IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT IDT add_mask = -1e+30 * (1.0 - bool_masks) + IDT IDT IDT start_probs = tf.add(start_probs, add_mask) + IDT IDT IDT end_probs = tf.add(end_probs, add_mask) + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, pretrained_embeddings, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.flags = flags + IDT IDT self.h_size = self.flags.state_size + IDT IDT self.p_size = self.flags.output_size + IDT IDT self.q_size = self.flags.question_size + IDT IDT self.embed_size = self.flags.embedding_size + IDT IDT self.dropout = self.flags.dropout + IDT IDT self.encoder = , dropout= ) IDT IDT self.decoder = , output_size= , dropout= ) IDT IDT self.context_placeholder = , shape= , self.p_size), name= ) IDT IDT self.question_placeholder = , shape= , self.q_size), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) IDT IDT self.saver = tf.train.Saver() + + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def mixer(self, q_states, ctx_states): + IDT IDT with vs.variable_scope('mixer'): + IDT IDT IDT A = , tf.transpose(q_states, perm= , 2, 1]))) ) IDT IDT IDT C_P = batch_matmul(A, q_states) + IDT IDT IDT C_P = tf.reshape(C_P, [-1, self.h_size]) + IDT IDT IDT P = tf.reshape(ctx_states, [-1, self.h_size]) + IDT IDT IDT P_final = , P], output_size= , bias= ) IDT IDT IDT P_final = tf.reshape(P_final, [-1, self.p_size, self.h_size]) + IDT IDT IDT return P_final + + IDT def filter(self, Q, P): + IDT IDT with vs.variable_scope('filter'): + IDT IDT IDT Qn = , dim= ) IDT IDT IDT Pn = , dim= ) IDT IDT IDT R = , tf.transpose(Pn, perm= , 2, 1])) ) IDT IDT IDT r = , axis= ) IDT IDT IDT r = , axis= ) IDT IDT IDT P_filtered = P * r + IDT IDT IDT return P_filtered + + IDT def coattention(self, P, Q, masks): + IDT IDT P_t = , perm= , 2, 1]) ) IDT IDT Q_t = , perm= , 2, 1]) ) IDT IDT L = tf.batch_matmul(P, Q_t) + IDT IDT AQ = , dim= ) IDT IDT AD = , perm= , 2, 1]), dim= ) IDT IDT CQ = tf.batch_matmul(P_t, AQ) + IDT IDT contexts = tf.concat(1, [Q_t, CQ]) + IDT IDT CD = tf.batch_matmul(contexts, AD) + IDT IDT lstm_inputs = , [P_t, CD]), perm= , 2, 1]) ) IDT IDT cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_fw = , output_keep_prob= ) IDT IDT cell_bw = , output_keep_prob= ) IDT IDT all_states, _ = , cell_bw, lstm_inputs, sequence_length= , dtype= ) IDT IDT U = tf.concat(2, all_states) + IDT IDT U = U[:, :self.p_size, :] + IDT IDT return U + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT P_filtered = self.context_embeddings + IDT IDT P_filtered = , P= ) IDT IDT question_states, final_question_state = , self.mask_q_placeholder, attention_inputs= , model_type= , reuse= , name= ) IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, attention_inputs= , initial_state= , model_type= , reuse= , name= ) IDT IDT feed_states = , ctx_states= ) IDT IDT self.start_probs, self.end_probs = , masks= , initial_state= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT """ + IDT IDT IDT q_mean,q_var = , axes= , shift= , name= , keep_dims= ) IDT IDT IDT ctx_mean,ctx_var = , axes= , shift= , name= , keep_dims= ) + + + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + + + IDT IDT IDT q_scale = , shape= ,self.embed_size],initializer= ) IDT IDT IDT ctx_scale = , shape= ,self.embed_size],initializer= ) IDT IDT IDT q_offset = , shape= ,self.embed_size],initializer= ) IDT IDT IDT ctx_offset = , shape= ,self.embed_size],initializer= ) + + IDT IDT IDT self.question_embeddings = , q_mean, q_var, q_offset,q_scale, variance_epsilon= ) IDT IDT IDT self.context_embeddings = , ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon= ) IDT IDT IDT """ + + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def answer(self, session, data): + IDT IDT yp_lst = [] + IDT IDT yp2_lst = [] + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle= ) IDT IDT IDT yp, yp2 = self.decode(session, *batch) + IDT IDT IDT yp_lst.append(yp) + IDT IDT IDT yp2_lst.append(yp2) + IDT IDT IDT prog_train.update(i + 1, [('Answering Questions....', 0.0)]) + IDT IDT + IDT IDT yp_all = , axis= ) IDT IDT yp2_all = , axis= ) IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(session= , context_batch= , question_batch= , answer_span_batch= , mask_ctx_batch= , mask_q_batch= ) + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset[0]) + IDT IDT else: + IDT IDT IDT inds = np.random.choice(len(dataset[0]), sample) + IDT IDT IDT sampled = [elem[inds] for elem in dataset] + IDT IDT IDT context = [context[i] for i in inds] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT cur_em = exact_match_score(pred_words, actual_words) + IDT IDT IDT em.append(float(cur_em)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + IDT IDT return np.mean(f1), np.mean(em) + + IDT def run_epoch(self, sess, train_set, val_set, train_context, val_context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT + IDT IDT prog_val = ) IDT IDT for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT + IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) + IDT def train(self, session, dataset, val_dataset, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + IDT IDT train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + IDT IDT val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + IDT IDT val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = [elem[:self.flags.batch_size * 1] for elem in train_dataset] + IDT IDT IDT val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + IDT IDT IDT num_epochs = 100 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , train_context= , val_context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT self.saver.save(session, train_dir + '/' + self.flags.run_name + '.ckpt') + + IDT def minibatches(self, data, batch_size, shuffle= ) IDT IDT num_data = len(data[0]) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + IDT IDT indices = np.arange(num_data) + IDT IDT if shuffle: + IDT IDT IDT np.random.shuffle(indices) + IDT IDT for minibatch_start in np.arange(0, num_data, batch_size): + IDT IDT IDT minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + IDT IDT IDT yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import math +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +mnist = , one_hot= , reshape= , validation_size= )np.set_printoptions(threshold= )x = tf.placeholder(tf.float32, [None, 28, 28, 1]) +y_ = tf.placeholder(tf.float32, [None, 10]) +pkeep = tf.placeholder(tf.float32) +lr = tf.placeholder(tf.float32) +K = 6 +L = 12 +M = 24 +N = 200 +w1 = , 6, 1, K], stddev= )b1 = tf.Variable(tf.constant(0.1, tf.float32, [K])) +w2 = , 5, K, L], stddev= )b2 = tf.Variable(tf.constant(0.1, tf.float32, [L])) +w3 = , 4, L, M], stddev= )b3 = tf.Variable(tf.constant(0.1, tf.float32, [M])) +w4 = , N], stddev= )b4 = tf.Variable(tf.constant(0.1, tf.float32, [N])) +w5 = , 10], stddev= )b5 = tf.Variable(tf.constant(0.1, tf.float32, [10])) +y1 = , w1, strides= , 1, 1, 1], padding= )y2 = , w2, strides= , 2, 2, 1], padding= )y3 = , w3, strides= , 2, 2, 1], padding= )yy3 = , shape= , 7 * 7 * M]) )y4 = tf.nn.relu(tf.matmul(yy3, w4) + b4) +yy4 = tf.nn.dropout(y4, pkeep) +Ylogits = tf.matmul(yy4, w5) + b5 +y = tf.nn.softmax(Ylogits) +cross_entropy = , labels= )cross_entropy = tf.reduce_mean(cross_entropy) * 100 +is_correct = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) +train_step = tf.train.AdamOptimizer(lr).minimize(cross_entropy) +init = tf.global_variables_initializer() +session = tf.Session() +session.run(init) +for i in range(10000): + IDT batch_x, batch_y = mnist.train.next_batch(100) + IDT train_data = {x: batch_x, y_: batch_y} + IDT + IDT max_learning_rate = 0.003 + IDT min_learning_rate = 0.0001 + IDT decay_speed = 2000.0 + IDT learning_rate = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i / decay_speed) + IDT session.run(train_step, {x: batch_x, y_: batch_y, lr: learning_rate, pkeep: 0.75}) +np.savetxt('test1.txt', session.run(w1).flatten()) +np.savetxt('bias1.txt', session.run(b1).flatten()) +np.savetxt('test2.txt', session.run(w2).flatten()) +np.savetxt('bias2.txt', session.run(b2).flatten()) +np.savetxt('test3.txt', session.run(w3).flatten()) +np.savetxt('bias3.txt', session.run(b3).flatten()) +np.savetxt('test4.txt', session.run(w4).flatten()) +np.savetxt('bias4.txt', session.run(b4).flatten()) +np.savetxt('test5.txt', session.run(w5).flatten()) +np.savetxt('bias5.txt', session.run(b5).flatten()) +import tensorflow as tf +import numpy as np + + +class Actor: + + IDT def __init__(self, num_inputs, h_size, name): + IDT IDT """Constructor of Actor class. + + IDT IDT IDT Args: + IDT IDT IDT IDT num_inputs (int): Number of the inputs of the actor + IDT IDT IDT IDT h_size (int): Size of the LSTM output. + IDT IDT IDT IDT name (str): name of the context. + IDT IDT """ + IDT IDT self.num_variables = 10 + IDT IDT self.cell = , state_is_tuple= ) IDT IDT self.inp = , num_inputs], dtype= ) IDT IDT self.initializer = tf.contrib.layers.xavier_initializer() + IDT IDT self.batch_size = , shape= ) IDT IDT self.train_length = ) IDT IDT self.rnn_inp = tf.reshape(self.inp, [self.batch_size, self.train_length, num_inputs]) + IDT IDT self.state_in = self.cell.zero_state(self.batch_size, tf.float32) + IDT IDT self.rnn, self.rnn_state = , cell= , dtype= , initial_state= , scope= ) IDT IDT self.rnn = , shape= , h_size]) ) IDT IDT self.b1 = tf.Variable(self.initializer([1, 1000])) + IDT IDT self.W1 = tf.Variable(self.initializer([h_size, 1000])) + IDT IDT self.h1 = tf.nn.relu(tf.matmul(self.rnn, self.W1) + self.b1) + IDT IDT self.b2 = tf.Variable(self.initializer([1, 100])) + IDT IDT self.W2 = tf.Variable(self.initializer([1000, 100])) + IDT IDT self.h2 = tf.nn.relu(tf.matmul(self.h1, self.W2) + self.b2) + IDT IDT self.b3 = tf.Variable(self.initializer([1, 50])) + IDT IDT self.W3 = tf.Variable(self.initializer([100, 50])) + IDT IDT self.h3 = tf.nn.relu(tf.matmul(self.h2, self.W3) + self.b3) + IDT IDT self.b4 = tf.Variable(self.initializer([1, 1])) + IDT IDT self.W4 = tf.Variable(self.initializer([50, 1])) + IDT IDT self.a_unscaled = tf.nn.tanh(tf.matmul(self.h3, self.W4) + self.b4) + IDT IDT self.a = tf.multiply(self.a_unscaled, 0.05) + IDT IDT self.network_params = tf.trainable_variables()[-self.num_variables:] + IDT IDT self.critic_gradient = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.unnormalized_actor_gradients = tf.gradients(self.a, self.network_params, -self.critic_gradient) + IDT IDT self.actor_gradients = list(map(lambda x: tf.div(x, 32), self.unnormalized_actor_gradients)) + IDT IDT self.optimizer = tf.train.AdamOptimizer(0.0001) + IDT IDT self.upd = self.optimizer.apply_gradients(zip(self.actor_gradients, self.network_params)) + IDT IDT self.update_parameters = [] + + IDT def create_op_holder(self, params, tau): + IDT IDT + IDT IDT self.update_parameters = [self.network_params[i].assign(tf.multiply(params[i], tau) + tf.multiply(self.network_params[i], 1.0 - tau)) for i in range(len(self.network_params))] + + +class Critic: + + IDT def __init__(self, num_inputs, h_size, name): + IDT IDT """Constructor of Critic class. + + IDT IDT IDT Args: + IDT IDT IDT IDT num_inputs (int): Number of the inputs of the critic + IDT IDT IDT IDT h_size (int): Size of the LSTM output. + IDT IDT IDT IDT name (str): name of the context. + IDT IDT """ + IDT IDT self.num_variables = 10 + IDT IDT self.cell = , state_is_tuple= ) IDT IDT self.inp = , num_inputs], dtype= ) IDT IDT self.initializer = tf.contrib.layers.xavier_initializer() + IDT IDT self.batch_size = , shape= ) IDT IDT self.train_length = ) IDT IDT self.rnn_inp = tf.reshape(self.inp, [self.batch_size, self.train_length, num_inputs]) + IDT IDT self.state_in = self.cell.zero_state(self.batch_size, tf.float32) + IDT IDT self.rnn, self.rnn_state = , cell= , dtype= , initial_state= , scope= ) IDT IDT self.rnn = , shape= , h_size]) ) IDT IDT self.b1 = tf.Variable(self.initializer([1, 1000])) + IDT IDT self.W1 = tf.Variable(self.initializer([h_size, 1000])) + IDT IDT self.h1 = tf.nn.relu(tf.matmul(self.rnn, self.W1) + self.b1) + IDT IDT self.b2 = tf.Variable(self.initializer([1, 100])) + IDT IDT self.W2 = tf.Variable(self.initializer([1000, 100])) + IDT IDT self.h2 = tf.nn.relu(tf.matmul(self.h1, self.W2) + self.b2) + IDT IDT self.b3 = tf.Variable(self.initializer([1, 50])) + IDT IDT self.W3 = tf.Variable(self.initializer([100, 50])) + IDT IDT self.h3 = tf.nn.relu(tf.matmul(self.h2, self.W3) + self.b3) + IDT IDT self.b4 = tf.Variable(self.initializer([1, 1])) + IDT IDT self.W4 = tf.Variable(self.initializer([50, 1])) + IDT IDT self.q = tf.matmul(self.h3, self.W4) + self.b4 + IDT IDT self.network_params = tf.trainable_variables()[-self.num_variables:] + IDT IDT self.target_q = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.loss = tf.reduce_mean(tf.square(self.target_q - self.q)) + IDT IDT self.optimizer = tf.train.AdamOptimizer(0.0001) + IDT IDT self.upd = self.optimizer.minimize(self.loss) + IDT IDT self.critic_gradients = tf.gradients(self.q, self.inp) + IDT IDT self.update_parameters = [] + + IDT def create_op_holder(self, params, tau): + IDT IDT + IDT IDT self.update_parameters = [self.network_params[i].assign(tf.multiply(params[i], tau) + tf.multiply(self.network_params[i], 1.0 - tau)) for i in range(len(self.network_params))] + + +class Agent: + + IDT def __init__(self, a_dof, c_dof, h_size, name, batch_size, train_length, tau): + IDT IDT """Constructor of Agent class. Each agent is composed of the main actor-critic pair and the target actor-critic + IDT IDT pair. Target architectures help stabilizing the training of the agents. + + IDT IDT IDT IDT IDT Args: + IDT IDT IDT IDT IDT IDT h_size (int): Size of the LSTM output. + IDT IDT IDT IDT IDT IDT name (str): name of the context. + IDT IDT IDT IDT """ + IDT IDT self.actor = Actor(a_dof, h_size, name + '_actor') + IDT IDT self.critic = Critic(c_dof, h_size, name + '_critic') + IDT IDT self.actor_target = Actor(a_dof, h_size, name + '_actor_target') + IDT IDT self.critic_target = Critic(c_dof, h_size, name + '_critic_target') + IDT IDT self.batch_size = batch_size + IDT IDT self.train_length = train_length + IDT IDT self.state_train = np.zeros([self.batch_size, h_size]), np.zeros([self.batch_size, h_size]) + IDT IDT self.actor_target.create_op_holder(self.actor.network_params, tau) + IDT IDT self.critic_target.create_op_holder(self.critic.network_params, tau) + + IDT def a_actor_operation(self, session, inp, state): + IDT IDT return session.run([self.actor.a, self.actor.rnn_state], feed_dict= , self.actor.state_in: state, self.actor.batch_size: 1, self.actor.train_length: 1}) ) + IDT def a_actor_training(self, session, inp): + IDT IDT return session.run(self.actor.a, feed_dict= , self.actor.state_in: self.state_train, self.actor.batch_size: self.batch_size, self.actor.train_length: self.train_length}) ) + IDT def a_target_actor_training(self, session, inp): + IDT IDT return session.run(self.actor_target.a, feed_dict= , self.actor_target.state_in: self.state_train, self.actor_target.batch_size: self.batch_size, self.actor_target.train_length: self.train_length}) ) + IDT def q_target_critic(self, session, inp): + IDT IDT return session.run(self.critic_target.q, feed_dict= , self.critic_target.train_length: self.train_length, self.critic_target.batch_size: self.batch_size, self.critic_target.state_in: self.state_train}) ) + IDT def gradients_critic(self, session, inp): + IDT IDT return session.run(self.critic.critic_gradients, feed_dict= , self.critic.train_length: self.train_length, self.critic.batch_size: self.batch_size, self.critic.state_in: self.state_train}) ) + IDT def update_critic(self, session, inp, q): + IDT IDT session.run(self.critic.upd, feed_dict= , self.critic.target_q: q, self.critic.train_length: self.train_length, self.critic.batch_size: self.batch_size, self.critic.state_in: self.state_train}) ) + IDT def update_actor(self, session, inp, gradients): + IDT IDT session.run(self.actor.upd, feed_dict= , self.actor.state_in: self.state_train, self.actor.critic_gradient: gradients, self.actor.batch_size: self.batch_size, self.actor.train_length: self.train_length}) ) + IDT def update_targets(self, session): + IDT IDT session.run(self.actor_target.update_parameters) + IDT IDT session.run(self.critic_target.update_parameters) + + IDT def initialize_targets(self, session): + IDT IDT session.run([self.actor_target.network_params[i].assign(self.actor.network_params[i]) for i in range(len(self.actor.network_params))]) + IDT IDT session.run([self.critic_target.network_params[i].assign(self.critic.network_params[i]) for i in range(len(self.critic.network_params))]) + + IDT def importance(self, session, inp_q, inp_q_target, r, gamma, state, new_state): + IDT IDT act_q = , feed_dict= , self.critic_target.train_length: 1, self.critic_target.batch_size: 1, self.critic_target.state_in: state}) ) IDT IDT tar_q = , feed_dict= , self.critic_target.train_length: 1, self.critic_target.batch_size: 1, self.critic_target.state_in: new_state}) ) IDT IDT return np.abs(r + gamma * tar_q - act_q) +import numpy as np +import tensorflow as tf +X = , [10, 10], name= )Y1 = , [10, 20], name= )Y2 = , [10, 20], name= )initial_shared_layer_weights = np.random.rand(10, 20) +initial_Y1_layer_weights = np.random.rand(20, 20) +initial_Y2_layer_weights = np.random.rand(20, 20) +shared_layer_weights = , name= , dtype= )Y1_layer_weights = , name= , dtype= )Y2_layer_weights = , name= , dtype= )shared_layer = tf.nn.relu(tf.matmul(X, shared_layer_weights)) +Y1_layer = tf.nn.relu(tf.matmul(shared_layer, Y1_layer_weights)) +Y2_layer = tf.nn.relu(tf.matmul(shared_layer, Y2_layer_weights)) +Y1_Loss = tf.nn.l2_loss(Y1 - Y1_layer) +Y2_Loss = tf.nn.l2_loss(Y2 - Y2_layer) +Joint_Loss = Y1_Loss + Y2_Loss +Optimiser = tf.train.AdamOptimizer().minimize(Joint_Loss) +Y1_op = tf.train.AdamOptimizer().minimize(Y1_Loss) +Y2_op = tf.train.AdamOptimizer().minimize(Y2_Loss) +with tf.Session() as session: + IDT session.run(tf.initialize_all_variables()) + IDT _, Joint_Loss = session.run([Optimiser, Joint_Loss], {X: np.random.rand(10, 10) * 10, Y1: np.random.rand(10, 20) * 10, Y2: np.random.rand(10, 20) * 10}) + IDT +import os, sys, time +import numpy as np +import tensorflow as tf +import datagen as dg + + +class Config(object): + IDT """ The model hyperparams and data information + + IDT """ + + IDT def __init__(self, config_list= , 64, 1000, 1024, 15, 0.9, 2e-05, 0.001, 25]) ) IDT IDT [self.embed_size, self.batch_size, self.label_size, self.hidden_size, self.max_epochs, self.dropout, self.lr, self.l2, self.max_ques_len] = config_list + + +class CNN_LSTM_VQAModel: + IDT """ Visual Question Answering using CNN and LSTM model. + + IDT """ + + IDT def load_data(self, debug= ) IDT IDT + IDT IDT self.train_set, self.test_set, self.vocab, self.ans_lk_table = dg.build_voc_and_get_data(self.config.max_ques_len) + IDT IDT self.wv, _, _ = dg.word_embed(self.vocab, 'glove.6B.300d') + IDT IDT label_size = len(self.ans_lk_table) + IDT IDT assert label_size == self.config.label_size + IDT IDT for i in range(len(self.train_set)): + IDT IDT IDT ans_id = self.train_set[i][2] + IDT IDT IDT ans_vec = np.zeros(label_size) + IDT IDT IDT ans_vec[ans_id] = 1 + IDT IDT IDT self.train_set[i][2] = ans_vec + IDT IDT if debug: + IDT IDT IDT self.valid_set = self.train_set[1024:2048] + IDT IDT IDT self.train_set = self.train_set[:1024] + IDT IDT IDT self.test_set = self.test_set[:65] + IDT IDT else: + IDT IDT IDT self.valid_set = self.train_set[:len(self.train_set) // 4] + IDT IDT IDT self.train_set = self.train_set[len(self.train_set) // 4:] + + IDT def add_placeholders(self): + IDT IDT """ placeholder tensors for input and labels + + IDT IDT IDT input_placeholder: input tensor of shape + IDT IDT IDT IDT IDT IDT IDT IDT (None, max_ques_len), type tf.int32 + IDT IDT IDT label_placeholder: label tensor of shape + IDT IDT IDT IDT IDT IDT IDT IDT (batch_size, label_size), type tf.float32 + IDT IDT IDT dropout_placeholder: Dropout value placeholder (scalar) + IDT IDT IDT IDT IDT IDT IDT IDT type tf.float32 + IDT IDT """ + IDT IDT self.input_placeholder = , shape= , self.config.max_ques_len], name= ) IDT IDT self.labels_placeholder = , shape= , self.config.label_size], name= ) IDT IDT self.dropout_placeholder = , name= ) + IDT def add_embedding(self, wv= ) IDT IDT with tf.device('/cpu:0'): + IDT IDT IDT if wv is not None: + IDT IDT IDT IDT embedding = , shape= , initializer= , trainable= ) IDT IDT IDT else: + IDT IDT IDT IDT embedding = , shape= , self.config.embed_size], trainable= ) IDT IDT IDT inputs = tf.nn.embedding_lookup(embedding, self.input_placeholder) + IDT IDT IDT tmp = tf.split(1, self.config.max_ques_len, inputs) + IDT IDT IDT inputs = [tf.squeeze(x, [1]) for x in tmp] + IDT IDT return inputs + + IDT def add_projection(self, output, cnn_outputs= ) IDT IDT with tf.variable_scope('Projection'): + IDT IDT IDT U = tf.get_variable('U', [self.config.hidden_size, self.config.label_size]) + IDT IDT IDT b = tf.get_variable('b', [self.config.label_size]) + IDT IDT IDT output = tf.matmul(output, U) + b + IDT IDT return output + + IDT def add_lstm_model(self, inputs): + IDT IDT """ LSTM model for Question Encoding. + + IDT IDT IDT inputs: list (length= ) IDT IDT IDT IDT IDT IDT (batch_size, embed_size), type tf.float32 + + IDT IDT IDT lstm_output: final state of lstm, tensor of shape + IDT IDT IDT IDT IDT IDT (batch_size, hidden_size), type tf.float32 + IDT IDT """ + IDT IDT batch_size, hidden_size, embed_size = self.config.batch_size, self.config.hidden_size, self.config.embed_size + IDT IDT with tf.variable_scope('InputDropout'): + IDT IDT IDT inputs = [tf.nn.dropout(x, self.dropout_placeholder) for x in inputs] + IDT IDT with tf.variable_scope('LSTM') as scope: + IDT IDT IDT self.initial_state = tf.zeros([batch_size, hidden_size]) + IDT IDT IDT ht = self.initial_state + IDT IDT IDT Ct = ht + IDT IDT IDT for step, xt in enumerate(inputs): + IDT IDT IDT IDT if step > 0: + IDT IDT IDT IDT IDT scope.reuse_variables() + IDT IDT IDT IDT Wf = tf.get_variable('Wf', [embed_size, hidden_size]) + IDT IDT IDT IDT Uf = tf.get_variable('Uf', [hidden_size, hidden_size]) + IDT IDT IDT IDT bf = tf.get_variable('bf', [hidden_size]) + IDT IDT IDT IDT ft = tf.nn.sigmoid(tf.matmul(xt, Wf) + tf.matmul(ht, Uf) + bf) + IDT IDT IDT IDT Wi = tf.get_variable('Wi', [embed_size, hidden_size]) + IDT IDT IDT IDT Ui = tf.get_variable('Ui', [hidden_size, hidden_size]) + IDT IDT IDT IDT bi = tf.get_variable('bi', [hidden_size]) + IDT IDT IDT IDT it = tf.nn.sigmoid(tf.matmul(xt, Wi) + tf.matmul(ht, Ui) + bi) + IDT IDT IDT IDT Wc = tf.get_variable('Wc', [embed_size, hidden_size]) + IDT IDT IDT IDT Uc = tf.get_variable('Uc', [hidden_size, hidden_size]) + IDT IDT IDT IDT bc = tf.get_variable('bc', [hidden_size]) + IDT IDT IDT IDT _Ct = tf.nn.tanh(tf.matmul(xt, Wc) + tf.matmul(ht, Uc) + bc) + IDT IDT IDT IDT Ct = ft * Ct + it * _Ct + IDT IDT IDT IDT Wo = tf.get_variable('Wo', [embed_size, hidden_size]) + IDT IDT IDT IDT Uo = tf.get_variable('Uo', [hidden_size, hidden_size]) + IDT IDT IDT IDT bo = tf.get_variable('bo', [hidden_size]) + IDT IDT IDT IDT ot = tf.nn.sigmoid(tf.matmul(xt, Wo) + tf.matmul(ht, Uo) + bo) + IDT IDT IDT IDT ht = ot * tf.nn.tanh(Ct) + IDT IDT IDT self.final_state = ht + IDT IDT with tf.variable_scope('LSTMDropout'): + IDT IDT IDT lstm_output = tf.nn.dropout(self.final_state, self.dropout_placeholder) + IDT IDT return lstm_output + + IDT def add_loss_op(self, y): + IDT IDT cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, self.labels_placeholder)) + IDT IDT tf.add_to_collection('total_loss', cross_entropy) + IDT IDT loss = tf.add_n(tf.get_collection('total_loss')) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT optimizer = tf.train.AdamOptimizer(self.config.lr) + IDT IDT train_op = optimizer.minimize(loss) + IDT IDT return train_op + + IDT def __init__(self, config): + IDT IDT self.config = config + IDT IDT self.load_data(debug= ) IDT IDT self.add_placeholders() + IDT IDT self.inputs = self.add_embedding(self.wv) + IDT IDT self.lstm_output = self.add_lstm_model(self.inputs) + IDT IDT self.output = self.add_projection(self.lstm_output) + IDT IDT self.loss = self.add_loss_op(self.output) + IDT IDT self.predictions = tf.nn.softmax(self.output) + IDT IDT one_hot_prediction = tf.argmax(self.predictions, 1) + IDT IDT correct_predictions = tf.equal(tf.argmax(self.labels_placeholder, 1), one_hot_prediction) + IDT IDT self.correct_predictions = tf.reduce_sum(tf.cast(correct_predictions, 'int32')) + IDT IDT self.train_step = self.add_training_op(self.loss) + + IDT def run_epoch(self, session, input_data, train_op= ) IDT IDT config = self.config + IDT IDT dropout = config.dropout + IDT IDT if not train_op: + IDT IDT IDT train_op = tf.no_op() + IDT IDT IDT dropout = 1 + IDT IDT total_steps = len(input_data) // self.config.batch_size + IDT IDT total_loss = [] + IDT IDT for step, (x, y) in enumerate(dg.data_iter(input_data, config.batch_size)): + IDT IDT IDT feed = {self.input_placeholder: x, self.labels_placeholder: y, self.dropout_placeholder: dropout} + IDT IDT IDT loss, _ = , train_op], feed_dict= ) IDT IDT IDT total_loss.append(loss) + IDT IDT IDT if step % 10 == 0: + IDT IDT IDT IDT sys.stdout.write('\r{} / {} : loss = , total_steps, loss)) ) IDT IDT IDT IDT sys.stdout.flush() + IDT IDT + IDT IDT return np.mean(total_loss) + + IDT def predict(self, session, data): + IDT IDT + IDT IDT dropout = 1 + IDT IDT pred_res = [] + IDT IDT confi_res = [] + IDT IDT total_steps = len(data) // self.config.batch_size + IDT IDT for step, (x, y) in enumerate(dg.data_iter(data, self.config.batch_size)): + IDT IDT IDT if x.shape[0] < self.config.batch_size: + IDT IDT IDT IDT break + IDT IDT IDT feed = {self.input_placeholder: x, self.dropout_placeholder: dropout} + IDT IDT IDT preds = , feed_dict= ) IDT IDT IDT predicted_indices = ) IDT IDT IDT pred_res.extend(predicted_indices) + IDT IDT IDT confident = ) IDT IDT IDT confi_res.extend(confident) + IDT IDT IDT if step % 10 == 0: + IDT IDT IDT IDT sys.stdout.write('\r{} / {}'.format(step, total_steps)) + IDT IDT IDT IDT sys.stdout.flush() + IDT IDT + IDT IDT return pred_res, confi_res + + +def test_VQA(): + IDT config = Config() + IDT with tf.variable_scope('CNN_LSTM_VQA') as scope: + IDT IDT model = CNN_LSTM_VQAModel(config) + IDT init = tf.initialize_all_variables() + IDT saver = tf.train.Saver() + IDT with tf.Session() as session: + IDT IDT best_val_loss = float('inf') + IDT IDT best_val_epoch = 0 + IDT IDT session.run(init) + IDT IDT for epoch in range(config.max_epochs): + IDT IDT IDT ) + IDT IDT IDT start = time.time() + IDT IDT IDT train_loss = model.run_epoch(session, model.train_set, model.train_step) + IDT IDT IDT valid_loss = model.run_epoch(session, model.valid_set) + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT - start)) + IDT IDT IDT if valid_loss < best_val_loss: + IDT IDT IDT IDT best_val_loss = valid_loss + IDT IDT IDT IDT best_val_epoch = epoch + IDT IDT IDT IDT if not os.path.exists('./weights'): + IDT IDT IDT IDT IDT os.makedirs('./weights') + IDT IDT IDT IDT saver.save(session, './weights/Vqa.weights') + IDT IDT saver.restore(session, './weights/Vqa.weights') + IDT IDT + IDT IDT pred_indices, _ = model.predict(session, model.test_set) + IDT IDT test_ans_list = [x[2] for x in model.test_set] + IDT IDT num = len(test_ans_list) - len(test_ans_list) % config.batch_size + IDT IDT test_ans_list = test_ans_list[:num] + IDT IDT accuracy = np.equal(pred_indices, test_ans_list).sum() / len(pred_indices) + IDT IDT ) + + +if __name__ == '__main__': + IDT test_VQA() +import tensorflow as tf +import numpy as np +from data_exploration import batch_generator +import time +n_input = 4735 +n_hidden_1 = 900 +n_hidden_2 = 900 +n_hidden_3 = 90 +n_hidden_4 = 9 +n_classes = 1 +BATCHSIZE = 100 +batch_handle = batch_generator(BATCHSIZE) + + +def gen(): + IDT while True: + IDT IDT + IDT IDT yield next(batch_handle) + + +def mlp(): + IDT sequence1 = , 4735), dtype= ) IDT sequence2 = , 4735), dtype= ) IDT distance = , 1), dtype= ) IDT layer11 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(sequence1, weights['h1']), biases['b1'])), 1) + IDT layer12 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(sequence2, weights['h1']), biases['b1'])), 1) + IDT layer21 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer11, weights['h2']), biases['b2'])), 1) + IDT layer22 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer12, weights['h2']), biases['b2'])), 1) + IDT layer31 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer21, weights['h3']), biases['b3'])), 1) + IDT layer32 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer22, weights['h3']), biases['b3'])), 1) + IDT layer41 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer31, weights['h4']), biases['b4'])), 1) + IDT layer42 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer32, weights['h4']), biases['b4'])), 1) + IDT layer5 = tf.nn.tanh(tf.add(layer41, layer42)) + IDT predict = , 1, keepdims= ) IDT error = tf.reduce_mean(tf.square(tf.subtract(predict, distance))) + IDT optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(error) + IDT init = tf.global_variables_initializer() + IDT return sequence1, sequence2, init, distance, error, optimizer + + +weights = , n_hidden_1], stddev= , 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev= , 'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3], stddev= , 'h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4], stddev= , 'out': tf.Variable(tf.random_normal([n_hidden_4, n_classes], stddev= )biases = {'b1': tf.Variable(tf.random_normal([n_hidden_1])), 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 'b3': tf.Variable(tf.random_normal([n_hidden_3])), 'b4': tf.Variable(tf.random_normal([n_hidden_4])), 'out': tf.Variable(tf.random_normal([n_classes]))} + + +def data_convert(): + IDT a, b = next(batch_handle) + IDT x = np.array(a) + IDT y = np.array(b) + IDT s1 = x[:, 0:4735] + IDT s2 = x[:, 4735:] + IDT d = y + IDT return s1, s2, d + + +if __name__ == '__main__': + IDT start = time.time() + IDT s1, s2, init, distance, out, opt = mlp() + IDT with tf.Session() as session: + IDT IDT session.run(init) + IDT IDT for i in range(0, 100): + IDT IDT IDT seq1, seq2, dis = data_convert() + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT - start)) +import tensorflow as tf +import numpy as np +import time +import random +from src.attention_wrapper import BahdanauAttention, AttentionWrapper + + +class model(object): + + IDT def __init__(self, params, embedding): + IDT IDT random.seed(params['seed']) + IDT IDT self.keep_prob_in = params['dropout_input'] + IDT IDT self.keep_prob_fc = params['dropout_fully_connected'] + IDT IDT self.hidden_unit_lstm = params['hidden_unit_lstm'] + IDT IDT self.hidden_unit_fc = params['hidden_unit_fully_connected'] + IDT IDT self.setup_placeholder() + IDT IDT self.setup_embedding(embedding) + IDT IDT self.setup_model() + IDT IDT self.setup_optimizer() + + IDT def encoder(self): + IDT IDT with tf.variable_scope('encoded_headline'): + IDT IDT IDT headline_lstm = tf.contrib.rnn.BasicLSTMCell(self.hidden_unit_lstm) + IDT IDT IDT self.encoded_headline, _ = , self.headline_embedding, self.H, dtype= ) IDT IDT with tf.variable_scope('encoded_document'): + IDT IDT IDT document_lstm = tf.contrib.rnn.BasicLSTMCell(self.hidden_unit_lstm) + IDT IDT IDT self.encoded_document, _ = , self.document_embedding, self.D, dtype= ) + IDT def decoder(self): + IDT IDT with tf.variable_scope('attention'): + IDT IDT IDT attention_mechanism = , self.encoded_headline, memory_sequence_length= ) IDT IDT IDT lstm = tf.contrib.rnn.BasicLSTMCell(self.hidden_unit_lstm) + IDT IDT IDT attention = , attention_mechanism, output_attention= , attention_input_fn= , y: tf.concat([x, y], axis= ) IDT IDT IDT weight, _ = , self.encoded_document, dtype= ) IDT IDT self.weight = , axis= ) + IDT def fully_connected(self): + IDT IDT with tf.variable_scope('fully_connected'): + IDT IDT IDT h1 = , self.hidden_unit_fc, activation_fn= ) IDT IDT IDT h2 = tf.nn.dropout(tf.nn.relu(tf.contrib.layers.batch_norm(h1)), self.dropout_fc) + IDT IDT IDT self.logits = , 2, activation_fn= ) + IDT def setup_placeholder(self): + IDT IDT self.headline = , shape= , None], name= ) IDT IDT self.document = , shape= , None], name= ) IDT IDT self.H = , shape= , name= ) IDT IDT self.D = , shape= , name= ) IDT IDT self.stance = , shape= , 2], name= ) IDT IDT self.dropout_in = , shape= , name= ) IDT IDT self.dropout_fc = , shape= , name= ) + IDT def setup_embedding(self, embedding): + IDT IDT _embedding = , name= , dtype= , trainable= ) IDT IDT headline_embedding = , self.headline, name= ) IDT IDT document_embedding = , self.document, name= ) IDT IDT self.headline_embedding = tf.nn.dropout(headline_embedding, self.dropout_in) + IDT IDT self.document_embedding = tf.nn.dropout(document_embedding, self.dropout_in) + + IDT def setup_model(self): + IDT IDT self.encoder() + IDT IDT self.decoder() + IDT IDT self.fully_connected() + + IDT def setup_optimizer(self): + IDT IDT self.loss = tf.losses.mean_squared_error(self.stance, self.logits) + IDT IDT self.optimizer = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def feed(self, mini_batch, train= ) IDT IDT headline, document, stance, H, D = mini_batch + IDT IDT input_feed = {self.headline: headline, self.document: document, self.H: H, self.D: D, self.stance: stance, self.dropout_in: self.keep_prob_in if train else 1, self.dropout_fc: self.keep_prob_fc if train else 1} + IDT IDT return input_feed + + IDT def train(self, session, mini_batch): + IDT IDT _, loss = session.run([self.optimizer, self.loss], self.feed(mini_batch)) + IDT IDT return loss + + IDT def test(self, session, mini_batch): + IDT IDT logits, loss = session.run([self.logits, self.loss], self.feed(mini_batch, False)) + IDT IDT return logits, loss + + IDT def evaluate(self, session, dataset, batch_size, th): + IDT IDT prob = [] + IDT IDT acc = 0.0 + IDT IDT counter = {'agree': 0, 'disagree': 0, 'discuss': 0} + IDT IDT for j in range(0, len(dataset), batch_size): + IDT IDT IDT data = self.create_minibatch(dataset, batch_size, j) + IDT IDT IDT logits, loss = self.test(session, data) + IDT IDT IDT if len(dataset) - j < batch_size: + IDT IDT IDT IDT logits = logits[:len(dataset) - j] + IDT IDT IDT for i in logits: + IDT IDT IDT IDT prob.append(i) + IDT IDT IDT stances = data[2] + IDT IDT IDT for i in range(len(logits)): + IDT IDT IDT IDT if logits[i, 0] > th or logits[i, 1] > th: + IDT IDT IDT IDT IDT s = np.argmax(logits[i]) + IDT IDT IDT IDT IDT if stances[i, s] == 1: + IDT IDT IDT IDT IDT IDT acc += 1 + IDT IDT IDT IDT IDT if s == 0: + IDT IDT IDT IDT IDT IDT counter['agree'] += 1 + IDT IDT IDT IDT IDT elif s == 1: + IDT IDT IDT IDT IDT IDT counter['disagree'] += 1 + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT counter['discuss'] += 1 + IDT IDT IDT IDT IDT if np.sum(stances[i]) == 0: + IDT IDT IDT IDT IDT IDT acc += 1 + IDT IDT )) + IDT IDT + IDT IDT return prob + + IDT def create_minibatch(self, dataset, batch_size, start): + IDT IDT minibatch = list(dataset[start:start + batch_size]) + IDT IDT if len(minibatch) < batch_size: + IDT IDT IDT minibatch.extend(dataset[-(batch_size - len(minibatch)):]) + IDT IDT headline_length = max(len(h) for h, d, s in minibatch) + IDT IDT document_length = max(len(d) for h, d, s in minibatch) + IDT IDT headlines, documents, stances = [], [], [] + IDT IDT for h, d, s in minibatch: + IDT IDT IDT headlines.append(h + [(0) for _ in range(headline_length - len(h))]) + IDT IDT IDT documents.append(d + [(0) for _ in range(document_length - len(d))]) + IDT IDT IDT stances.append(s) + IDT IDT return np.array(headlines), np.array(documents), np.array(stances), np.array([headline_length for _ in range(batch_size)]), np.array([document_length for _ in range(batch_size)]) + + IDT def run(self, session, dataset, num_epoch, batch_size, threshold): + IDT IDT train, test = dataset + IDT IDT start = time.time() + IDT IDT for i in range(num_epoch): + IDT IDT IDT random.shuffle(train) + IDT IDT IDT for j in range(0, len(train), batch_size): + IDT IDT IDT IDT mini_batch = self.create_minibatch(train, batch_size, j) + IDT IDT IDT IDT loss = self.train(session, mini_batch) + IDT IDT IDT IDT if j % (50 * batch_size) == 0: + IDT IDT IDT IDT IDT - start) / 60, loss)) + IDT IDT IDT if i % 5 == 0: + IDT IDT IDT IDT self.evaluate(session, test, batch_size, threshold) + IDT IDT IDT tf.train.Saver().save(session, './model/model_{}_epoch.chk'.format(i + 1)) +import numpy as np +import tensorflow as tf + + +class AutoEncoder: + + IDT def __init__(self, model_name, keep_prob, learning_rate, dim): + IDT IDT self.dim = dim + IDT IDT self.keep_prob = keep_prob + IDT IDT self.model_name = model_name + IDT IDT self.learning_rate = learning_rate + IDT IDT self.declare_placeholder() + IDT IDT self.contruct_network() + IDT IDT self.define_loss() + IDT IDT self.define_optimizer() + IDT IDT self.define_saver() + IDT IDT self.create_session() + IDT IDT self.declare_init() + IDT IDT self.run_init() + + IDT def declare_placeholder(self): + IDT IDT self.x = , shape= , self.dim[0], self.dim[1])) ) IDT IDT self.dropout = tf.placeholder(tf.float32) + + IDT def contruct_network(self): + IDT IDT self.encoder_1W = tf.Variable(tf.truncated_normal([self.dim[0] * self.dim[1], int(self.dim[0] * self.dim[1] / 2)])) + IDT IDT self.encoder_1b = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2)])) + IDT IDT self.encoder_2W = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2), int(self.dim[0] * self.dim[1] / 4)])) + IDT IDT self.encoder_2b = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 4)])) + IDT IDT self.decoder_1W = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 4), int(self.dim[0] * self.dim[1] / 2)])) + IDT IDT self.decoder_1b = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2)])) + IDT IDT self.decoder_2W = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2), self.dim[0] * self.dim[1]])) + IDT IDT self.decoder_2b = tf.Variable(tf.truncated_normal([self.dim[0] * self.dim[1]])) + IDT IDT self.enc1 = tf.nn.tanh(tf.nn.bias_add(tf.matmul(tf.reshape(self.x, [-1, self.dim[0] * self.dim[1]]), self.encoder_1W), self.encoder_1b)) + IDT IDT self.enc1 = , keep_prob= ) IDT IDT self.feat = tf.nn.tanh(tf.nn.bias_add(tf.matmul(self.enc1, self.encoder_2W), self.encoder_2b)) + IDT IDT self.feat = , keep_prob= ) IDT IDT self.dec1 = tf.nn.tanh(tf.nn.bias_add(tf.matmul(self.feat, self.decoder_1W), self.decoder_1b)) + IDT IDT self.dec1 = , keep_prob= ) IDT IDT self.output = tf.reshape(tf.nn.bias_add(tf.matmul(self.dec1, self.decoder_2W), self.decoder_2b), [-1, self.dim[0], self.dim[1]]) + IDT IDT self.output = , keep_prob= ) + IDT def define_loss(self): + IDT IDT self.loss = tf.reduce_mean(tf.square(self.output - self.x)) + + IDT def define_optimizer(self): + IDT IDT self.optimizer = ) + IDT def declare_init(self): + IDT IDT self.init = tf.global_variables_initializer() + + IDT def run_init(self): + IDT IDT self.session.run(self.init) + + IDT def create_session(self): + IDT IDT self.session = tf.Session() + + IDT def define_saver(self): + IDT IDT self.saver = tf.train.Saver() + + IDT def save_model(self): + IDT IDT self.saver.save(self.session, self.model_name) + + IDT def restore_model(self): + IDT IDT self.saver.restore(self.session, self.model_name) + + IDT def run_training(self, input): + IDT IDT self.session.run(self.optimizer, feed_dict= , self.dropout: self.keep_prob}) ) + IDT def get_loss(self, input): + IDT IDT return self.session.run(self.loss, feed_dict= , self.dropout: 1}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +import logging +from adam import ADAMOptimizer +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) + + +class RBM(object): + + IDT def __init__(self, n_features, n_hidden, visible_placeholder= , contrastive_divergence_level= , learning_rate= , persistent_contrastive_divergence= , n_fantasy_states= , name= ) IDT IDT """ + IDT IDT A restricted boltzmann machine implemented in tensorflow + IDT IDT + IDT IDT :param n_features: The input dimensionality + IDT IDT :param n_hidden: The hidden layer size + IDT IDT :param visible_placeholder: Optional. Is used for chaining. + IDT IDT A tensorflow tensor that represents the input [batch x n_features]. + IDT IDT :param contrastive_divergence_level: How often to sample to get the model distribution + IDT IDT :param learning_rate: The learning rate for gradient ascent on the likelihood + IDT IDT :param persistent_contrastive_divergence: Use a persistent model state for model distribution. Replaces + IDT IDT contrastive divergence + IDT IDT :param fantasy_states: Number of fantasy samples to keep in the persistent mode. + IDT IDT :param name: tensorflow name of this model + IDT IDT """ + IDT IDT if visible_placeholder is None: + IDT IDT IDT self.visible_placeholder = , shape= , n_features)) ) IDT IDT else: + IDT IDT IDT self.visible_placeholder = visible_placeholder + IDT IDT self.n_features = n_features + IDT IDT self.n_hidden = n_hidden + IDT IDT self.learning_rate = learning_rate + IDT IDT self.contrastive_divergence_level = contrastive_divergence_level + IDT IDT self.persistent_contrastive_divergence = persistent_contrastive_divergence + IDT IDT self.name = name + IDT IDT with tf.name_scope(name): + IDT IDT IDT self.w = , self.n_hidden), mean= , stddev= , name= ) IDT IDT IDT self.b_h = ,), mean= , stddev= , name= ) IDT IDT IDT self.b_v = ,), mean= , stddev= , name= ) IDT IDT IDT self.hidden_data_probability = tf.nn.sigmoid(tf.matmul(self.visible_placeholder, self.w) + self.b_h) + IDT IDT IDT self.hidden_state_plus = RBM._sample_from(self.hidden_data_probability, 'hidden_state_plus') + IDT IDT IDT self.hidden_placeholder = , shape= , self.n_hidden)) ) IDT IDT IDT visible_prop = , self.w, transpose_b= ) IDT IDT IDT self.visible_from_hidden = RBM._sample_from(visible_prop, 'visible_from_hidden') + IDT IDT IDT if self.persistent_contrastive_divergence: + IDT IDT IDT IDT self.visible_state_persistent = , self.n_features), dtype= , trainable= , name= ) IDT IDT IDT IDT self.hidden_state_persistent = , self.n_hidden), dtype= , trainable= , name= ) IDT IDT IDT IDT current_hidden_state = self.hidden_state_persistent + IDT IDT IDT IDT current_visible_state = self.visible_state_persistent + IDT IDT IDT IDT for i in range(self.contrastive_divergence_level): + IDT IDT IDT IDT IDT hidden_probability = tf.nn.sigmoid(tf.matmul(current_visible_state, self.w) + self.b_h) + IDT IDT IDT IDT IDT current_hidden_state = RBM._sample_from(hidden_probability, 'hidden_state_minus_{}'.format(i)) + IDT IDT IDT IDT IDT visible_probability = , self.w, transpose_b= ) IDT IDT IDT IDT IDT current_visible_state = RBM._sample_from(visible_probability, 'visible_state_minus_{}'.format(i)) + IDT IDT IDT IDT self.visible_state_minus = , current_visible_state, name= ) IDT IDT IDT IDT self.hidden_state_minus = , current_hidden_state, name= ) IDT IDT IDT else: + IDT IDT IDT IDT current_hidden = self.hidden_data_probability + IDT IDT IDT IDT for i in range(self.contrastive_divergence_level): + IDT IDT IDT IDT IDT visible_prop = , self.w, transpose_b= ) IDT IDT IDT IDT IDT self.visible_state_minus = RBM._sample_from(visible_prop, 'visible_state_minus_{}'.format(i)) + IDT IDT IDT IDT IDT hidden_prop = tf.nn.sigmoid(tf.matmul(self.visible_state_minus, self.w) + self.b_h) + IDT IDT IDT IDT IDT self.hidden_state_minus = RBM._sample_from(hidden_prop, 'hidden_state_minus_{}'.format(i)) + IDT IDT IDT gradient_w = , self.hidden_state_plus, transpose_a= , self.hidden_state_minus, transpose_a= , dtype= ) IDT IDT IDT gradient_b_h = , reduction_indices= , reduction_indices= ) IDT IDT IDT gradient_b_v = , reduction_indices= , reduction_indices= ) IDT IDT IDT self.reconstruction_error = , axis= ) IDT IDT IDT beta = 0.2 + IDT IDT IDT self.update_w = , minimize= , beta1= , beta2= , learning_rate= ) IDT IDT IDT self.update_b_h = , minimize= , beta1= , beta2= , learning_rate= ) IDT IDT IDT self.update_b_v = , minimize= , beta1= , beta2= , learning_rate= ) IDT IDT IDT self.train_step = [self.update_w, self.update_b_h, self.update_b_v] + IDT IDT IDT self.logarithmic_energy = , self.w) * self.hidden_placeholder, axis= , axis= , axis= ) IDT IDT IDT self.state_probabilities = tf.nn.softmax(self.logarithmic_energy) + + IDT def _sample_from(vector, name): + IDT IDT return tf.cast(tf.less(tf.random_uniform(tf.shape(vector)), vector), tf.float32, name= ) + IDT def train(self, features, n_iterations= , batch_size= , session= ) IDT IDT """ + IDT IDT Perform training of the boltzmann machine given an input distribution + IDT IDT + IDT IDT :param features: numpy array representing the input distribution [n_samples x n_features] + IDT IDT :param n_iterations: Number of iterations to train + IDT IDT :param batch_size: Number of samples to use in one training iteration + IDT IDT :param session: tensorflow session to use for the training. If None, default will be used. + IDT IDT """ + IDT IDT if session is None: + IDT IDT IDT session = tf.get_default_session() + IDT IDT train_range = range(n_iterations) + IDT IDT n_samples = features.shape[0] + IDT IDT for i in train_range: + IDT IDT IDT random_indices = , n_samples, size= ) IDT IDT IDT feed_dict = {self.visible_placeholder: features[random_indices]} + IDT IDT IDT reconstruction_error, _, _, _ = , self.update_b_h, self.update_w, self.update_b_v], feed_dict= ) IDT IDT IDT if i % 20 == 0: + IDT IDT IDT IDT logger.info('reconstruction error in iteration {} of {} is {}'.format(i, n_iterations, reconstruction_error)) + + IDT def sample_visible(self, n_samples= , hidden_states= , session= ) IDT IDT """ + IDT IDT Sample visible layer states from the boltzmann machine. Either use hidden_states for initializing the hidden + IDT IDT layer if given or use a binary random hidden_state. + IDT IDT + IDT IDT :param n_samples: Optional. Number of states to sample. + IDT IDT :param hidden_states: Optional. If given, sample the visible states from these hidden states. + IDT IDT :param session: tensorflow session to use. If None, use default session. + IDT IDT :return: An array with visible states. The number of states returned is n_samples if given or the row number of + IDT IDT hidden_states. If nothing was given, a single sample is returned. + IDT IDT """ + IDT IDT if session is None: + IDT IDT IDT session = tf.get_default_session() + IDT IDT if hidden_states is None: + IDT IDT IDT hidden_states = , 2, size= , self.n_hidden)) ) IDT IDT return session.run(self.visible_from_hidden, feed_dict= ) + IDT def sample_hidden(self, n_samples= , visible_states= , session= ) IDT IDT """ + IDT IDT Sample hidden layer states from the boltzmann machine. Either use visible_states for initializing the visible + IDT IDT layer if given or use a binary random hidden_state. + IDT IDT + IDT IDT :param n_samples: Optional. Number of states to sample. + IDT IDT :param visible_states: Optional. If given, sample the hidden states from these visible states. + IDT IDT :param session: tensorflow session to use. If None, use default session. + IDT IDT :return: An array with hidden states. The number of states returned is n_samples if given or the row number of + IDT IDT visible_states. If nothing was given, a single sample is returned. + IDT IDT """ + IDT IDT if session is None: + IDT IDT IDT session = tf.get_default_session() + IDT IDT if visible_states is None: + IDT IDT IDT visible_states = , 2, size= , self.n_features)) ) IDT IDT return session.run(self.hidden_state_plus, feed_dict= ) + +if __name__ == '__main__': + IDT import sklearn.datasets as datasets + IDT import sklearn.svm as svm + IDT digits = datasets.load_digits() + IDT features = , dtype= ) IDT target = digits.target + IDT n_features = features.shape[1] + IDT n_hidden = 300 + IDT n_hidden2 = 300 + IDT n_hidden3 = 300 + IDT n_batch = 2000 + IDT n_iterations = 1000 + IDT input_placeholder = , shape= , n_features)) ) IDT rbm = , n_hidden, input_placeholder, name= , n_fantasy_states= , persistent_contrastive_divergence= , contrastive_divergence_level= , learning_rate= ) IDT rbm2 = , n_hidden2, rbm.hidden_state_plus, learning_rate= , name= , n_fantasy_states= ) IDT rbm3 = , n_hidden3, rbm2.hidden_state_plus, learning_rate= , name= , n_fantasy_states= ) IDT svc = ) IDT svc2 = ) IDT svc2.fit(features[:-100], target[:-100]) + IDT direct_svc_score = svc2.score(features[-100:], target[-100:]) + IDT session = tf.Session() + IDT session.run(tf.initialize_all_variables()) + IDT for i in range(n_iterations): + IDT IDT random_indices = , features.shape[0], size= ) IDT IDT session.run([rbm.update_w, rbm.update_b_h, rbm.update_b_v], feed_dict= ) IDT IDT if i % 100 == 0: + IDT IDT IDT hidden = , feed_dict= ) IDT IDT IDT svc.fit(hidden[:-100], target[:-100]) + IDT IDT IDT , direct_svc_score)) + IDT IDT IDT )) + IDT IDT IDT hd = , feed_dict= ) IDT IDT IDT p = , feed_dict= , rbm.hidden_placeholder: hd}) ) IDT IDT IDT + IDT for i in range(n_iterations): + IDT IDT random_indices = , features.shape[0], size= ) IDT IDT session.run([rbm2.update_w, rbm2.update_b_h, rbm2.update_b_v], feed_dict= ) IDT for i in range(n_iterations): + IDT IDT random_indices = , features.shape[0], size= ) IDT IDT session.run([rbm3.update_w, rbm3.update_b_h, rbm3.update_b_v], feed_dict= ) IDT IDT if i % 500 == 0: + IDT IDT IDT hidden = , feed_dict= ) IDT IDT IDT svc.fit(hidden[:-100], target[:-100]) + IDT IDT IDT , direct_svc_score)) + IDT import matplotlib.pyplot as plt + IDT for i in range(20): + IDT IDT hidden = , feed_dict= , 64))}) ) IDT IDT hidden = , 2, size= , n_hidden3)) ) IDT IDT hidden3 = , feed_dict= ) IDT IDT hidden2 = , feed_dict= ) IDT IDT visible = , feed_dict= ) IDT IDT plt.imshow(visible.reshape(8, 8), cmap= ) IDT IDT plt.show() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class GRUAttnCell(tf.nn.rnn_cell.GRUCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(GRUAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(gru_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(tf.nn.rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, gru_state + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(LSTMAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, lstm_state + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT IDT -vocab_dim: dimension of the embeddings + IDT """ + + IDT def __init__(self, size, vocab_dim, name): + IDT IDT self.size = size + IDT IDT self.vocab_dim = vocab_dim + IDT IDT self.name = name + + IDT def encode(self, inputs, masks, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(self.name): + IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(self.size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT elif model_type == 'gru': + IDT IDT IDT IDT cell = GRUAttnCell(self.size, attention_inputs) + IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT cell = LSTMAttnCell(self.size, attention_inputs) + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT outputs, final_state = , inputs, sequence_length= , dtype= , initial_state= ) IDT IDT return outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, output_size, name): + IDT IDT self.output_size = output_size + IDT IDT self.name = name + + IDT def decode(self, knowledge_rep, masks, dropout, state_size, model_type= ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT + IDT + IDT + IDT + + IDT + IDT + IDT + IDT + IDT IDT with tf.variable_scope(self.name): + IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(state_size) + IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(state_size) + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT knowledge_rep = tf.squeeze(knowledge_rep, -1) + IDT IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer1'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer2'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer3'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer4'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, self.output_size, True, 1.0) + IDT IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer1'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer2'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer3'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer4'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, self.output_size, True, 1.0) + IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT a = tf.constant(-1e+30) + IDT IDT b = tf.constant(1.0) + IDT IDT add_mask = a * (b - bool_masks) + IDT IDT start_probs = start_probs + add_mask + IDT IDT end_probs = end_probs + add_mask + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, encoder, decoder, pretrained_embeddings, max_ctx_len, max_q_len, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param encoder: tuple of 2 encoders that you constructed in train.py + IDT IDT :param decoder: a decoder that you constructed in train.py + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.question_encoder, self.context_encoder = encoder + IDT IDT self.decoder = decoder + IDT IDT self.max_ctx_len = max_ctx_len + IDT IDT self.max_q_len = max_q_len + IDT IDT self.embed_size = encoder[0].vocab_dim + IDT IDT self.flags = flags + IDT IDT self.context_placeholder = , shape= , self.max_ctx_len), name= ) IDT IDT self.question_placeholder = , shape= , self.max_q_len), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def mixer(self, final_q_state, ctx_states, model_type= ) IDT IDT if model_type == 'gru': + IDT IDT IDT pass + IDT IDT elif model_type == 'lstm': + IDT IDT IDT final_q_state = final_q_state[-1] + IDT IDT else: + IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT with vs.variable_scope('mixer'): + IDT IDT IDT ht = tf.nn.rnn_cell._linear(final_q_state, self.flags.state_size, True, 1.0) + IDT IDT IDT ht = , axis= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT ctx_state_rep = ctx_states * scores + IDT IDT return ctx_state_rep + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT question_states, final_question_state = , self.mask_q_placeholder, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT feed_states = batch_matmul(ctx_states, tf.expand_dims(final_question_state, 2)) + IDT IDT self.start_probs, self.end_probs = , self.mask_ctx_placeholder, self.dropout_placeholder, self.flags.state_size, model_type= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT self.question_embeddings = tf.reshape(question_embeddings, [-1, self.max_q_len, self.embed_size]) + IDT IDT IDT context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT self.context_embeddings = tf.reshape(context_embeddings, [-1, self.max_ctx_len, self.embed_size]) + + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = np.array(map(np.array, context_batch)) + IDT IDT input_feed[self.question_placeholder] = np.array(map(np.array, question_batch)) + IDT IDT input_feed[self.mask_ctx_placeholder] = np.array(map(np.array, mask_ctx_batch)) + IDT IDT input_feed[self.mask_q_placeholder] = np.array(mask_q_batch) + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def answer(self, session, data): + IDT IDT data = np.array(data).T + IDT IDT yp, yp2 = self.decode(session, *data) + IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch) + + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset) + IDT IDT else: + IDT IDT IDT sampled = dataset[np.random.choice(dataset.shape[0], sample)] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT sampled = sampled.T + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][sampled[2][i][0]:sampled[2][i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT em.append(exact_match_score(pred_words, actual_words)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), None, sample)) + IDT IDT return f1, em + + IDT def run_epoch(self, sess, train_set, val_set, context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT if self.flags.debug == 0 or self.flags.debug == 1: + IDT IDT IDT prog_val = ) IDT IDT IDT for i, batch in enumerate(minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT IDT IDT + IDT IDT IDT train_f1, train_em = , train_set, context= , sample= , log= , eval_set= ) IDT IDT IDT val_f1, val_em = , val_set, context= , sample= , log= , eval_set= ) + IDT def train(self, session, saver, dataset, contexts, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT train_dataset, val_dataset = dataset + IDT IDT train_mask = [None, None] + IDT IDT val_mask = [None, None] + IDT IDT train_dataset[0], train_mask[0] = self.pad(train_dataset[0], self.max_ctx_len) + IDT IDT train_dataset[1], train_mask[1] = self.pad(train_dataset[1], self.max_q_len) + IDT IDT val_dataset[0], val_mask[0] = self.pad(val_dataset[0], self.max_ctx_len) + IDT IDT val_dataset[1], val_mask[1] = self.pad(val_dataset[1], self.max_q_len) + IDT IDT for i in range(1, len(train_dataset[0])): + IDT IDT IDT assert len(train_dataset[0][i]) = ) IDT IDT IDT assert len(train_dataset[1][i]) = ) IDT IDT for i in range(1, len(val_dataset[0])): + IDT IDT IDT assert len(val_dataset[0][i]) = ) IDT IDT IDT assert len(val_dataset[1][i]) = ) IDT IDT + IDT IDT train_dataset.extend(train_mask) + IDT IDT val_dataset.extend(val_mask) + IDT IDT train_dataset = np.array(train_dataset).T + IDT IDT val_dataset = np.array(val_dataset).T + IDT IDT train_context = contexts[0] + IDT IDT val_context = contexts[1] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = train_dataset[:self.flags.batch_size] + IDT IDT IDT val_dataset = val_dataset[:self.flags.batch_size] + IDT IDT IDT num_epochs = 20 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT saver.save(session, train_dir) + IDT IDT self.evaluate_answer(session, train_dataset, train_context, sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session, val_dataset, val_context, sample= , log= , eval_set= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + + +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = , labels= ) IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y1 = tf.nn.dropout(y1, 0.5) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = , labels= ) IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT global_step = tf.Variable(0) + IDT W1 = , fc1_size], stddev= ) IDT b1 = tf.Variable(tf.zeros([fc1_size])) + IDT W2 = , fc2_size], stddev= ) IDT b2 = tf.Variable(tf.zeros([fc2_size])) + IDT W3 = , fc3_size], stddev= ) IDT b3 = tf.Variable(tf.zeros([fc3_size])) + IDT W4 = , num_labels], stddev= ) IDT b4 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + IDT y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + IDT logits = tf.matmul(y3, W4) + b4 + IDT loss = , labels= ) IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + IDT learning_rate = , global_step, 1000, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + IDT y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + IDT valid_logits = tf.matmul(y3_valid, W4) + b4 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + IDT y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + IDT test_logits = tf.matmul(y3_test, W4) + b4 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +from tqdm import tqdm +from utils import BatchGenerator +import os +import pdb + + +class NNClassifier: + + IDT def _inference(self, X, is_train): + IDT IDT self.hidden_size = hidden_size = 30 + IDT IDT self.embed_placeholder = , shape= , 300]) ) IDT IDT dropout = 0.3 + IDT IDT word_embedding = self.embed_placeholder + IDT IDT embedding_b = tf.nn.embedding_lookup(word_embedding, X[:, (0), :]) + IDT IDT embedding_q = tf.nn.embedding_lookup(word_embedding, X[:, (1), :]) + IDT IDT with tf.variable_scope('Body_GRU'): + IDT IDT IDT with tf.variable_scope('Body_GRU_fw'): + IDT IDT IDT IDT cell_b_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT with tf.variable_scope('Body_GRU_bw'): + IDT IDT IDT IDT cell_b_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT lengths_b = , (0), :]), axis= ) IDT IDT IDT output_b, out_state_b = , cell_b_fw, embedding_b, sequence_length= , dtype= ) IDT IDT IDT output_b = , output_b[1]], axis= ) IDT IDT IDT out_state_b = , out_state_b[1]], axis= ) IDT IDT with tf.variable_scope('Question_GRU'): + IDT IDT IDT with tf.variable_scope('Question_GRU_fw'): + IDT IDT IDT IDT cell_q_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT with tf.variable_scope('Question_GRU_bw'): + IDT IDT IDT IDT cell_q_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + IDT IDT IDT lengths_q = , (1), :]), axis= ) IDT IDT IDT output_q, out_state_q = , cell_q_fw, embedding_q, sequence_length= , dtype= ) IDT IDT IDT output_q = , output_q[1]], axis= ) IDT IDT IDT out_state_q = , out_state_q[1]], axis= ) IDT IDT C_d = self._dynamic_coattention(output_b, output_q) + IDT IDT with tf.variable_scope('match_lstm_GRU_dynamic_coattention'): + IDT IDT IDT with tf.variable_scope('match_gru_fw'): + IDT IDT IDT IDT cell_m_fw = tf.nn.rnn_cell.GRUCell(2 * hidden_size) + IDT IDT IDT with tf.variable_scope('match_gru_bw'): + IDT IDT IDT IDT cell_m_bw = tf.nn.rnn_cell.GRUCell(2 * hidden_size) + IDT IDT IDT lengths_q = , (1), :]), axis= ) IDT IDT IDT output_m, out_state_m = , cell_m_fw, C_d, sequence_length= , dtype= ) IDT IDT IDT output_m = , output_m[1]], axis= ) IDT IDT IDT out_state_m = , out_state_m[1]], axis= ) IDT IDT output = , out_state_q, out_state_b], axis= ) IDT IDT output = , units= ) IDT IDT output = tf.nn.relu(output) + IDT IDT dense = , units= ) IDT IDT return dense + + IDT def _dynamic_coattention(self, output_b, output_q): + IDT IDT with tf.variable_scope('dynamic_coattention'): + IDT IDT IDT timestep = tf.shape(output_q)[1] + IDT IDT IDT batch_size = tf.shape(output_q)[0] + IDT IDT IDT L = tf.matmul(output_b, tf.transpose(output_q, [0, 2, 1])) + IDT IDT IDT A_q = , dim= , name= ) IDT IDT IDT A_b = , dim= , [0, 2, 1], name= ) IDT IDT IDT C_q = tf.matmul(tf.transpose(A_q, [0, 2, 1]), output_b) + IDT IDT IDT C_b = tf.matmul(tf.transpose(A_b, [0, 2, 1]), output_q) + IDT IDT IDT C_d = , [0, 2, 1]), tf.concat([output_q, C_q], axis= ) IDT IDT return C_d + + IDT def _match_lstm(self, i, output_b, output_q, a_array): + IDT IDT hidden_size = self.hidden_size * 2 + IDT IDT timestep = tf.shape(output_b)[1] + IDT IDT output_q_i = tf.reshape(output_q[:, (i), :], [-1, 1, hidden_size]) + IDT IDT with tf.variable_scope('match_lstm'): + IDT IDT IDT we = , shape= , 1, hidden_size], dtype= , initializer= , 1)) ) IDT IDT IDT W_s = , shape= , hidden_size], dtype= , initializer= , 1)) ) IDT IDT IDT W_t = , shape= , hidden_size], dtype= , initializer= , 1)) ) IDT IDT IDT temp1 = tf.reshape(tf.matmul(tf.reshape(output_b, [-1, hidden_size]), W_s), [-1, timestep, hidden_size]) + IDT IDT IDT temp2 = tf.reshape(tf.matmul(tf.reshape(output_q_i, [-1, hidden_size]), W_t), [-1, 1, hidden_size]) + IDT IDT IDT middle = tf.tanh(temp1 + temp2) + IDT IDT IDT e_i = , axis= , keep_dims= ) IDT IDT IDT a_i = , dim= ) IDT IDT IDT a_i = , axis= , keep_dims= ) IDT IDT IDT a_array = , tf.concat([a_i, tf.reshape(output_q_i, [-1, hidden_size])], axis= ) IDT IDT i = tf.add(i, 1) + IDT IDT return i, output_b, output_q, a_array + + IDT def _attention(self, inputs, attention_size, time_major= , return_alphas= ) IDT IDT """ + IDT IDT Attention mechanism layer which reduces RNN/Bi-RNN outputs with Attention vector. + IDT IDT The idea was proposed in the article by Z. Yang et al., "Hierarchical Attention Networks + IDT IDT for Document Classification", 2016: http://www.aclweb.org/anthology/N16-1174. + IDT IDT Args: + IDT IDT IDT inputs: The Attention inputs. + IDT IDT IDT IDT Matches outputs of RNN/Bi-RNN layer (not final state): + IDT IDT IDT IDT IDT In case of RNN, this must be RNN outputs `Tensor`: + IDT IDT IDT IDT IDT IDT If time_major == False (default), this must be a tensor of shape: + IDT IDT IDT IDT IDT IDT IDT `[batch_size, max_time, cell.output_size]`. + IDT IDT IDT IDT IDT IDT If time_major == True, this must be a tensor of shape: + IDT IDT IDT IDT IDT IDT IDT `[max_time, batch_size, cell.output_size]`. + IDT IDT IDT IDT IDT In case of Bidirectional RNN, this must be a tuple (outputs_fw, outputs_bw) containing the forward and + IDT IDT IDT IDT IDT the backward RNN outputs `Tensor`. + IDT IDT IDT IDT IDT IDT If time_major == False (default), + IDT IDT IDT IDT IDT IDT IDT outputs_fw is a `Tensor` shaped: + IDT IDT IDT IDT IDT IDT IDT `[batch_size, max_time, cell_fw.output_size]` + IDT IDT IDT IDT IDT IDT IDT and outputs_bw is a `Tensor` shaped: + IDT IDT IDT IDT IDT IDT IDT `[batch_size, max_time, cell_bw.output_size]`. + IDT IDT IDT IDT IDT IDT If time_major == True, + IDT IDT IDT IDT IDT IDT IDT outputs_fw is a `Tensor` shaped: + IDT IDT IDT IDT IDT IDT IDT `[max_time, batch_size, cell_fw.output_size]` + IDT IDT IDT IDT IDT IDT IDT and outputs_bw is a `Tensor` shaped: + IDT IDT IDT IDT IDT IDT IDT `[max_time, batch_size, cell_bw.output_size]`. + IDT IDT IDT attention_size: Linear size of the Attention weights. + IDT IDT IDT time_major: The shape format of the `inputs` Tensors. + IDT IDT IDT IDT If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`. + IDT IDT IDT IDT If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`. + IDT IDT IDT IDT Using `time_major = True` is a bit more efficient because it avoids + IDT IDT IDT IDT transposes at the beginning and end of the RNN calculation. However, + IDT IDT IDT IDT most TensorFlow data is batch-major, so by default this function + IDT IDT IDT IDT accepts input and emits output in batch-major form. + IDT IDT IDT return_alphas: Whether to return attention coefficients variable along with layer's output. + IDT IDT IDT IDT Used for visualization purpose. + IDT IDT Returns: + IDT IDT IDT The Attention output `Tensor`. + IDT IDT IDT In case of RNN, this will be a `Tensor` shaped: + IDT IDT IDT IDT `[batch_size, cell.output_size]`. + IDT IDT IDT In case of Bidirectional RNN, this will be a `Tensor` shaped: + IDT IDT IDT IDT `[batch_size, cell_fw.output_size + cell_bw.output_size]`. + IDT IDT """ + IDT IDT if isinstance(inputs, tuple): + IDT IDT IDT inputs = tf.concat(inputs, 2) + IDT IDT if time_major: + IDT IDT IDT inputs = tf.array_ops.transpose(inputs, [1, 0, 2]) + IDT IDT inputs_shape = inputs.shape + IDT IDT sequence_length = inputs_shape[1].value + IDT IDT hidden_size = inputs_shape[2].value + IDT IDT with tf.variable_scope('attention'): + IDT IDT IDT W_omega = , shape= , attention_size], dtype= , initializer= , 0.1)) ) IDT IDT IDT b_omega = , shape= , dtype= , initializer= , 0.1)) ) IDT IDT IDT u_omega = , shape= , dtype= , initializer= , 0.1)) ) IDT IDT v = tf.tanh(tf.matmul(tf.reshape(inputs, [-1, hidden_size]), W_omega) + tf.reshape(b_omega, [1, -1])) + IDT IDT vu = tf.matmul(v, tf.reshape(u_omega, [-1, 1])) + IDT IDT exps = tf.reshape(tf.exp(vu), [-1, sequence_length]) + IDT IDT alphas = exps / tf.reshape(tf.reduce_sum(exps, 1), [-1, 1]) + IDT IDT output = tf.reduce_sum(inputs * tf.reshape(alphas, [-1, sequence_length, 1]), 1) + IDT IDT if not return_alphas: + IDT IDT IDT return output + IDT IDT else: + IDT IDT IDT return output, alphas + + IDT def _iter(self, X, y, tensor_loss, train_op, placeholder, metric_tensors): + IDT IDT self._session.run(tf.local_variables_initializer()) + IDT IDT metric_scores = {} + IDT IDT for metric in self._metrics: + IDT IDT IDT metric_scores[metric] = 0 + IDT IDT batch_generator = BatchGenerator(X, y, self._batch_size) + IDT IDT for b in tqdm(range(X.shape[0] // self._batch_size + 1)): + IDT IDT IDT batch = next(batch_generator) + IDT IDT IDT feed_dict = {placeholder['x']: batch['x'], placeholder['y']: batch['y'], self.embed_placeholder: self._embedding} + IDT IDT IDT if train_op is not None: + IDT IDT IDT IDT loss, _, metrics = , train_op, metric_tensors], feed_dict= ) IDT IDT IDT else: + IDT IDT IDT IDT loss, metrics = , metric_tensors], feed_dict= ) IDT IDT if train_op == None: + IDT IDT IDT self._history.append(metrics['accuracy'][1]) + IDT IDT IDT if self._early_stop != None: + IDT IDT IDT IDT self._history[-1] = max(self._history[-1:-self._early_stop - 1:-1]) + IDT IDT summary = tf.Summary() + IDT IDT summary.value.add(tag= , simple_value= ) IDT IDT + IDT IDT for metric in self._metrics: + IDT IDT IDT score = float(metrics[metric][1]) + IDT IDT IDT summary.value.add(tag= , simple_value= ) IDT IDT IDT , end='') + IDT IDT + IDT IDT return summary + + IDT def __init__(self, learning_rate= , batch_size= , n_iters= , name= , valid= , embedding= , early_stop= ) IDT IDT self._batch_size = batch_size + IDT IDT self._n_iters = n_iters + IDT IDT self._metrics = {'accuracy': tf.metrics.accuracy} + IDT IDT self._global_step = , dtype= , trainable= , name= ) IDT IDT self._loss = tf.losses.softmax_cross_entropy + IDT IDT self._optimizer = tf.train.AdamOptimizer(learning_rate) + IDT IDT self._name = name + IDT IDT self._valid = valid + IDT IDT self._embedding = embedding + IDT IDT self._history = [] + IDT IDT self._early_stop = early_stop + + IDT def __del__(self): + IDT IDT if not self._session._closed: + IDT IDT IDT self._session.close() + IDT IDT tf.reset_default_graph() + + IDT def fit(self, X, y): + IDT IDT self._n_classes = y.shape[1] + IDT IDT os.mkdir(self._name) + IDT IDT os.mkdir(os.path.join(self._name, 'train')) + IDT IDT os.mkdir(os.path.join(self._name, 'valid')) + IDT IDT placeholder = , shape= , X.shape[1], X.shape[2])), 'y': tf.placeholder(tf.int32, shape= , y.shape[1])) ) IDT IDT with tf.variable_scope('nn') as scope: + IDT IDT IDT y_prob = , is_train= ) IDT IDT IDT loss = self._loss(placeholder['y'], y_prob) + IDT IDT IDT reg_const = 0.0005 + IDT IDT IDT l2 = reg_const * sum([tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()]) + IDT IDT IDT loss += l2 + IDT IDT IDT yp = tf.cast(placeholder['y'], tf.float32) + IDT IDT IDT yt = y_prob + IDT IDT IDT pos = , axis= ) IDT IDT IDT neg = , reduction_indices= ) IDT IDT IDT train_op = self._optimizer.minimize(loss) + IDT IDT IDT add_global_step_op = tf.assign(self._global_step, self._global_step + 1) + IDT IDT IDT metric_tensors = {} + IDT IDT IDT for metric in self._metrics: + IDT IDT IDT IDT y_pred_argmax = , axis= ) IDT IDT IDT IDT y_true_argmax = , axis= ) IDT IDT IDT IDT metric_tensors[metric] = self._metrics[metric](y_true_argmax, y_pred_argmax) + IDT IDT gpu_options = ) IDT IDT self._session = ) IDT IDT summary_writer = {'train': tf.summary.FileWriter(os.path.join(self._name, 'train'), self._session.graph), 'valid': tf.summary.FileWriter(os.path.join(self._name, 'valid'), self._session.graph)} + IDT IDT self._session.run(tf.global_variables_initializer()) + IDT IDT saver = tf.train.Saver() + IDT IDT ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoint/')) + IDT IDT if ckpt and ckpt.model_checkpoint_path: + IDT IDT IDT saver.restore(self._session, ckpt.model_checkpoint_path) + IDT IDT initial_step = ) IDT IDT ) + IDT IDT for i in range(initial_step, self._n_iters): + IDT IDT IDT + IDT IDT IDT _ = self._session.run([add_global_step_op]) + IDT IDT IDT summary = self._iter(X, y, loss, train_op, placeholder, metric_tensors) + IDT IDT IDT summary_writer['train'].add_summary(summary, i) + IDT IDT IDT summary_writer['train'].flush() + IDT IDT IDT saver.save(self._session, 'checkpoint/session', i) + IDT IDT IDT if self._valid is not None: + IDT IDT IDT IDT + IDT IDT IDT IDT summary = self._iter(self._valid['x'], self._valid['y'], loss, None, placeholder, metric_tensors) + IDT IDT IDT IDT summary_writer['valid'].add_summary(summary, i) + IDT IDT IDT IDT summary_writer['valid'].flush() + IDT IDT IDT IDT if self._early_stop is not None: + IDT IDT IDT IDT IDT ) + IDT IDT IDT IDT IDT if len(self._history) > self._early_stop and self._history[-1] == self._history[-1 - self._early_stop]: + IDT IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT IDT return + + IDT def predict(self, X, prob= , remove_s= ) IDT IDT with tf.variable_scope('nn', reuse= ) IDT IDT IDT X_placeholder = , shape= , X.shape[1], X.shape[2])) ) IDT IDT IDT y_prob = , is_train= ) IDT IDT IDT if remove_s: + IDT IDT IDT IDT y_prob = tf.slice(y_prob, [0, 0], [X.shape[0], 5]) + IDT IDT IDT y_prob = tf.nn.softmax(y_prob) + IDT IDT IDT y_max = , axis= ) IDT IDT IDT y_pred = , tf.reshape(y_max, (-1, 1))), dtype= ) IDT IDT IDT y_, y_prob = , y_prob], feed_dict= , self.embed_placeholder: self._embedding}) ) IDT IDT if not prob: + IDT IDT IDT return y_ + IDT IDT else: + IDT IDT IDT return y_, y_prob +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import random +import math +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.ops import rnn_cell +from tensorflow.python.ops.nn import bidirectional_dynamic_rnn +from tensorflow.python.ops.nn import dynamic_rnn +from tensorflow.python.ops.nn import sparse_softmax_cross_entropy_with_logits +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +from evaluate import exact_match_score, f1_score +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class GRUAttnCell(rnn_cell.GRUCell): + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.hs = encoder_output + IDT IDT super(GRUAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = rnn_cell._linear(gru_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + IDT IDT return out, out + + +def matrix_multiply_with_batch(matrix= , batch= , matrixByBatch= ) IDT ret = None + IDT if matrixByBatch: + IDT IDT ret = tf.scan(lambda a, x: tf.matmul(matrix, x), batch) + IDT else: + IDT IDT n = batch.get_shape().as_list()[1] + IDT IDT m = batch.get_shape().as_list()[2] + IDT IDT c = matrix.get_shape().as_list()[1] + IDT IDT batch = tf.reshape(batch, [-1, m]) + IDT IDT ret = tf.matmul(batch, matrix) + IDT IDT ret = tf.reshape(ret, [-1, n, c]) + IDT return ret + + +class Encoder(object): + + IDT def __init__(self, state_size, embedding_size, output_size): + IDT IDT self.state_size = state_size + IDT IDT self.embedding_size = embedding_size + IDT IDT self.output_size = output_size + IDT IDT self.h_q = None + IDT IDT self.h_p = None + IDT IDT self.H_q = None + IDT IDT self.H_p = None + + IDT def encode_v2(self, question_embeddings, document_embeddings, question_mask, context_mask, encoderb_state_input, dropout_keep_prob): + IDT IDT """ encode_v2() + """ + IDT IDT with vs.variable_scope('encoder'): + IDT IDT IDT lstm_cell = tf.nn.rnn_cell.LSTMCell(self.embedding_size) + IDT IDT IDT question_length = , tf.int32), reduction_indices= ) IDT IDT IDT + IDT IDT IDT Q_prime, _ = , tf.transpose(question_embeddings, [0, 2, 1]), sequence_length= , time_major= , dtype= ) IDT IDT IDT Q_prime = tf.transpose(Q_prime, [0, 2, 1]) + IDT IDT IDT + IDT IDT IDT W_Q = tf.get_variable('W_Q', (self.embedding_size, self.embedding_size)) + IDT IDT IDT b_Q = tf.get_variable('b_Q', (self.embedding_size, 1)) + IDT IDT IDT Q = , batch= , matrixByBatch= ) IDT IDT IDT + IDT IDT IDT tf.get_variable_scope().reuse_variables() + IDT IDT IDT + IDT IDT IDT context_length = , tf.int32), reduction_indices= ) IDT IDT IDT D, _ = , tf.transpose(document_embeddings, [0, 2, 1]), sequence_length= , time_major= , dtype= ) IDT IDT IDT D = tf.transpose(D, [0, 2, 1]) + IDT IDT IDT + IDT IDT IDT L = tf.matmul(tf.transpose(D, [0, 2, 1]), Q) + IDT IDT IDT A_Q = tf.nn.softmax(L) + IDT IDT IDT A_D = tf.nn.softmax(tf.transpose(L, [0, 2, 1])) + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT C_Q = batch_matmul(D, A_Q) + IDT IDT IDT + IDT IDT IDT concat = tf.concat(1, [Q, C_Q]) + IDT IDT IDT + IDT IDT IDT C_D = batch_matmul(tf.concat(1, [Q, C_Q]), A_D) + IDT IDT IDT + IDT IDT IDT final_D = tf.concat(1, [D, C_D]) + IDT IDT IDT + IDT IDT IDT return final_D + + IDT def encode(self, question_embeddings, context_embeddings, question_mask, context_mask, encoder_state_input, dropout_keep_prob, batch_size): + IDT IDT """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + IDT IDT with vs.variable_scope('encoder', True): + IDT IDT IDT with vs.variable_scope('question', True): + IDT IDT IDT IDT lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + IDT IDT IDT IDT lstm_cell = , output_keep_prob= ) IDT IDT IDT IDT question_length = , tf.int32), reduction_indices= ) IDT IDT IDT IDT + IDT IDT IDT IDT self.H_q, _ = , question_embeddings, sequence_length= , time_major= , dtype= , swap_memory= ) IDT IDT IDT IDT self.h_q = self.H_q[:, (1), :] + IDT IDT IDT IDT + IDT IDT IDT IDT + IDT IDT IDT with vs.variable_scope('context', True): + IDT IDT IDT IDT context_length = , tf.int32), reduction_indices= ) IDT IDT IDT IDT + IDT IDT IDT IDT context_lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + IDT IDT IDT IDT context_lstm_cell = , output_keep_prob= ) IDT IDT IDT IDT self.H_p, _ = , context_embeddings, sequence_length= , time_major= , dtype= , swap_memory= ) IDT IDT IDT IDT self.h_p = self.H_p[:, (1), :] + IDT IDT IDT IDT + IDT IDT IDT IDT + IDT IDT IDT return self.h_q, self.h_p + + +class Decoder(object): + + IDT def __init__(self, output_size, state_size): + IDT IDT self.state_size = state_size + IDT IDT self.output_size = output_size + + IDT def decode_v2(self, final_D, W, W_prime, context_mask): + IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT a_s = , batch= , [0, 2, 1]), matrixByBatch= ) IDT IDT IDT + IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT lstm_cell = tf.nn.rnn_cell.LSTMCell(self.output_size) + IDT IDT IDT context_length = , tf.int32), reduction_indices= ) IDT IDT IDT + IDT IDT IDT final_D_prime, _ = , final_D, sequence_length= , time_major= , dtype= ) IDT IDT IDT + IDT IDT IDT a_e = , batch= , [0, 2, 1]), matrixByBatch= ) IDT IDT IDT + IDT IDT IDT a_e = , batch= , [0, 2, 1]), matrixByBatch= ) IDT IDT IDT + IDT IDT return a_s, a_e + + IDT def decode(self, h_q, h_p): + IDT IDT """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT a_s = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT a_e = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + IDT IDT return a_s, a_e + + +class QASystem(object): + + IDT def __init__(self, encoder, decoder, **kwargs): + IDT IDT """ + Initializes your System + + :param encoder: an encoder that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + IDT IDT self.max_question_len = 60 + IDT IDT self.max_context_len = 301 + IDT IDT self.max_answer_len = 46 + IDT IDT self.n_classes = 2 + IDT IDT self.saver = None + IDT IDT self.encoder = encoder + IDT IDT self.decoder = decoder + IDT IDT self.question_embeddings = None + IDT IDT self.context_embeddings = None + IDT IDT self.a_s_probs = None + IDT IDT self.a_e_probs = None + IDT IDT self.loss = None + IDT IDT self.train_op = None + IDT IDT self.grad_norm = None + IDT IDT self.gradients = None + IDT IDT self.clipped_gradients = None + IDT IDT self.state_size = kwargs['state_size'] + IDT IDT self.embed_path = kwargs['embed_path'] + IDT IDT self.embedding_size = kwargs['embedding_size'] + IDT IDT self.output_size = kwargs['output_size'] + IDT IDT self.optimizer = kwargs['optimizer'] + IDT IDT self.initial_learning_rate = kwargs['learning_rate'] + IDT IDT self.global_step = , trainable= ) IDT IDT self.epochs = kwargs['epochs'] + IDT IDT self.batch_size = kwargs['batch_size'] + IDT IDT self.max_gradient_norm = kwargs['max_gradient_norm'] + IDT IDT self.dropout_keep_prob = kwargs['dropout_keep_prob'] + IDT IDT self.train_dir = kwargs['train_dir'] + IDT IDT self.question_input_placeholder = tf.placeholder(tf.int32, (None, self.max_question_len)) + IDT IDT self.context_input_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + IDT IDT self.question_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_question_len)) + IDT IDT self.context_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_context_len)) + IDT IDT self.start_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + IDT IDT self.end_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + IDT IDT self.dropout_placeholder = tf.placeholder(tf.float32, ()) + IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.setup_training_op() + + IDT def setup_system(self): + IDT IDT """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + IDT IDT W = tf.get_variable('W', (3 * self.embedding_size, 1)) + IDT IDT W_prime = tf.get_variable('W_prime', (3 * self.embedding_size, 1)) + IDT IDT final_D = self.encoder.encode_v2(self.question_embeddings, self.context_embeddings, self.question_mask_placeholder, self.context_mask_placeholder, None, self.dropout_keep_prob) + IDT IDT self.a_s_probs, self.a_e_probs = self.decoder.decode_v2(final_D, W, W_prime, self.context_mask_placeholder) + + IDT def setup_loss(self): + IDT IDT """ + Set up your loss computation here + :return: + """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT l1 = , labels= ) IDT IDT IDT l2 = , labels= ) IDT IDT IDT self.loss = l1 + l2 + + IDT def setup_training_op(self): + IDT IDT """ + Sets up the training ops. + + Creates an optimizer and applies the gradients to all trainable variables. + Clips the global norm of the gradients. + """ + IDT IDT lr = tf.train.exponential_decay(self.initial_learning_rate, self.global_step, 1000, 0.96) + IDT IDT opt = ) IDT IDT self.gradients, params = zip(*opt.compute_gradients(self.loss)) + IDT IDT for param in params: + IDT IDT IDT + IDT IDT self.clipped_gradients, _ = tf.clip_by_global_norm(self.gradients, self.max_gradient_norm) + IDT IDT grads_and_params = zip(self.clipped_gradients, params) + IDT IDT self.grad_norm = tf.global_norm(self.clipped_gradients) + IDT IDT self.train_op = , global_step= ) + IDT def setup_embeddings(self): + IDT IDT """ + Loads distributed word representations based on placeholder tokens + :return: + """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT pretrained_embeddings = np.load(self.embed_path)['glove'] + IDT IDT IDT self.question_embeddings = , name= , dtype= ) IDT IDT IDT self.question_embeddings = tf.nn.embedding_lookup(self.question_embeddings, self.question_input_placeholder) + IDT IDT IDT self.question_embeddings = tf.reshape(self.question_embeddings, [-1, self.embedding_size, self.max_question_len]) + IDT IDT IDT + IDT IDT IDT self.context_embeddings = , name= , dtype= ) IDT IDT IDT self.context_embeddings = tf.nn.embedding_lookup(self.context_embeddings, self.context_input_placeholder) + IDT IDT IDT self.context_embeddings = tf.reshape(self.context_embeddings, [-1, self.embedding_size, self.max_context_len]) + IDT IDT IDT + + IDT def optimize(self, session, train_batch, dropout_keep_prob= ) IDT IDT """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + IDT IDT question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*train_batch) + IDT IDT feed = {} + IDT IDT feed[self.question_input_placeholder] = question_batch + IDT IDT feed[self.context_input_placeholder] = context_batch + IDT IDT feed[self.question_mask_placeholder] = question_mask_batch + IDT IDT feed[self.context_mask_placeholder] = context_mask_batch + IDT IDT feed[self.start_answer_placeholder] = start_answer_batch + IDT IDT feed[self.end_answer_placeholder] = end_answer_batch + IDT IDT feed[self.dropout_placeholder] = dropout_keep_prob + IDT IDT _, loss, grad_norm = , self.loss, self.grad_norm], feed_dict= ) IDT IDT return loss, grad_norm + + IDT def test(self, session, valid_x, valid_y): + IDT IDT """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + IDT IDT input_feed = {} + IDT IDT input_feed['valid_x'] = valid_x + IDT IDT if valid_y is not None: + IDT IDT IDT input_feed['valid_y'] = valid_y + IDT IDT output_feed = [] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def decode(self, session, test_x): + IDT IDT """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + IDT IDT question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*test_x) + IDT IDT input_feed = {} + IDT IDT input_feed[self.question_input_placeholder] = question_batch + IDT IDT input_feed[self.context_input_placeholder] = context_batch + IDT IDT input_feed[self.question_mask_placeholder] = question_mask_batch + IDT IDT input_feed[self.context_mask_placeholder] = context_mask_batch + IDT IDT input_feed[self.start_answer_placeholder] = start_answer_batch + IDT IDT input_feed[self.end_answer_placeholder] = end_answer_batch + IDT IDT input_feed[self.dropout_placeholder] = 1.0 + IDT IDT _, loss, grad_norm = , self.loss, self.grad_norm], feed_dict= ) IDT IDT a_s = , session= ) IDT IDT a_e = , session= ) IDT IDT return a_s, a_e + + IDT def answer(self, session, test_x): + IDT IDT yp, yp2 = self.decode(session, test_x) + IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, session, dataset, model_path, model_name): + IDT IDT """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + IDT IDT new_saver = tf.train.import_meta_graph(model_path + model_name) + IDT IDT new_saver.restore(session, tf.train.latest_checkpoint(model_path)) + IDT IDT f1s = [] + IDT IDT ems = [] + IDT IDT step = 1000 + IDT IDT for start_idx in range(0, len(dataset['val']), step): + IDT IDT IDT end_idx = min(start_idx + step, len(dataset['val'])) + IDT IDT IDT f1s_one_batch, ems_one_batch = , dataset['val'][start_idx:end_idx], sample= , log= ) IDT IDT IDT f1s += f1s_one_batch + IDT IDT IDT ems += ems_one_batch + IDT IDT f1_total = sum(f1s) / float(len(f1s)) + IDT IDT em_total = sum(ems) / float(len(ems)) + IDT IDT + IDT IDT + + IDT def evaluate_answer(self, session, dataset, sample= , log= ) IDT IDT """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + IDT IDT batch = dataset + IDT IDT if sample is None: + IDT IDT IDT sample = len(dataset) + IDT IDT else: + IDT IDT IDT random_indices = [random.randint(0, len(dataset)) for _ in range(sample)] + IDT IDT IDT batch = [dataset[idx] for idx in random_indices] + IDT IDT question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*batch) + IDT IDT a_s, a_e = self.answer(session, batch) + IDT IDT true_a_s = , axis= ) IDT IDT true_a_e = , axis= ) IDT IDT + IDT IDT + IDT IDT + IDT IDT + IDT IDT answers = [context_batch[i][a_s[i]:a_e[i] + 1] for i in range(len(a_s))] + IDT IDT true_answers = [context_batch[i][true_a_s[i]:true_a_e[i] + 1] for i in range(len(true_a_s))] + IDT IDT f1s = [] + IDT IDT ems = [] + IDT IDT for i in range(len(true_answers)): + IDT IDT IDT answer = answers[i] + IDT IDT IDT true_answer = true_answers[i] + IDT IDT IDT f1_one_example = f1_score(answer, true_answer) + IDT IDT IDT f1s.append(f1_one_example) + IDT IDT IDT em_one_example = exact_match_score(answer, true_answer) + IDT IDT IDT ems.append(em_one_example) + IDT IDT f1 = np.sum(f1s) / float(sample) + IDT IDT em = np.sum(ems) / float(sample) + IDT IDT if log: + IDT IDT IDT logging.info('F1: {}, EM: {}, for {} samples'.format(f1, em, sample)) + IDT IDT return f1s, ems + + IDT def train(self, session, dataset, train_dir): + IDT IDT """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious appoarch can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + IDT IDT dataset = self.preprocess_dataset(dataset) + IDT IDT init = tf.global_variables_initializer() + IDT IDT session.run(init) + IDT IDT self.validate(session, dataset, 'saved_models/', 'baselinev2_model_epoch_9_iter_8000.meta') + IDT IDT exit() + IDT IDT for epoch in range(self.epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.epochs) + IDT IDT IDT self.run_epoch(session, dataset['train'], epoch) + IDT IDT IDT self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch)) + IDT IDT IDT tic = time.time() + IDT IDT IDT params = tf.trainable_variables() + IDT IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT IDT toc = time.time() + IDT IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT evaluate_answer(session, dataset['val'], sample= , log= ) + IDT def preprocess_dataset(self, dataset): + IDT IDT for data_subset_name in ['train', 'val']: + IDT IDT IDT for i in range(len(dataset[data_subset_name])): + IDT IDT IDT IDT question = dataset[data_subset_name][i][0] + IDT IDT IDT IDT context = dataset[data_subset_name][i][1] + IDT IDT IDT IDT answer = dataset[data_subset_name][i][2] + IDT IDT IDT IDT padded_question, question_mask = self.pad_sequence(question, self.max_question_len) + IDT IDT IDT IDT padded_context, context_mask = self.pad_sequence(context, self.max_context_len) + IDT IDT IDT IDT start_answer = [0] * self.max_context_len + IDT IDT IDT IDT if answer[0] < self.output_size: + IDT IDT IDT IDT IDT start_answer[answer[0]] = 1 + IDT IDT IDT IDT end_answer = [0] * self.max_context_len + IDT IDT IDT IDT if answer[1] < self.output_size: + IDT IDT IDT IDT IDT end_answer[answer[1]] = 1 + IDT IDT IDT IDT dataset[data_subset_name][i] = padded_question, padded_context, question_mask, context_mask, start_answer, end_answer + IDT IDT return dataset + + IDT def pad_sequence(self, sequence, max_length): + IDT IDT new_sequence = [] + IDT IDT mask = [] + IDT IDT if len(sequence) >= max_length: + IDT IDT IDT new_sequence = sequence[0:max_length] + IDT IDT IDT mask = [True] * max_length + IDT IDT elif len(sequence) < max_length: + IDT IDT IDT delta = max_length - len(sequence) + IDT IDT IDT new_sequence = sequence + [0] * delta + IDT IDT IDT mask = [True] * len(sequence) + [False] * delta + IDT IDT return new_sequence, mask + + IDT def run_epoch(self, session, train_examples, epoch_no): + IDT IDT for i, batch in enumerate(self.minibatches(train_examples, self.batch_size, shuffle= ) IDT IDT IDT ) + IDT IDT IDT loss, grad_norm = self.optimize(session, batch, self.dropout_keep_prob) + IDT IDT IDT + IDT IDT IDT if i % 100 == 0: + IDT IDT IDT IDT self.evaluate_answer(session, train_examples, sample= , log= ) IDT IDT IDT if i % 100 == 0: + IDT IDT IDT IDT self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch_no) + '_iter_' + str(i)) + + IDT def get_tiny_batches(self, data): + IDT IDT return [data[0:10], data[10:20]] + + IDT def minibatches(self, data, batch_size, shuffle= ) IDT IDT if shuffle: + IDT IDT IDT random.shuffle(data) + IDT IDT num_batches = int(math.ceil(len(data) / batch_size)) + IDT IDT q, r = divmod(len(data), num_batches) + IDT IDT indices = [(q * i + min(i, r)) for i in xrange(num_batches + 1)] + IDT IDT return [data[indices[i]:indices[i + 1]] for i in xrange(num_batches)] +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from neural.full_connect import load_reformat_not_mnist, accuracy + + +def tf_better_nn(offset_range= , regular= , drop_out= , lrd= ) IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_node_count = 1024 + IDT IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count])) + IDT IDT biases1 = tf.Variable(tf.zeros([hidden_node_count])) + IDT IDT weights2 = tf.Variable(tf.truncated_normal([hidden_node_count, num_labels])) + IDT IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT ys = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT hidden = tf.nn.relu(ys) + IDT IDT h_fc = hidden + IDT IDT valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + IDT IDT valid_hidden1 = tf.nn.relu(valid_y0) + IDT IDT test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + IDT IDT test_hidden1 = tf.nn.relu(test_y0) + IDT IDT keep_prob = tf.placeholder(tf.float32) + IDT IDT if drop_out: + IDT IDT IDT hidden_drop = tf.nn.dropout(hidden, keep_prob) + IDT IDT IDT h_fc = hidden_drop + IDT IDT logits = tf.matmul(h_fc, weights2) + biases2 + IDT IDT logits_predict = tf.matmul(hidden, weights2) + biases2 + IDT IDT valid_predict = tf.matmul(valid_hidden1, weights2) + biases2 + IDT IDT test_predict = tf.matmul(test_hidden1, weights2) + biases2 + IDT IDT l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(biases1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(biases2) + IDT IDT if not regular: + IDT IDT IDT l2_loss = 0 + IDT IDT beta = 0.002 + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT if lrd: + IDT IDT IDT cur_step = tf.Variable(0) + IDT IDT IDT starter_learning_rate = 0.1 + IDT IDT IDT learning_rate = , cur_step, 10000, 0.96, staircase= ) IDT IDT IDT optimizer = , global_step= ) IDT IDT train_prediction = tf.nn.softmax(logits_predict) + IDT IDT valid_prediction = tf.nn.softmax(valid_predict) + IDT IDT test_prediction = tf.nn.softmax(test_predict) + IDT num_steps = 30001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT if offset_range == -1: + IDT IDT IDT IDT offset_range = train_labels.shape[0] - batch_size + IDT IDT IDT offset = step * batch_size % offset_range + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +def tf_deep_nn(regular= , drop_out= , lrd= , layer_cnt= ) IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_node_count = 1024 + IDT IDT hidden_stddev = np.sqrt(2.0 / 784) + IDT IDT weights1 = , hidden_node_count], stddev= ) IDT IDT biases1 = tf.Variable(tf.zeros([hidden_node_count])) + IDT IDT weights = [] + IDT IDT biases = [] + IDT IDT hidden_cur_cnt = hidden_node_count + IDT IDT for i in range(layer_cnt - 2): + IDT IDT IDT if hidden_cur_cnt > 2: + IDT IDT IDT IDT hidden_next_cnt = int(hidden_cur_cnt / 2) + IDT IDT IDT else: + IDT IDT IDT IDT hidden_next_cnt = 2 + IDT IDT IDT hidden_stddev = np.sqrt(2.0 / hidden_cur_cnt) + IDT IDT IDT weights.append(tf.Variable(tf.truncated_normal([hidden_cur_cnt, hidden_next_cnt], stddev= ) IDT IDT IDT biases.append(tf.Variable(tf.zeros([hidden_next_cnt]))) + IDT IDT IDT hidden_cur_cnt = hidden_next_cnt + IDT IDT y0 = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT hidden = tf.nn.relu(y0) + IDT IDT hidden_drop = hidden + IDT IDT keep_prob = 0.5 + IDT IDT if drop_out: + IDT IDT IDT hidden_drop = tf.nn.dropout(hidden, keep_prob) + IDT IDT valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + IDT IDT valid_hidden = tf.nn.relu(valid_y0) + IDT IDT test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + IDT IDT test_hidden = tf.nn.relu(test_y0) + IDT IDT for i in range(layer_cnt - 2): + IDT IDT IDT y1 = tf.matmul(hidden_drop, weights[i]) + biases[i] + IDT IDT IDT hidden_drop = tf.nn.relu(y1) + IDT IDT IDT if drop_out: + IDT IDT IDT IDT keep_prob += 0.5 * i / (layer_cnt + 1) + IDT IDT IDT IDT hidden_drop = tf.nn.dropout(hidden_drop, keep_prob) + IDT IDT IDT y0 = tf.matmul(hidden, weights[i]) + biases[i] + IDT IDT IDT hidden = tf.nn.relu(y0) + IDT IDT IDT valid_y0 = tf.matmul(valid_hidden, weights[i]) + biases[i] + IDT IDT IDT valid_hidden = tf.nn.relu(valid_y0) + IDT IDT IDT test_y0 = tf.matmul(test_hidden, weights[i]) + biases[i] + IDT IDT IDT test_hidden = tf.nn.relu(test_y0) + IDT IDT weights2 = , num_labels], stddev= ) IDT IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden_drop, weights2) + biases2 + IDT IDT logits_predict = tf.matmul(hidden, weights2) + biases2 + IDT IDT valid_predict = tf.matmul(valid_hidden, weights2) + biases2 + IDT IDT test_predict = tf.matmul(test_hidden, weights2) + biases2 + IDT IDT l2_loss = 0 + IDT IDT if regular: + IDT IDT IDT l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + IDT IDT IDT for i in range(len(weights)): + IDT IDT IDT IDT l2_loss += tf.nn.l2_loss(weights[i]) + IDT IDT IDT beta = 0.25 / batch_size + IDT IDT IDT beta = 1e-05 + IDT IDT IDT l2_loss *= beta + IDT IDT loss = , labels= ) IDT IDT if lrd: + IDT IDT IDT cur_step = , trainable= ) IDT IDT IDT starter_learning_rate = 0.4 + IDT IDT IDT learning_rate = , cur_step, 100000, 0.96, staircase= ) IDT IDT IDT optimizer = , global_step= ) IDT IDT else: + IDT IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits_predict) + IDT IDT valid_prediction = tf.nn.softmax(valid_predict) + IDT IDT test_prediction = tf.nn.softmax(test_predict) + IDT num_steps = 20001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset_range = train_labels.shape[0] - batch_size + IDT IDT IDT offset = step * batch_size % offset_range + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +if __name__ == '__main__': + IDT image_size = 28 + IDT num_labels = 10 + IDT train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = load_reformat_not_mnist(image_size, num_labels) + IDT tf_deep_nn(layer_cnt= , lrd= , drop_out= , regular= )from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import argparse +import math +import random +import os +import tensorflow as tf +import numpy as np +from six.moves import xrange as range +from utils import * +import pdb +from time import gmtime, strftime +NUM_CLASSES = len(get_chars_to_index_mapping()) +NUM_HIDDEN_LAYERS = 2 +DROPOUT_KEEP_PROB = 1 + + +class Config: + IDT """Holds model hyperparams and data information. + + The config class is used to store various hyperparameters and dataset + information parameters. Model objects are passed a Config() object at + instantiation. + """ + IDT context_size = 0 + IDT num_mfcc_features = 13 + IDT num_final_features = num_mfcc_features * (2 * context_size + 1) + IDT batch_size = 16 + IDT num_classes = NUM_CLASSES + IDT num_hidden = 50 + IDT num_epochs = 100 + IDT l2_lambda = 0.0001 + IDT lr = 0.001 + + +class CTCModel: + IDT """ + Implements a recursive neural network with a single hidden layer attached to CTC loss. + This network will predict a sequence of TIDIGITS (e.g. z1039) for a given audio wav file. + """ + + IDT def add_placeholders(self): + IDT IDT """Generates placeholder variables to represent the input tensors + + These placeholders are used as inputs by the rest of the model building and will be fed + data during training. Note that when "None" is in a placeholder's shape, it's flexible + (so we can use different batch sizes without rebuilding the model). + + Adds following nodes to the computational graph: + + inputs_placeholder: Input placeholder tensor of shape (None, None, num_final_features), type tf.float32 + targets_placeholder: Sparse placeholder, type tf.int32. You don't need to specify shape dimension. + seq_lens_placeholder: Sequence length placeholder tensor of shape (None), type tf.int32 + + TODO: Add these placeholders to self as the instance variables + self.inputs_placeholder + self.targets_placeholder + self.seq_lens_placeholder + + HINTS: + - Use tf.sparse_placeholder(tf.int32) for targets_placeholder. This is required by TF's ctc_loss op. + - Inputs is of shape [batch_size, max_timesteps, num_final_features], but we allow flexible sizes for + batch_size and max_timesteps (hence the shape definition as [None, None, num_final_features]. + + (Don't change the variable names) + """ + IDT IDT inputs_placeholder = , shape= , None, Config.num_final_features), name= ) IDT IDT targets_placeholder = , name= ) IDT IDT seq_lens_placeholder = , shape= , name= ) IDT IDT keep_prob_placeholder = , name= ) IDT IDT self.inputs_placeholder = inputs_placeholder + IDT IDT self.targets_placeholder = targets_placeholder + IDT IDT self.seq_lens_placeholder = seq_lens_placeholder + IDT IDT self.keep_prob_placeholder = keep_prob_placeholder + + IDT def create_feed_dict(self, inputs_batch, targets_batch, seq_lens_batch, keep_prob): + IDT IDT """Creates the feed_dict for the digit recognizer. + + A feed_dict takes the form of: + + feed_dict = { + : , + .... + } + + Hint: The keys for the feed_dict should be a subset of the placeholder + tensors created in add_placeholders. + + Args: + inputs_batch: A batch of input data. + targets_batch: A batch of targets data. + seq_lens_batch: A batch of seq_lens data. + Returns: + feed_dict: The feed dictionary mapping from placeholders to values. + """ + IDT IDT feed_dict = {self.inputs_placeholder: inputs_batch, self.targets_placeholder: targets_batch, self.seq_lens_placeholder: seq_lens_batch, self.keep_prob_placeholder: keep_prob} + IDT IDT return feed_dict + + IDT def add_prediction_op(self, train): + IDT IDT """Applies a GRU RNN over the input data, then an affine layer projection. Steps to complete + in this function: + + - Roll over inputs_placeholder with GRUCell, producing a Tensor of shape [batch_s, max_timestep, + num_hidden]. + - Apply a W * f + b transformation over the data, where f is each hidden layer feature. This + should produce a Tensor of shape [batch_s, max_timesteps, num_classes]. Set this result to + "logits". + + Remember: + * Use the xavier initialization for matrices (W, but not b). + * W should be shape [num_hidden, num_classes]. num_classes for our dataset is 12 + * tf.contrib.rnn.GRUCell, tf.contrib.rnn.MultiRNNCell and tf.nn.dynamic_rnn are of interest + """ + IDT IDT forward_cell_multi = [] + IDT IDT backward_cell_multi = [] + IDT IDT for _ in range(NUM_HIDDEN_LAYERS): + IDT IDT IDT forward_cell = , activation= , input_keep_prob= , output_keep_prob= ) IDT IDT IDT forward_cell_multi.append(forward_cell) + IDT IDT IDT backward_cell = , activation= , input_keep_prob= , output_keep_prob= ) IDT IDT IDT backward_cell_multi.append(backward_cell) + IDT IDT forward_cell_multi = tf.contrib.rnn.MultiRNNCell(forward_cell_multi) + IDT IDT backward_cell_multi = tf.contrib.rnn.MultiRNNCell(backward_cell_multi) + IDT IDT outputs, _ = , backward_cell_multi, self.inputs_placeholder, sequence_length= , dtype= ) IDT IDT outputs = tf.concat(outputs, 2) + IDT IDT W = , shape= , Config.num_classes], dtype= , initializer= ) IDT IDT b = , shape= ,), dtype= , initializer= ) IDT IDT max_timesteps = tf.shape(outputs)[1] + IDT IDT num_hidden = tf.shape(outputs)[2] + IDT IDT f = tf.reshape(outputs, [-1, num_hidden]) + IDT IDT logits = tf.add(tf.matmul(f, W), b) + IDT IDT logits = tf.reshape(logits, [-1, max_timesteps, Config.num_classes]) + IDT IDT self.logits = logits + + IDT def add_loss_op(self): + IDT IDT """Adds Ops for the loss function to the computational graph. + + - Use tf.nn.ctc_loss to calculate the CTC loss for each example in the batch. You'll need self.logits, + self.targets_placeholder, self.seq_lens_placeholder for this. Set variable ctc_loss to + the output of tf.nn.ctc_loss + - You will need to first tf.transpose the data so that self.logits is shaped [max_timesteps, batch_s, + num_classes]. + - Configure tf.nn.ctc_loss so that identical consecutive labels are allowed + - Compute L2 regularization cost for all trainable variables. Use tf.nn.l2_loss(var). + + """ + IDT IDT ctc_loss = [] + IDT IDT l2_cost = 0.0 + IDT IDT self.logits = , perm= , 0, 2]) ) IDT IDT tf.identity(self.logits, name= ) IDT IDT ctc_loss = , self.logits, self.seq_lens_placeholder, ctc_merge_repeated= , preprocess_collapse_repeated= ) IDT IDT for var in tf.trainable_variables(): + IDT IDT IDT if len(var.get_shape().as_list()) > 1: + IDT IDT IDT IDT l2_cost += tf.nn.l2_loss(var) + IDT IDT loss_without_invalid_paths = tf.boolean_mask(ctc_loss, tf.less(ctc_loss, tf.constant(10000.0))) + IDT IDT self.num_valid_examples = tf.cast(tf.shape(loss_without_invalid_paths)[0], tf.int32) + IDT IDT cost = tf.reduce_mean(loss_without_invalid_paths) + IDT IDT self.loss = Config.l2_lambda * l2_cost + cost + + IDT def add_training_op(self): + IDT IDT """Sets up the training Ops. + + Creates an optimizer and applies the gradients to all trainable variables. The Op returned by this + function is what must be passed to the `sess.run()` call to cause the model to train. See + + https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html + + for more information. + + Use tf.train.AdamOptimizer for this model. Call optimizer.minimize() on self.loss. + + """ + IDT IDT optimizer = ) IDT IDT self.optimizer = optimizer + + IDT def add_decoder_and_cer_op(self): + IDT IDT """Setup the decoder and add the word error rate calculations here. + + Tip: You will find tf.nn.ctc_beam_search_decoder and tf.edit_distance methods useful here. + Also, report the mean cer over the batch in variable cer + + """ + IDT IDT decoded_sequence = None + IDT IDT cer = None + IDT IDT decoded_sequence = , self.seq_lens_placeholder, merge_repeated= ) IDT IDT cer = , self.targets_placeholder), name= ) IDT IDT tf.summary.scalar('loss', self.loss) + IDT IDT tf.summary.scalar('cer', cer) + IDT IDT self.decoded_sequence = decoded_sequence + IDT IDT self.cer = cer + + IDT def add_summary_op(self): + IDT IDT self.merged_summary_op = tf.summary.merge_all() + + IDT def build(self, train): + IDT IDT self.add_placeholders() + IDT IDT self.add_prediction_op(train) + IDT IDT self.add_loss_op() + IDT IDT self.add_training_op() + IDT IDT self.add_decoder_and_cer_op() + IDT IDT self.add_summary_op() + + IDT def train_on_batch(self, session, train_inputs_batch, train_targets_batch, train_seq_len_batch, train= ) IDT IDT keep_prob = 1.0 + IDT IDT if train: + IDT IDT IDT keep_prob = DROPOUT_KEEP_PROB + IDT IDT feed = self.create_feed_dict(train_inputs_batch, train_targets_batch, train_seq_len_batch, keep_prob) + IDT IDT batch_cost, cer, batch_num_valid_ex, summary = session.run([self.loss, self.cer, self.num_valid_examples, self.merged_summary_op], feed) + IDT IDT if math.isnan(batch_cost): + IDT IDT IDT return 0 + IDT IDT if train: + IDT IDT IDT _ = session.run([self.optimizer], feed) + IDT IDT return batch_cost, cer, summary + + IDT def print_results(self, session, train_inputs_batch, train_targets_batch, train_seq_len_batch): + IDT IDT train_feed = self.create_feed_dict(train_inputs_batch, train_targets_batch, train_seq_len_batch, 1.0) + IDT IDT train_first_batch_preds = , feed_dict= ) IDT IDT compare_predicted_to_true(train_first_batch_preds, train_targets_batch) + + IDT def __init__(self, train= ) IDT IDT self.build(train) + + +def load_args(): + IDT parser = argparse.ArgumentParser() + IDT parser.add_argument('--train_path', nargs= , default= , type= , help= ) IDT parser.add_argument('--val_path', nargs= , default= , type= , help= ) IDT parser.add_argument('--save_every', nargs= , default= , type= , help= ) IDT parser.add_argument('--print_every', nargs= , default= , type= , help= ) IDT parser.add_argument('--save_to_file', nargs= , default= , type= , help= ) IDT parser.add_argument('--load_from_file', nargs= , default= , type= , help= ) IDT parser.add_argument('--test_path', nargs= , default= , type= , help= ) IDT return parser.parse_args() + + +def train_model(logs_path, num_batches_per_epoch, train_feature_minibatches, train_labels_minibatches, train_seqlens_minibatches, val_feature_minibatches, val_labels_minibatches, val_seqlens_minibatches): + IDT with tf.Graph().as_default(): + IDT IDT model = ) IDT IDT init = tf.global_variables_initializer() + IDT IDT saver = ) IDT IDT with tf.Session() as session: + IDT IDT IDT session.run(init) + IDT IDT IDT train_writer = tf.summary.FileWriter(logs_path + '/train', session.graph) + IDT IDT IDT val_writer = tf.summary.FileWriter(logs_path + '/dev', session.graph) + IDT IDT IDT global_start = time.time() + IDT IDT IDT step_ii = 0 + IDT IDT IDT curr_epoch = 0 + IDT IDT IDT while True: + IDT IDT IDT IDT total_train_cost = total_train_cer = 0 + IDT IDT IDT IDT start = time.time() + IDT IDT IDT IDT for batch in random.sample(range(num_batches_per_epoch), num_batches_per_epoch): + IDT IDT IDT IDT IDT cur_batch_size = len(train_seqlens_minibatches[batch]) + IDT IDT IDT IDT IDT batch_cost, batch_cer, summary = , train_feature_minibatches[batch], train_labels_minibatches[batch], train_seqlens_minibatches[batch], train= ) IDT IDT IDT IDT IDT total_train_cost += batch_cost * cur_batch_size + IDT IDT IDT IDT IDT total_train_cer += batch_cer * cur_batch_size + IDT IDT IDT IDT IDT train_writer.add_summary(summary, step_ii) + IDT IDT IDT IDT IDT step_ii += 1 + IDT IDT IDT IDT train_cost = total_train_cost / num_examples + IDT IDT IDT IDT train_cer = total_train_cer / num_examples + IDT IDT IDT IDT val_batch_cost, val_batch_cer, summary = , val_feature_minibatches[0], val_labels_minibatches[0], val_seqlens_minibatches[0], train= ) IDT IDT IDT IDT val_writer.add_summary(summary, step_ii) + IDT IDT IDT IDT log = 'Epoch {}/{}, train_cost = {:.3f}, train_ed = {:.3f}, val_cost = {:.3f}, val_ed = {:.3f}, time = {:.3f}' + IDT IDT IDT IDT - start)) + IDT IDT IDT IDT if args.print_every is not None and (curr_epoch + 1) % args.print_every == 0: + IDT IDT IDT IDT IDT batch_ii = 0 + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT model.print_results(session, train_feature_minibatches[batch_ii], train_labels_minibatches[batch_ii], train_seqlens_minibatches[batch_ii]) + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT model.print_results(session, val_feature_minibatches[batch_ii][:10], val_labels_minibatches[batch_ii][:10], val_seqlens_minibatches[batch_ii][:10]) + IDT IDT IDT IDT if args.save_every is not None and args.save_to_file is not None and (curr_epoch + 1) % args.save_every == 0: + IDT IDT IDT IDT IDT saver.save(session, args.save_to_file, global_step= ) IDT IDT IDT IDT curr_epoch += 1 + + +def test(test_dataset, trained_weights_file): + IDT test_feature_minibatches, test_labels_minibatches, test_seqlens_minibatches = , batch_size= ) IDT test_feature_minibatches = pad_all_batches(test_feature_minibatches) + IDT with tf.Graph().as_default(): + IDT IDT with tf.Session() as session: + IDT IDT IDT new_saver = , clear_devices= ) IDT IDT IDT new_saver.restore(session, trained_weights_file) + IDT IDT IDT + IDT IDT IDT start = time.time() + IDT IDT IDT cer = tf.get_default_graph().get_tensor_by_name('cer:0') + IDT IDT IDT test_feed_dict = {'inputs_placeholder:0': test_feature_minibatches[0], 'targets_placeholder/indices:0': test_labels_minibatches[0][0], 'targets_placeholder/values:0': test_labels_minibatches[0][1], 'targets_placeholder/shape:0': test_labels_minibatches[0][2], 'seq_lens_placeholder:0': test_seqlens_minibatches[0], 'keep_prob_placeholder:0': 1.0} + IDT IDT IDT test_cer = session.run([cer], test_feed_dict) + IDT IDT IDT log = 'test_cer = {:.3f}, time = {:.3f}' + IDT IDT IDT - start)) + IDT IDT IDT if args.print_every is not None: + IDT IDT IDT IDT batch_ii = 0 + IDT IDT IDT IDT logits = tf.get_default_graph().get_tensor_by_name('logits:0') + IDT IDT IDT IDT seq_lens_placeholder = tf.get_default_graph().get_tensor_by_name('seq_lens_placeholder:0') + IDT IDT IDT IDT decoded_sequence = , seq_lens_placeholder, merge_repeated= ) IDT IDT IDT IDT test_results_feed_dict = {'inputs_placeholder:0': test_feature_minibatches[batch_ii], 'targets_placeholder/indices:0': test_labels_minibatches[batch_ii][0], 'targets_placeholder/values:0': test_labels_minibatches[batch_ii][1], 'targets_placeholder/shape:0': test_labels_minibatches[batch_ii][2], 'seq_lens_placeholder:0': test_seqlens_minibatches[batch_ii], + IDT IDT IDT IDT IDT 'keep_prob_placeholder:0': 1.0} + IDT IDT IDT IDT train_first_batch_preds = , feed_dict= ) IDT IDT IDT IDT compare_predicted_to_true(train_first_batch_preds, test_labels_minibatches[batch_ii]) + + +def pad_all_batches(batch_feature_array): + IDT for batch_num in range(len(batch_feature_array)): + IDT IDT batch_feature_array[batch_num] = pad_sequences(batch_feature_array[batch_num])[0] + IDT return batch_feature_array + + +if __name__ == '__main__': + IDT args = load_args() + IDT if args.test_path == 'no': + IDT IDT logs_path = 'tensorboard/' + strftime('%Y_%m_%d_%H_%M_%S', gmtime()) + '_lr=%f' % Config.lr + IDT IDT train_dataset = load_dataset(args.train_path) + IDT IDT train_feature_minibatches, train_labels_minibatches, train_seqlens_minibatches = , batch_size= ) IDT IDT val_dataset = load_dataset(args.val_path) + IDT IDT val_feature_minibatches, val_labels_minibatches, val_seqlens_minibatches = , batch_size= ) IDT IDT train_feature_minibatches = pad_all_batches(train_feature_minibatches) + IDT IDT val_feature_minibatches = pad_all_batches(val_feature_minibatches) + IDT IDT num_examples = np.sum([batch.shape[0] for batch in train_feature_minibatches]) + IDT IDT num_batches_per_epoch = int(math.ceil(num_examples / Config.batch_size)) + IDT IDT train_model(logs_path, num_batches_per_epoch, train_feature_minibatches, train_labels_minibatches, train_seqlens_minibatches, val_feature_minibatches, val_labels_minibatches, val_seqlens_minibatches) + IDT else: + IDT IDT if args.load_from_file is None: + IDT IDT IDT raise ValueError('must specify weights to load through --load_from_file') + IDT IDT test_dataset = load_dataset(args.test_path) + IDT IDT test(test_dataset, args.load_from_file) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import sys +if sys.version[0] == '2': + IDT import cPickle as pkl +else: + IDT import pickle as pkl +import numpy as np +import tensorflow as tf +from PNN.author import utils +dtype = utils.DTYPE + + +class Model: + + IDT def __init__(self): + IDT IDT self.sess = None + IDT IDT self.X = None + IDT IDT self.y = None + IDT IDT self.layer_keeps = None + IDT IDT self.vars = None + IDT IDT self.keep_prob_train = None + IDT IDT self.keep_prob_test = None + + IDT def run(self, fetches, X= , y= , mode= ) IDT IDT feed_dict = {} + IDT IDT if type(self.X) is list: + IDT IDT IDT for i in range(len(X)): + IDT IDT IDT IDT feed_dict[self.X[i]] = X[i] + IDT IDT else: + IDT IDT IDT feed_dict[self.X] = X + IDT IDT if y is not None: + IDT IDT IDT feed_dict[self.y] = y + IDT IDT if self.layer_keeps is not None: + IDT IDT IDT if mode == 'train': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_train + IDT IDT IDT elif mode == 'test': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_test + IDT IDT return self.sess.run(fetches, feed_dict) + + IDT def dump(self, model_path): + IDT IDT var_map = {} + IDT IDT for name, var in self.vars.iteritems(): + IDT IDT IDT var_map[name] = self.run(var) + IDT IDT pkl.dump(var_map, open(model_path, 'wb')) + IDT IDT + + +class LR(Model): + + IDT def __init__(self, input_dim= , output_dim= , init_path= , opt_algo= , learning_rate= , l2_weight= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , logits= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FM(Model): + + IDT def __init__(self, input_dim= , output_dim= , factor_order= , init_path= , opt_algo= , learning_rate= , l2_w= , l2_v= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT v = self.vars['v'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + IDT IDT IDT xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + IDT IDT IDT p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FNN(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT node_in = num_inputs * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class DeepFM(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('weight_%d' % i, [field_sizes[i], 1], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('bias', [1], 'zero', dtype)) + IDT IDT node_in = num_inputs * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = [self.vars['weight_%d' % i] for i in range(num_inputs)] + IDT IDT IDT v = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b = self.vars['bias'] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xv = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], v[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xv + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT xv = tf.reshape(xv, [-1, num_inputs, embed_size]) + IDT IDT IDT p = 0.5 * tf.reduce_sum(tf.square(tf.reduce_sum(xv, 1)) - tf.reduce_sum(tf.square(xv), 1), 1) + IDT IDT IDT xw = tf.reduce_sum(xw, 1) + IDT IDT IDT logits = tf.reshape(l + xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class CCPM(Model): + + IDT def __init__(self, field_sizes= , embed_size= , filter_sizes= , layer_acts= , drop_out= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + IDT IDT init_vars.append(('b1', [1], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + IDT IDT IDT f1 = self.vars['f1'] + IDT IDT IDT l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + IDT IDT IDT f2 = self.vars['f2'] + IDT IDT IDT l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT l = tf.matmul(l, w1) + b1 + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN1(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + IDT IDT IDT l = tf.concat([xw, ip], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN2(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= , layer_norm= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT init_vars.append(('kernel', [embed_size, num_pairs, embed_size], 'xavier', dtype)) + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT k = self.vars['kernel'] + IDT IDT IDT p = tf.expand_dims(p, 1) + IDT IDT IDT kp = tf.reduce_sum(tf.multiply(tf.transpose(tf.reduce_sum(tf.multiply(p, k), -1), [0, 2, 1]), q), -1) + IDT IDT IDT l = tf.concat([xw, kp], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= )import tensorflow as tf +import tensorflow.contrib.rnn as rnn +from math import sqrt +from numpy import argmax +dropout_prob = 0.5 +n_kept_model = 2 +k_word_candidate = 3 +k_bpos_candidate = 3 +buffer_max_len = 300 +stack_max_len = 200 +actions_max_len = 400 +lstm_dim = 100 +subword_lstm_dim = 64 +output_dim = 20 +word_vocab_size = 100004 +subword_vocab_size = 100004 +word_emb_dim = 64 +param_emb_dim = 30 +candidate_emb_dim = 20 +n_pos = 16 +n_bpos = 61 +n_label = 38 +n_class = 106 +buffer = , buffer_max_len, lstm_dim]), trainable= , name= )stack = , stack_max_len, lstm_dim]), trainable= , name= )actions = , actions_max_len, lstm_dim]), trainable= , name= )subword_ph = , [None], name= )word_candidates_ph = , [None, None], name= )bpos_ph = , [None, None], name= )word_ph = , None, name= )subword_list_ph = , [None], name= )pos_ph = , None, name= )relation_action_ph = , [None], name= )action_ph = , None, name= )output_mask = , [None], name= )label = , None, name= )stack_len = , name= )buffer_len = , name= )actions_len = , name= )subwords_len = , name= )word_lm_emb = , word_emb_dim]), trainable= , name= )subword_emb = , word_emb_dim]), trainable= , name= )word_emb_ph = tf.placeholder(tf.float32, [word_vocab_size, word_emb_dim]) +subword_emb_ph = tf.placeholder(tf.float32, [word_vocab_size, word_emb_dim]) +assign_word_embedding = , word_emb_ph, validate_shape= )assign_subword_embedding = , subword_emb_ph, validate_shape= )word_emb = , candidate_emb_dim], minval= , maxval= , name= )bpos_emb = , candidate_emb_dim], minval= , maxval= , name= )pos_emb = , param_emb_dim], minval= , maxval= , name= )action_emb = , param_emb_dim], minval= , maxval= , name= ) + +def nn_run_lstm_input(input_vec, dim, seq_len, scope_name): + IDT with tf.variable_scope(scope_name) as vs: + IDT IDT lstm_fw_cell = , reuse= ) IDT IDT lstm_outputs, _ = , input_vec, dtype= , scope= , sequence_length= ) IDT IDT outputs = tf.reshape(lstm_outputs[:, (0), :], [1, dim]) + IDT IDT return outputs + + +with tf.name_scope('input_layer'): + IDT padded_buffer = tf.pad(buffer, [[0, 0], [0, buffer_max_len - buffer_len], [0, 0]]) + IDT padded_stack = tf.pad(stack, [[0, 0], [0, stack_max_len - stack_len], [0, 0]]) + IDT reshaped_padded_buffer = tf.reshape(padded_buffer, [1, buffer_max_len, lstm_dim]) + IDT reshaped_padded_stack = tf.reshape(padded_stack, [1, stack_max_len, lstm_dim]) + IDT lstm_buffer = nn_run_lstm_input(reshaped_padded_buffer, lstm_dim, buffer_len, 'lstm_buffer') + IDT lstm_stack = nn_run_lstm_input(reshaped_padded_stack, lstm_dim, stack_len, 'lstm_stack') + IDT input_concat_vec = , lstm_buffer], axis= ) IDT input_dim = 2 * lstm_dim + IDT input_weight = , output_dim], stddev= , name= ) IDT input_bias = , name= ) IDT input_out = tf.nn.relu(tf.matmul(input_concat_vec, input_weight) + input_bias) +with tf.name_scope('output_layer'): + IDT output_weight = , n_class], stddev= , name= ) IDT output_bias = , name= ) IDT out = tf.matmul(input_out, output_weight) + output_bias + IDT prediction = tf.multiply(tf.nn.softmax(out), output_mask) +with tf.name_scope('calculate_loss'): + IDT dropped_hidden = tf.nn.dropout(input_out, dropout_prob) + IDT dropped_output = tf.matmul(dropped_hidden, output_weight) + output_bias + IDT one_hot_labels = , n_class, on_value= , off_value= ) IDT ce_loss = , labels= ) IDT loss = ce_loss +with tf.name_scope('initial_buffer'): + IDT mapped_subwords_b = tf.nn.embedding_lookup(subword_emb, subword_ph) + IDT mapped_word_can = tf.nn.embedding_lookup(word_emb, word_candidates_ph) + IDT reshaped_word_can = tf.reshape(mapped_word_can, [tf.shape(mapped_word_can)[0], -1]) + IDT mapped_bpos = tf.nn.embedding_lookup(bpos_emb, bpos_ph) + IDT reshaped_bpos = tf.reshape(mapped_bpos, [tf.shape(mapped_bpos)[0], -1]) + IDT buffer_concat_vec = , reshaped_word_can, reshaped_bpos], axis= ) IDT input_dim = (k_word_candidate + k_bpos_candidate) * candidate_emb_dim + word_emb_dim + IDT buffer_weight = , lstm_dim], stddev= , name= ) IDT buffer_bias = , name= ) IDT buffer_list = tf.nn.relu(tf.matmul(buffer_concat_vec, buffer_weight) + buffer_bias) + IDT reshaped_buffer = tf.expand_dims(buffer_list, 0) + IDT assign_buffer = , reshaped_buffer, validate_shape= )with tf.name_scope('remove_buffer'): + IDT shorten_buffer = buffer[:, :-1, :] + IDT remove_from_buffer = , shorten_buffer, validate_shape= )with tf.name_scope('stack_config'): + IDT stack_word_dim = word_emb_dim + subword_lstm_dim + param_emb_dim + IDT stack_word_weight = , lstm_dim], stddev= , name= ) IDT stack_word_bias = , name= ) IDT stack_rel_dim = 2 * lstm_dim + param_emb_dim + IDT stack_rel_weight = , lstm_dim], stddev= , name= ) IDT stack_rel_bias = , name= )with tf.name_scope('action_config'): + IDT action_weight = , lstm_dim], stddev= , name= ) IDT action_bias = , name= )with tf.name_scope('stack_word_node'): + IDT mapped_word = tf.nn.embedding_lookup(word_lm_emb, word_ph) + IDT mapped_subwords_s = tf.nn.embedding_lookup(subword_emb, subword_list_ph) + IDT subword_lstm = nn_run_lstm_input(tf.expand_dims(mapped_subwords_s, 0), subword_lstm_dim, subwords_len, 'lstm_subword') + IDT mapped_pos = tf.nn.embedding_lookup(pos_emb, pos_ph) + IDT stack_concat_vec = , subword_lstm, mapped_pos], axis= ) IDT stack_vec = tf.nn.relu(tf.matmul(stack_concat_vec, stack_word_weight) + stack_word_bias) + IDT reshaped_stack_vec = tf.expand_dims(stack_vec, 0) +with tf.name_scope('left_arc'): + IDT left_dep_node = stack[:, (-2), :] + IDT right_head_node = stack[:, (-1), :] + IDT la_mapped_action = tf.nn.embedding_lookup(action_emb, relation_action_ph) + IDT left_arc_concat = , left_dep_node, la_mapped_action], axis= ) IDT composed_left_arc = tf.nn.tanh(tf.matmul(left_arc_concat, stack_rel_weight) + stack_rel_bias) + IDT reshaped_left_arc_vec = tf.expand_dims(composed_left_arc, 0) +with tf.name_scope('right_arc'): + IDT left_head_node = stack[:, (-2), :] + IDT right_dep_node = stack[:, (-1), :] + IDT ra_mapped_action = tf.nn.embedding_lookup(action_emb, relation_action_ph) + IDT right_arc_concat = , right_dep_node, ra_mapped_action], axis= ) IDT composed_right_arc = tf.nn.tanh(tf.matmul(right_arc_concat, stack_rel_weight) + stack_rel_bias) + IDT reshaped_right_arc_vec = tf.expand_dims(composed_right_arc, 0) +with tf.name_scope('modify_stack'): + IDT initial_stack = , reshaped_stack_vec, validate_shape= ) IDT new_word_stack = , reshaped_stack_vec], axis= ) IDT add_word_to_stack = , new_word_stack, validate_shape= ) IDT new_replaced_word = , :-1, :], reshaped_stack_vec], axis= ) IDT replace_word_tos = , new_replaced_word, validate_shape= ) IDT new_stack_left_arc = , :-2, :], reshaped_left_arc_vec], axis= ) IDT left_arc_replace = , new_stack_left_arc, validate_shape= ) IDT new_stack_right_arc = , :-2, :], reshaped_right_arc_vec], axis= ) IDT right_arc_replace = , new_stack_right_arc, validate_shape= )with tf.name_scope('modify_actions'): + IDT mapped_actions = tf.nn.embedding_lookup(action_emb, action_ph) + IDT action_vec = tf.nn.relu(tf.matmul(mapped_actions, action_weight) + action_bias) + IDT reshaped_action_vec = tf.expand_dims(action_vec, 0) + IDT initial_actions = , reshaped_action_vec, validate_shape= ) IDT new_actions = , reshaped_action_vec], axis= ) IDT add_to_actions = , new_actions, validate_shape= )with tf.name_scope('optimize'): + IDT optimizer = ) IDT variable_list = tf.trainable_variables() + IDT compute_grad = , var_list= ) IDT gradient_tensor_ph = , name= ) IDT calc_gradient_sum = tf.reduce_sum(gradient_tensor_ph, 0) + IDT gradient_ph = [(tf.placeholder(tf.float32), grad_info[1]) for grad_info in compute_grad] + IDT apply_grad = optimizer.apply_gradients(gradient_ph) +init = tf.global_variables_initializer() +saver = ) + +class ParserModel: + + IDT def __init__(self, params, embeddings= , model_path= ) IDT IDT self.params = params + IDT IDT self.session = tf.Session() + IDT IDT if model_path is not None: + IDT IDT IDT saver.restore(self.session, model_path) + IDT IDT else: + IDT IDT IDT if embeddings is None: + IDT IDT IDT IDT raise Exception('You must feed embeddings value if model_path is not defined.') + IDT IDT IDT feed_dict = {word_emb_ph: embeddings['word'], subword_emb_ph: embeddings['subword']} + IDT IDT IDT self.session.run(init) + IDT IDT IDT self.session.run([assign_word_embedding, assign_subword_embedding], feed_dict= ) IDT IDT self.subwords = list() + IDT IDT self.tos_subwords = list() + IDT IDT self.stack_len = 0 + IDT IDT self.buffer_len = 0 + IDT IDT self.actions_len = 0 + IDT IDT self.gradients = [list() for _ in range(len(compute_grad))] + IDT IDT self.stack_states = list() + IDT IDT self.action_state = None + + IDT def calc_loss(self, action_label, feasible_actions): + IDT IDT feed_dict = {label: [action_label], output_mask: feasible_actions, stack_len: self.stack_len, buffer_len: self.buffer_len, actions_len: self.actions_len} + IDT IDT train_loss = , feed_dict= ) IDT IDT grad_op = [grad[0] for grad in compute_grad if grad[0] is not None] + IDT IDT grad_idx = [idx for idx, grad in enumerate(compute_grad) if grad[0] is not None] + IDT IDT gradients = , feed_dict= ) IDT IDT for idx, value in zip(grad_idx, gradients): + IDT IDT IDT self.gradients[idx].append(value) + IDT IDT return train_loss + + IDT def train(self): + IDT IDT feed_dict = dict() + IDT IDT for grad_idx, grad_value in enumerate(self.gradients): + IDT IDT IDT if len(grad_value) > 0: + IDT IDT IDT IDT computed_value = , feed_dict= ) IDT IDT IDT IDT feed_dict[gradient_ph[grad_idx][0]] = computed_value + IDT IDT IDT else: + IDT IDT IDT IDT feed_dict[gradient_ph[grad_idx][0]] = self.session.run(tf.zeros(tf.shape(compute_grad[grad_idx][1]))) + IDT IDT self.session.run(apply_grad, feed_dict= ) + IDT def predict(self, feasible_actions): + IDT IDT feed_dict = {output_mask: feasible_actions, stack_len: self.stack_len, buffer_len: self.buffer_len, actions_len: self.actions_len} + IDT IDT pred = , feed_dict= ) IDT IDT return argmax(pred) + + IDT def save_model(self, model_path, global_step): + IDT IDT saver.save(self.session, model_path, global_step= ) IDT IDT + + IDT def initial_parser_model(self, subwords, word_candidates, bpos_candidates, real_subword): + IDT IDT self.tos_subwords = list() + IDT IDT self.subwords = real_subword + IDT IDT self.buffer_len = len(real_subword) + 1 + IDT IDT self.stack_len = 1 + IDT IDT self.actions_len = 1 + IDT IDT for grad_list in self.gradients: + IDT IDT IDT grad_list.clear() + IDT IDT feed_dict = {subword_ph: [1] + list(reversed(subwords)), word_candidates_ph: [[1] * k_word_candidate] + list(reversed(word_candidates)), bpos_ph: [[n_bpos - 1] * k_bpos_candidate] + list(reversed(bpos_candidates)), buffer_len: self.buffer_len} + IDT IDT self.session.run(assign_buffer, feed_dict= ) IDT IDT feed_dict = {word_ph: [1], subword_list_ph: [1], pos_ph: [n_pos - 1], stack_len: self.buffer_len, subwords_len: 1} + IDT IDT self.session.run(initial_stack, feed_dict= ) + IDT def take_action(self, action_index): + IDT IDT action, params = self.params['reverse_action_map'][action_index] + IDT IDT if action == 'SHIFT': + IDT IDT IDT self.take_action_shift(params) + IDT IDT elif action == 'APPEND': + IDT IDT IDT self.take_action_append(params) + IDT IDT elif action == 'LEFT-ARC': + IDT IDT IDT self.take_action_left_arc(params) + IDT IDT else: + IDT IDT IDT self.take_action_right_arc(params) + IDT IDT self.actions_len += 1 + + IDT def take_action_shift(self, pos_tag): + IDT IDT word = self.subwords[0] + IDT IDT self.tos_subwords = [word] + IDT IDT self.subwords = self.subwords[1:] + IDT IDT self.stack_len += 1 + IDT IDT self.buffer_len -= 1 + IDT IDT feed_dict = self.get_word_stack_feed_dict(word, self.tos_subwords, pos_tag) + IDT IDT self.session.run([add_word_to_stack, remove_from_buffer], feed_dict= ) + IDT def take_action_append(self, pos_tag): + IDT IDT subword = self.subwords[0] + IDT IDT self.tos_subwords.append(subword) + IDT IDT word = ''.join(self.tos_subwords) + IDT IDT self.subwords = self.subwords[1:] + IDT IDT self.buffer_len -= 1 + IDT IDT feed_dict = self.get_word_stack_feed_dict(word, self.tos_subwords, pos_tag) + IDT IDT self.session.run([replace_word_tos, remove_from_buffer], feed_dict= ) + IDT def take_action_left_arc(self, rel): + IDT IDT self.stack_len -= 1 + IDT IDT feed_dict = {relation_action_ph: [self.params['dep_label_map'][rel]], stack_len: self.stack_len} + IDT IDT self.session.run(left_arc_replace, feed_dict= ) + IDT def take_action_right_arc(self, rel): + IDT IDT self.stack_len -= 1 + IDT IDT feed_dict = {relation_action_ph: [self.params['dep_label_map'][rel]], stack_len: self.stack_len} + IDT IDT self.session.run(right_arc_replace, feed_dict= ) + IDT def get_word_stack_feed_dict(self, word, subwords, pos): + IDT IDT feed_dict = {word_ph: [self.params['word_map'].get(word, 0)], subword_list_ph: [self.params['subword_map'].get(subword, 0) for subword in subwords], pos_ph: [self.params['pos_map'][pos]], buffer_len: self.buffer_len, stack_len: self.buffer_len, subwords_len: len(self.subwords)} + IDT IDT return feed_dict +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import datetime +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data/') + + +def discriminator(image, reuse= ) IDT if reuse: + IDT IDT tf.get_variable_scope().reuse_variables() + IDT weights1 = , [5, 5, 1, 32], initializer= ) IDT bais1 = , [32], initializer= ) IDT layer1 = , filter= , strides= , 1, 1, 1], padding= ) IDT layer1 = layer1 + bais1 + IDT layer1 = tf.nn.relu(layer1) + IDT layer1 = pooling_layer(layer1, 5, 2) + IDT weights2 = , [5, 5, 32, 64], initializer= ) IDT bais2 = , [64], initializer= ) IDT layer2 = , filter= , strides= , 1, 1, 1], padding= ) IDT layer2 = layer2 + bais2 + IDT layer2 = tf.nn.relu(layer2) + IDT layer2 = pooling_layer(layer2, 5, 2) + IDT weights3 = , [7 * 7 * 64, 1024], initializer= ) IDT bais3 = , [1024], initializer= ) IDT layer3 = tf.reshape(layer2, [-1, 7 * 7 * 64]) + IDT layer3 = tf.matmul(layer3, weights3) + IDT layer3 = layer3 + bais3 + IDT layer3 = tf.nn.relu(layer3) + IDT weights4 = , [1024, 1], initializer= ) IDT bais4 = , [1], initializer= ) IDT layer4 = tf.matmul(layer3, weights4) + bais4 + IDT return layer4 + + +def pooling_layer(layer, kernel_size, stride_size): + IDT kernel = [1, kernel_size, kernel_size, 1] + IDT stride = [1, stride_size, stride_size, 1] + IDT return tf.nn.max_pool(layer, ksize= , strides= , padding= ) + +def conv_layer(layer, kernel, input_shape, output_shape, stride_size, biases): + IDT weights = self.add_weights([kernel, kernel, input_shape, output_shape]) + IDT biases = self.add_biases([output_shape]) + IDT stride = [1, stride_size, stride_size, 1] + IDT layer = , weights, strides= , padding= ) IDT return layer + + +def generator(batch_size, dimension): + IDT z = , dimension], mean= , stddev= , name= ) IDT weights1 = , [dimension, 3136], dtype= , initializer= ) IDT bais1 = , [3136], initializer= ) IDT layer1 = tf.matmul(z, weights1) + bais1 + IDT layer1 = tf.reshape(layer1, [-1, 56, 56, 1]) + IDT layer1 = , epsilon= , scope= ) IDT layer1 = tf.nn.relu(layer1) + IDT weights2 = , [3, 3, 1, dimension / 2], dtype= , initializer= ) IDT bais2 = , [dimension / 2], initializer= ) IDT layer2 = , weights2, strides= , 2, 2, 1], padding= ) IDT layer2 = layer2 + bais2 + IDT layer2 = , epsilon= , scope= ) IDT layer2 = tf.nn.relu(layer2) + IDT layer2 = tf.image.resize_images(layer2, [56, 56]) + IDT weights3 = , [3, 3, dimension / 2, dimension / 4], dtype= , initializer= ) IDT bais3 = , [dimension / 4], initializer= ) IDT layer3 = , weights3, strides= , 2, 2, 1], padding= ) IDT layer3 = layer3 + bais3 + IDT layer3 = , epsilon= , scope= ) IDT layer3 = tf.nn.relu(layer3) + IDT layer3 = tf.image.resize_images(layer3, [56, 56]) + IDT weights4 = , [1, 1, dimension / 4, 1], dtype= , initializer= ) IDT bais4 = , [1], initializer= ) IDT layer4 = , weights4, strides= , 2, 2, 1], padding= ) IDT layer4 = layer4 + bais4 + IDT layer4 = tf.sigmoid(layer4) + IDT return layer4 + + +session = tf.Session() +batch_size = 50 +dimensions = 100 +x_placeholder = , shape= , 28, 28, 1], name= )generator_z = generator(batch_size, dimensions) +discriminator_x = discriminator(x_placeholder) +discriminator_g = , reuse= )generator_loss = , labels= )discriminator_loss_real = , labels= , 1], 0.9))) )discriminator_loss_fake = , labels= )discriminator_loss = discriminator_loss_real + discriminator_loss_fake +tvars = tf.trainable_variables() +discriminator_vars = [var for var in tvars if 'discriminator_' in var.name] +generator_vars = [var for var in tvars if 'generator_' in var.name] +with tf.variable_scope(tf.get_variable_scope(), reuse= ) IDT discriminator_trainer_fake = , var_list= ) IDT discriminator_trainer_real = , var_list= ) IDT generator_trainer = , var_list= )tf.summary.scalar('Generator_loss', generator_loss) +tf.summary.scalar('Discriminator_loss_real', discriminator_loss_real) +tf.summary.scalar('Discriminator_loss_fake', discriminator_loss_fake) +discriminator_real_count_ph = tf.placeholder(tf.float32) +discriminator_fake_count_ph = tf.placeholder(tf.float32) +generator_count_ph = tf.placeholder(tf.float32) +tf.summary.scalar('d_real_count', discriminator_real_count_ph) +tf.summary.scalar('d_fake_count', discriminator_fake_count_ph) +tf.summary.scalar('g_count', generator_count_ph) +discriminator_on_generated = tf.reduce_mean(discriminator(generator(batch_size, dimensions))) +discriminator_on_real = tf.reduce_mean(discriminator(x_placeholder)) +tf.summary.scalar('d_on_generated_eval', discriminator_on_generated) +tf.summary.scalar('d_on_real_eval', discriminator_on_real) +images_for_tensorboard = generator(batch_size, dimensions) +tf.summary.image('Generated_images', images_for_tensorboard, 10) +merged = tf.summary.merge_all() +logdir = 'tensorboard/gan/' +writer = tf.summary.FileWriter(logdir, session.graph) + +saver = tf.train.Saver() +session.run(tf.global_variables_initializer()) +generatorLoss = 0 +discriminatorLossReal, discriminatorLossFake = 1, 1 +discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 +for i in range(100000): + IDT real_image_batch = mnist.train.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + IDT if discriminatorLossFake > 0.6: + IDT IDT _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_fake, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + IDT IDT discriminator_fake_count += 1 + IDT if generatorLoss > 0.5: + IDT IDT _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([generator_trainer, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + IDT IDT generator_count += 1 + IDT if discriminatorLossReal > 0.45: + IDT IDT _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_real, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + IDT IDT discriminator_real_count += 1 + IDT if i % 10 == 0: + IDT IDT real_image_batch = mnist.validation.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + IDT IDT summary = session.run(merged, {x_placeholder: real_image_batch, discriminator_real_count_ph: discriminator_real_count, discriminator_fake_count_ph: discriminator_fake_count, generator_count_ph: generator_count}) + IDT IDT writer.add_summary(summary, i) + IDT IDT discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 + IDT if i % 1000 == 0: + IDT IDT images = session.run(generator(3, dimensions)) + IDT IDT discriminator_result = session.run(discriminator(x_placeholder), {x_placeholder: images}) + IDT IDT ) + IDT IDT for j in range(3): + IDT IDT IDT + IDT IDT IDT im = images[(j), :, :, (0)] + IDT IDT IDT plt.imsave('imageAt' + str(i) + 'TyreeMath.png', im.reshape([28, 28]), cmap= ) IDT if i % 5000 == 0: + IDT IDT save_path = , 'models/pretrained_gan.ckpt', global_step= ) IDT IDT +test_images = session.run(generator(10, 100)) +test_eval = session.run(discriminator(x_placeholder), {x_placeholder: test_images}) +real_images = mnist.validation.next_batch(10)[0].reshape([10, 28, 28, 1]) +real_eval = session.run(discriminator(x_placeholder), {x_placeholder: real_images}) +for i in range(10): + IDT + IDT plt.imsave('generated_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap= )for i in range(10): + IDT + IDT plt.imsave('real_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +class CNN_TwoConvLayers: + + IDT def __init__(self, model_name, session, learning_rate, optimizer, learning_decay_rate, filter_sizes, num_filters, max_sentence_length, num_classes, embeddings, new_embeddings, embedding_dim, vocabulary_size, static, max_l2_norm, regularization_lambda, dropout_keep_prob): + IDT IDT self.model_name = model_name + IDT IDT self.session = session + IDT IDT self.learning_rate = learning_rate + IDT IDT self.optimizer = optimizer + IDT IDT self.dropout_keep_prob_train = dropout_keep_prob + IDT IDT self.regularization_lambda = regularization_lambda + IDT IDT self.input_x = , max_sentence_length), dtype= , name= ) IDT IDT self.input_y = , num_classes), dtype= , name= ) IDT IDT self.dropout_keep_prob = , name= ) IDT IDT self.embeddings_placeholder = , shape= , embedding_dim)) ) IDT IDT self.embeddings = , trainable= ) IDT IDT self.new_embeddings = , trainable= ) IDT IDT self.all_embeddings = , self.new_embeddings], axis= ) IDT IDT self.embedded_words = tf.nn.embedding_lookup(self.all_embeddings, self.input_x) + IDT IDT self.input_x_expanded = tf.expand_dims(self.embedded_words, -1) + IDT IDT num_filters_conv1 = int(num_filters / 3) + IDT IDT num_filters_conv2 = num_filters - num_filters_conv1 + IDT IDT self.pool_results = [] + IDT IDT for i, filter_size in enumerate(filter_sizes): + IDT IDT IDT filter1 = , shape= , embedding_dim, 1, num_filters_conv1), dtype= , initializer= ) IDT IDT IDT if max_l2_norm != 0: + IDT IDT IDT IDT filter1 = tf.clip_by_norm(filter1, max_l2_norm) + IDT IDT IDT bias1 = , shape= ,))) ) IDT IDT IDT conv1 = , filter= , strides= , 1, 1, 1], padding= ) IDT IDT IDT relu1 = tf.nn.relu(tf.nn.bias_add(conv1, bias1)) + IDT IDT IDT conv1_dim = max_sentence_length - filter_size + 1 + IDT IDT IDT filter2 = , shape= , 1, num_filters_conv1, num_filters_conv2), dtype= , initializer= ) IDT IDT IDT if max_l2_norm != 0: + IDT IDT IDT IDT filter2 = tf.clip_by_norm(filter2, max_l2_norm) + IDT IDT IDT bias2 = , shape= ,))) ) IDT IDT IDT conv2 = , filter= , strides= , 1, 1, 1], padding= ) IDT IDT IDT relu2 = tf.nn.relu(tf.nn.bias_add(conv2, bias2)) + IDT IDT IDT conv2_dim = conv1_dim - filter_size + 1 + IDT IDT IDT pooled = , ksize= , conv2_dim, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT self.pool_results.append(pooled) + IDT IDT num_filters_total = num_filters_conv2 * len(filter_sizes) + IDT IDT self.flat = tf.reshape(tf.concat(self.pool_results, 3), [-1, num_filters_total]) + IDT IDT self.dropout = tf.nn.dropout(self.flat, self.dropout_keep_prob) + IDT IDT W = , shape= , num_classes), dtype= , initializer= ) IDT IDT b = , shape= ,))) ) IDT IDT self.output = tf.nn.xw_plus_b(self.dropout, W, b) + IDT IDT self.predictions = tf.argmax(self.output, 1) + IDT IDT losses = , logits= ) IDT IDT if regularization_lambda != 0: + IDT IDT IDT l2_loss = tf.nn.l2_loss(W) + IDT IDT IDT self.loss = , tf.multiply(self.regularization_lambda, l2_loss)), name= ) IDT IDT else: + IDT IDT IDT self.loss = , name= ) IDT IDT self.optimizer = ) IDT IDT self.train_op = self.optimizer.minimize(self.loss) + IDT IDT self.saver = tf.train.Saver() + IDT IDT self.session.run(tf.global_variables_initializer(), feed_dict= ) + IDT def train_step(self, input_x, input_y): + IDT IDT _, loss = , self.loss], feed_dict= , self.input_y: input_y, self.dropout_keep_prob: self.dropout_keep_prob_train}) ) IDT IDT return loss + + IDT def feed(self, input_x): + IDT IDT return self.session.run([self.output, self.predictions], feed_dict= , self.dropout_keep_prob: 1}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class GRUAttnCell(tf.nn.rnn_cell.GRUCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(GRUAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(gru_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(tf.nn.rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, gru_state + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(LSTMAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, lstm_state + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT IDT -vocab_dim: dimension of the embeddings + IDT """ + + IDT def __init__(self, size, vocab_dim, name): + IDT IDT self.size = size + IDT IDT self.vocab_dim = vocab_dim + IDT IDT self.name = name + + IDT def encode(self, inputs, masks, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(self.name): + IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(self.size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT elif model_type == 'gru': + IDT IDT IDT IDT cell = GRUAttnCell(self.size, attention_inputs) + IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT cell = LSTMAttnCell(self.size, attention_inputs) + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT outputs, final_state = , inputs, sequence_length= , dtype= , initial_state= ) IDT IDT return outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, output_size, name): + IDT IDT self.output_size = output_size + IDT IDT self.name = name + + IDT def decode(self, knowledge_rep, masks, dropout, state_size, model_type= ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT + IDT + IDT + IDT + + IDT + IDT + IDT + IDT + IDT IDT with tf.variable_scope(self.name): + IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(state_size) + IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(state_size) + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT knowledge_rep = tf.squeeze(knowledge_rep, -1) + IDT IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer1'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer2'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer3'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + IDT IDT IDT IDT start_probs = tf.nn.dropout(start_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('startlayer4'): + IDT IDT IDT IDT start_probs = tf.nn.rnn_cell._linear(start_probs, self.output_size, True, 1.0) + IDT IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer1'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer2'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer3'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + IDT IDT IDT IDT end_probs = tf.nn.dropout(end_probs, 1 - dropout) + IDT IDT IDT with vs.variable_scope('endlayer4'): + IDT IDT IDT IDT end_probs = tf.nn.rnn_cell._linear(end_probs, self.output_size, True, 1.0) + IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT a = tf.constant(-1e+30) + IDT IDT b = tf.constant(1.0) + IDT IDT add_mask = a * (b - bool_masks) + IDT IDT start_probs = start_probs + add_mask + IDT IDT end_probs = end_probs + add_mask + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, encoder, decoder, pretrained_embeddings, max_ctx_len, max_q_len, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param encoder: tuple of 2 encoders that you constructed in train.py + IDT IDT :param decoder: a decoder that you constructed in train.py + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.question_encoder, self.context_encoder = encoder + IDT IDT self.decoder = decoder + IDT IDT self.max_ctx_len = max_ctx_len + IDT IDT self.max_q_len = max_q_len + IDT IDT self.embed_size = encoder[0].vocab_dim + IDT IDT self.flags = flags + IDT IDT self.context_placeholder = , shape= , self.max_ctx_len), name= ) IDT IDT self.question_placeholder = , shape= , self.max_q_len), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def mixer(self, final_q_state, ctx_states, model_type= ) IDT IDT if model_type == 'gru': + IDT IDT IDT pass + IDT IDT elif model_type == 'lstm': + IDT IDT IDT final_q_state = final_q_state[-1] + IDT IDT else: + IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT with vs.variable_scope('mixer'): + IDT IDT IDT ht = tf.nn.rnn_cell._linear(final_q_state, self.flags.state_size, True, 1.0) + IDT IDT IDT ht = , axis= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT ctx_state_rep = ctx_states * scores + IDT IDT return ctx_state_rep + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT question_states, final_question_state = , self.mask_q_placeholder, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, encoder_state_input= , attention_inputs= , model_type= ) IDT IDT feed_states = batch_matmul(ctx_states, tf.expand_dims(final_question_state, 2)) + IDT IDT self.start_probs, self.end_probs = , self.mask_ctx_placeholder, self.dropout_placeholder, self.flags.state_size, model_type= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT self.question_embeddings = tf.reshape(question_embeddings, [-1, self.max_q_len, self.embed_size]) + IDT IDT IDT context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT self.context_embeddings = tf.reshape(context_embeddings, [-1, self.max_ctx_len, self.embed_size]) + + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = np.array(map(np.array, context_batch)) + IDT IDT input_feed[self.question_placeholder] = np.array(map(np.array, question_batch)) + IDT IDT input_feed[self.mask_ctx_placeholder] = np.array(map(np.array, mask_ctx_batch)) + IDT IDT input_feed[self.mask_q_placeholder] = np.array(mask_q_batch) + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def answer(self, session, data): + IDT IDT data = np.array(data).T + IDT IDT yp, yp2 = self.decode(session, *data) + IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch) + + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset) + IDT IDT else: + IDT IDT IDT sampled = dataset[np.random.choice(dataset.shape[0], sample)] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT sampled = sampled.T + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][sampled[2][i][0]:sampled[2][i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT em.append(exact_match_score(pred_words, actual_words)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), None, sample)) + IDT IDT return f1, em + + IDT def run_epoch(self, sess, train_set, val_set, context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT + IDT IDT if self.flags.debug == 0: + IDT IDT IDT prog_val = ) IDT IDT IDT for i, batch in enumerate(minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT IDT IDT + IDT IDT IDT train_f1, train_em = , train_set, context= , sample= , log= , eval_set= ) IDT IDT IDT val_f1, val_em = , val_set, context= , sample= , log= , eval_set= ) + IDT def train(self, session, saver, dataset, contexts, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT train_dataset, val_dataset = dataset + IDT IDT train_mask = [None, None] + IDT IDT val_mask = [None, None] + IDT IDT train_dataset[0], train_mask[0] = self.pad(train_dataset[0], self.max_ctx_len) + IDT IDT train_dataset[1], train_mask[1] = self.pad(train_dataset[1], self.max_q_len) + IDT IDT val_dataset[0], val_mask[0] = self.pad(val_dataset[0], self.max_ctx_len) + IDT IDT val_dataset[1], val_mask[1] = self.pad(val_dataset[1], self.max_q_len) + IDT IDT for i in range(1, len(train_dataset[0])): + IDT IDT IDT assert len(train_dataset[0][i]) = ) IDT IDT IDT assert len(train_dataset[1][i]) = ) IDT IDT for i in range(1, len(val_dataset[0])): + IDT IDT IDT assert len(val_dataset[0][i]) = ) IDT IDT IDT assert len(val_dataset[1][i]) = ) IDT IDT + IDT IDT train_dataset.extend(train_mask) + IDT IDT val_dataset.extend(val_mask) + IDT IDT train_dataset = np.array(train_dataset).T + IDT IDT val_dataset = np.array(val_dataset).T + IDT IDT train_context = contexts[0] + IDT IDT val_context = contexts[1] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = train_dataset[:self.flags.batch_size] + IDT IDT IDT val_dataset = val_dataset[:self.flags.batch_size] + IDT IDT IDT num_epochs = 20 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT saver.save(session, train_dir) + IDT IDT self.evaluate_answer(session, train_dataset, train_context, sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session, val_dataset, val_context, sample= , log= , eval_set= )from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT data_d = , keep_prob= ) IDT output1 = tf.matmul(data_d, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf + + +class sigGF: + + IDT def __init__(self, image_shape, person_num): + IDT IDT self.image_shape = image_shape + IDT IDT self.person_num = person_num + IDT IDT self.X = tf.placeholder(tf.float32, [None, self.image_shape[0], self.image_shape[1], self.image_shape[2]]) + IDT IDT self.Y_M = tf.placeholder(tf.float32, [None, person_num]) + IDT IDT self.Y_D = tf.placeholder(tf.float32, [None, 2]) + IDT IDT self.keep_prob = tf.placeholder(tf.float32) + IDT IDT self.global_step = , trainable= , name= ) IDT IDT self.feature_vec = None + IDT IDT self.cost_M = None + IDT IDT self.cost_D = None + IDT IDT self.logit_M = None + IDT IDT self.logit_D = None + IDT IDT self.optimizer = None + IDT IDT self.build_model() + IDT IDT self.saver = tf.train.Saver(tf.global_variables()) + + IDT def build_model(self): + IDT IDT _input = self.X + IDT IDT W1 = , 3, 3, 32], stddev= ) IDT IDT L1 = , W1, strides= , 1, 1, 1], padding= ) IDT IDT L1 = tf.nn.relu(L1) + IDT IDT L1 = , ksize= , 4, 4, 1], strides= , 4, 4, 1], padding= ) IDT IDT W2 = , 3, 32, 64], stddev= ) IDT IDT L2 = , W2, strides= , 1, 1, 1], padding= ) IDT IDT L2 = tf.nn.relu(L2) + IDT IDT L2 = , ksize= , 4, 4, 1], strides= , 4, 4, 1], padding= ) IDT IDT L3 = tf.contrib.layers.flatten(L2) + IDT IDT L3 = , 512, activation= ) IDT IDT L3 = tf.layers.dropout(L3, self.keep_prob) + IDT IDT L4 = tf.contrib.layers.flatten(L3) + IDT IDT L4 = , 256, activation= ) IDT IDT L4 = tf.layers.dropout(L4, self.keep_prob) + IDT IDT self.feature_vec = L4 + IDT IDT self.logit_M = , self.person_num, activation= , trainable= ) IDT IDT self.cost_M = , labels= ) IDT IDT self.logit_D = , 2, activation= , trainable= ) IDT IDT self.cost_D = , labels= ) IDT IDT self.optimizer = tf.train.AdamOptimizer(0.001).minimize(self.cost_M + self.cost_D) + + IDT def train(self, session, X, Y_M, Y_D): + IDT IDT return session.run([self.optimizer, self.cost_M, self.cost_D], feed_dict= , self.Y_M: Y_M, self.Y_D: Y_D, self.keep_prob: 0.7}) ) + IDT def predict(self, session, X): + IDT IDT return session.run([self.logit_M, self.logit_D], feed_dict= , self.keep_prob: 1}) ) + IDT def test(self, session, X, Y_M, Y_D): + IDT IDT self.is_correct = tf.equal(tf.argmax(self.logit_M, 1), tf.argmax(self.Y_M, 1)) + IDT IDT self.acc_M = tf.reduce_mean(tf.cast(self.is_correct, tf.float32)) + IDT IDT self.is_correct2 = tf.equal(tf.argmax(self.logit_D, 1), tf.argmax(self.Y_D, 1)) + IDT IDT self.acc_D = tf.reduce_mean(tf.cast(self.is_correct2, tf.float32)) + IDT IDT return session.run([self.acc_M, self.acc_D], feed_dict= , self.Y_M: Y_M, self.Y_D: Y_D, self.keep_prob: 1}) ) + IDT def get_feature(self, session, X): + IDT IDT return session.run(self.feature_vec, feed_dict= , self.keep_prob: 1}) ) + +class sigG: + + IDT def __init__(self, image_shape, person_num): + IDT IDT self.image_shape = image_shape + IDT IDT self.person_num = person_num + IDT IDT self.X = tf.placeholder(tf.float32, [None, self.image_shape[0], self.image_shape[1], self.image_shape[2]]) + IDT IDT self.Y_M = tf.placeholder(tf.float32, [None, person_num]) + IDT IDT self.keep_prob = tf.placeholder(tf.float32) + IDT IDT self.global_step = , trainable= , name= ) IDT IDT self.feature_vec = None + IDT IDT self.cost_M = None + IDT IDT self.logit_M = None + IDT IDT self.optimizer = None + IDT IDT self.build_model() + IDT IDT self.saver = tf.train.Saver(tf.global_variables()) + + IDT def build_model(self): + IDT IDT _input = self.X + IDT IDT W1 = , 3, 3, 32], stddev= ) IDT IDT L1 = , W1, strides= , 1, 1, 1], padding= ) IDT IDT L1 = tf.nn.relu(L1) + IDT IDT L1 = , ksize= , 4, 4, 1], strides= , 4, 4, 1], padding= ) IDT IDT W2 = , 3, 32, 64], stddev= ) IDT IDT L2 = , W2, strides= , 1, 1, 1], padding= ) IDT IDT L2 = tf.nn.relu(L2) + IDT IDT L2 = , ksize= , 4, 4, 1], strides= , 4, 4, 1], padding= ) IDT IDT L3 = tf.contrib.layers.flatten(L2) + IDT IDT L3 = , 512, activation= ) IDT IDT L3 = tf.layers.dropout(L3, self.keep_prob) + IDT IDT L4 = tf.contrib.layers.flatten(L3) + IDT IDT L4 = , 256, activation= ) IDT IDT L4 = tf.layers.dropout(L4, self.keep_prob) + IDT IDT self.feature_vec = L4 + IDT IDT self.logit_M = , self.person_num, activation= , trainable= ) IDT IDT self.cost_M = , labels= ) IDT IDT self.optimizer = tf.train.AdamOptimizer(0.001).minimize(self.cost_M) + + IDT def train(self, session, X, Y_M): + IDT IDT return session.run([self.optimizer, self.cost_M], feed_dict= , self.Y_M: Y_M, self.keep_prob: 0.7}) ) + IDT def predict(self, session, X): + IDT IDT return session.run(self.logit_M, feed_dict= , self.keep_prob: 1}) ) + IDT def test(self, session, X, Y_M): + IDT IDT self.is_correct = tf.equal(tf.argmax(self.logit_M, 1), tf.argmax(self.Y_M, 1)) + IDT IDT self.acc_M = tf.reduce_mean(tf.cast(self.is_correct, tf.float32)) + IDT IDT return session.run(self.acc_M, feed_dict= , self.Y_M: Y_M, self.keep_prob: 1}) ) + IDT def get_feature(self, session, X): + IDT IDT return session.run(self.feature_vec, feed_dict= , self.keep_prob: 1}) )import tensorflow as tf +import numpy as np +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT self.rewards = , shape= ,), name= ) IDT IDT self.indices = , shape= , 2), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.tanh(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + IDT IDT self.select_prob = tf.gather_nd(self.prob, self.indices) + IDT IDT self.result = tf.multiply(self.select_prob, self.rewards) + IDT IDT self.entropy = -tf.reduce_sum(tf.multiply(self.prob, tf.nn.softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)))) + IDT IDT self.pg_loss = tf.reduce_mean(self.result) + l2 * self.entropy + IDT IDT self.pg_optimization = ) IDT IDT self.optimization = ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.saved_path = './saved_model' + IDT IDT self.count = 0 + + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def policy_train(self, session, inputs, outputs, rewards): + IDT IDT form, pos, deprel = inputs + IDT IDT indices = [] + IDT IDT for aid in range(len(outputs)): + IDT IDT IDT indices.append([aid, outputs[aid]]) + IDT IDT _, cost, indices, prob, res = , self.pg_loss, self.indices, self.prob, self.result], feed_dict= , self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) ) IDT IDT self.count += 1 + IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + IDT def variable_output(self, session): + IDT IDT form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + IDT IDT return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + IDT def save(self, session): + IDT IDT self.saved_path = self.saver.save(session, './saved_model') + + IDT def restore(self, session): + IDT IDT self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + IDT IDT session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + IDT IDT session.run(self.update_op) +import tensorflow as tf +import numpy as np +import os + + +class PG_Crypto(object): + + IDT def __init__(self, feature_number, hidden_units_number= , 32, 2], learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.s = , shape= , feature_number], name= ) IDT IDT self.a = , shape= , name= ) IDT IDT self.r = , shape= , name= ) IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.dropout_keep_prob = , shape= , name= ) IDT IDT with tf.variable_scope('rnn', initializer= , regularizer= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_out = tf.nn.softmax(self.a_prob) + IDT IDT with tf.variable_scope('reward'): + IDT IDT IDT negative_cross_entropy = , labels= ) IDT IDT with tf.variable_scope('train'): + IDT IDT IDT optimizer = ) IDT IDT IDT self.loss = tf.reduce_mean(negative_cross_entropy * self.r) + IDT IDT IDT self.train_op = optimizer.minimize(self.loss) + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act= , use_bias= ) IDT IDT output = inputs + IDT IDT for n in output_shape: + IDT IDT IDT output = , n, activation= , use_bias= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + IDT def _add_gru_cell(self, units_number, activation= ) IDT IDT return tf.contrib.rnn.GRUCell(num_units= , activation= ) + IDT def train(self, drop= ) IDT IDT feed = {self.a: np.array(self.a_buffer), self.r: np.array(self.r_buffer), self.s: np.array(self.s_buffer), self.dropout_keep_prob: drop} + IDT IDT _, loss = , self.loss], feed_dict= ) IDT IDT return loss + + IDT def restore_buffer(self): + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + + IDT def save_transation(self, s, a, r): + IDT IDT self.a_buffer.append(a) + IDT IDT self.r_buffer.append(r) + IDT IDT self.s_buffer.append(s) + + IDT def trade(self, s, train= , drop= ) IDT IDT feed = {self.s: s, self.dropout_keep_prob: drop} + IDT IDT a_prob = , feed_dict= ) IDT IDT a_prob = a_prob[0].flatten() + IDT IDT if train: + IDT IDT IDT a_indices = np.arange(a_prob.shape[0]) + IDT IDT IDT return np.random.choice(a_indices, p= ) IDT IDT else: + IDT IDT IDT return np.argmax(a_prob) + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) + + +class PG_Crypto_portfolio(object): + + IDT def __init__(self, feature_number, action_size= , hidden_units_number= , 300, 128], learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.s = , shape= , feature_number], name= ) IDT IDT self.a = , shape= , action_size], name= ) IDT IDT self.r = , shape= , name= ) IDT IDT self.action_size = action_size + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.dropout_keep_prob = , shape= , name= ) IDT IDT with tf.variable_scope('policy', initializer= , regularizer= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_out = tf.nn.softmax(self.a_prob) + IDT IDT with tf.variable_scope('reward'): + IDT IDT IDT negative_cross_entropy = , labels= ) IDT IDT with tf.variable_scope('train'): + IDT IDT IDT optimizer = ) IDT IDT IDT self.loss = tf.reduce_mean(negative_cross_entropy * self.r) + IDT IDT IDT self.train_op = optimizer.minimize(self.loss) + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act= , use_bias= ) IDT IDT output = inputs + IDT IDT for n in output_shape: + IDT IDT IDT output = , n, activation= , use_bias= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + IDT def train(self, drop= ) IDT IDT random_index = np.arange(len(self.a_buffer)) + IDT IDT np.random.shuffle(random_index) + IDT IDT feed = {self.a: np.array(self.a_buffer)[random_index], self.r: np.array(self.r_buffer)[random_index], self.s: np.array(self.s_buffer)[random_index], self.dropout_keep_prob: drop} + IDT IDT _, loss = , self.loss], feed_dict= ) IDT IDT return loss + + IDT def restore_buffer(self): + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + + IDT def save_transation(self, s, a, r): + IDT IDT self.a_buffer.append(a) + IDT IDT self.r_buffer.append(r) + IDT IDT self.s_buffer.append(s) + + IDT def trade(self, s, train= , drop= ) IDT IDT feed = {self.s: s, self.dropout_keep_prob: drop} + IDT IDT a_prob = , feed_dict= ) IDT IDT a_prob = a_prob[0].flatten() + IDT IDT if train: + IDT IDT IDT a_indices = np.arange(a_prob.shape[0]) + IDT IDT IDT target_index = , p= ) IDT IDT IDT a = np.zeros(a_prob.shape[0]) + IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT return a + IDT IDT else: + IDT IDT IDT target_index = np.argmax(a_prob) + IDT IDT IDT a = np.zeros(a_prob.shape[0]) + IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT return a + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) + + +class RPG_Crypto_portfolio(object): + + IDT def __init__(self, feature_number, action_size= , hidden_units_number= , 64], learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.s = , shape= , feature_number], name= ) IDT IDT self.a = , shape= , action_size], name= ) IDT IDT self.r = , shape= , name= ) IDT IDT self.s_next = , shape= , feature_number], name= ) IDT IDT self.action_size = action_size + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.s_next_buffer = [] + IDT IDT self.dropout_keep_prob = , shape= , name= ) IDT IDT with tf.variable_scope('rnn_encoder', initializer= , regularizer= ) IDT IDT IDT cell = , activation= , keep_prob= ) IDT IDT IDT self.rnn_input = , axis= ) IDT IDT IDT self.rnn_output, _ = , cell= , dtype= ) IDT IDT IDT self.rnn_output = , axis= ) IDT IDT with tf.variable_scope('supervised', initializer= , regularizer= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + IDT IDT with tf.variable_scope('policy_gradient', initializer= , regularizer= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_out = , axis= ) IDT IDT IDT self.negative_cross_entropy = , labels= ) IDT IDT with tf.variable_scope('train'): + IDT IDT IDT optimizer_rl = ) IDT IDT IDT optimizer_sl = ) IDT IDT IDT self.rlloss = tf.reduce_mean(self.negative_cross_entropy * self.r) + IDT IDT IDT self.slloss = tf.reduce_mean(self.state_loss) + IDT IDT IDT self.rltrain_op = optimizer_rl.minimize(self.rlloss) + IDT IDT IDT self.sltrain_op = optimizer_sl.minimize(self.slloss) + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act= , use_bias= ) IDT IDT output = inputs + IDT IDT for n in output_shape: + IDT IDT IDT output = , n, activation= , use_bias= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + IDT def _add_GRU(self, units_number, activation= , keep_prob= ) IDT IDT cell = , activation= ) IDT IDT cell = , input_keep_prob= ) IDT IDT return cell + + IDT def _add_GRUs(self, units_number, activation, keep_prob= ) IDT IDT cells = , activation= , a in zip(units_number, activation)]) ) IDT IDT return cells + + IDT def _add_gru_cell(self, units_number, activation= ) IDT IDT return tf.contrib.rnn.GRUCell(num_units= , activation= ) + IDT def train(self, drop= ) IDT IDT feed = {self.a: np.array(self.a_buffer), self.r: np.array(self.r_buffer), self.s: np.array(self.s_buffer), self.s_next: np.array(self.s_next_buffer), self.dropout_keep_prob: drop} + IDT IDT self.session.run([self.rltrain_op, self.sltrain_op], feed_dict= ) + IDT def restore_buffer(self): + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.s_next_buffer = [] + + IDT def save_current_state(self, s): + IDT IDT self.s_buffer.append(s) + + IDT def save_transation(self, a, r, s_next): + IDT IDT self.a_buffer.append(a) + IDT IDT self.r_buffer.append(r) + IDT IDT self.s_next_buffer.append(s_next) + + IDT def trade(self, s, train= , drop= , prob= ) IDT IDT feed = {self.s: np.array(self.s_buffer), self.dropout_keep_prob: drop} + IDT IDT a_prob = , feed_dict= ) IDT IDT a_prob = a_prob[-1][-1].flatten() + IDT IDT if train: + IDT IDT IDT a_indices = np.arange(a_prob.shape[0]) + IDT IDT IDT target_index = , p= ) IDT IDT IDT a = np.zeros(a_prob.shape[0]) + IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT return a + IDT IDT else: + IDT IDT IDT if prob: + IDT IDT IDT IDT return a_prob + IDT IDT IDT target_index = np.argmax(a_prob) + IDT IDT IDT a = np.zeros(a_prob.shape[0]) + IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT return a + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) + + +class DRL_Crypto_portfolio(object): + + IDT def __init__(self, feature_number, action_size= , c= , hidden_units_number= , 64], learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.s = , shape= , feature_number], name= ) IDT IDT self.d = , shape= , action_size - 1], name= ) IDT IDT self.s_buffer = [] + IDT IDT self.d_buffer = [] + IDT IDT self.c = c + IDT IDT self.action_size = action_size + IDT IDT self.dropout_keep_prob = , shape= , name= ) IDT IDT with tf.variable_scope('rnn_encoder', initializer= , regularizer= ) IDT IDT IDT cells = , action_size], activation= , tf.nn.relu]) ) IDT IDT IDT self.rnn_input = , axis= ) IDT IDT IDT self.rnn_output, _ = , cell= , dtype= ) IDT IDT IDT self.a_prob = , axis= ) IDT IDT with tf.variable_scope('direct_RL', initializer= , regularizer= ) IDT IDT IDT self.a_out = , axis= ) IDT IDT IDT self.a_out = , shape= , self.action_size]), self.a_out), axis= ) IDT IDT IDT self.reward = , :-1] - self.c * tf.abs(self.a_out[1:, :-1] - self.a_out[:-1, :-1]), axis= ) IDT IDT IDT self.total_reward = tf.reduce_sum(self.reward) + IDT IDT IDT self.mean_reward = tf.reduce_mean(self.reward) + IDT IDT with tf.variable_scope('train'): + IDT IDT IDT optimizer = ) IDT IDT IDT self.train_op = optimizer.minimize(-self.mean_reward) + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act= , use_bias= ) IDT IDT output = inputs + IDT IDT for n in output_shape: + IDT IDT IDT output = , n, activation= , use_bias= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + IDT def _add_GRU(self, units_number, activation= , keep_prob= ) IDT IDT cell = , activation= ) IDT IDT cell = , input_keep_prob= ) IDT IDT return cell + + IDT def _add_GRUs(self, units_number, activation, keep_prob= ) IDT IDT cells = , activation= , a in zip(units_number, activation)]) ) IDT IDT return cells + + IDT def _add_gru_cell(self, units_number, activation= ) IDT IDT return tf.contrib.rnn.GRUCell(num_units= , activation= ) + IDT def train(self, drop= ) IDT IDT feed = {self.s: np.array(self.s_buffer), self.d: np.array(self.d_buffer), self.dropout_keep_prob: drop} + IDT IDT self.session.run([self.train_op], feed_dict= ) + IDT def restore_buffer(self): + IDT IDT self.s_buffer = [] + IDT IDT self.d_buffer = [] + + IDT def save_current_state(self, s, d): + IDT IDT self.s_buffer.append(s) + IDT IDT self.d_buffer.append(d) + + IDT def trade(self, train= , drop= , prob= ) IDT IDT feed = {self.s: np.array(self.s_buffer), self.dropout_keep_prob: drop} + IDT IDT a_prob = , feed_dict= ) IDT IDT a_prob = a_prob[-1][-1].flatten() + IDT IDT return a_prob + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) + + +class DuelingDQN_portfolio(object): + + IDT def __init__(self, a_dim, s_dim, buffer_size, batch_size, update_target_interval= , epsilon= , gamma= , learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.a_dim, self.s_dim = a_dim, s_dim + IDT IDT self.buffer_size = buffer_size + IDT IDT self.batch_size = batch_size + IDT IDT self.buffer = , self.s_dim * 2 + 2), dtype= ) IDT IDT self.buffer_length = 0 + IDT IDT self.update_target_interval = update_target_interval + IDT IDT self.critic_loss = 0 + IDT IDT self.total_step = 0 + IDT IDT self.epsilon = epsilon + IDT IDT self.learning_rate = learning_rate + IDT IDT self.gamma = gamma + IDT IDT self.s = , [None, self.s_dim], name= ) IDT IDT self.s_next = , [None, self.s_dim], name= ) IDT IDT self.q_next = , [None, self.a_dim], name= ) IDT IDT with tf.variable_scope('q_eval', initializer= , regularizer= ) IDT IDT IDT self.q_eval = self._build_net(self.s, scope) + IDT IDT with tf.variable_scope('q_target', initializer= , regularizer= ) IDT IDT IDT self.q_target = self._build_net(self.s_next, scope) + IDT IDT with tf.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.squared_difference(self.q_next, self.q_eval)) + IDT IDT with tf.variable_scope('train'): + IDT IDT IDT self.train_op = tf.train.RMSPropOptimizer(self.learning_rate).minimize(self.loss) + IDT IDT IDT self.e_params = , scope= ) IDT IDT IDT self.t_params = , scope= ) IDT IDT IDT self.update_q_target_op = [tf.assign(t, e) for t, e in zip(self.t_params, self.e_params)] + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _build_net(self, s, scope): + IDT IDT with tf.variable_scope(scope): + IDT IDT IDT net = , 64, activation= , name= ) IDT IDT IDT net = , 32, activation= , name= ) IDT IDT IDT value = , 1, activation= , name= ) IDT IDT IDT advantage = , self.a_dim, activation= , name= ) IDT IDT IDT q = , axis= , keep_dims= ) IDT IDT IDT return q + + IDT def trade(self, s, train= ) IDT IDT q = self.session.run(self.q_eval, {self.s: s}) + IDT IDT a = np.argmax(q) + IDT IDT action = np.zeros(self.a_dim) + IDT IDT if train: + IDT IDT IDT if np.random.uniform() < self.epsilon: + IDT IDT IDT IDT action[a] = 1.0 + IDT IDT IDT IDT return action + IDT IDT IDT else: + IDT IDT IDT IDT action[np.random.randint(0, self.a_dim)] = 1.0 + IDT IDT IDT IDT return action + IDT IDT action[a] = 1.0 + IDT IDT return action + + IDT def update_target(self): + IDT IDT self.session.run(self.update_q_target_op) + + IDT def train(self): + IDT IDT if self.buffer_length < self.buffer_size: + IDT IDT IDT return + IDT IDT if self.total_step % self.update_target_interval == 0: + IDT IDT IDT self.session.run(self.update_q_target_op) + IDT IDT s, a, r, s_next = self.get_transition_batch() + IDT IDT q_eval, q_target = self.session.run([self.q_eval, self.q_target], {self.s: s, self.s_next: s_next}) + IDT IDT b_indices = , dtype= ) IDT IDT q_next = q_eval.copy() + IDT IDT q_next[b_indices, a.astype(np.int)] = , axis= ) IDT IDT _, self.critic_loss = self.session.run([self.train_op, self.loss], {self.s: s, self.q_next: q_next}) + IDT IDT self.total_step += 1 + + IDT def save_transition(self, s, a, r, s_next): + IDT IDT a = np.argmax(a) + IDT IDT transition = np.hstack((s, [a], [r], s_next)) + IDT IDT self.buffer[(self.buffer_length % self.buffer_size), :] = transition + IDT IDT self.buffer_length += 1 + + IDT def get_transition_batch(self): + IDT IDT indices = , size= ) IDT IDT batch = self.buffer[(indices), :] + IDT IDT s = batch[:, :self.s_dim] + IDT IDT a = batch[:, self.s_dim:self.s_dim + 1] + IDT IDT r = batch[:, -self.s_dim - 1:-self.s_dim] + IDT IDT s_next = batch[:, -self.s_dim:] + IDT IDT return s, a, r, s_next + + IDT def restore_buffer(self): + IDT IDT self.buffer = np.zeros((self.buffer_size, self.s_dim + 1 + 1 + self.s_dim)) + IDT IDT self.buffer_length = 0 + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) + + +class RPG_CryptoNG(object): + + IDT def __init__(self, feature_number, action_size= , hidden_units_number= , 64], learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.s = , shape= , None, feature_number], name= ) IDT IDT self.a = , shape= , None, action_size], name= ) IDT IDT self.r = , shape= , None], name= ) IDT IDT self.s_next = , shape= , None, feature_number], name= ) IDT IDT self.action_size = action_size + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.s_next_buffer = [] + IDT IDT self.dropout_keep_prob = , shape= , name= ) IDT IDT with tf.variable_scope('rnn_encoder', initializer= , regularizer= ) IDT IDT IDT cell = , activation= , keep_prob= ) IDT IDT IDT self.rnn_output, _ = , cell= , dtype= ) IDT IDT with tf.variable_scope('supervised', initializer= , regularizer= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + IDT IDT with tf.variable_scope('policy_gradient', initializer= , regularizer= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_out = , axis= ) IDT IDT IDT self.negative_cross_entropy = , labels= ) IDT IDT with tf.variable_scope('train'): + IDT IDT IDT optimizer_rl = ) IDT IDT IDT optimizer_sl = ) IDT IDT IDT self.rlloss = tf.reduce_mean(self.negative_cross_entropy * self.r) + IDT IDT IDT self.slloss = tf.reduce_mean(self.state_loss) + IDT IDT IDT self.rltrain_op = optimizer_rl.minimize(self.rlloss) + IDT IDT IDT self.sltrain_op = optimizer_sl.minimize(self.slloss) + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act= , use_bias= ) IDT IDT output = inputs + IDT IDT for n in output_shape: + IDT IDT IDT output = , n, activation= , use_bias= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + IDT def _add_GRU(self, units_number, activation= , keep_prob= ) IDT IDT cell = , activation= ) IDT IDT cell = , input_keep_prob= ) IDT IDT return cell + + IDT def _add_GRUs(self, units_number, activation, keep_prob= ) IDT IDT cells = , activation= , a in zip(units_number, activation)]) ) IDT IDT return cells + + IDT def _add_gru_cell(self, units_number, activation= ) IDT IDT return tf.contrib.rnn.GRUCell(num_units= , activation= ) + IDT def train(self, drop= ) IDT IDT feed = , axes= , 0, 2]), self.r: np.transpose(np.array(self.r_buffer), axes= , 0]), self.s: np.transpose(np.array(self.s_buffer), axes= , 0, 2]), self.s_next: np.transpose(np.array(self.s_next_buffer), axes= , 0, 2]) ) IDT IDT self.session.run([self.rltrain_op, self.sltrain_op], feed_dict= ) + IDT def restore_buffer(self): + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.s_next_buffer = [] + + IDT def save_current_state(self, s): + IDT IDT self.s_buffer.append(s) + + IDT def save_transation(self, a, r, s_next): + IDT IDT self.a_buffer.append(a) + IDT IDT self.r_buffer.append(r) + IDT IDT self.s_next_buffer.append(s_next) + + IDT def trade(self, train= , drop= , prob= ) IDT IDT feed = , axes= , 0, 2]) ) IDT IDT a_prob = , feed_dict= ) IDT IDT a_prob = a_prob[:, (-1), :] + IDT IDT actions = [] + IDT IDT if train: + IDT IDT IDT for ap in a_prob: + IDT IDT IDT IDT if prob: + IDT IDT IDT IDT IDT np.clip(np.random.normal(0.5, 0.25), 0, 1) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT a_indices = np.arange(ap.shape[0]) + IDT IDT IDT IDT IDT target_index = , p= ) IDT IDT IDT IDT IDT a = np.zeros(ap.shape[0]) + IDT IDT IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT IDT IDT actions.append(a) + IDT IDT IDT return np.array(actions) + IDT IDT else: + IDT IDT IDT if prob: + IDT IDT IDT IDT return a_prob + IDT IDT IDT actions = [] + IDT IDT IDT for ap in a_prob: + IDT IDT IDT IDT target_index = np.argmax(ap) + IDT IDT IDT IDT a = np.zeros(ap.shape[0]) + IDT IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT IDT actions.append(a) + IDT IDT IDT return np.array(actions) + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) + + +class RPG_Portfolio_Stable(object): + + IDT def __init__(self, feature_number, action_size= , hidden_units_number= , 64], learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.s = , shape= , None, feature_number], name= ) IDT IDT self.a = , shape= , None, action_size], name= ) IDT IDT self.r = , shape= , None], name= ) IDT IDT self.s_next = , shape= , None, feature_number], name= ) IDT IDT self.action_size = action_size + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.s_next_buffer = [] + IDT IDT self.dropout_keep_prob = , shape= , name= ) IDT IDT with tf.variable_scope('rnn_encoder', initializer= , regularizer= ) IDT IDT IDT cell = , activation= , keep_prob= ) IDT IDT IDT self.rnn_output, _ = , cell= , dtype= ) IDT IDT with tf.variable_scope('supervised', initializer= , regularizer= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + IDT IDT with tf.variable_scope('policy_gradient', initializer= , regularizer= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_out = , axis= ) IDT IDT IDT self.negative_cross_entropy = , labels= ) IDT IDT with tf.variable_scope('train'): + IDT IDT IDT optimizer_rl = ) IDT IDT IDT optimizer_sl = ) IDT IDT IDT self.rl_loss = tf.reduce_mean(self.negative_cross_entropy * self.r) + IDT IDT IDT self.sl_loss = tf.reduce_mean(self.state_loss) + IDT IDT IDT self.rl_train_op = optimizer_rl.minimize(self.rl_loss) + IDT IDT IDT self.sl_train_op = optimizer_sl.minimize(self.sl_loss) + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act= , use_bias= ) IDT IDT output = inputs + IDT IDT for n in output_shape: + IDT IDT IDT output = , n, activation= , use_bias= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + IDT def _add_GRU(self, units_number, activation= , keep_prob= ) IDT IDT cell = , activation= ) IDT IDT cell = , input_keep_prob= ) IDT IDT return cell + + IDT def _add_GRUs(self, units_number, activation, keep_prob= ) IDT IDT cells = , activation= , a in zip(units_number, activation)]) ) IDT IDT return cells + + IDT def _add_gru_cell(self, units_number, activation= ) IDT IDT return tf.contrib.rnn.GRUCell(num_units= , activation= ) + IDT def train(self, drop= ) IDT IDT feed = , axes= , 0, 2]), self.r: np.transpose(np.array(self.r_buffer), axes= , 0]), self.s: np.transpose(np.array(self.s_buffer), axes= , 0, 2]), self.s_next: np.transpose(np.array(self.s_next_buffer), axes= , 0, 2]) ) IDT IDT self.session.run([self.rl_train_op, self.sl_train_op], feed_dict= ) + IDT def restore_buffer(self): + IDT IDT self.a_buffer = [] + IDT IDT self.r_buffer = [] + IDT IDT self.s_buffer = [] + IDT IDT self.s_next_buffer = [] + + IDT def save_current_state(self, s): + IDT IDT self.s_buffer.append(s) + + IDT def save_transation(self, a, r, s_next): + IDT IDT self.a_buffer.append(a) + IDT IDT self.r_buffer.append(r) + IDT IDT self.s_next_buffer.append(s_next) + + IDT def trade(self, train= , kp= , prob= ) IDT IDT feed = , axes= , 0, 2]) ) IDT IDT a_prob = , feed_dict= , (-1) ) IDT IDT actions = [] + IDT IDT if train: + IDT IDT IDT for ap in a_prob: + IDT IDT IDT IDT if prob: + IDT IDT IDT IDT IDT np.clip(np.random.normal(0.5, 0.25), 0, 1) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT a_indices = np.arange(ap.shape[0]) + IDT IDT IDT IDT IDT target_index = , p= ) IDT IDT IDT IDT IDT a = np.zeros(ap.shape[0]) + IDT IDT IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT IDT IDT actions.append(a) + IDT IDT IDT return np.array(actions) + IDT IDT else: + IDT IDT IDT if prob: + IDT IDT IDT IDT return a_prob + IDT IDT IDT actions = [] + IDT IDT IDT for ap in a_prob: + IDT IDT IDT IDT target_index = np.argmax(ap) + IDT IDT IDT IDT a = np.zeros(ap.shape[0]) + IDT IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT IDT actions.append(a) + IDT IDT IDT return np.array(actions) + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) + + +class DDRPG(object): + + IDT def __init__(self, s_dim, asset_number, buffer_size= , batch_size= , tau= , softmax_tau= , gamma= , actor_rnn_units= , critic_rnn_units= , actor_dnn_units= , critic_dnn_units= , learning_rate_a= , learning_rate_c= ) IDT IDT tf.reset_default_graph() + IDT IDT self.buffer_size = buffer_size + IDT IDT self.batch_size = batch_size + IDT IDT self.pointer = 0 + IDT IDT self.asset_number = asset_number + IDT IDT self.a_dim, self.s_dim = 1, s_dim + IDT IDT self.tau = tau + IDT IDT self.softmax_tau = softmax_tau + IDT IDT self.gamma = gamma + IDT IDT self.lr_a = learning_rate_a + IDT IDT self.lr_c = learning_rate_c + IDT IDT self.actor_rnn_units = actor_rnn_units + IDT IDT self.critic_rnn_units = critic_rnn_units + IDT IDT self.actor_dnn_units = actor_dnn_units + IDT IDT self.critic_dnn_units = critic_dnn_units + IDT IDT self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.r_buffer = np.zeros((self.asset_number, self.buffer_size, 1)) + IDT IDT self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + IDT IDT self.s = tf.placeholder(tf.float32, [None, None, self.s_dim], 's') + IDT IDT self.s_next = tf.placeholder(tf.float32, [None, None, self.s_dim], 's_next') + IDT IDT self.r = tf.placeholder(tf.float32, [None, None, 1], 'r') + IDT IDT self.keep_prob = tf.placeholder(tf.float32, [], 'dropout') + IDT IDT with tf.variable_scope('actor', initializer= , mean= , stddev= ) IDT IDT IDT self.a = , rnn_units= , dnn_units= , scope= , keep_prob= , trainable= ) IDT IDT IDT a_next = , rnn_units= , dnn_units= , scope= , keep_prob= , trainable= ) IDT IDT with tf.variable_scope('critic', initializer= , mean= , stddev= ) IDT IDT IDT q = , a= , rnn_units= , dnn_units= , scope= , keep_prob= , trainable= ) IDT IDT IDT q_next = , a= , rnn_units= , dnn_units= , scope= , keep_prob= , trainable= ) IDT IDT self.ap_params = , scope= ) IDT IDT self.at_params = , scope= ) IDT IDT self.cp_params = , scope= ) IDT IDT self.ct_params = , scope= ) IDT IDT self.soft_replace = [[tf.assign(ta, (1 - self.tau) * ta + self.tau * pa), tf.assign(tc, (1 - self.tau) * tc + self.tau * pc)] for ta, pa, tc, pc in zip(self.at_params, self.ap_params, self.ct_params, self.cp_params)] + IDT IDT q_target = self.r + self.gamma * q_next + IDT IDT with tf.variable_scope('actor_loss'): + IDT IDT IDT a_loss = -tf.reduce_mean(q) + IDT IDT with tf.variable_scope('critic_loss'): + IDT IDT IDT c_loss = , predictions= ) IDT IDT with tf.variable_scope('actor_train'): + IDT IDT IDT self.a_train = , var_list= ) IDT IDT with tf.variable_scope('critic_loss'): + IDT IDT IDT self.c_train = , var_list= ) IDT IDT self.sess = tf.Session() + IDT IDT self.sess.run(tf.global_variables_initializer()) + + IDT def trade(self, train= , kp= , epsilon= ) IDT IDT start_point = self.pointer + 1 - self.batch_size + IDT IDT action = self.sess.run(self.a, {self.s: self.s_buffer[:, 0 if start_point < 0 else start_point:self.pointer + 1, :], self.keep_prob: kp})[:, (-1), :].flatten() + IDT IDT if train: + IDT IDT IDT if np.random.rand() < epsilon: + IDT IDT IDT IDT return np.exp(action / self.softmax_tau) / np.sum(np.exp(action / self.softmax_tau)) + IDT IDT IDT action = , scale= ) IDT IDT IDT action = np.exp(action / self.softmax_tau) / np.sum(np.exp(action / self.softmax_tau)) + IDT IDT IDT return action + IDT IDT else: + IDT IDT IDT return np.exp(action / self.softmax_tau) / np.sum(np.exp(action / self.softmax_tau)) + + IDT def train(self, kp= ) IDT IDT if self.pointer < self.batch_size: + IDT IDT IDT return + IDT IDT sample_start = np.random.randint(0, self.pointer - self.batch_size + 1) + IDT IDT self.sess.run(self.soft_replace) + IDT IDT self.sess.run(self.a_train, {self.s: self.s_buffer[:, sample_start:sample_start + self.batch_size], self.keep_prob: kp}) + IDT IDT self.sess.run(self.c_train, {self.s: self.s_buffer[:, sample_start:sample_start + self.batch_size], self.a: self.a_buffer[:, sample_start:sample_start + self.batch_size], self.r: self.r_buffer[:, sample_start:sample_start + self.batch_size], self.s_next: self.s_next_buffer[:, sample_start:sample_start + self.batch_size], self.keep_prob: kp}) + + IDT def save_current_state(self, s): + IDT IDT self.s_buffer[:, (self.pointer), :] = s + + IDT def save_transition(self, a, r, s_next): + IDT IDT self.a_buffer[:, (self.pointer), :] = , (None) ) IDT IDT self.s_next_buffer[:, (self.pointer), :] = s_next + IDT IDT self.r_buffer[:, (self.pointer), :] = , (None) ) + IDT def settle(self): + IDT IDT self.pointer += 1 + + IDT def restore_buffer(self): + IDT IDT self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.r_buffer = np.zeros((self.asset_number, self.buffer_size, 1)) + IDT IDT self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + IDT IDT self.pointer = 0 + + IDT def _build_a(self, s, rnn_units, dnn_units, scope, keep_prob, trainable): + IDT IDT with tf.variable_scope(scope): + IDT IDT IDT cell = , activation= , keep_prob= , trainable= ) IDT IDT IDT out, _ = , cell= , dtype= ) IDT IDT IDT out = , output_shape= , activations= , drop_keep_prob= , trainable= ) IDT IDT IDT return out + + IDT def _build_c(self, s, a, rnn_units, dnn_units, scope, keep_prob, trainable): + IDT IDT with tf.variable_scope(scope): + IDT IDT IDT rnn_input = , a], axis= ) IDT IDT IDT cell = , activation= , keep_prob= , trainable= ) IDT IDT IDT out, _ = , cell= , dtype= ) IDT IDT IDT out = , output_shape= , activations= , drop_keep_prob= , trainable= ) IDT IDT IDT q = , output_shape= , activations= , drop_keep_prob= , trainable= ) IDT IDT IDT return q + + IDT def _add_GRU(self, units_number, activation= , keep_prob= , trainable= ) IDT IDT cell = , activation= ) IDT IDT cell = , input_keep_prob= ) IDT IDT return cell + + IDT def _add_dense_layer(self, inputs, output_shape, activations, drop_keep_prob, use_bias= , trainable= ) IDT IDT output = inputs + IDT IDT for n, a in zip(output_shape, activations): + IDT IDT IDT output = , n, activation= , use_bias= , trainable= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + +class RPG_CryptoNG_ShareVNG(object): + + IDT def __init__(self, s_dim, asset_number, a_dim, buffer_size= , batch_size= , hidden_units_number= , 64], learning_rate= ) IDT IDT tf.reset_default_graph() + IDT IDT self.a_dim = a_dim + IDT IDT self.buffer_size = buffer_size + IDT IDT self.batch_size = batch_size + IDT IDT self.asset_number = asset_number + IDT IDT self.s_dim = s_dim + IDT IDT self.pointer = 0 + IDT IDT self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + IDT IDT self.r_buffer = np.zeros((self.asset_number, self.buffer_size)) + IDT IDT self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.s = , shape= , None, self.s_dim], name= ) IDT IDT self.a = , shape= , None, self.a_dim], name= ) IDT IDT self.r = , shape= , None], name= ) IDT IDT self.s_next = , shape= , None, s_dim], name= ) IDT IDT self.dropout_keep_prob = , shape= , name= ) IDT IDT with tf.variable_scope('rnn_encoder', initializer= , regularizer= ) IDT IDT IDT cell = , activation= , keep_prob= ) IDT IDT IDT self.rnn_output, _ = , cell= , dtype= ) IDT IDT with tf.variable_scope('supervised', initializer= , regularizer= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_predict = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + IDT IDT with tf.variable_scope('policy_gradient', initializer= , regularizer= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_prob = , output_shape= , drop_keep_prob= , act= , use_bias= ) IDT IDT IDT self.a_out = , axis= ) IDT IDT IDT self.negative_cross_entropy = , labels= ) IDT IDT with tf.variable_scope('train'): + IDT IDT IDT optimizer_rl = ) IDT IDT IDT optimizer_sl = ) IDT IDT IDT self.rl_loss = tf.reduce_mean(self.negative_cross_entropy * self.r) + IDT IDT IDT self.sl_loss = tf.reduce_mean(self.state_loss) + IDT IDT IDT self.rl_train_op = optimizer_rl.minimize(self.rl_loss) + IDT IDT IDT self.sl_train_op = optimizer_sl.minimize(self.sl_loss) + IDT IDT self.init_op = tf.global_variables_initializer() + IDT IDT self.session = tf.Session() + IDT IDT self.saver = tf.train.Saver() + + IDT def init_model(self): + IDT IDT self.session.run(self.init_op) + + IDT def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act= , use_bias= ) IDT IDT output = inputs + IDT IDT for n in output_shape: + IDT IDT IDT output = , n, activation= , use_bias= ) IDT IDT IDT output = tf.nn.dropout(output, drop_keep_prob) + IDT IDT return output + + IDT def _add_GRU(self, units_number, activation= , keep_prob= ) IDT IDT cell = , activation= ) IDT IDT cell = , input_keep_prob= ) IDT IDT return cell + + IDT def _add_GRUs(self, units_number, activation, keep_prob= ) IDT IDT cells = , activation= , a in zip(units_number, activation)]) ) IDT IDT return cells + + IDT def _add_gru_cell(self, units_number, activation= ) IDT IDT return tf.contrib.rnn.GRUCell(num_units= , activation= ) + IDT def train(self, kp= ) IDT IDT if self.pointer < self.buffer_size - 1: + IDT IDT IDT return + IDT IDT max_upper_bound = self.buffer_size - self.batch_size + 1 + IDT IDT lower = , high= ) IDT IDT upper = lower + +self.batch_size + IDT IDT feed = {self.a: self.a_buffer[:, lower:upper, :], self.r: self.r_buffer[:, lower:upper], self.s: self.s_buffer[:, lower:upper, :], self.s_next: self.s_next_buffer[:, lower:upper, :], self.dropout_keep_prob: kp} + IDT IDT self.session.run([self.rl_train_op, self.sl_train_op], feed_dict= ) + IDT def restore_buffer(self): + IDT IDT self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + IDT IDT self.r_buffer = np.zeros((self.asset_number, self.buffer_size)) + IDT IDT self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + IDT IDT self.pointer = 0 + + IDT def save_current_state(self, s): + IDT IDT if self.pointer < self.buffer_size - 1: + IDT IDT IDT self.s_buffer[:, (self.pointer), :] = s + IDT IDT else: + IDT IDT IDT self.s_buffer[:, :-1, :] = self.s_buffer[:, 1:, :] + IDT IDT IDT self.s_buffer[:, (-1), :] = s + + IDT def save_transition(self, a, r, s_next): + IDT IDT if self.pointer < self.buffer_size - 1: + IDT IDT IDT self.a_buffer[:, (self.pointer), :] = a + IDT IDT IDT self.s_next_buffer[:, (self.pointer), :] = s_next + IDT IDT IDT self.r_buffer[:, (self.pointer)] = r + IDT IDT IDT self.pointer += 1 + IDT IDT else: + IDT IDT IDT self.a_buffer[:, :-1, :] = self.a_buffer[:, 1:, :] + IDT IDT IDT self.a_buffer[:, (-1), :] = a + IDT IDT IDT self.r_buffer[:, :-1] = self.r_buffer[:, 1:] + IDT IDT IDT self.r_buffer[:, (-1)] = r + IDT IDT IDT self.s_next_buffer[:, :-1, :] = self.s_next_buffer[:, 1:, :] + IDT IDT IDT self.s_next_buffer[:, (-1), :] = s_next + + IDT def trade(self, train= , kp= , prob= ) IDT IDT feed = {self.s: self.s_buffer, self.dropout_keep_prob: kp} + IDT IDT a_prob = , feed_dict= , (self.pointer) ) IDT IDT action = [] + IDT IDT if train: + IDT IDT IDT for ap in a_prob: + IDT IDT IDT IDT if prob: + IDT IDT IDT IDT IDT action.append(np.clip(np.random.normal(ap, 0.5), 0, 1)) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT a_indices = np.arange(ap.shape[0]) + IDT IDT IDT IDT IDT target_index = , p= ) IDT IDT IDT IDT IDT a = np.zeros(ap.shape[0]) + IDT IDT IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT IDT IDT action.append(a) + IDT IDT IDT return np.array(action) + IDT IDT else: + IDT IDT IDT if prob: + IDT IDT IDT IDT return a_prob + IDT IDT IDT action = [] + IDT IDT IDT for ap in a_prob: + IDT IDT IDT IDT target_index = np.argmax(ap) + IDT IDT IDT IDT a = np.zeros(ap.shape[0]) + IDT IDT IDT IDT a[target_index] = 1.0 + IDT IDT IDT IDT action.append(a) + IDT IDT IDT return np.array(action) + + IDT def load_model(self, model_path= ) IDT IDT self.saver.restore(self.session, model_path + '/model') + + IDT def save_model(self, model_path= ) IDT IDT if not os.path.exists(model_path): + IDT IDT IDT os.mkdir(model_path) + IDT IDT model_file = model_path + '/model' + IDT IDT self.saver.save(self.session, model_file) +from tensorflow.examples.tutorials.mnist import input_data +import tensorflow as tf +import os +mnist = , one_hot= )log_dir = '/tmp/tensorflow/mnist/logs/sample' + + +def weight_variable(shape): + IDT with tf.name_scope('hidden1'): + IDT IDT initial = , stddev= , name= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +def run_deep_nn_with_interactive_session(): + IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT x_image = tf.reshape(x, [-1, 28, 28, 1]) + IDT y_ = tf.placeholder(tf.float32, [None, 10]) + IDT W_conv1 = weight_variable([5, 5, 1, 32]) + IDT b_conv1 = bias_variable([32]) + IDT h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + IDT + IDT + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT + IDT + IDT W_conv2 = weight_variable([5, 5, 32, 64]) + IDT b_conv2 = bias_variable([64]) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + IDT + IDT + IDT h_pool2 = max_pool_2x2(h_conv2) + IDT + IDT + IDT W_fc1 = weight_variable([7 * 7 * 64, 1024]) + IDT b_fc1 = bias_variable([1024]) + IDT h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + IDT h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + IDT + IDT + IDT keep_prob = tf.placeholder(tf.float32) + IDT h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + IDT W_fc2 = weight_variable([1024, 10]) + IDT b_fc2 = bias_variable([10]) + IDT y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 + IDT + IDT + IDT cross_entropy = , logits= ) IDT train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + IDT correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT session = tf.InteractiveSession() + IDT session.run(tf.global_variables_initializer()) + IDT for i in range(200): + IDT IDT batch = mnist.train.next_batch(50) + IDT IDT if i % 100 == 0: + IDT IDT IDT train_accuracy = , y_: batch[1], keep_prob: 1.0}) ) IDT IDT IDT ) + IDT IDT train_step.run(feed_dict= , y_: batch[1], keep_prob: 0.5}) ) IDT ) + + +def run_deep_nn_with_session(): + IDT x = tf.placeholder(tf.float32, [None, 784]) + IDT x_image = tf.reshape(x, [-1, 28, 28, 1]) + IDT y_ = tf.placeholder(tf.float32, [None, 10]) + IDT W_conv1 = weight_variable([5, 5, 1, 32]) + IDT b_conv1 = bias_variable([32]) + IDT h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT W_conv2 = weight_variable([5, 5, 32, 64]) + IDT b_conv2 = bias_variable([64]) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + IDT h_pool2 = max_pool_2x2(h_conv2) + IDT W_fc1 = weight_variable([7 * 7 * 64, 1024]) + IDT b_fc1 = bias_variable([1024]) + IDT h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + IDT h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + IDT keep_prob = tf.placeholder(tf.float32) + IDT h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + IDT W_fc2 = weight_variable([1024, 10]) + IDT b_fc2 = bias_variable([10]) + IDT y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 + IDT cross_entropy = , logits= ) IDT train_step_operation = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + IDT correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) + IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT with tf.Session() as session: + IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT for i in range(200): + IDT IDT IDT batch = mnist.train.next_batch(50) + IDT IDT IDT feed_dict = {x: batch[0], y_: batch[1], keep_prob: 0.5} + IDT IDT IDT _, loss_value = , cross_entropy], feed_dict= ) IDT IDT IDT + IDT IDT IDT if i % 100 == 0: + IDT IDT IDT IDT feed_dict = {x: batch[0], y_: batch[1], keep_prob: 1.0} + IDT IDT IDT IDT train_accuracy = ) IDT IDT IDT IDT ) + IDT IDT ) + + +if __name__ == '__main__': + IDT run_deep_nn_with_session() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = , labels= ) IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y1 = tf.nn.dropout(y1, 0.5) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = , labels= ) IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT global_step = tf.Variable(0) + IDT W1 = , fc1_size], stddev= ) IDT b1 = tf.Variable(tf.zeros([fc1_size])) + IDT W2 = , fc2_size], stddev= ) IDT b2 = tf.Variable(tf.zeros([fc2_size])) + IDT W3 = , fc3_size], stddev= ) IDT b3 = tf.Variable(tf.zeros([fc3_size])) + IDT W4 = , num_labels], stddev= ) IDT b4 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + IDT y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + IDT logits = tf.matmul(y3, W4) + b4 + IDT loss = , labels= ) IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + IDT learning_rate = , global_step, 1000, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + IDT y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + IDT valid_logits = tf.matmul(y3_valid, W4) + b4 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + IDT y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + IDT test_logits = tf.matmul(y3_test, W4) + b4 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import os +import re +path_file = 'C:\\Users\\stecose\\Documents\\Documents\\AiHearingModel' +save_path = 'C:\\Users\\stecose\\Documents\\Documents\\AiHearingModel/models/md0' +names = set() +for line in open(os.path.join(path_file, 'words.txt')): + IDT if len(line.strip()) > 3 and len(line.strip()) < 15: + IDT IDT names.add(line.strip().lower()) +NAMES = names +chars = list('abcdefghijklmnopqrstuvwxyz') + ['', ''] +indices_for_chars = {c: i for i, c in enumerate(chars)} +NAME_MAX_LEN = 15 + + +def name_to_vec(name, maxlen= ) IDT v = , dtype= ) IDT null_idx = indices_for_chars[''] + IDT v.fill(null_idx) + IDT for i, c in enumerate(name): + IDT IDT if i >= maxlen: + IDT IDT IDT break + IDT IDT n = indices_for_chars.get(c, null_idx) + IDT IDT v[i] = n + IDT v[min(len(name), maxlen - 1)] = indices_for_chars[''] + IDT return v + + +def vec_to_name(vec): + IDT name = '' + IDT for x in vec: + IDT IDT char = chars[x] + IDT IDT if len(char) == 1: + IDT IDT IDT name += char + IDT IDT elif char == '': + IDT IDT IDT return name + IDT return name + + + --> ', name_to_vec('nate')) +name_vecs = np.array([name_to_vec(n) for n in names]) + + +def weight_var(shape, stddev= , weight_decay= , name= ) IDT initial = , stddev= ) IDT v = , name= ) IDT if weight_decay > 0: + IDT IDT l2 = tf.nn.l2_loss(v) * weight_decay + IDT IDT tf.add_to_collection('losses', l2) + IDT return v + + +def leaky_relu(x, leak= , name= ) IDT with tf.variable_scope(name): + IDT IDT f1 = 0.5 * (1 + leak) + IDT IDT f2 = 0.5 * (1 - leak) + IDT IDT return f1 * x + f2 * abs(x) + + +def relu(x): + IDT return leaky_relu(x) + + +def create_conv(input, out_channels, patch_size= , stride= , batch_norm= , dropout= ) IDT in_channels = input.get_shape()[-1].value + IDT w = weight_var([patch_size, patch_size, in_channels, out_channels]) + IDT b = , stddev= ) IDT conv = , w, strides= , stride, stride, 1], padding= ) IDT activation = relu(conv + b) + IDT return activation + + +def text_conv(input, out_channels, patch_size= , stride= , dropout= , pool_size= ) IDT in_channels = input.get_shape()[-1].value + IDT w = weight_var([patch_size, in_channels, out_channels]) + IDT b = , stddev= ) IDT conv = , w, stride= , padding= ) IDT activation = relu(conv + b) + IDT return activation + + +def create_fc(input, out_size): + IDT in_size = input.get_shape()[-1].value + IDT w = , out_size], weight_decay= ) IDT b = , weight_decay= ) IDT x = tf.matmul(input, w) + IDT return relu(x + b) + + +name_placeholder = , NAME_MAX_LEN], dtype= , name= )Z_SIZE = 64 + + +def encoder_lstm(names): + IDT with tf.variable_scope('encoder'): + IDT IDT cells = , state_is_tuple= ) IDT IDT lstm = , state_is_tuple= ) IDT IDT one_hot = , len(chars), dtype= ) IDT IDT outputs, state = , one_hot, dtype= ) IDT IDT outputs_flat = tf.reshape(outputs, [-1, Z_SIZE * NAME_MAX_LEN]) + IDT IDT z_mean = create_fc(outputs_flat, Z_SIZE) + IDT IDT z_stddev = create_fc(outputs_flat, Z_SIZE) + IDT IDT return z_mean, z_stddev + + +z_mean, z_stddev = encoder_lstm(name_placeholder) +session = tf.Session() +session.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) + + +def sample_z(z_mean, z_stddev): + IDT samples = , 0, 1, dtype= ) IDT return z_mean + samples * z_stddev + + +z_vals = sample_z(z_mean, z_stddev) + + +def decoder_lstm(z): + IDT z_repeated_over_time = tf.tile(tf.reshape(z, [-1, 1, Z_SIZE]), [1, NAME_MAX_LEN, 1]) + IDT cells = , state_is_tuple= , 2 * 2 * Z_SIZE, len(chars) ) IDT lstm = , state_is_tuple= ) IDT outputs, state = , z_repeated_over_time, dtype= ) IDT return outputs + + +z_input = , [None, Z_SIZE], name= )use_z_input = , shape= , name= )decoder_input = tf.cond(use_z_input > 0, lambda : z_input, lambda : z_vals) +decoded = decoder_lstm(decoder_input) +diff_loss = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(decoded, name_placeholder)) +kl_divergence = tf.reduce_mean(0.5 * tf.reduce_sum(tf.square(z_mean) + tf.square(z_stddev) - tf.log(tf.square(z_stddev)) - 1, 1)) +loss = diff_loss + kl_divergence +decoded_vecs = , axis= )learn_rate = , name= )optimizer = tf.train.AdamOptimizer(learn_rate) +global_step = tf.contrib.framework.get_or_create_global_step() +train_step = , global_step= )session.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) +session = tf.Session() +init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) +session.run(init_op) +saver = None +if save_path: + IDT if not os.path.exists(save_path): + IDT IDT os.mkdir(save_path) + IDT saver = tf.train.Saver() + IDT ckpt = tf.train.get_checkpoint_state(save_path) + IDT if ckpt and ckpt.model_checkpoint_path: + IDT IDT saver.restore(session, ckpt.model_checkpoint_path) + IDT IDT + IDT else: + IDT IDT +else: + IDT +train = True +while train: + IDT nnames = , size= ) IDT feed_dict = {name_placeholder: nnames, z_input: np.zeros((64, Z_SIZE)), use_z_input: 0, learn_rate: 0.001} + IDT _, loss_, step_ = , loss, global_step], feed_dict= ) IDT if step_ % 200 == 0: + IDT IDT output_ = , feed_dict= ) IDT IDT ) + IDT IDT , vec_to_name(output_[0]))) + IDT IDT if step_ % 600 == 0: + IDT IDT IDT saver.save(session, save_path + '/model.ckpt', global_step= ) IDT IDT IDT + IDT if step_ > 1000000: + IDT IDT train = False + + +def reconstruct(name): + IDT feed_dict = {name_placeholder: np.array([name_to_vec(name)]), z_input: np.zeros((64, Z_SIZE)), use_z_input: 0, learn_rate: 0.01} + IDT output_ = , feed_dict= ) IDT return vec_to_name(output_[0]) + + +import itertools +top5 = itertools.islice(names, 5) +for name in list(names)[:10]: + IDT ) +for name in ['word', 'happy', 'winter', 'candle', 'cherish']: + IDT ) +for name in ['embedding', 'automobile', 'air', 'larynx']: + IDT ) +for name in ['ufhoe', 'xyzy', 'ihwrfoecoei']: + IDT ) + + +def nameliness(word): + IDT r = reconstruct(word) + IDT return sum([(1 if a = , b in zip(word, r)]) / float(len(word)) ) + +for name in ['nate', 'july', 'fridge', 'gienigoe', 'chzsiucf', 'xyxyzzy']: + IDT ) + + +def make_batches(list, size= ) IDT batches = [] + IDT while len(list): + IDT IDT batches.append(list[:min(len(list), size)]) + IDT IDT list = list[len(batches[-1]):] + IDT return batches + + +embeddings = {} +for batch in make_batches(list(names)): + IDT feed_dict = {name_placeholder: np.array([name_to_vec(name) for name in batch]), z_input: np.zeros((len(batch), Z_SIZE)), use_z_input: 0} + IDT output_ = , feed_dict= ) IDT for name, vec in list(zip(batch, output_)): + IDT IDT embeddings[tuple(name)] = vec + + +def embed(name): + IDT feed_dict = {name_placeholder: np.array([name_to_vec(name)]), z_input: np.zeros((1, Z_SIZE)), use_z_input: 0} + IDT output_ = , feed_dict= ) IDT return output_[0] + + +def nearest(embedding): + + IDT def distance(name): + IDT IDT return np.linalg.norm(embedding - embeddings[name]) + IDT return ''.join(min(embeddings.keys(), key= ) + +def unembed(embedding): + IDT feed_dict = {name_placeholder: np.zeros((1, NAME_MAX_LEN)), z_input: np.array([embedding]), use_z_input: 1} + IDT output_ = , feed_dict= ) IDT return vec_to_name(output_[0]) + + +) == 'nate') +for name in ['nate', 'yikes', 'panda', 'ixzhxzi', 'justxn']: + IDT )) + + +def blend_names(name1, name2): + IDT e1 = embed(name1) + IDT e2 = embed(name2) + IDT for i in range(11): + IDT IDT blend = i / 10.0 + IDT IDT + e2 * blend)) + + +blend_names('amy', 'francisco') +blend_names('nathaniel', 'chen') +blend_names('will', 'william') +)) +)) +for name in ['nate', 'willy', 'sam', 'polly', 'jacob']: + IDT * 2)) +for name in ['nancy', 'barry', 'chance', 'rachel', 'gloria']: + IDT )) + - embed('albert') + embed('robert'))) + - embed('albert') + embed('justin'))) + - embed('albert') + embed('joseph'))) + - embed('albert') + embed('nate'))) + + +def generate(): + IDT return unembed(np.random.normal(size= ) + +for _ in range(10): + IDT ) + + +def variations_on(name): + IDT z = embed(name) + IDT for i in range(10): + IDT IDT noise = np.random.normal(z.shape) + IDT IDT ) + + +variations_on('nate') +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +""" +Reformat into a shape that's more adapted to the models we're going to train: + + IDT data as a flat matrix, + IDT labels as float 1-hot encodings. + +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +""" +Problem 1 + +Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor t using nn.l2_loss(t). The right amount of regularization should improve your validation / test accuracy. +""" +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y1 = tf.nn.dropout(y1, 0.5) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT global_step = tf.Variable(0) + IDT W1 = , fc1_size], stddev= ) IDT b1 = tf.Variable(tf.zeros([fc1_size])) + IDT W2 = , fc2_size], stddev= ) IDT b2 = tf.Variable(tf.zeros([fc2_size])) + IDT W3 = , fc3_size], stddev= ) IDT b3 = tf.Variable(tf.zeros([fc3_size])) + IDT W4 = , num_labels], stddev= ) IDT b4 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + IDT y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + IDT logits = tf.matmul(y3, W4) + b4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + IDT learning_rate = , global_step, 1000, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + IDT y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + IDT valid_logits = tf.matmul(y3_valid, W4) + b4 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + IDT y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + IDT test_logits = tf.matmul(y3_test, W4) + b4 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) +, test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import dihedral as nn + + +class CNN: + + IDT def __init__(self): + IDT IDT self.tfx = None + IDT IDT self.tfp = None + IDT IDT self.tfy = None + IDT IDT self.tftrain_step = None + IDT IDT self.mse = None + IDT IDT self.tfkp = None + IDT IDT self.tfacc = None + IDT IDT self.train_counter = 0 + IDT IDT self.test = None + + IDT def NN(self, x): + IDT IDT assert x.get_shape().as_list() == [None, 424, 424, 3] + IDT IDT x = , 8 * 4, w= , s= , input_repr= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 209, 209, 8 * 4] + IDT IDT x = , 8 * 8, w= , s= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 101, 101, 8 * 8] + IDT IDT x = , 8 * 16, w= , s= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 47, 47, 8 * 16] + IDT IDT x = , 8 * 32, w= , s= ) IDT IDT x = nn.relu(nn.convolution(x)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 20, 20, 8 * 32] + IDT IDT x = , 8 * 64, w= , s= ) IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 9, 9, 8 * 64] + IDT IDT x = , 128, output_repr= ) IDT IDT x = , 8 * 256, w= , input_repr= ) IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT assert x.get_shape().as_list() == [None, 1, 1, 8 * 256] + IDT IDT x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + IDT IDT x = nn.relu(nn.fullyconnected(x, 8 * 256)) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.relu(nn.fullyconnected(x, 8 * 256)) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT self.test = x + IDT IDT x = , 37, output_repr= ) IDT IDT assert x.get_shape().as_list() == [None, 37] + IDT IDT c1 = tf.nn.softmax(x[:, 0:3]) + IDT IDT c2 = tf.nn.softmax(x[:, 3:5]) * c1[:, 1:2] + IDT IDT c3 = tf.nn.softmax(x[:, 5:7]) * c2[:, 1:2] + IDT IDT c4 = tf.nn.softmax(x[:, 7:9]) * c2[:, 1:2] + IDT IDT c5 = tf.nn.softmax(x[:, 9:13]) * c2[:, 1:2] + IDT IDT c6 = tf.nn.softmax(x[:, 13:15]) + IDT IDT c7 = tf.nn.softmax(x[:, 15:18]) * c1[:, 0:1] + IDT IDT c8 = tf.nn.softmax(x[:, 18:25]) * c6[:, 0:1] + IDT IDT c9 = tf.nn.softmax(x[:, 25:28]) * c2[:, 0:1] + IDT IDT c10 = tf.nn.softmax(x[:, 28:31]) * c4[:, 0:1] + IDT IDT c11 = tf.nn.softmax(x[:, 31:37]) * c4[:, 0:1] + IDT IDT return tf.concat(1, [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11]) + + IDT def create_architecture(self): + IDT IDT self.tfkp = tf.placeholder_with_default(tf.constant(1.0, tf.float32), []) + IDT IDT self.tfacc = tf.placeholder_with_default(tf.constant(0.0, tf.float32), []) + IDT IDT x = self.tfx = tf.placeholder(tf.float32, [None, 424, 424, 3]) + IDT IDT self.tfp = self.NN(x) + IDT IDT self.tfy = tf.placeholder(tf.float32, [None, 37]) + IDT IDT self.mse = tf.reduce_mean(tf.square(self.tfp - self.tfy)) + IDT IDT self.tftrain_step = , epsilon= ) + IDT @staticmethod + IDT def split_test_train(images_path, labels_csv): + IDT IDT import csv + IDT IDT import os + IDT IDT with open(labels_csv) as f: + IDT IDT IDT reader = csv.reader(f) + IDT IDT IDT rows = [row for row in reader] + IDT IDT IDT labels = np.array([[float(x) for x in r[1:]] for r in rows[1:]]).astype(np.float32) + IDT IDT files = [(images_path + '/' + f) for f in sorted(os.listdir(images_path))] + IDT IDT n = 2000 + IDT IDT return (files[:n], labels[:n]), (files[n:], labels[n:]) + + IDT @staticmethod + IDT def load(files): + IDT IDT from scipy.ndimage import imread + IDT IDT n = len(files) + IDT IDT xs = , 424, 424, 3), dtype= ) IDT IDT for i in range(n): + IDT IDT IDT xs[i] = , mode= ) IDT IDT return CNN.prepare(xs) + + IDT @staticmethod + IDT def prepare(images): + IDT IDT images = images - np.array([0.04543276, 0.04002843, 0.02984124]) + IDT IDT images = images / np.array([0.08930177, 0.0741211, 0.0656323]) + IDT IDT return images + + IDT @staticmethod + IDT def batch(files, labels): + IDT IDT ids = , 20, replace= ) IDT IDT xs = CNN.load([files[i] for i in ids]) + IDT IDT ys = labels[ids] + IDT IDT for i in range(len(xs)): + IDT IDT IDT s = np.random.uniform(0.8, 1.2) + IDT IDT IDT u = np.random.uniform(-0.1, 0.1) + IDT IDT IDT xs[i] = xs[i] * s + u + IDT IDT return xs, ys + + IDT def train(self, session, xs, ys, options= , run_metadata= ) IDT IDT acc = 0.8 ** (self.train_counter / 1000.0) + IDT IDT _, mse = , self.mse], feed_dict= , self.tfy: ys, self.tfkp: 0.5, self.tfacc: acc}, options= , run_metadata= ) IDT IDT self.train_counter += 1 + IDT IDT return mse + + IDT def predict(self, session, xs): + IDT IDT return session.run(self.tfp, feed_dict= , self.tfkp: 1.0, self.tfacc: 0.0}) ) + IDT def predict_mse(self, session, xs, ys): + IDT IDT return session.run([self.tfp, self.mse], feed_dict= , self.tfy: ys, self.tfkp: 1.0, self.tfacc: 0.0}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import sys +if sys.version[0] == '2': + IDT import cPickle as pkl +else: + IDT import pickle as pkl +import numpy as np +import tensorflow as tf +import utils +dtype = utils.DTYPE + + +class Model: + + IDT def __init__(self): + IDT IDT self.sess = None + IDT IDT self.X = None + IDT IDT self.y = None + IDT IDT self.layer_keeps = None + IDT IDT self.vars = None + IDT IDT self.keep_prob_train = None + IDT IDT self.keep_prob_test = None + + IDT def run(self, fetches, X= , y= , mode= ) IDT IDT feed_dict = {} + IDT IDT if type(self.X) is list: + IDT IDT IDT for i in range(len(X)): + IDT IDT IDT IDT feed_dict[self.X[i]] = X[i] + IDT IDT else: + IDT IDT IDT feed_dict[self.X] = X + IDT IDT if y is not None: + IDT IDT IDT feed_dict[self.y] = y + IDT IDT if self.layer_keeps is not None: + IDT IDT IDT if mode == 'train': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_train + IDT IDT IDT elif mode == 'test': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_test + IDT IDT return self.sess.run(fetches, feed_dict) + + IDT def dump(self, model_path): + IDT IDT var_map = {} + IDT IDT for name, var in self.vars.iteritems(): + IDT IDT IDT var_map[name] = self.run(var) + IDT IDT pkl.dump(var_map, open(model_path, 'wb')) + IDT IDT + + +class LR(Model): + + IDT def __init__(self, input_dim= , output_dim= , init_path= , opt_algo= , learning_rate= , l2_weight= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , logits= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FM(Model): + + IDT def __init__(self, input_dim= , output_dim= , factor_order= , init_path= , opt_algo= , learning_rate= , l2_w= , l2_v= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('v', [input_dim, factor_order], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT v = self.vars['v'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + IDT IDT IDT xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + IDT IDT IDT p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FNN(Model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT factor_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = factor_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT layer_input = layer_sizes[i] + IDT IDT IDT layer_output = layer_sizes[i + 1] + IDT IDT IDT init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + IDT IDT IDT x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + IDT IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class CCPM(Model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT embedding_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = embedding_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('f1', [embedding_order, layer_sizes[2], 1, 2], 'tnormal', dtype)) + IDT IDT init_vars.append(('f2', [embedding_order, layer_sizes[3], 2, 2], 'tnormal', dtype)) + IDT IDT init_vars.append(('w1', [2 * 3 * embedding_order, 1], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [1], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.concat([(tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) + b0[i]) for i in range(num_inputs)], 1), layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT l = tf.transpose(tf.reshape(l, [-1, num_inputs, embedding_order, 1]), [0, 2, 1, 3]) + IDT IDT IDT f1 = self.vars['f1'] + IDT IDT IDT l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), num_inputs / 2), [0, 1, 3, 2]) + IDT IDT IDT f2 = self.vars['f2'] + IDT IDT IDT l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embedding_order * 3 * 2]), layer_acts[1]), self.layer_keeps[1]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1, layer_acts[2]), self.layer_keeps[2]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN1(Model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , layer_l2= , kernel_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT factor_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = factor_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('k1', [num_inputs, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT layer_input = layer_sizes[i] + IDT IDT IDT layer_output = layer_sizes[i + 1] + IDT IDT IDT init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + IDT IDT IDT x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT k1 = self.vars['k1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT p = tf.reduce_sum(tf.reshape(tf.matmul(tf.reshape(tf.transpose(tf.reshape(l, [-1, num_inputs, factor_order]), [0, 2, 1]), [-1, num_inputs]), k1), [-1, factor_order, layer_sizes[2]]), 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1 + p, layer_acts[1]), self.layer_keeps[1]) + IDT IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + IDT IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT if kernel_l2 is not None: + IDT IDT IDT IDT self.loss += kernel_l2 * tf.nn.l2_loss(k1) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN2(Model): + + IDT def __init__(self, layer_sizes= , layer_acts= , drop_out= , layer_l2= , kernel_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(layer_sizes[0]) + IDT IDT factor_order = layer_sizes[1] + IDT IDT for i in range(num_inputs): + IDT IDT IDT layer_input = layer_sizes[0][i] + IDT IDT IDT layer_output = factor_order + IDT IDT IDT init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + IDT IDT IDT init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + IDT IDT init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('k1', [factor_order * factor_order, layer_sizes[2]], 'tnormal', dtype)) + IDT IDT init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT layer_input = layer_sizes[i] + IDT IDT IDT layer_output = layer_sizes[i + 1] + IDT IDT IDT init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + IDT IDT IDT x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT k1 = self.vars['k1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT z = tf.reduce_sum(tf.reshape(l, [-1, num_inputs, factor_order]), 1) + IDT IDT IDT p = tf.reshape(tf.matmul(tf.reshape(z, [-1, factor_order, 1]), tf.reshape(z, [-1, 1, factor_order])), [-1, factor_order * factor_order]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + tf.matmul(p, k1) + b1, layer_acts[1]), self.layer_keeps[1]) + IDT IDT IDT for i in range(2, len(layer_sizes) - 1): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.reshape(l, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + IDT IDT IDT IDT for i in range(1, len(layer_sizes) - 1): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT if kernel_l2 is not None: + IDT IDT IDT IDT self.loss += kernel_l2 * tf.nn.l2_loss(k1) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf + + +class VFAConfig(object): + IDT height_dim = 84 + IDT width_dim = 84 + IDT action_space = [0, 1, 2, 3, 4, 5] + IDT lr = 0.001 + IDT discount = 0.99 + IDT alpha_reg = 0.0001 + IDT beta_reg = 0.0001 + IDT nb_steps = 4 + + +class ValueFunctionApproximation: + IDT + + IDT def __init__(self, vfa_config): + IDT IDT self.vfa_config = vfa_config + IDT IDT self.add_placeholders() + IDT IDT self.add_variables() + IDT IDT self.Q_inputs = self.add_comp_graph(self.input_placeholder) + IDT IDT self.next_actions = tf.argmax(self.Q_inputs, 1) + IDT IDT self.max_Q_inputs = tf.reduce_max(self.Q_inputs, 1) + IDT IDT self.max_Q_inputs = tf.mul(self.max_Q_inputs, self.terminal_placeholder) + IDT IDT self.Q_inputs = tf.reduce_sum(tf.mul(self.Q_inputs, self.action_placeholder), 1) + IDT IDT self.calculate_loss = self.add_loss_op(self.Q_inputs) + IDT IDT self.train_step = self.add_training_op(self.calculate_loss) + + IDT def add_placeholders(self): + IDT IDT self.input_placeholder = , shape= , self.vfa_config.height_dim, self.vfa_config.width_dim, 3], name= ) IDT IDT self.action_placeholder = , shape= , len(self.vfa_config.action_space)], name= ) IDT IDT self.output_placeholder = , shape= , name= ) IDT IDT self.terminal_placeholder = , shape= , name= ) IDT IDT self.reward_placeholder = , shape= , name= ) + IDT def add_variables(self): + IDT IDT self.weights = {'wc1': tf.Variable(tf.random_normal([8, 8, 3, 32])), 'wc2': tf.Variable(tf.random_normal([4, 4, 32, 64])), 'wc3': tf.Variable(tf.random_normal([3, 3, 64, 64])), 'wd1': tf.Variable(tf.random_normal([11 * 11 * 64, 512])), 'out': tf.Variable(tf.random_normal([512, len(self.vfa_config.action_space)]))} + IDT IDT self.biases = {'bc1': tf.Variable(tf.random_normal([32])), 'bc2': tf.Variable(tf.random_normal([64])), 'bc3': tf.Variable(tf.random_normal([64])), 'bd1': tf.Variable(tf.random_normal([512])), 'out': tf.Variable(tf.random_normal([len(self.vfa_config.action_space)]))} + + IDT def conv2d(self, inputs, W, b, strides= ) IDT IDT x = , W, strides= , strides, strides, 1], padding= ) IDT IDT x = tf.nn.bias_add(x, b) + IDT IDT return tf.nn.relu(x) + + IDT def fc2d(self, inputs, W, b): + IDT IDT fc1 = tf.reshape(inputs, [-1, W.get_shape().as_list()[0]]) + IDT IDT fc1 = tf.add(tf.matmul(fc1, W), b) + IDT IDT return tf.nn.relu(fc1) + + IDT def add_comp_graph(self, inputs): + IDT IDT conv1 = , self.weights['wc1'], self.biases['bc1'], strides= ) IDT IDT conv2 = , self.weights['wc2'], self.biases['bc2'], strides= ) IDT IDT conv3 = self.conv2d(conv2, self.weights['wc3'], self.biases['bc3']) + IDT IDT fc1 = self.fc2d(conv3, self.weights['wd1'], self.biases['bd1']) + IDT IDT output = tf.add(tf.matmul(fc1, self.weights['out']), self.biases['out']) + IDT IDT return output + + IDT def add_loss_op(self, inputs): + IDT IDT loss = tf.nn.l2_loss(self.reward_placeholder + self.vfa_config.discount ** self.vfa_config.nb_steps * self.output_placeholder - inputs) + IDT IDT tf.add_to_collection('total_loss', loss) + IDT IDT total_loss = tf.add_n(tf.get_collection('total_loss')) + IDT IDT return total_loss + + IDT def add_training_op(self, loss): + IDT IDT optimizer = tf.train.AdamOptimizer(self.vfa_config.lr) + IDT IDT train_op = optimizer.minimize(loss) + IDT IDT return train_op + + IDT def run_batch(self, session, inputs, actions, outputs, terminal_states, rewards): + IDT IDT feed = {self.input_placeholder: outputs, self.terminal_placeholder: terminal_states} + IDT IDT Q_outputs = , feed_dict= ) IDT IDT feed = {self.input_placeholder: inputs, self.action_placeholder: actions, self.output_placeholder: Q_outputs, self.reward_placeholder: rewards} + IDT IDT session.run(self.train_step, feed_dict= ) + IDT def best_next_actions(self, session, inputs): + IDT IDT feed = {self.input_placeholder: inputs} + IDT IDT next_actions = , feed_dict= ) IDT IDT return next_actions + + IDT def value_function(self, session, inputs, actions): + IDT IDT feed = {self.input_placeholder: inputs, self.action_placeholder: actions} + IDT IDT value_function = , feed_dict= ) IDT IDT return value_function +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import logz +import time +import math + + +class Config(object): + IDT n_features = 11 + IDT n_classes = 3 + IDT dropout = 0.5 + IDT hidden_size_1 = 128 + IDT hidden_size_2 = 256 + IDT hidden_size_3 = 64 + IDT batch_size = 256 + IDT lr = 0.0005 + IDT itera = 20 + IDT train_itera = 20 + IDT envname = 'Hopper-v1' + IDT max_steps = 1000 + + +class NN(object): + + IDT def add_placeholders(self): + IDT IDT self.input_placeholder = , shape= , Config.n_features), name= ) IDT IDT self.labels_placeholder = , shape= , Config.n_classes), name= ) IDT IDT self.dropout_placeholder = , name= ) IDT IDT self.is_training = tf.placeholder(tf.bool) + + IDT def create_feed_dict(self, inputs_batch, labels_batch= , dropout= , is_training= ) IDT IDT if labels_batch is None: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT else: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT return feed_dict + + IDT def add_prediction_op(self): + IDT IDT self.global_step = tf.Variable(0) + IDT IDT with tf.name_scope('layer1'): + IDT IDT IDT hidden1 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer2'): + IDT IDT IDT hidden2 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer3'): + IDT IDT IDT hidden3 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('output'): + IDT IDT IDT pred = , num_outputs= , activation_fn= ) IDT IDT return pred + + IDT def add_loss_op(self, pred): + IDT IDT loss = , labels= ) IDT IDT tf.summary.scalar('loss', loss) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + IDT IDT with tf.control_dependencies(extra_update_ops): + IDT IDT IDT learning_rate = , self.global_step, 1000, 0.8, staircase= ) IDT IDT IDT train_op = , global_step= ) IDT IDT return train_op + + IDT def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + IDT IDT feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + IDT IDT rs, _, loss = , self.train_op, self.loss], feed_dict= ) IDT IDT train_writer.add_summary(rs, i) + IDT IDT return loss + + IDT def __init__(self, config): + IDT IDT self.config = config + IDT IDT self.build() + + IDT def fit(self, sess, train_x, train_y): + IDT IDT loss = self.train_on_batch(sess, train_x, train_y) + + IDT def build(self): + IDT IDT with tf.name_scope('inputs'): + IDT IDT IDT self.add_placeholders() + IDT IDT with tf.name_scope('predict'): + IDT IDT IDT self.pred = self.add_prediction_op() + IDT IDT with tf.name_scope('loss'): + IDT IDT IDT self.loss = self.add_loss_op(self.pred) + IDT IDT with tf.name_scope('train'): + IDT IDT IDT self.train_op = self.add_training_op(self.loss) + + IDT def get_pred(self, sess, inputs_batch): + IDT IDT feed = , dropout= , is_training= ) IDT IDT p = , feed_dict= ) IDT IDT return p + + +def load(path): + IDT all = np.load(path) + IDT X = all['arr_0'] + IDT y = all['arr_1'] + IDT y1 = y.reshape(y.shape[0], y.shape[2]) + IDT return X, y1 + + +def main(): + IDT PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + IDT train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + IDT train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + IDT logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_BC_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + IDT X_train, y_train = load(train_path) + IDT + IDT + IDT with tf.Graph().as_default(): + IDT IDT config = Config() + IDT IDT nn = NN(config) + IDT IDT init = tf.global_variables_initializer() + IDT IDT saver = , keep_checkpoint_every_n_hours= ) IDT IDT shuffle_batch_x, shuffle_batch_y = , y_train], batch_size= , capacity= , min_after_dequeue= , enqueue_many= ) IDT IDT with tf.Session() as session: + IDT IDT IDT merged = tf.summary.merge_all() + IDT IDT IDT train_writer = tf.summary.FileWriter(train_log_path, session.graph) + IDT IDT IDT session.run(init) + IDT IDT IDT coord = tf.train.Coordinator() + IDT IDT IDT threads = tf.train.start_queue_runners(session, coord) + IDT IDT IDT for j in tqdm.tqdm(range(Config.itera)): + IDT IDT IDT IDT i = 0 + IDT IDT IDT IDT try: + IDT IDT IDT IDT IDT for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + IDT IDT IDT IDT IDT IDT batch_x, batch_y = session.run([shuffle_batch_x, shuffle_batch_y]) + IDT IDT IDT IDT IDT IDT loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + IDT IDT IDT IDT IDT IDT i += 1 + IDT IDT IDT IDT IDT IDT if i % 1000 == 0: + IDT IDT IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT IDT IDT saver.save(session, os.path.join(PROJECT_ROOT, 'model/model_ckpt'), global_step= ) IDT IDT IDT IDT except tf.errors.OutOfRangeError: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT finally: + IDT IDT IDT IDT IDT coord.request_stop() + IDT IDT IDT IDT coord.join(threads) + IDT IDT IDT IDT env = gym.make(Config.envname) + IDT IDT IDT IDT rollouts = 20 + IDT IDT IDT IDT returns = [] + IDT IDT IDT IDT for _ in range(rollouts): + IDT IDT IDT IDT IDT obs = env.reset() + IDT IDT IDT IDT IDT done = False + IDT IDT IDT IDT IDT totalr = 0.0 + IDT IDT IDT IDT IDT steps = 0 + IDT IDT IDT IDT IDT while not done: + IDT IDT IDT IDT IDT IDT action = nn.get_pred(session, obs[(None), :]) + IDT IDT IDT IDT IDT IDT obs, r, done, _ = env.step(action) + IDT IDT IDT IDT IDT IDT totalr += r + IDT IDT IDT IDT IDT IDT steps += 1 + IDT IDT IDT IDT IDT IDT if steps >= Config.max_steps: + IDT IDT IDT IDT IDT IDT IDT break + IDT IDT IDT IDT IDT returns.append(totalr) + IDT IDT IDT IDT logz.log_tabular('Iteration', j) + IDT IDT IDT IDT logz.log_tabular('AverageReturn', np.mean(returns)) + IDT IDT IDT IDT logz.log_tabular('StdReturn', np.std(returns)) + IDT IDT IDT IDT logz.dump_tabular() + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class DecoderCell(tf.nn.rnn_cell.BasicLSTMCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, U, masks, scope= ) IDT IDT self.U = U + IDT IDT U_shape = U.get_shape() + IDT IDT self.p_size = int(U_shape[1]) + IDT IDT self.hid_dim = int(U_shape[2]) + IDT IDT self.masks = masks + IDT IDT super(DecoderCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT c_tot, h_tot = state + IDT IDT IDT c_prev = c_tot[:, :self.hid_dim] + IDT IDT IDT h_prev = h_tot[:, :self.hid_dim] + IDT IDT IDT alpha = h_tot[:, self.hid_dim:self.hid_dim + self.p_size] + IDT IDT IDT beta = h_tot[:, self.hid_dim + self.p_size:] + IDT IDT IDT s_prev = , axis= ) IDT IDT IDT e_prev = , axis= ) IDT IDT IDT s_prev = , dtype= ) IDT IDT IDT e_prev = , dtype= ) IDT IDT IDT with vs.variable_scope('index_into_U'): + IDT IDT IDT IDT batch_size = tf.shape(self.U)[0] + IDT IDT IDT IDT a = , batch_size, dtype= ) IDT IDT IDT IDT a = , axis= ) IDT IDT IDT IDT s_prev = , axis= ) IDT IDT IDT IDT e_prev = , axis= ) IDT IDT IDT IDT s_prev = tf.concat(1, [a, s_prev]) + IDT IDT IDT IDT e_prev = tf.concat(1, [a, e_prev]) + IDT IDT IDT IDT U_s = tf.gather_nd(self.U, s_prev) + IDT IDT IDT IDT U_e = tf.gather_nd(self.U, e_prev) + IDT IDT IDT IDT lstm_input = tf.concat(1, [U_s, U_e]) + IDT IDT self._num_units = self.hid_dim + IDT IDT prev_state = tf.nn.rnn_cell.LSTMStateTuple(c_prev, h_prev) + IDT IDT lstm_out, lstm_state = super(DecoderCell, self).__call__(lstm_input, prev_state, scope) + IDT IDT self._num_units = self.hid_dim + self.p_size + IDT IDT c_state, h_state = lstm_state + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT H_i = tf.expand_dims(h_state, 1) + IDT IDT IDT H_i = tf.tile(H_i, [1, self.p_size, 1]) + IDT IDT IDT U_s = tf.expand_dims(U_s, 1) + IDT IDT IDT U_s = tf.tile(U_s, [1, self.p_size, 1]) + IDT IDT IDT U_e = tf.expand_dims(U_e, 1) + IDT IDT IDT U_e = tf.tile(U_e, [1, self.p_size, 1]) + IDT IDT IDT U_reshaped = tf.reshape(self.U, [-1, self.hid_dim]) + IDT IDT IDT H_i_reshaped = tf.reshape(H_i, [-1, self.hid_dim]) + IDT IDT IDT U_s_reshaped = tf.reshape(U_s, [-1, self.hid_dim]) + IDT IDT IDT U_e_reshaped = tf.reshape(U_e, [-1, self.hid_dim]) + IDT IDT IDT with vs.variable_scope('start_probs'): + IDT IDT IDT IDT start_probs = , H_i_reshaped, U_s_reshaped, U_e_reshaped], output_size= , bias= ) IDT IDT IDT IDT start_probs = tf.nn.tanh(start_probs) + IDT IDT IDT IDT with vs.variable_scope('second_layer'): + IDT IDT IDT IDT IDT start_probs = , output_size= , bias= ) IDT IDT IDT IDT start_probs = tf.reshape(start_probs, [-1, self.p_size]) + IDT IDT IDT with vs.variable_scope('end_probs'): + IDT IDT IDT IDT end_probs = , H_i_reshaped, U_s_reshaped, U_e_reshaped], output_size= , bias= ) IDT IDT IDT IDT end_probs = tf.nn.tanh(end_probs) + IDT IDT IDT IDT with vs.variable_scope('second_layer'): + IDT IDT IDT IDT IDT end_probs = , output_size= , bias= ) IDT IDT IDT IDT end_probs = tf.reshape(end_probs, [-1, self.p_size]) + IDT IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT IDT add_mask = -1e+30 * (1.0 - bool_masks) + IDT IDT IDT start_probs = tf.add(start_probs, add_mask) + IDT IDT IDT end_probs = tf.add(end_probs, add_mask) + IDT IDT IDT c_state = tf.concat(1, [c_state, c_tot[:, self.hid_dim:]]) + IDT IDT IDT h_state = tf.concat(1, [h_state, start_probs, end_probs]) + IDT IDT IDT probs = tf.concat(1, [start_probs, end_probs]) + IDT IDT IDT return h_state, tf.nn.rnn_cell.LSTMStateTuple(c_state, h_state) + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT """ + + IDT def __init__(self, hidden_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.dropout = dropout + + IDT def encode(self, inputs, masks, attention_inputs= , model_type= , name= , reuse= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(name, reuse= ) IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Attention not implemented.') + IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT outputs, final_state = , inputs, sequence_length= , dtype= ) IDT IDT IDT return outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, hidden_size, output_size, dropout, num_iter= ) IDT IDT self.hidden_size = hidden_size + IDT IDT self.output_size = output_size + IDT IDT self.dropout = dropout + IDT IDT self.num_iter = num_iter + + IDT def decode(self, knowledge_rep, masks, model_type= ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT IDT and output a probability estimation over + IDT IDT all paragraph tokens on which token should be + IDT IDT the start of the answer span, and which should be + IDT IDT the end of the answer span. + + IDT IDT :param knowledge_rep: it is a representation of the paragraph and question, + IDT IDT IDT IDT IDT IDT IDT decided by how you choose to implement the encoder + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('decoder'): + IDT IDT IDT cell = , U= , masks= ) IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT all_probs, final_probs = , knowledge_rep[:, :self.num_iter, :], dtype= ) IDT IDT IDT start_probs = all_probs[:, :, self.hidden_size:self.hidden_size + self.output_size] + IDT IDT IDT end_probs = all_probs[:, :, self.hidden_size + self.output_size:] + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, pretrained_embeddings, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.flags = flags + IDT IDT self.h_size = self.flags.state_size + IDT IDT self.p_size = self.flags.output_size + IDT IDT self.q_size = self.flags.question_size + IDT IDT self.dropout = 1.0 - self.flags.dropout + IDT IDT self.embed_size = self.flags.embedding_size + IDT IDT self.encoder = , dropout= ) IDT IDT self.decoder = , output_size= , dropout= ) IDT IDT self.context_placeholder = , shape= , self.p_size), name= ) IDT IDT self.question_placeholder = , shape= , self.q_size), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) IDT IDT self.saver = tf.train.Saver() + + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def coattention(self, P, Q, masks): + IDT IDT P_t = , perm= , 2, 1]) ) IDT IDT Q_t = , perm= , 2, 1]) ) IDT IDT L = tf.batch_matmul(P, Q_t) + IDT IDT AQ = , dim= ) IDT IDT AD = , perm= , 2, 1]), dim= ) IDT IDT CQ = tf.batch_matmul(P_t, AQ) + IDT IDT contexts = tf.concat(1, [Q_t, CQ]) + IDT IDT CD = tf.batch_matmul(contexts, AD) + IDT IDT lstm_inputs = , [P_t, CD]), perm= , 2, 1]) ) IDT IDT cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_fw = , output_keep_prob= ) IDT IDT cell_bw = , output_keep_prob= ) IDT IDT all_states, _ = , cell_bw, lstm_inputs, sequence_length= , dtype= ) IDT IDT U = tf.concat(2, all_states) + IDT IDT U = U[:, :self.p_size, :] + IDT IDT return U + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT question_states, final_question_state = , self.mask_q_placeholder, attention_inputs= , model_type= , reuse= ) IDT IDT batch_size = tf.shape(question_states)[0] + IDT IDT sent_vec = , shape= , 1, self.h_size), initializer= ) IDT IDT sent_vec = tf.tile(sent_vec, [batch_size, 1, 1]) + IDT IDT question_states = tf.concat(1, [question_states, sent_vec]) + IDT IDT Q = tf.reshape(question_states, [-1, self.h_size]) + IDT IDT Q = , output_size= , bias= ) IDT IDT Q = tf.nn.tanh(tf.reshape(Q, [-1, self.q_size + 1, self.h_size])) + IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, attention_inputs= , model_type= , reuse= ) IDT IDT sent_vec = , shape= , 1, self.h_size), initializer= ) IDT IDT sent_vec = tf.tile(sent_vec, [batch_size, 1, 1]) + IDT IDT P = tf.concat(1, [ctx_states, sent_vec]) + IDT IDT U = , Q= , masks= ) IDT IDT self.start_probs, self.end_probs = , masks= , model_type= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (0), :], self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (0), :], self.answer_span_placeholder[:, (1)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (1), :], self. + IDT IDT IDT IDT answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (1), :], self.answer_span_placeholder[:, (1)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (2), :], self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (2 + IDT IDT IDT IDT ), :], self.answer_span_placeholder[:, (1)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (3), :], self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (3), :], self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0][:, (-1), :], outputs[1][:, (-1), :] + + IDT def answer(self, session, data): + IDT IDT yp_lst = [] + IDT IDT yp2_lst = [] + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle= ) IDT IDT IDT yp, yp2 = self.decode(session, *batch) + IDT IDT IDT yp_lst.append(yp) + IDT IDT IDT yp2_lst.append(yp2) + IDT IDT IDT prog_train.update(i + 1, [('computing F1...', 1)]) + IDT IDT + IDT IDT yp_all = , axis= ) IDT IDT yp2_all = , axis= ) IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(session= , context_batch= , question_batch= , answer_span_batch= , mask_ctx_batch= , mask_q_batch= ) + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset[0]) + IDT IDT else: + IDT IDT IDT inds = np.random.choice(len(dataset[0]), sample) + IDT IDT IDT sampled = [elem[inds] for elem in dataset] + IDT IDT IDT context = [context[i] for i in inds] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT cur_em = exact_match_score(pred_words, actual_words) + IDT IDT IDT em.append(float(cur_em)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + IDT IDT return np.mean(f1), np.mean(em) + + IDT def run_epoch(self, sess, train_set, val_set, train_context, val_context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT + IDT IDT prog_val = ) IDT IDT for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT + IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) + IDT def train(self, session, dataset, val_dataset, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + IDT IDT train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + IDT IDT val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + IDT IDT val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = [elem[:self.flags.batch_size] for elem in train_dataset] + IDT IDT IDT val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + IDT IDT IDT num_epochs = 100 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , train_context= , val_context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT self.saver.save(session, train_dir + '/qa.ckpt') + + IDT def minibatches(self, data, batch_size, shuffle= ) IDT IDT num_data = len(data[0]) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + IDT IDT indices = np.arange(num_data) + IDT IDT if shuffle: + IDT IDT IDT np.random.shuffle(indices) + IDT IDT for minibatch_start in np.arange(0, num_data, batch_size): + IDT IDT IDT minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + IDT IDT IDT yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Oct 6, 2017 +This file contains sequence models for training on skeleton data +@author: Amin +""" +import os +from mpl_toolkits.mplot3d import Axes3D +import matplotlib.pyplot as plt +from shutil import copyfile +import matlab.engine +import re +import time +import numpy as np +import tensorflow as tf +import sys +from network_params import * +from tensorflow.python.ops.init_ops import TruncatedNormal +from tensorflow.python.ops.rnn_cell_impl import * +cnt = 0 + + +class JointLSTM: + IDT """ + IDT custom lstm cells comprised of num_joints seperate lstm cells + IDT """ + + IDT def __init__(self, num_joints= , num_units_each_cell= ) IDT IDT self.cells = , state_is_tuple= , initializer= ) IDT IDT self.num_cells = num_joints + IDT IDT self.mem_size_each = num_units_each_cell + IDT IDT self.mem_size_global = num_units_each_cell * num_joints + IDT IDT self.cell_op_states = [None] * self.num_cells + + IDT def __call__(self, global_mem, inputs): + IDT IDT for i in range(self.num_cells): + IDT IDT IDT if self.cell_op_states[i] == None: + IDT IDT IDT IDT init_state = self.cells[i].zero_state(batch_size, tf.float32) + IDT IDT IDT IDT self.cell_op_states[i] = , init_state, scope= ) IDT IDT IDT else: + IDT IDT IDT IDT self.cell_op_states[i] = , (self.cell_op_states[i], global_mem[i * self.mem_size_each:(i + 1) * self.mem_size_each]), scope= ) IDT IDT with tf.variable_scope('whole_op_gate'): + IDT IDT IDT W = tf.get_variable('W', [self.mem_size_global + self.num_cells * 3, self.mem_size_global]) + IDT IDT IDT b = , [self.mem_size_global], initializer= ) IDT IDT input_to_op_gate = , tf.reshape(inputs, [batch_size, 3 * self.num_cells])], axis= ) IDT IDT output_from_op_gate = tf.nn.sigmoid(tf.matmul(input_to_op_gate, W) + b) + IDT IDT concatenated_mem = , axis= ) IDT IDT final_mem = tf.multiply(output_from_op_gate, concatenated_mem) + IDT IDT return final_mem + + +def reset_graph(): + IDT if 'sess' in globals() and sess: + IDT IDT sess.close() + IDT tf.reset_default_graph() + + +def build_rnn_graph_per_joint(state_size= , num_classes= , batch_size= , num_steps= , learning_rate= ) IDT """ + IDT considering only one joints using dynamic rnn and lstm cells + IDT """ + IDT reset_graph() + IDT num_joints = len(considered_joints) + IDT b_size = , [], name= ) IDT x = , [batch_size, num_joints, num_steps, 3], name= ) IDT y = , [batch_size], name= ) IDT rnn_inputs = tf.transpose(x, [2, 1, 0, 3]) + IDT num_units_each_cell = 10 + IDT cell = , num_units_each_cell= ) IDT final_mems = , x: cell(a, x), rnn_inputs, initializer= , num_joints * num_units_each_cell])) ) IDT + IDT final_layer_op = tf.squeeze(final_mems[(-1), :, :]) + IDT with tf.variable_scope('softmax'): + IDT IDT W = tf.get_variable('W', [num_joints * num_units_each_cell, num_classes]) + IDT IDT b = , [num_classes], initializer= ) IDT final_logits = tf.matmul(final_layer_op, W) + b + IDT total_loss = , labels= , num_classes))) ) IDT train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + IDT predictions = tf.nn.softmax(final_logits) + IDT pred2 = tf.argmax(predictions, 1) + IDT correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + IDT accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + IDT return dict(x= , y= , total_loss= , train_step= , preds= , saver= , b_size= , acc= , pred2= , final_layer_op_= ) + +def build_basic_rnn_graph_with_list3(state_size= , num_classes= , batch_size= , num_steps= , learning_rate= ) IDT """ + IDT considering only one joints using dynamic rnn and lstm cells + IDT """ + IDT reset_graph() + IDT num_joints = len(considered_joints) + IDT b_size = , [], name= ) IDT x = , [batch_size, num_joints, num_steps, 3], name= ) IDT y = , [batch_size], name= ) IDT rnn_inputs = tf.squeeze(x[:, (-2), :]) + IDT cell = , state_is_tuple= , initializer= ) IDT init_state = cell.zero_state(b_size, tf.float32) + IDT rnn_outputs, final_state = , rnn_inputs, dtype= ) IDT rnn_op_final = tf.squeeze(rnn_outputs[:, (-1), :]) + IDT with tf.variable_scope('softmax'): + IDT IDT W = tf.get_variable('W', [state_size, num_classes]) + IDT IDT b = , [num_classes], initializer= ) IDT final_logits = tf.matmul(rnn_op_final, W) + b + IDT total_loss = , labels= , num_classes))) ) IDT train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + IDT predictions = tf.nn.softmax(final_logits) + IDT pred2 = tf.argmax(predictions, 1) + IDT correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + IDT accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + IDT return dict(x= , y= , init_state= , final_state= , total_loss= , train_step= , preds= , saver= , b_size= , acc= , pred2= ) + +def build_basic_rnn_graph_with_list2(state_size= , num_classes= , batch_size= , num_steps= , learning_rate= ) IDT """ + IDT considering only one joints + IDT """ + IDT reset_graph() + IDT num_joints = len(considered_joints) + IDT b_size = , [], name= ) IDT x = , [batch_size, num_joints, num_steps, 3], name= ) IDT y = , [batch_size], name= ) IDT rnn_inputs = , (-2), :]), axis= ) IDT cell = tf.nn.rnn_cell.BasicRNNCell(state_size) + IDT init_state = cell.zero_state(b_size, tf.float32) + IDT rnn_outputs, final_state = , rnn_inputs, dtype= ) IDT rnn_op_last = tf.squeeze(rnn_outputs[-1]) + IDT with tf.variable_scope('softmax'): + IDT IDT W = tf.get_variable('W', [state_size, num_classes]) + IDT IDT b = , [num_classes], initializer= ) IDT logits = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, state_size]), W) + b, [-1, num_steps, num_classes]) + IDT final_logits = tf.matmul(rnn_op_last, W) + b + IDT total_loss = , labels= , num_classes))) ) IDT train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + IDT predictions = tf.nn.softmax(final_logits) + IDT pred2 = tf.argmax(predictions, 1) + IDT correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + IDT accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + IDT return dict(x= , y= , init_state= , final_state= , total_loss= , train_step= , preds= , saver= , b_size= , acc= , pred2= ) + +def build_basic_rnn_graph_with_list(state_size= , num_classes= , batch_size= , num_steps= , learning_rate= ) IDT reset_graph() + IDT num_joints = len(considered_joints) + IDT b_size = , [], name= ) IDT x = , [batch_size, num_joints, num_steps, 3], name= ) IDT y = , [batch_size], name= ) IDT rnn_inputs = , perm= , 2, 1, 3]), num_steps, axis= , [-1, num_steps, num_joints * 3]), num_steps, axis= ) IDT cell = tf.nn.rnn_cell.BasicRNNCell(state_size) + IDT init_state = cell.zero_state(b_size, tf.float32) + IDT rnn_outputs, final_state = , rnn_inputs, initial_state= ) IDT with tf.variable_scope('softmax'): + IDT IDT W = tf.get_variable('W', [state_size, num_classes]) + IDT IDT b = , [num_classes], initializer= ) IDT logits = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, state_size]), W) + b, [-1, num_steps, num_classes]) + IDT final_logits = , num_or_size_splits= , axis= ) IDT total_loss = , labels= ) IDT train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + IDT predictions = tf.nn.softmax(final_logits) + IDT pred2 = tf.argmax(predictions, 1) + IDT correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + IDT accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + IDT return dict(x= , y= , init_state= , final_state= , total_loss= , train_step= , preds= , saver= , b_size= , acc= , pred2= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import tensorflow as tf +import numpy as np + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +def randomize(dataset, labels): + IDT permutation = np.random.permutation(labels.shape[0]) + IDT shuffled_dataset = dataset[(permutation), :] + IDT shuffled_labels = labels[permutation] + IDT return shuffled_dataset, shuffled_labels + + +def make_neural_network(train_dataset, train_labels, test_dataset, test_labels, num_labels, features): + IDT train_dataset, train_labels = randomize(train_dataset, train_labels) + IDT batch_size = 64 + IDT beta = 0.001 + IDT hidden_nodes1 = 128 + IDT hidden_nodes2 = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , features)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_weights = tf.Variable(tf.truncated_normal([features, hidden_nodes1])) + IDT IDT hidden_biases = tf.Variable(tf.zeros([hidden_nodes1])) + IDT IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + IDT IDT keep_prob = tf.placeholder('float') + IDT IDT hidden_layer_drop = tf.nn.dropout(hidden_layer, keep_prob) + IDT IDT hidden_weights2 = tf.Variable(tf.truncated_normal([hidden_nodes1, hidden_nodes2])) + IDT IDT hidden_biases2 = tf.Variable(tf.zeros([hidden_nodes2])) + IDT IDT hidden_layer2 = tf.nn.relu(tf.matmul(hidden_layer_drop, hidden_weights2) + hidden_biases2) + IDT IDT hidden_layer_drop2 = tf.nn.dropout(hidden_layer2, keep_prob) + IDT IDT weights = tf.Variable(tf.truncated_normal([hidden_nodes2, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden_layer_drop2, weights) + biases + IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT IDT loss = tf.reduce_mean(loss + beta * tf.nn.l2_loss(weights)) + IDT IDT global_step = tf.Variable(0) + IDT IDT learnr = tf.placeholder('float') + IDT IDT learning_rate = , global_step, 100000, 0.95, staircase= ) IDT IDT optimizer = , global_step= ) IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT test_relu1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + IDT IDT test_relu2 = tf.nn.relu(tf.matmul(test_relu1, hidden_weights2) + hidden_biases2) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(test_relu2, weights) + biases) + IDT IDT num_steps = 2000 + IDT IDT with tf.Session(graph= ) IDT IDT IDT tf.initialize_all_variables().run() + IDT IDT IDT + IDT IDT IDT for step in range(num_steps): + IDT IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 1.0, learnr: 0.001} + IDT IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT IDT ) + IDT IDT IDT IDT IDT ) + IDT IDT IDT , test_labels)) +import numpy as np +import tensorflow as tf +import Utility as util +import math + + +class ConvolutionNeuralNetwork: + + IDT def __init__(self, d, k): + IDT IDT """ + IDT IDT :param d: dimensionality + IDT IDT :param k: number of classes + IDT IDT """ + IDT IDT self.D = d + IDT IDT self.K = k + IDT IDT self.NR_VALIDATION_DATA = 50 + IDT IDT self.NR_ITERATION = 20000 + IDT IDT self.BATCH_SIZE = 500 + IDT IDT self.SHOW_ACC = 100 + IDT IDT self.TRAIN_STEP = 0.0001 + IDT IDT self.EPSILON = 0.001 + IDT IDT self.W1_SHAPE = [5, 5, 3, 16] + IDT IDT self.B1_SHAPE = [16] + IDT IDT self.W2_SHAPE = [5, 5, 16, 20] + IDT IDT self.B2_SHAPE = [20] + IDT IDT self.W3_SHAPE = [5, 5, 20, 20] + IDT IDT self.B3_SHAPE = [20] + IDT IDT self.WFC_SHAPE = [320, k] + IDT IDT self.BFC_SHAPE = [k] + + IDT def training(self, features, labels): + IDT IDT """ + IDT IDT Training the Convolutional Neural Network + IDT IDT :param features: the training data [50000 x 3072] + IDT IDT :param labels: the true label for X [50000 x 1] + IDT IDT :return: return a dictionary which contains all learned parameters + IDT IDT """ + IDT IDT features = self.__preprocessing(features) + IDT IDT train_features = features[self.NR_VALIDATION_DATA:] + IDT IDT train_labels = labels[self.NR_VALIDATION_DATA:] + IDT IDT validation_features = features[0:self.NR_VALIDATION_DATA] + IDT IDT validation_labels = labels[0:self.NR_VALIDATION_DATA] + IDT IDT sess = tf.InteractiveSession() + IDT IDT x = , shape= , self.D]) ) IDT IDT y_ = , shape= ) IDT IDT x_image = tf.reshape(x, [-1, 32, 32, 3]) + IDT IDT W1 = self.__weight_variable(self.W1_SHAPE) + IDT IDT scale1 = tf.Variable(tf.ones(self.B1_SHAPE)) + IDT IDT beta1 = tf.Variable(tf.zeros(self.B1_SHAPE)) + IDT IDT W2 = self.__weight_variable(self.W2_SHAPE) + IDT IDT scale2 = tf.Variable(tf.ones(self.B2_SHAPE)) + IDT IDT beta2 = tf.Variable(tf.zeros(self.B2_SHAPE)) + IDT IDT W3 = self.__weight_variable(self.W3_SHAPE) + IDT IDT scale3 = tf.Variable(tf.ones(self.B3_SHAPE)) + IDT IDT beta3 = tf.Variable(tf.zeros(self.B3_SHAPE)) + IDT IDT WFC = self.__weight_variable(self.WFC_SHAPE) + IDT IDT bFC = self.__bias_variable(self.BFC_SHAPE) + IDT IDT Z1_conv = self.__convolution(x_image, W1) + IDT IDT batch_mean1, batch_var1 = tf.nn.moments(Z1_conv, [0]) + IDT IDT BN1 = tf.nn.batch_normalization(Z1_conv, batch_mean1, batch_var1, beta1, scale1, self.EPSILON) + IDT IDT H1_conv = self.__activation(BN1) + IDT IDT H1_pool = self.__pool(H1_conv) + IDT IDT Z2_conv = self.__convolution(H1_pool, W2) + IDT IDT batch_mean2, batch_var2 = tf.nn.moments(Z2_conv, [0]) + IDT IDT BN2 = tf.nn.batch_normalization(Z2_conv, batch_mean2, batch_var2, beta2, scale2, self.EPSILON) + IDT IDT H2_conv = self.__activation(BN2) + IDT IDT H2_pool = self.__pool(H2_conv) + IDT IDT Z3_conv = self.__convolution(H2_pool, W3) + IDT IDT batch_mean3, batch_var3 = tf.nn.moments(Z3_conv, [0]) + IDT IDT BN3 = tf.nn.batch_normalization(Z3_conv, batch_mean3, batch_var3, beta3, scale3, self.EPSILON) + IDT IDT H3_conv = self.__activation(BN3) + IDT IDT H3_pool = self.__pool(H3_conv) + IDT IDT H3_pool_flatten = tf.reshape(H3_pool, [-1, self.WFC_SHAPE[0]]) + IDT IDT HFC = tf.matmul(H3_pool_flatten, WFC) + bFC + IDT IDT y_conv = tf.nn.softmax(HFC) + IDT IDT cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y_conv, y_) + IDT IDT cross_entropy_mean = tf.reduce_mean(cross_entropy) + IDT IDT train_step = tf.train.AdamOptimizer(self.TRAIN_STEP).minimize(cross_entropy_mean) + IDT IDT correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_) + IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT for i in range(self.NR_ITERATION): + IDT IDT IDT batch = util.generate_batch(train_features, train_labels, self.BATCH_SIZE) + IDT IDT IDT train_step.run(feed_dict= , y_: batch[1]}) ) IDT IDT IDT if i % self.SHOW_ACC == 0: + IDT IDT IDT IDT train_accuracy = , y_: validation_labels}) ) IDT IDT IDT IDT + IDT IDT W1_final = W1.eval() + IDT IDT beta1_final = beta1.eval() + IDT IDT scale1_final = scale1.eval() + IDT IDT W2_final = W2.eval() + IDT IDT beta2_final = beta2.eval() + IDT IDT scale2_final = scale2.eval() + IDT IDT W3_final = W3.eval() + IDT IDT beta3_final = beta3.eval() + IDT IDT scale3_final = scale3.eval() + IDT IDT WFC_final = WFC.eval() + IDT IDT bFC_final = bFC.eval() + IDT IDT sess.close() + IDT IDT return {'W1': W1_final, 'beta1': beta1_final, 'scale1': scale1_final, 'W2': W2_final, 'beta2': beta2_final, 'scale2': scale2_final, 'W3': W3_final, 'beta3': beta3_final, 'scale3': scale3_final, 'WFC': WFC_final, 'bFC': bFC_final} + + IDT def predict(self, test_features, test_labels, nn): + IDT IDT """ + IDT IDT Predict data + IDT IDT :param test_features: testing data + IDT IDT :param test_labels: labels for test_features + IDT IDT :param nn: it is a dictionary which contains a Neural Network + IDT IDT :return: return the predicted labels and the accuracy + IDT IDT """ + IDT IDT test_features = self.__preprocessing(test_features) + IDT IDT x = , shape= , self.D]) ) IDT IDT W1 = , shape= ) IDT IDT beta1 = , shape= ) IDT IDT scale1 = , shape= ) IDT IDT W2 = , shape= ) IDT IDT beta2 = , shape= ) IDT IDT scale2 = , shape= ) IDT IDT W3 = , shape= ) IDT IDT beta3 = , shape= ) IDT IDT scale3 = , shape= ) IDT IDT WFC = , shape= ) IDT IDT bFC = , shape= ) IDT IDT x_image = tf.reshape(x, [-1, 32, 32, 3]) + IDT IDT Z1_conv = self.__convolution(x_image, W1) + IDT IDT batch_mean1, batch_var1 = tf.nn.moments(Z1_conv, [0]) + IDT IDT BN1 = tf.nn.batch_normalization(Z1_conv, batch_mean1, batch_var1, beta1, scale1, self.EPSILON) + IDT IDT H1_conv = self.__activation(BN1) + IDT IDT H1_pool = self.__pool(H1_conv) + IDT IDT Z2_conv = self.__convolution(H1_pool, W2) + IDT IDT batch_mean2, batch_var2 = tf.nn.moments(Z2_conv, [0]) + IDT IDT BN2 = tf.nn.batch_normalization(Z2_conv, batch_mean2, batch_var2, beta2, scale2, self.EPSILON) + IDT IDT H2_conv = self.__activation(BN2) + IDT IDT H2_pool = self.__pool(H2_conv) + IDT IDT Z3_conv = self.__convolution(H2_pool, W3) + IDT IDT batch_mean3, batch_var3 = tf.nn.moments(Z3_conv, [0]) + IDT IDT BN3 = tf.nn.batch_normalization(Z3_conv, batch_mean3, batch_var3, beta3, scale3, self.EPSILON) + IDT IDT H3_conv = self.__activation(BN3) + IDT IDT H3_pool = self.__pool(H3_conv) + IDT IDT H3_pool_flatten = tf.reshape(H3_pool, [-1, self.WFC_SHAPE[0]]) + IDT IDT HFC = tf.matmul(H3_pool_flatten, WFC) + bFC + IDT IDT y_conv = tf.nn.softmax(HFC) + IDT IDT sess = tf.InteractiveSession() + IDT IDT feed_dict = {x: test_features, W1: nn['W1'], beta1: nn['beta1'], scale1: nn['scale1'], W2: nn['W2'], beta2: nn['beta2'], scale2: nn['scale2'], W3: nn['W3'], beta3: nn['beta3'], scale3: nn['scale3'], WFC: nn['WFC'], bFC: nn['bFC']} + IDT IDT predicted_labels = , feed_dict= ) IDT IDT sess.close() + IDT IDT predicted_labels = , axis= ) IDT IDT acc = ) IDT IDT return predicted_labels, acc + + IDT def __preprocessing(self, X): + IDT IDT """ + IDT IDT Preprocessing the X data by zero-centered and normalized them. + IDT IDT :param X: the data. + IDT IDT :return: return the new zero-centered and normalized data. + IDT IDT """ + IDT IDT X = X.astype(np.float64) + IDT IDT X = , dtype= ) IDT IDT X = , dtype= ) IDT IDT return X + + IDT def __weight_variable(self, shape): + IDT IDT """ + IDT IDT Initialize the weights variable. + IDT IDT :param shape: the shape. + IDT IDT :return: return a TensorFlow variable + IDT IDT """ + IDT IDT if len(shape) == 4: + IDT IDT IDT initial = np.random.randn(shape[0], shape[1], shape[2], shape[3]) * math.sqrt(2.0 / (shape[0] * shape[1] * shape[2] * shape[3])) + IDT IDT else: + IDT IDT IDT initial = np.random.randn(shape[0], shape[1]) * math.sqrt(2.0 / (shape[0] * shape[1])) + IDT IDT return tf.Variable(initial, dtype= ) + IDT def __bias_variable(self, shape): + IDT IDT """ + IDT IDT Initialize the biases variable. + IDT IDT :param shape:t he shape. + IDT IDT :return: return a TensorFlow variable + IDT IDT """ + IDT IDT initial = , shape= ) IDT IDT return tf.Variable(initial) + + IDT def __convolution(self, x, W): + IDT IDT """ + IDT IDT The convolution layer calculation. + IDT IDT :param x: the data. + IDT IDT :param W: the weights. + IDT IDT :return: return the output of the convolution layer. + IDT IDT """ + IDT IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + IDT def __activation(self, x): + IDT IDT """ + IDT IDT The activation function. + IDT IDT :param x: the data. + IDT IDT :return: return the data after apply the activation. + IDT IDT """ + IDT IDT return tf.nn.relu(x) + + IDT def __pool(self, x): + IDT IDT """ + IDT IDT The pool layer. + IDT IDT :param x: the data. + IDT IDT :return: return the output of the pool layer. + IDT IDT """ + IDT IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +if __name__ == '__main__': + IDT D = 3072 + IDT K = 10 + IDT learn_data = 'result/CNN1_BN/cifar_10' + IDT final_accuracy = 0 + IDT batch_size = 500 + IDT cnn = ConvolutionNeuralNetwork(3072, 10) + IDT X, y, X_test, y_test = util.load_CIFAR10('data/') + IDT if util.file_exist(learn_data): + IDT IDT nn_parameter = util.unpickle(learn_data) + IDT else: + IDT IDT nn_parameter = cnn.training(X, y) + IDT IDT util.pickle_nn(learn_data, nn_parameter) + IDT util.create_file('result/CNN1_BN/submission.csv') + IDT for i in range(0, X_test.shape[0], batch_size): + IDT IDT batch_test_feature = X_test[i:i + batch_size, :] + IDT IDT batch_test_labels = y_test[i:i + batch_size] + IDT IDT predicted_labels, accuracy = cnn.predict(batch_test_feature, batch_test_labels, nn_parameter) + IDT IDT final_accuracy += accuracy + IDT IDT util.append_data_to_file('result/CNN1_BN/submission.csv', predicted_labels, i) + IDT nr_iteration = X_test.shape[0] / batch_size + IDT final_accuracy /= nr_iteration + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(text= ) IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message('Flatten') + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message('Fully Connected') + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message('Output') + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Tue May 16 12:30:50 2017 + +@author: Seagle + +""" +import os +os.chdir('C:/SeagleDLTrial') +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +lambda_l2 = 0.05 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracy_val.append(accuracy(valid_prediction.eval(), valid_labels)) +import matplotlib.pyplot as plt +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT logits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracy_val.append(accuracy(valid_prediction.eval(), valid_labels)) +import matplotlib.pyplot as plt +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +accuracy_val.index(max(accuracy_val)) +aa = [0, 2, 5, 5, 4, -1, 3] +[i for i, j in enumerate(aa) if j = )for i, j in enumerate(aa): + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W, stride): + IDT return tf.nn.conv2d(x, W, strides= , stride, stride, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +def createNetwork(action_count): + IDT W_conv1 = weight_variable([8, 8, 4, 32]) + IDT b_conv1 = bias_variable([32]) + IDT W_conv2 = weight_variable([4, 4, 32, 64]) + IDT b_conv2 = bias_variable([64]) + IDT W_conv3 = weight_variable([3, 3, 64, 64]) + IDT b_conv3 = bias_variable([64]) + IDT W_fc1 = weight_variable([1600, 512]) + IDT b_fc1 = bias_variable([512]) + IDT W_fc2 = weight_variable([512, action_count]) + IDT b_fc2 = bias_variable([action_count]) + IDT s = tf.placeholder('float', [None, 80, 80, 4]) + IDT h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + IDT h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + IDT h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + IDT h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + IDT readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + IDT return s, readout, h_fc1 + + +class DQNAgent(object): + + IDT def __init__(self, action_count): + IDT IDT self.action_count = action_count + IDT IDT self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + IDT IDT self.action_placeholder = tf.placeholder('float', [None, action_count]) + IDT IDT self.reward_placeholder = tf.placeholder('float', [None]) + IDT IDT self.readout_action = , self.action_placeholder), reduction_indices= ) IDT IDT self.losses = tf.square(self.reward_placeholder - self.readout_action) + IDT IDT self.loss = tf.reduce_mean(self.losses) + IDT IDT self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.loss) + IDT IDT tf.summary.scalar('loss', self.loss), + IDT IDT tf.summary.histogram('loss_hist', self.losses), + IDT IDT tf.summary.histogram('q_values_hist', self.readout), + IDT IDT tf.summary.scalar('max_q_value', tf.reduce_max(self.readout)) + IDT IDT self.summaries = tf.summary.merge_all() + + IDT def act(self, session, state): + IDT IDT scores = self.score_actions(session, [state])[0] + IDT IDT return np.argmax(scores) + + IDT def score_actions(self, session, state_batch): + IDT IDT return session.run(self.readout, feed_dict= ) + IDT def train(self, session, reward_batch, action_batch, state_batch): + IDT IDT summaries, _, loss = , self.train_step, self.loss], feed_dict= , self.action_placeholder: action_batch, self.state_placeholder: state_batch}) ) IDT IDT return summaries, loss +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +from itertools import islice +from sys import stdout +from tempfile import NamedTemporaryFile +import tensorflow as tf +from utils.model import Model +from data import load_and_preprocess_data +from data import score_arcs +from initialization import xavier_weight_init +from parser import minibatch_parse +from utils.generic_utils import Progbar +tf.flags.DEFINE_float('lr', 0.001, 'learning rate') +tf.flags.DEFINE_integer('hidden', 0, 'number of hidden layers if hidden > 0') +tf.flags.DEFINE_integer('hidden_size', 200, 'hidden size for each layer') +tf.flags.DEFINE_integer('epochs', 10, 'number of epochs') +tf.flags.DEFINE_float('l2_beta', 0, 'beta for computing l2 regularization') +tf.flags.DEFINE_string('activation', 'relu', 'activation function, can be relu or cube') +tf.flags.DEFINE_string('optimizer', 'adam', 'optimizer, can be adam or adagrad') +tf.flags.DEFINE_string('output', '', 'output filename for arcs') +FLAGS = tf.app.flags.FLAGS + + +class Config(object): + IDT """Holds model hyperparams and data information. + + IDT The config class is used to store various hyperparameters and dataset + IDT information parameters. Model objects are passed a Config() object at + IDT instantiation. + IDT """ + IDT n_word_ids = None + IDT n_tag_ids = None + IDT n_deprel_ids = None + IDT n_word_features = None + IDT n_tag_features = None + IDT n_deprel_features = None + IDT n_classes = None + IDT dropout = 0.5 + IDT embed_size = None + IDT hidden_size = FLAGS.hidden_size + IDT batch_size = 2048 + IDT n_epochs = FLAGS.epochs + IDT lr = FLAGS.lr + IDT l2_beta = FLAGS.l2_beta + IDT l2_loss = 0 + + +class ParserModel(Model): + IDT """ + IDT Implements a feedforward neural network with an embedding layer and single hidden layer. + IDT This network will predict which transition should be applied to a given partial parse + IDT configuration. + IDT """ + + IDT def add_placeholders(self): + IDT IDT """Generates placeholder variables to represent the input tensors + + IDT IDT These placeholders are used as inputs by the rest of the model + IDT IDT building and will be fed data during training. Note that when + IDT IDT "None" is in a placeholder's shape, it's flexible (so we can use + IDT IDT different batch sizes without rebuilding the model). + + IDT IDT Adds following nodes to the computational graph + + IDT IDT word_id_placeholder: + IDT IDT IDT Word feature placeholder of shape (None, n_word_features), + IDT IDT IDT type tf.int32 + IDT IDT tag_id_placeholder: + IDT IDT IDT POS tag feature placeholder of shape (None, n_tag_features), + IDT IDT IDT type tf.int32 + IDT IDT deprel_id_placeholder: + IDT IDT IDT Dependency relation feature placeholder of shape + IDT IDT IDT (None, n_deprel_features), type tf.int32 + IDT IDT class_placeholder: + IDT IDT IDT Labels placeholder tensor of shape (None, n_classes), + IDT IDT IDT type tf.float32 + IDT IDT dropout_placeholder: Dropout value placeholder (scalar), type + IDT IDT IDT tf.float32 + + IDT IDT Add these placeholders to self as attributes + IDT IDT IDT self.word_id_placeholder + IDT IDT IDT self.tag_id_placeholder + IDT IDT IDT self.deprel_id_placeholder + IDT IDT IDT self.class_placeholder + IDT IDT IDT self.dropout_placeholder + IDT IDT """ + IDT IDT self.word_id_placeholder = , shape= , self.config.n_word_features)) ) IDT IDT self.tag_id_placeholder = , shape= , self.config.n_tag_features)) ) IDT IDT self.deprel_id_placeholder = , shape= , self.config.n_deprel_features)) ) IDT IDT self.class_placeholder = , shape= , self.config.n_classes)) ) IDT IDT self.dropout_placeholder = , shape= ) + IDT def create_feed_dict(self, word_id_batch, tag_id_batch, deprel_id_batch, class_batch= , dropout= ) IDT IDT """Creates the feed_dict for the dependency parser. + + IDT IDT A feed_dict takes the form of: + + IDT IDT feed_dict = { + IDT IDT IDT IDT : , + IDT IDT IDT IDT .... + IDT IDT } + + IDT IDT The keys for the feed_dict should be a subset of the placeholder + IDT IDT tensors created in add_placeholders. When an argument is None, + IDT IDT don't add it to the feed_dict. + + IDT IDT Args: + IDT IDT IDT word_id_batch: A batch of word id features + IDT IDT IDT tag_id_batch: A batch of POS tag id features + IDT IDT IDT deprel_id_batch: A batch of dependency relation id features + IDT IDT IDT class_batch: A batch of class label data + IDT IDT IDT dropout: The dropout rate + IDT IDT Returns: + IDT IDT IDT feed_dict: The feed dictionary mapping from placeholders to values. + IDT IDT """ + IDT IDT feed_dict = {} + IDT IDT if type(word_id_batch) != ) IDT IDT IDT feed_dict[self.word_id_placeholder] = word_id_batch + IDT IDT if type(tag_id_batch) != ) IDT IDT IDT feed_dict[self.tag_id_placeholder] = tag_id_batch + IDT IDT if type(deprel_id_batch) != ) IDT IDT IDT feed_dict[self.deprel_id_placeholder] = deprel_id_batch + IDT IDT if type(class_batch) != ) IDT IDT IDT feed_dict[self.class_placeholder] = class_batch + IDT IDT if type(dropout) != ) IDT IDT IDT feed_dict[self.dropout_placeholder] = dropout + IDT IDT return feed_dict + + IDT def add_embeddings(self): + IDT IDT """Creates embeddings that map word, tag, deprels to vectors + + IDT IDT Embedding layers convert (sparse) ID representations to dense, + IDT IDT lower-dimensional representations. Inputs are integers, outputs + IDT IDT are floats. + + IDT IDT - Create 3 embedding matrices, one for each of the input types. + IDT IDT Input values index the rows of the matrices to extract. The + IDT IDT max bound (exclusive) on the values in the input can be found + IDT IDT in {n_word_ids, n_tag_ids, n_deprel_ids} + IDT IDT After lookup, the resulting tensors should each be of shape + IDT IDT (None, n, embed_size), where n is one of + IDT IDT {n_word_features, n_tag_features, n_deprel_features}. + IDT IDT - Initialize the word_id embedding matrix with + IDT IDT self.word_embeddings. Initialize the other two matrices + IDT IDT with the Xavier initialization you implemented + IDT IDT - Reshape the embedding tensors into shapes + IDT IDT (None, n * embed_size) + + IDT IDT ** Embedding matrices should be variables, not constants! ** + + IDT IDT Use tf.nn.embedding_lookup. Also take a look at tf.reshape + + IDT IDT Returns: + IDT IDT IDT word_embeddings : tf.Tensor of type tf.float32 and shape + IDT IDT IDT IDT (None, n_word_features * embed_size) + IDT IDT IDT tag_embeddings : tf.float32 (None, n_tag_features * embed_size) + IDT IDT IDT deprel_embeddings : tf.float32 + IDT IDT IDT IDT (None, n_deprel_features * embed_size) + IDT IDT """ + IDT IDT word_variables = tf.Variable(self.word_embeddings) + IDT IDT word_embeddings = tf.nn.embedding_lookup(word_variables, self.word_id_placeholder) + IDT IDT word_embeddings = , shape= , self.config.n_word_features * self.config.embed_size)) ) IDT IDT xavier_initializer = xavier_weight_init() + IDT IDT tag_variable = tf.Variable(xavier_initializer((self.config.n_tag_ids, self.config.embed_size))) + IDT IDT tag_embeddings = tf.nn.embedding_lookup(tag_variable, self.tag_id_placeholder) + IDT IDT tag_embeddings = , shape= , self.config.n_tag_features * self.config.embed_size)) ) IDT IDT deprel_variable = tf.Variable(xavier_initializer((self.config.n_deprel_ids, self.config.embed_size))) + IDT IDT deprel_embeddings = tf.nn.embedding_lookup(deprel_variable, self.deprel_id_placeholder) + IDT IDT deprel_embeddings = , shape= , self.config.n_deprel_features * self.config.embed_size)) ) IDT IDT if self.config.l2_beta: + IDT IDT IDT self.config.l2_loss += tf.nn.l2_loss(tag_variable) + tf.nn.l2_loss(deprel_variable) + IDT IDT return word_embeddings, tag_embeddings, deprel_embeddings + + IDT def add_prediction_op(self): + IDT IDT """Adds the single layer neural network + + IDT IDT The l + IDT IDT IDT h = Relu(W_w x_w + W_t x_t + W_d x_d + b1) + IDT IDT IDT h_drop = Dropout(h, dropout_rate) + IDT IDT IDT pred = h_drop U + b2 + + IDT IDT Note that we are not applying a softmax to pred. The softmax + IDT IDT will instead be done in the add_loss_op function, which improves + IDT IDT efficiency because we can use + IDT IDT IDT tf.nn.softmax_cross_entropy_with_logits + IDT IDT Excluding the softmax in predictions won't change the expected + IDT IDT transition. + + IDT IDT Use the Xavier initializer from initialization.py for W_ and + IDT IDT U. Initialize b1 and b2 with zeros. + + IDT IDT The dimensions of the various variables you will need to create + IDT IDT are: + IDT IDT IDT W_w : (n_word_features * embed_size, hidden_size) + IDT IDT IDT W_t : (n_tag_features * embed_size, hidden_size) + IDT IDT IDT W_d : (n_deprel_features * embed_size, hidden_size) + IDT IDT IDT b1: (hidden_size,) + IDT IDT IDT U: (hidden_size, n_classes) + IDT IDT IDT b2: (n_classes) + + IDT IDT Use the value self.dropout_placeholder in tf.nn.dropout directly + + IDT IDT Returns: + IDT IDT IDT pred: tf.Tensor of shape (batch_size, n_classes) + IDT IDT """ + IDT IDT x_w, x_t, x_d = self.add_embeddings() + IDT IDT xavier_initializer = xavier_weight_init() + IDT IDT W_w = tf.Variable(xavier_initializer((self.config.n_word_features * self.config.embed_size, self.config.hidden_size))) + IDT IDT W_t = tf.Variable(xavier_initializer((self.config.n_tag_features * self.config.embed_size, self.config.hidden_size))) + IDT IDT W_d = tf.Variable(xavier_initializer((self.config.n_deprel_features * self.config.embed_size, self.config.hidden_size))) + IDT IDT b1 = tf.Variable(tf.zeros((self.config.hidden_size,))) + IDT IDT U = tf.Variable(xavier_initializer((self.config.hidden_size, self.config.n_classes))) + IDT IDT b2 = tf.Variable(tf.zeros(self.config.n_classes)) + IDT IDT x = tf.matmul(x_w, W_w) + tf.matmul(x_t, W_t) + tf.matmul(x_d, W_d) + b1 + IDT IDT + IDT IDT if FLAGS.activation == 'cube': + IDT IDT IDT h = , tf.constant(3, dtype= ) IDT IDT else: + IDT IDT IDT h = tf.nn.relu(x) + IDT IDT if self.config.l2_beta: + IDT IDT IDT ) + IDT IDT IDT self.config.l2_loss += tf.nn.l2_loss(W_w) + tf.nn.l2_loss(W_t) + tf.nn.l2_loss(W_d) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(U) + tf.nn.l2_loss(b2) + IDT IDT + ' hidden layer(s) with size ' + str(FLAGS.hidden_size)) + IDT IDT if FLAGS.hidden: + IDT IDT IDT w, b = {}, {} + IDT IDT IDT for i in range(FLAGS.hidden): + IDT IDT IDT IDT w[i] = tf.Variable(xavier_initializer((self.config.hidden_size, self.config.hidden_size))) + IDT IDT IDT IDT b[i] = tf.Variable(tf.random_normal([self.config.hidden_size])) + + IDT IDT IDT def hidden_layers(x): + IDT IDT IDT IDT layer = tf.nn.relu(tf.matmul(x, w[0]) + b[0]) + IDT IDT IDT IDT for i in range(1, FLAGS.hidden): + IDT IDT IDT IDT IDT layer = tf.nn.relu(tf.matmul(layer, w[i]) + b[i]) + IDT IDT IDT IDT return layer + IDT IDT IDT h_drop = tf.nn.dropout(h, self.dropout_placeholder) + IDT IDT IDT layers = hidden_layers(h_drop) + IDT IDT IDT pred = tf.matmul(layers, U) + b2 + IDT IDT IDT if self.config.l2_beta: + IDT IDT IDT IDT for i in range(FLAGS.hidden): + IDT IDT IDT IDT IDT self.config.l2_loss += tf.nn.l2_loss(w[i]) + tf.nn.l2_loss(b[i]) + IDT IDT else: + IDT IDT IDT h_drop = tf.nn.dropout(h, self.dropout_placeholder) + IDT IDT IDT pred = tf.matmul(h_drop, U) + b2 + IDT IDT return pred + + IDT def add_loss_op(self, pred): + IDT IDT """Adds Ops for the loss function to the computational graph. + + IDT IDT In this case we are using cross entropy loss. The loss should be + IDT IDT averaged over all examples in the current minibatch. + + IDT IDT Use tf.nn.softmax_cross_entropy_with_logits to simplify your + IDT IDT implementation. You might find tf.reduce_mean useful. + + IDT IDT Args: + IDT IDT IDT pred: + IDT IDT IDT IDT A tensor of shape (batch_size, n_classes) containing + IDT IDT IDT IDT the output of the neural network before the softmax layer. + IDT IDT Returns: + IDT IDT IDT loss: A 0-d tensor (scalar) + IDT IDT """ + IDT IDT loss = , logits= ) IDT IDT if self.config.l2_beta: + IDT IDT IDT loss = tf.reduce_mean(loss + self.config.l2_beta * self.config.l2_loss) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT """Sets up the training Ops. + + IDT IDT Creates an optimizer and applies the gradients to all trainable + IDT IDT variables. The Op returned by this function is what must be + IDT IDT passed to the `sess.run()` call to cause the model to train. + + IDT IDT Use tf.train.AdamOptimizer for this model. + IDT IDT Calling optimizer.minimize() will return a train_op object. + + IDT IDT Args: + IDT IDT IDT loss: Loss tensor, from cross_entropy_loss. + IDT IDT Returns: + IDT IDT IDT train_op: The Op for training. + IDT IDT """ + IDT IDT ) + IDT IDT if FLAGS.optimizer == 'adagrad': + IDT IDT IDT optimizer = tf.train.AdagradOptimizer(self.config.lr) + IDT IDT else: + IDT IDT IDT optimizer = tf.train.AdamOptimizer(self.config.lr) + IDT IDT train_op = , global_step= ) IDT IDT return train_op + + IDT def fit_batch(self, word_id_batch, tag_id_batch, deprel_id_batch, class_batch): + IDT IDT feed = , tag_id_batch, deprel_id_batch, class_batch= , dropout= ) IDT IDT _, loss = , self.loss], feed_dict= ) IDT IDT return loss + + IDT def fit_epoch(self, train_data, batch_size= , incl_progbar= ) IDT IDT + IDT IDT if incl_progbar: + IDT IDT IDT progbar = ) IDT IDT for (word_id_batch, tag_id_batch, deprel_id_batch), class_batch in train_data: + IDT IDT IDT loss = self.fit_batch(word_id_batch, tag_id_batch, deprel_id_batch, class_batch) + IDT IDT IDT if incl_progbar: + IDT IDT IDT IDT progbar.add(word_id_batch.shape[0], [('Cross-entropy', loss)]) + + IDT def predict_on_batch(self, inputs_batch): + IDT IDT feed = self.create_feed_dict(*inputs_batch) + IDT IDT predictions = , feed_dict= ) IDT IDT return predictions + + IDT def predict(self, partial_parses): + IDT IDT + IDT IDT feats = self.transducer.pps2feats(partial_parses) + IDT IDT td_vecs = self.predict_on_batch(feats) + IDT IDT preds = [self.transducer.td_vec2trans_deprel(td_vec) for td_vec in td_vecs] + IDT IDT return preds + + IDT def eval(self, sentences, ex_arcs): + IDT IDT + IDT IDT act_arcs = minibatch_parse(sentences, self, self.config.batch_size) + IDT IDT ex_arcs = tuple([(a[0], a[1], self.transducer.id2deprel[a[2]]) for a in pp] for pp in ex_arcs) + IDT IDT if FLAGS.output: + IDT IDT IDT import json + IDT IDT IDT with open(FLAGS.output, 'w+') as f: + IDT IDT IDT IDT for row in act_arcs: + IDT IDT IDT IDT IDT f.write('%s\n' % json.dumps(row)) + IDT IDT return score_arcs(act_arcs, ex_arcs) + + IDT def __init__(self, transducer, sess, config, word_embeddings): + IDT IDT self.transducer = transducer + IDT IDT self.sess = sess + IDT IDT self.word_embeddings = word_embeddings + IDT IDT self.config = config + IDT IDT self.build() + + +def main(debug): + IDT """Main function + + IDT Args: + IDT debug : + IDT IDT whether to use a fraction of the data. Make sure to set to False + IDT IDT when you're ready to train your model for real! + IDT """ + IDT + IDT + IDT + IDT config = Config() + IDT data = ) IDT transducer, word_embeddings, train_data = data[:3] + IDT dev_sents, dev_arcs = data[3:5] + IDT test_sents, test_arcs = data[5:] + IDT config.n_word_ids = len(transducer.id2word) + 1 + IDT config.n_tag_ids = len(transducer.id2tag) + 1 + IDT config.n_deprel_ids = len(transducer.id2deprel) + 1 + IDT config.embed_size = word_embeddings.shape[1] + IDT for (word_batch, tag_batch, deprel_batch), td_batch in train_data.get_iterator(shuffled= ) IDT IDT config.n_word_features = word_batch.shape[-1] + IDT IDT config.n_tag_features = tag_batch.shape[-1] + IDT IDT config.n_deprel_features = deprel_batch.shape[-1] + IDT IDT config.n_classes = td_batch.shape[-1] + IDT IDT break + IDT ) + IDT if debug: + IDT IDT dev_sents = dev_sents[:500] + IDT IDT dev_arcs = dev_arcs[:500] + IDT IDT test_sents = test_sents[:500] + IDT IDT test_arcs = test_arcs[:500] + IDT if not debug: + IDT IDT weight_file = ) IDT with tf.Graph().as_default(), tf.Session() as session: + IDT IDT + IDT IDT start = time.time() + IDT IDT model = ParserModel(transducer, session, config, word_embeddings) + IDT IDT - start)) + IDT IDT init = tf.global_variables_initializer() + IDT IDT session.run(init) + IDT IDT saver = None if debug else tf.train.Saver() + IDT IDT + IDT IDT + IDT IDT + IDT IDT best_las = 0.0 + IDT IDT for epoch in range(config.n_epochs): + IDT IDT IDT ) + IDT IDT IDT if debug: + IDT IDT IDT IDT model.fit_epoch(list(islice(train_data, 3)), config.batch_size) + IDT IDT IDT else: + IDT IDT IDT IDT model.fit_epoch(train_data) + IDT IDT IDT stdout.flush() + IDT IDT IDT dev_las, dev_uas = model.eval(dev_sents, dev_arcs) + IDT IDT IDT best = dev_las > best_las + IDT IDT IDT if best: + IDT IDT IDT IDT best_las = dev_las + IDT IDT IDT IDT if not debug: + IDT IDT IDT IDT IDT saver.save(session, weight_file.name) + IDT IDT IDT + IDT IDT IDT , ' if best else ', ')) + IDT IDT IDT + IDT IDT IDT ) + IDT IDT if not debug: + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT saver.restore(session, weight_file.name) + IDT IDT IDT stdout.flush() + IDT IDT IDT las, uas = model.eval(test_sents, test_arcs) + IDT IDT IDT if las: + IDT IDT IDT IDT + IDT IDT IDT IDT , end=', ') + IDT IDT IDT + IDT IDT IDT ) + IDT IDT IDT + IDT return 0 + + +if __name__ == '__main__': + IDT main(False) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import random +import tensorflow as tf +import numpy as np +import gym +actor_network_verbose = False +update_gradients_verbose = False +actor_learning_rate = 0.0003 +critic_learning_rate = 0.00025 +n_episode = 1500 +gamma = 0.99 +num_tests = 50 +observation_placeholder = , shape= , 4]) )W1_actor = , shape= , 64]) )b1_actor = , shape= )W2_actor = , shape= , 64]) )b2_actor = , shape= )W3_actor = , shape= , 2]) )b3_actor = , shape= )hidden1_actor = tf.nn.relu(tf.matmul(observation_placeholder, W1_actor) + b1_actor) +hidden2_actor = tf.nn.relu(tf.matmul(hidden1_actor, W2_actor) + b2_actor) +scores = tf.nn.relu(tf.matmul(hidden2_actor, W3_actor) + b3_actor) +probs = tf.nn.softmax(scores) +discount_placeholder = tf.placeholder(tf.float32) +gain_placeholder = tf.placeholder(tf.float32) +action_placeholder = , shape= , 2]) )log_probs = tf.log(probs) * action_placeholder +grads_actor = tf.gradients(log_probs, [W1_actor, b1_actor, W2_actor, b2_actor, W3_actor, b3_actor]) +update_W1_actor = W1_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[0]) +update_b1_actor = b1_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[1]) +update_W2_actor = W2_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[2]) +update_b2_actor = b2_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[3]) +update_W3_actor = W3_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[4]) +update_b3_actor = b3_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[5]) +W1_critic = , shape= , 64]) )b1_critic = , shape= )W2_critic = , shape= , 64]) )b2_critic = , shape= )W3_critic = , shape= , 1]) )b3_critic = , shape= )hidden1_critic = tf.nn.relu(tf.matmul(observation_placeholder, W1_critic) + b1_critic) +hidden2_critic = tf.nn.relu(tf.matmul(hidden1_critic, W2_critic) + b2_critic) +values = tf.matmul(hidden2_critic, W3_critic) + b3_critic +grads_critic = tf.gradients(values, [W1_critic, b1_critic, W2_critic, b2_critic, W3_critic, b3_critic]) +update_W1_critic = W1_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[0]) +update_b1_critic = b1_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[1]) +update_W2_critic = W2_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[2]) +update_b2_critic = b2_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[3]) +update_W3_critic = W3_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[4]) +update_b3_critic = b3_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[5]) + + +def actor_network(observation, session, verbose= ) IDT probabilities = , feed_dict= ) IDT if verbose: + IDT IDT + IDT if random.random() <= probabilities[0][0]: + IDT IDT return 0 + IDT else: + IDT IDT return 1 + + +def update_actor_gradients(discount, gain, observation, action, session, verbose= ) IDT if action == 0: + IDT IDT mask = [[1, 0]] + IDT else: + IDT IDT mask = [[0, 1]] + IDT result = , log_probs, grads_actor, update_W1_actor, update_b1_actor, update_W2_actor, update_b2_actor, update_W3_actor, update_b3_actor], feed_dict= , discount_placeholder: discount, gain_placeholder: gain, action_placeholder: mask}) ) IDT if verbose: + IDT IDT + + +def update_critic_gradients(gain, observation, session, verbose= ) IDT result = , update_W1_critic, update_b1_critic, update_W2_critic, update_b2_critic, update_W3_critic, update_b3_critic], feed_dict= , gain_placeholder: gain}) ) IDT if verbose: + IDT IDT + + +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT sess.run(init) + IDT env = gym.make('CartPole-v1') + IDT + IDT for episode in range(n_episode): + IDT IDT observation = env.reset() + IDT IDT reward = 0 + IDT IDT done = False + IDT IDT total_reward = 0.0 + IDT IDT memory = [] + IDT IDT discount = 1.0 + IDT IDT step = 0 + IDT IDT while not done: + IDT IDT IDT action = , sess, verbose= ) IDT IDT IDT next_observation, next_reward, next_done, _ = env.step(action) + IDT IDT IDT if next_done: + IDT IDT IDT IDT next_reward = -100 + IDT IDT IDT gain_with_critic = , feed_dict= ) IDT IDT IDT if not next_done: + IDT IDT IDT IDT gain_with_critic += , feed_dict= ) IDT IDT IDT update_actor_gradients(discount= , gain= , observation= , action= , session= , verbose= ) IDT IDT IDT update_critic_gradients(gain= , observation= , session= , verbose= ) IDT IDT IDT discount *= gamma + IDT IDT IDT total_reward += next_reward + IDT IDT IDT observation, reward, done = next_observation, next_reward, next_done + IDT IDT IDT step += 1 + IDT IDT ) + IDT + IDT + IDT input('Press Enter to start testing!') + IDT + IDT rewards_sum = 0.0 + IDT for episode in range(num_tests): + IDT IDT observation = env.reset() + IDT IDT reward = 0 + IDT IDT done = False + IDT IDT total_reward = 0.0 + IDT IDT while not done: + IDT IDT IDT env.render() + IDT IDT IDT action = actor_network(observation, sess) + IDT IDT IDT next_observation, next_reward, next_done, _ = env.step(action) + IDT IDT IDT total_reward += next_reward + IDT IDT IDT observation, reward, done = next_observation, next_reward, next_done + IDT IDT + IDT IDT rewards_sum += total_reward + IDT + IDT + IDT env.close() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +Created on Tue Aug 22 13:23:07 2017 +Chapter 5 +basic of tensorflow +@author: gihun +""" +import numpy as np +import tensorflow as tf +np.set_printoptions(linewidth= , threshold= )""" +basic concept and principle +@ graph and session +graph: define skeleton "cast" (only sturcture declaration) for "metal" (real memory in GPU) + IDT IDT building graph is "complile process of the structure" +session: object managing "metal" (real memory in GPU) + IDT IDT calling session.run is "runtime stage of the structure + +@ there is 3 type of tensor object +1. placeholder : interface for CPU --> GPU memory copy. it serves as gateway from numpy value to tensor value + IDT IDT placeholder is used for input. you must "feed" the input value (numpy value) to placebolder + IDT IDT when you evaluate tensor that depnds on the placeholder. +2. Variable : it holds its own GPU memory. it is not temporary. + IDT IDT Variable is used as weights in network. It is the only we can save after training. +3. Tensor : it holds temporary GPU memory. tensor exists in order to represent intermediate result + IDT IDT The type of all intermediate result is tensor. +ex) + IDT a : placeholder + IDT b : variable + IDT c = a+b --> c is tensor IDT +""" +tf.reset_default_graph() +tf.ones([3, 3]) +tf.zeros([3, 3]) +x = , dtype= , shape= , 3]) )w = , initializer= , 3], dtype= )y = x + w +sess = tf.Session() +sess.run(tf.global_variables_initializer()) +val = , feed_dict= , 3], dtype= ) + + +class Eval: + + IDT def __init__(self): + IDT IDT self.sess = tf.Session() + + IDT def global_var_init(self): + IDT IDT self.sess.run(tf.global_variables_initializer()) + + IDT def __call__(self, x, feed_dict= ) IDT IDT return self.sess.run(x, feed_dict) + + +tf.reset_default_graph() +x = , dtype= , shape= , 3, 3, 1]) )feed_dict = {x: np.ones([1, 3, 3, 1], np.float32)} +e = Eval() +.as_list()) +) +, feed_dict)) +tf.reset_default_graph() +x2 = , dtype= , shape= , 3, 3, 1]) )feed_dict = {x2: np.ones([2, 3, 3, 1], np.float32)} +e = Eval() +.as_list()) +) +, feed_dict)) +tf.reset_default_graph() +x3 = , dtype= , shape= , None, None, None]) )feed_dict = {x3: np.ones([4, 10, 10, 3], np.float32)} +e = Eval() +.as_list()) +) +, feed_dict)) +tf.reset_default_graph() +x = , dtype= , shape= , 3, 3, 1]) )x_ = np.reshape(np.arange(0, 27, 1, np.float32), [3, 3, 3, 1]) +feed_dict = {x: x_} +e = Eval() +, feed_dict)) +, feed_dict)) +) +, feed_dict)) +) +, feed_dict)) +) +, feed_dict)) +) +, feed_dict)) +) +tf.reset_default_graph() +x = , dtype= , shape= , 3]) )x_ = np.reshape(np.arange(0, 9, 1, np.float32), [3, 3]) +feed_dict = {x: x_} +e = Eval() + +) +, feed_dict)) +) +, feed_dict)) +.get_shape().as_list()) +) +tf.reset_default_graph() +x = , dtype= , shape= , 3]) )y = , dtype= , shape= , 3]) )x_ = np.reshape(np.arange(0, 9, 1, np.float32), [3, 3]) +y_ = np.reshape(np.arange(0, 9, 1, np.float32), [3, 3]) +feed_dict = {x: x_, y: y_} +e = Eval() + + +, feed_dict)) +) +, feed_dict)) +) +, feed_dict)) +for a in e(tf.unstack(x, axis= , feed_dict) ) IDT +, feed_dict)) +for a in e(tf.split(x, 3, axis= , feed_dict) ) IDT +) +tf.reset_default_graph() +x = , dtype= , shape= , None, None, 1]) )w = , dtype= , shape= , 3, 1, 3]) )b = , dtype= , shape= )conv = , w, strides= , 1, 1, 1], padding= , name= )add = conv + b +relu = , name= )pool = , [1, 2, 2, 1], strides= , 2, 2, 1], padding= , name= )e = Eval() +x_ = np.tile(-np.identity(3, np.float32), [3, 3])[(None), :, :, (None)] +x_.shape +w_ = np.array([[1, 1, 1], [0, 1, 0], [0, 0, 0]], np.float32)[:, :, (None), (None)] +w_ = , axis= )w_.shape +feed_dict = {x: x_, w: w_, b: np.array([1.5] * 3, np.float32)} +, :, :, (0)]) +, (0)]) +, feed_dict)) +[(0), :, :, (0)]) +[(0), :, :, (0)]) +[(0), :, :, (0)]) +[(0), :, :, (0)]) +) +, feed_dict)) +) +, feed_dict)) +tf.reset_default_graph() +x = , dtype= , shape= , 10]) )w = , dtype= , shape= , 10]) )b = , dtype= , shape= )full = , w), b, name= )x_ = np.ones([3, 10], np.float32) +w_ = np.ones([10, 10], np.float32) +feed_dict = {x: x_, w: w_, b: [0.5] * 10} +e = Eval() + + +) +tf.reset_default_graph() +x = , dtype= , shape= , None, None, 1]) )w = , dtype= , shape= , 3, 3, 1]) )b = , dtype= , shape= )conv = , w, strides= , 1, 1, 1], output_shape= , 5, 5, 3], padding= , name= )add = conv + b +relu = , name= )e = Eval() +x_ = -np.identity(5, np.float32)[(None), :, :, (None)] +w_ = np.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]], np.float32)[:, :, (None), (None)] +w_ = , axis= )feed_dict = {x: x_, w: w_, b: np.array([0.5] * 3, np.float32)} +, :, :, (0)]) +, (0)]) +[(0), :, :, (0)]) +[(0), :, :, (0)]) +[(0), :, :, (0)]) +""" +conv2d_transpose needs output_shape and it should be static shape. +so I recomend you not to use conv2d_transpose, but conv2d. +""" +tf.reset_default_graph() +x3 = , dtype= , shape= , None, None, None, 1]) )w3 = , dtype= , shape= , 3, 3, 1, 3]) )b3 = , dtype= , shape= )conv3d = , w3, strides= , 1, 1, 1, 1], padding= , name= )add = conv3d + b3 +relu = , name= )e = Eval() +x_ = -np.zeros([5, 5, 5])[(None), :, :, :, (None)] +for i in range(5): + IDT x_[0, i, i, i, 0] = -1 +w_ = np.zeros([3, 3, 3])[:, :, :, (None), (None)] +w_[1, 1, 1, 0, 0] = 1 +w_[0, 1, 1, 0, 0] = 1 +w_ = , axis= )feed_dict = {x3: x_, w3: w_, b3: np.array([0.5] * 3, np.float32)} +, :, :, :, (0)]) +, (0)]) +[(0), :, :, :, (0)]) +[(0), :, :, :, (0)]) +[(0), :, :, :, (0)]) +tf.reset_default_graph() +x = , 3], dtype= )e = Eval() +e.global_var_init() +e(x) +""" +variable must initialize! if not, error : Attemptimg to use uninitialized value Variable +On the other hand, in case of placeholder, we must use it with feed_dict(like initializing) +""" +tf.shape +y.get_shape() +y.get_shape().as_list() +tf.reduce_sum +tf.reduce_mean +tf.reduce_prod +tf.reshape +tf.tile +tf.concat +tf.stack +tf.unstack +tf.nn.relu +tf.nn.conv2d +tf.nn.conv2d_transpose +tf.nn.conv3d +tf.nn.conv3d_transpose +tf.nn.max_pool +tf.nn.moments +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import math as math +pickle_file = 'data/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +lambd = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT reg_loss = tf.nn.l2_loss(weights) + IDT loss = tf.reduce_mean(loss + lambd * reg_loss) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_units = 1024 +lambd = 0.01 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT biases1 = tf.Variable(tf.zeros([hidden_units])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT parameters = {'W1': weights1, 'b1': biases1, 'W2': weights2, 'b2': biases2} + + IDT def forward_propagation(tf_train_dataset, parameters): + IDT IDT weights1 = parameters['W1'] + IDT IDT weights2 = parameters['W2'] + IDT IDT biases1 = parameters['b1'] + IDT IDT biases2 = parameters['b2'] + IDT IDT Z1 = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT A1 = tf.nn.relu(Z1) + IDT IDT logits = tf.matmul(A1, weights2) + biases2 + IDT IDT return logits + IDT logits = forward_propagation(tf_train_dataset, parameters) + IDT loss = , logits= ) IDT reg_loss = tf.nn.l2_loss(parameters['W1']) + tf.nn.l2_loss(parameters['W2']) + IDT loss = tf.reduce_mean(loss + lambd * reg_loss) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(forward_propagation(tf_valid_dataset, parameters)) + IDT test_prediction = tf.nn.softmax(forward_propagation(tf_test_dataset, parameters)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_units = 1024 +lambd = 0.01 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + IDT biases1 = tf.Variable(tf.zeros([hidden_units])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT parameters = {'W1': weights1, 'b1': biases1, 'W2': weights2, 'b2': biases2} + + IDT def forward_propagation(tf_train_dataset, parameters): + IDT IDT weights1 = parameters['W1'] + IDT IDT weights2 = parameters['W2'] + IDT IDT biases1 = parameters['b1'] + IDT IDT biases2 = parameters['b2'] + IDT IDT Z1 = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT A1 = tf.nn.relu(Z1) + IDT IDT loss = , keep_prob= , noise_shape= , seed= ) IDT IDT logits = tf.matmul(A1, weights2) + biases2 + IDT IDT return logits + IDT logits = forward_propagation(tf_train_dataset, parameters) + IDT loss = , logits= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(forward_propagation(tf_valid_dataset, parameters)) + IDT test_prediction = tf.nn.softmax(forward_propagation(tf_test_dataset, parameters)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +beta = 0.001 +hidden_nodes_1 = 1024 +hidden_nodes_2 = int(hidden_nodes_1 * 0.5) +hidden_nodes_3 = int(hidden_nodes_1 * np.power(0.5, 2)) +hidden_nodes_4 = int(hidden_nodes_1 * np.power(0.5, 3)) +hidden_nodes_5 = int(hidden_nodes_1 * np.power(0.5, 4)) +graph = tf.Graph() +with graph.as_default(): + IDT + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_1 = , hidden_nodes_1], stddev= ) IDT biases_1 = tf.Variable(tf.zeros([hidden_nodes_1])) + IDT weights_2 = , hidden_nodes_2], stddev= ) IDT biases_2 = tf.Variable(tf.zeros([hidden_nodes_2])) + IDT weights_3 = , hidden_nodes_3], stddev= ) IDT biases_3 = tf.Variable(tf.zeros([hidden_nodes_3])) + IDT weights_4 = , hidden_nodes_4], stddev= ) IDT biases_4 = tf.Variable(tf.zeros([hidden_nodes_4])) + IDT weights_5 = , hidden_nodes_5], stddev= ) IDT biases_5 = tf.Variable(tf.zeros([hidden_nodes_5])) + IDT weights_6 = , num_labels], stddev= ) IDT biases_6 = tf.Variable(tf.zeros([num_labels])) + IDT + IDT logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + IDT hidden_layer_1 = tf.nn.relu(logits_1) + IDT keep_prob = tf.placeholder('float') + IDT hidden_layer_1_dropout = tf.nn.dropout(hidden_layer_1, keep_prob) + IDT logits_2 = tf.matmul(hidden_layer_1_dropout, weights_2) + biases_2 + IDT hidden_layer_2 = tf.nn.relu(logits_2) + IDT hidden_layer_2_dropout = tf.nn.dropout(hidden_layer_2, keep_prob) + IDT logits_3 = tf.matmul(hidden_layer_2_dropout, weights_3) + biases_3 + IDT hidden_layer_3 = tf.nn.relu(logits_3) + IDT hidden_layer_3_dropout = tf.nn.dropout(hidden_layer_3, keep_prob) + IDT logits_4 = tf.matmul(hidden_layer_3_dropout, weights_4) + biases_4 + IDT hidden_layer_4 = tf.nn.relu(logits_4) + IDT hidden_layer_4_dropout = tf.nn.dropout(hidden_layer_4, keep_prob) + IDT logits_5 = tf.matmul(hidden_layer_4_dropout, weights_5) + biases_5 + IDT hidden_layer_5 = tf.nn.relu(logits_5) + IDT hidden_layer_5_dropout = tf.nn.dropout(hidden_layer_5, keep_prob) + IDT logits_6 = tf.matmul(hidden_layer_5_dropout, weights_6) + biases_6 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_6, tf_train_labels)) + IDT + IDT optimizer = tf.train.AdamOptimizer(0.005).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits_6) + IDT valid_logits_1 = tf.matmul(tf_valid_dataset, weights_1) + biases_1 + IDT valid_relu_1 = tf.nn.relu(valid_logits_1) + IDT valid_logits_2 = tf.matmul(valid_relu_1, weights_2) + biases_2 + IDT valid_relu_2 = tf.nn.relu(valid_logits_2) + IDT valid_logits_3 = tf.matmul(valid_relu_2, weights_3) + biases_3 + IDT valid_relu_3 = tf.nn.relu(valid_logits_3) + IDT valid_logits_4 = tf.matmul(valid_relu_3, weights_4) + biases_4 + IDT valid_relu_4 = tf.nn.relu(valid_logits_4) + IDT valid_logits_5 = tf.matmul(valid_relu_4, weights_5) + biases_5 + IDT valid_relu_5 = tf.nn.relu(valid_logits_5) + IDT valid_logits_6 = tf.matmul(valid_relu_5, weights_6) + biases_6 + IDT valid_prediction = tf.nn.softmax(valid_logits_6) + IDT test_logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + IDT test_relu_1 = tf.nn.relu(test_logits_1) + IDT test_logits_2 = tf.matmul(test_relu_1, weights_2) + biases_2 + IDT test_relu_2 = tf.nn.relu(test_logits_2) + IDT test_logits_3 = tf.matmul(test_relu_2, weights_3) + biases_3 + IDT test_relu_3 = tf.nn.relu(test_logits_3) + IDT test_logits_4 = tf.matmul(test_relu_3, weights_4) + biases_4 + IDT test_relu_4 = tf.nn.relu(test_logits_4) + IDT test_logits_5 = tf.matmul(test_relu_4, weights_5) + biases_5 + IDT test_relu_5 = tf.nn.relu(test_logits_5) + IDT test_logits_6 = tf.matmul(test_relu_5, weights_6) + biases_6 + IDT test_prediction = tf.nn.softmax(test_logits_6) +num_steps = 15000 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT )) + IDT IDT IDT , valid_labels))) + IDT , test_labels))) +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import logz +import time +import math + + +class Config(object): + IDT n_features = 11 + IDT n_classes = 3 + IDT dropout = 0.5 + IDT hidden_size_1 = 128 + IDT hidden_size_2 = 256 + IDT hidden_size_3 = 64 + IDT batch_size = 256 + IDT lr = 0.0005 + IDT itera = 20 + IDT train_itera = 20 + IDT envname = 'Hopper-v1' + IDT max_steps = 1000 + + +class NN(object): + + IDT def add_placeholders(self): + IDT IDT self.input_placeholder = , shape= , Config.n_features), name= ) IDT IDT self.labels_placeholder = , shape= , Config.n_classes), name= ) IDT IDT self.dropout_placeholder = , name= ) IDT IDT self.is_training = tf.placeholder(tf.bool) + + IDT def create_feed_dict(self, inputs_batch, labels_batch= , dropout= , is_training= ) IDT IDT if labels_batch is None: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT else: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT return feed_dict + + IDT def add_prediction_op(self): + IDT IDT self.global_step = tf.Variable(0) + IDT IDT with tf.name_scope('layer1'): + IDT IDT IDT hidden1 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer2'): + IDT IDT IDT hidden2 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer3'): + IDT IDT IDT hidden3 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('output'): + IDT IDT IDT pred = , num_outputs= , activation_fn= ) IDT IDT return pred + + IDT def add_loss_op(self, pred): + IDT IDT loss = , labels= ) IDT IDT tf.summary.scalar('loss', loss) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + IDT IDT with tf.control_dependencies(extra_update_ops): + IDT IDT IDT learning_rate = , self.global_step, 1000, 0.8, staircase= ) IDT IDT IDT train_op = , global_step= ) IDT IDT return train_op + + IDT def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + IDT IDT feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + IDT IDT rs, _, loss = , self.train_op, self.loss], feed_dict= ) IDT IDT train_writer.add_summary(rs, i) + IDT IDT return loss + + IDT def __init__(self, config): + IDT IDT self.config = config + IDT IDT self.build() + + IDT def fit(self, sess, train_x, train_y): + IDT IDT loss = self.train_on_batch(sess, train_x, train_y) + + IDT def build(self): + IDT IDT with tf.name_scope('inputs'): + IDT IDT IDT self.add_placeholders() + IDT IDT with tf.name_scope('predict'): + IDT IDT IDT self.pred = self.add_prediction_op() + IDT IDT with tf.name_scope('loss'): + IDT IDT IDT self.loss = self.add_loss_op(self.pred) + IDT IDT with tf.name_scope('train'): + IDT IDT IDT self.train_op = self.add_training_op(self.loss) + + IDT def get_pred(self, sess, inputs_batch): + IDT IDT feed = , dropout= , is_training= ) IDT IDT p = , feed_dict= ) IDT IDT return p + + +def load(path): + IDT all = np.load(path) + IDT X = all['arr_0'] + IDT y = all['arr_1'] + IDT y1 = y.reshape(y.shape[0], y.shape[2]) + IDT return X, y1 + + +def main(): + IDT PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + IDT train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + IDT train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + IDT logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_BC_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + IDT X_train, y_train = load(train_path) + IDT + IDT + IDT with tf.Graph().as_default(): + IDT IDT config = Config() + IDT IDT nn = NN(config) + IDT IDT init = tf.global_variables_initializer() + IDT IDT saver = , keep_checkpoint_every_n_hours= ) IDT IDT shuffle_batch_x, shuffle_batch_y = , y_train], batch_size= , capacity= , min_after_dequeue= , enqueue_many= ) IDT IDT with tf.Session() as session: + IDT IDT IDT merged = tf.summary.merge_all() + IDT IDT IDT train_writer = tf.summary.FileWriter(train_log_path, session.graph) + IDT IDT IDT session.run(init) + IDT IDT IDT coord = tf.train.Coordinator() + IDT IDT IDT threads = tf.train.start_queue_runners(session, coord) + IDT IDT IDT for j in tqdm.tqdm(range(Config.itera)): + IDT IDT IDT IDT i = 0 + IDT IDT IDT IDT try: + IDT IDT IDT IDT IDT for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + IDT IDT IDT IDT IDT IDT batch_x, batch_y = session.run([shuffle_batch_x, shuffle_batch_y]) + IDT IDT IDT IDT IDT IDT loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + IDT IDT IDT IDT IDT IDT i += 1 + IDT IDT IDT IDT IDT IDT if i % 1000 == 0: + IDT IDT IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT IDT IDT saver.save(session, os.path.join(PROJECT_ROOT, 'model/model_ckpt'), global_step= ) IDT IDT IDT IDT except tf.errors.OutOfRangeError: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT finally: + IDT IDT IDT IDT IDT coord.request_stop() + IDT IDT IDT IDT coord.join(threads) + IDT IDT IDT IDT env = gym.make(Config.envname) + IDT IDT IDT IDT rollouts = 20 + IDT IDT IDT IDT returns = [] + IDT IDT IDT IDT for _ in range(rollouts): + IDT IDT IDT IDT IDT obs = env.reset() + IDT IDT IDT IDT IDT done = False + IDT IDT IDT IDT IDT totalr = 0.0 + IDT IDT IDT IDT IDT steps = 0 + IDT IDT IDT IDT IDT while not done: + IDT IDT IDT IDT IDT IDT action = nn.get_pred(session, obs[(None), :]) + IDT IDT IDT IDT IDT IDT obs, r, done, _ = env.step(action) + IDT IDT IDT IDT IDT IDT totalr += r + IDT IDT IDT IDT IDT IDT steps += 1 + IDT IDT IDT IDT IDT IDT if steps >= Config.max_steps: + IDT IDT IDT IDT IDT IDT IDT break + IDT IDT IDT IDT IDT returns.append(totalr) + IDT IDT IDT IDT logz.log_tabular('Iteration', j) + IDT IDT IDT IDT logz.log_tabular('AverageReturn', np.mean(returns)) + IDT IDT IDT IDT logz.log_tabular('StdReturn', np.std(returns)) + IDT IDT IDT IDT logz.dump_tabular() + + +if __name__ == '__main__': + IDT main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import sys +import numpy as np +import tensorflow as tf +from sklearn import cross_validation +from sklearn.cross_validation import KFold +from sklearn import metrics + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +class TextCNN(object): + + IDT def __init__(self, train_dataset, train_labels, valid_dataset, valid_labels, embeddings, vocabulary, l2_reg_lambda, num_steps, batch_size, num_filters, filter_sizes_1, filter_sizes_2, filter_sizes_3, dropout_keep_prob, lexical, shuffling): + IDT IDT vocab_size = len(vocabulary) + IDT IDT sequence_length = train_dataset.shape[1] + IDT IDT train_size = train_dataset.shape[0] + IDT IDT num_classes = 7 + IDT IDT filter_sizes = [filter_sizes_1, filter_sizes_2, filter_sizes_3] + IDT IDT num_filters_total = num_filters * len(filter_sizes) + IDT IDT embedding_size = embeddings.shape[1] + IDT IDT embeddings_number = embeddings.shape[0] + IDT IDT graph = tf.Graph() + IDT IDT with graph.as_default(): + IDT IDT IDT tf.set_random_seed(10) + IDT IDT IDT input_x = , shape= , sequence_length]) ) IDT IDT IDT input_y = , shape= , num_classes]) ) IDT IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT IDT tf_argmax_dataset = tf.constant(valid_dataset) + IDT IDT IDT reg_coef = tf.placeholder(tf.float32) + IDT IDT IDT l2_loss = tf.constant(0.0) + IDT IDT IDT weights_conv = , embedding_size, 1, num_filters], stddev= , seed= , filter_size in enumerate(filter_sizes) ) IDT IDT IDT biases_conv = , shape= ) IDT IDT IDT weight_output = , num_classes], stddev= , seed= ) IDT IDT IDT bias_output = , shape= ) IDT IDT IDT embeddings_const = , shape= , embedding_size]) ) IDT IDT IDT embeddings_tuned = tf.Variable(embeddings_const) + IDT IDT IDT embedded_chars = tf.nn.embedding_lookup(embeddings_tuned, input_x) + IDT IDT IDT embedded_chars_expanded = tf.expand_dims(embedded_chars, -1) + IDT IDT IDT embedded_chars_valid = tf.nn.embedding_lookup(embeddings_tuned, tf_valid_dataset) + IDT IDT IDT embedded_chars_expanded_valid = tf.expand_dims(embedded_chars_valid, -1) + IDT IDT IDT embedded_chars_argmax = tf.nn.embedding_lookup(embeddings_tuned, tf_argmax_dataset) + IDT IDT IDT embedded_chars_expanded_argmax = tf.expand_dims(embedded_chars_argmax, -1) + + IDT IDT IDT def model(data, dropout_prob): + IDT IDT IDT IDT pooled_outputs = [] + IDT IDT IDT IDT for i, filter_size in enumerate(filter_sizes): + IDT IDT IDT IDT IDT conv = , weights_conv[i], strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + IDT IDT IDT IDT IDT pooled = , ksize= , sequence_length - filter_size + 1, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT pooled_outputs.append(pooled) + IDT IDT IDT IDT h_pool = tf.concat(3, pooled_outputs) + IDT IDT IDT IDT h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + IDT IDT IDT IDT h_drop = tf.nn.dropout(h_pool_flat, dropout_prob) + IDT IDT IDT IDT scores = tf.nn.xw_plus_b(h_drop, weight_output, bias_output) + IDT IDT IDT IDT return scores + + IDT IDT IDT def model_argmax(data, dropout_prob): + IDT IDT IDT IDT argmaxs = [] + IDT IDT IDT IDT maximums = [] + IDT IDT IDT IDT pooled_outputs = [] + IDT IDT IDT IDT for i, filter_size in enumerate(filter_sizes): + IDT IDT IDT IDT IDT conv = , weights_conv[i], strides= , 1, 1, 1], padding= ) IDT IDT IDT IDT IDT h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + IDT IDT IDT IDT IDT maximum = tf.reduce_max(h, tf.to_int32(1)) + IDT IDT IDT IDT IDT maximums.append(maximum) + IDT IDT IDT IDT IDT argmax = tf.argmax(h, tf.to_int32(1)) + IDT IDT IDT IDT IDT argmaxs.append(argmax) + IDT IDT IDT IDT return argmaxs, maximums + IDT IDT IDT scores = model(embedded_chars_expanded, dropout_keep_prob) + IDT IDT IDT train_prediction = tf.nn.softmax(scores) + IDT IDT IDT losses = tf.nn.softmax_cross_entropy_with_logits(scores, tf.cast(input_y, tf.float32)) + IDT IDT IDT for i in range(len(weights_conv)): + IDT IDT IDT IDT l2_loss += tf.nn.l2_loss(weights_conv[i]) + IDT IDT IDT l2_loss += tf.nn.l2_loss(weight_output) + IDT IDT IDT loss = tf.reduce_mean(losses) + reg_coef * l2_loss + IDT IDT IDT global_step = , trainable= ) IDT IDT IDT optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss) + IDT IDT IDT argmaxs, maximums = model_argmax(embedded_chars_expanded_argmax, 1.0) + IDT IDT IDT maximum1 = maximums[0] + IDT IDT IDT maximum2 = maximums[1] + IDT IDT IDT maximum3 = maximums[2] + IDT IDT IDT argmax1 = argmaxs[0] + IDT IDT IDT argmax2 = argmaxs[1] + IDT IDT IDT argmax3 = argmaxs[2] + IDT IDT IDT valid_prediction = tf.nn.softmax(model(embedded_chars_expanded_valid, 1.0)) + IDT IDT with tf.Session(graph= ) IDT IDT IDT session.run(tf.initialize_all_variables(), feed_dict= ) IDT IDT IDT + IDT IDT IDT for step in range(num_steps): + IDT IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size] + IDT IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size] + IDT IDT IDT IDT feed_dict = {input_x: batch_data, input_y: batch_labels, reg_coef: l2_reg_lambda, embeddings_const: embeddings} + IDT IDT IDT IDT _, l, predictions, embeddings = session.run([optimizer, loss, train_prediction, embeddings_tuned], feed_dict) + IDT IDT IDT IDT if not step % 100: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT ) + IDT IDT IDT IDT IDT + IDT IDT IDT maximum1 = , feed_dict= ) IDT IDT IDT maximum1 = np.asarray(maximum1) + IDT IDT IDT maximum2 = , feed_dict= ) IDT IDT IDT maximum2 = np.asarray(maximum2) + IDT IDT IDT maximum3 = , feed_dict= ) IDT IDT IDT maximum3 = np.asarray(maximum3) + IDT IDT IDT argmax1 = , feed_dict= ) IDT IDT IDT argmax1 = np.asarray(argmax1) + IDT IDT IDT argmax2 = , feed_dict= ) IDT IDT IDT argmax2 = np.asarray(argmax2) + IDT IDT IDT argmax3 = , feed_dict= ) IDT IDT IDT argmax3 = np.asarray(argmax3) + IDT IDT IDT np.save('argmax_filter_sizes_1_tuned.npy', argmax1) + IDT IDT IDT np.save('argmax_filter_sizes_2_tuned.npy', argmax2) + IDT IDT IDT np.save('argmax_filter_sizes_3_tuned.npy', argmax3) + IDT IDT IDT np.save('maximum_filter_sizes_1_tuned.npy', maximum1) + IDT IDT IDT np.save('maximum_filter_sizes_2_tuned.npy', maximum2) + IDT IDT IDT np.save('maximum_filter_sizes_3_tuned.npy', maximum3) + IDT IDT IDT self.valid_predictions = , feed_dict= ) IDT IDT IDT self.valid_predictions = np.asarray(self.valid_predictions).reshape(valid_labels.shape) + IDT IDT IDT predictions_label = np.argmax(self.valid_predictions, 1) + IDT IDT IDT labels = ['coffeemachine', 'cutlery', 'microwave', 'toaster', 'trashcan', 'vacuum', 'washer'] + IDT IDT IDT self.prediction_labels_char = [labels[i] for i in predictions_label] + IDT IDT IDT self.prediction_labels_char = np.asarray(self.prediction_labels_char) + IDT IDT IDT np.save('gold_labels_tuned.npy', predictions_label) + IDT IDT IDT self.valid_accuracy = accuracy(self.valid_predictions, np.asarray(valid_labels)) + IDT IDT IDT self.embeddings_final = embeddings +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() + +import joker as act_tf +import tensorflow as ans_tf + +import numpy as np +rrr = ,)).astype(np.float32) )sss = ,)).astype(np.float32) ) + +def test_forward_conv(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , shape= , 3, 2, 5]) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2), w: np.arange(3 * 3 * 2 * 5).reshape(3, 3, 2, 5)}) ) IDT IDT return res + + +def test_forward_conv2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , shape= , 3, 2, 5]) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2), w: sss.reshape(3, 3, 2, 5)}) ) IDT IDT return res + + +def test_backward_conv(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = , dtype= , 3, 2, 5)) ) IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op, feed_dict= , 4, 4, 2)}) ) IDT IDT res = w.eval() + IDT IDT return res + + +def test_backward_conv2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = tf.Variable(sss.reshape(3, 3, 2, 5)) + IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT grad_W, = tf.gradients(loss, [w]) + IDT IDT res = , 4, 4, 2)}) ) IDT IDT return res + + +def test_backward_conv3(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT w = tf.Variable(sss.reshape(3, 3, 2, 5)) + IDT IDT conv1 = , w, strides= , 1, 1, 1], padding= ) IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT loss = tf.reduce_sum(conv1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT grad_W, = tf.gradients(loss, [x]) + IDT IDT res = , 4, 4, 2)}) ) IDT IDT return res + + +def test_forward_maxpool(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2)}) ) IDT IDT return res + + +def test_forward_maxpool2(tf): + IDT with tf.Session() as sess: + IDT IDT x = , shape= , 4, 4, 2]) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT res = , feed_dict= , 4, 4, 2)}) ) IDT IDT return res + + +def test_backward_maxpool(tf): + IDT with tf.Session() as sess: + IDT IDT x = , dtype= , 4, 4, 2)) ) IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT loss = tf.reduce_sum(pool1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op) + IDT IDT res = x.eval() + IDT IDT return res + + +def test_backward_maxpool2(tf): + IDT with tf.Session() as sess: + IDT IDT x = tf.Variable(rrr.reshape(3, 4, 4, 2)) + IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT loss = tf.reduce_sum(pool1) + IDT IDT train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT sess.run(train_op) + IDT IDT res = x.eval() + IDT IDT return res + + +if __name__ == '__main__': + IDT testcases = [test_forward_conv, test_forward_conv2, test_backward_conv, test_backward_conv2, test_backward_conv3, test_forward_maxpool2, test_backward_maxpool2] + IDT for item in testcases: + IDT IDT ans = item(ans_tf) + IDT IDT + IDT IDT + IDT IDT + IDT IDT res = item(act_tf) + IDT IDT np.testing.assert_allclose(res, ans, atol= ) IDT IDT + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import sys +import time +import numpy as np +import tensorflow as tf +from tensorflow.python.ops import gen_nn_ops +from tensorflow.python.ops import nn_ops +from tensorflow.python.ops import sparse_ops + + +class SparseXentTest(tf.test.TestCase): + + IDT def _npXent(self, features, labels): + IDT IDT features = np.reshape(features, [-1, features.shape[-1]]) + IDT IDT labels = np.reshape(labels, [-1]) + IDT IDT batch_dim = 0 + IDT IDT class_dim = 1 + IDT IDT batch_size = features.shape[batch_dim] + IDT IDT e = , axis= , [batch_size, 1])) ) IDT IDT probs = , axis= , [batch_size, 1]) ) IDT IDT labels_mat = np.zeros_like(probs).astype(probs.dtype) + IDT IDT labels_mat[np.arange(batch_size), labels] = 1.0 + IDT IDT bp = probs - labels_mat + IDT IDT l = , axis= ) IDT IDT return l, bp + + IDT def _testXent(self, np_features, np_labels): + IDT IDT np_loss, np_backprop = self._npXent(np_features, np_labels) + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(np_features, np_labels) + IDT IDT IDT tf_loss, tf_backprop = sess.run([loss, backprop]) + IDT IDT self.assertAllCloseAccordingToType(np_loss, tf_loss) + IDT IDT self.assertAllCloseAccordingToType(np_backprop, tf_backprop) + + IDT def testSingleClass(self): + IDT IDT for label_dtype in (np.int32, np.int64): + IDT IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT IDT loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(np.array([[1.0], [-1.0], [0.0]]).astype(np.float32), np.array([0, 0, 0]).astype(label_dtype)) + IDT IDT IDT IDT tf_loss, tf_backprop = sess.run([loss, backprop]) + IDT IDT IDT self.assertAllClose([0.0, 0.0, 0.0], tf_loss) + IDT IDT IDT self.assertAllClose([[0.0], [0.0], [0.0]], tf_backprop) + + IDT def testInvalidLabel(self): + IDT IDT features = [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]] + IDT IDT labels = [4, 3, 0, -1] + IDT IDT if tf.test.is_built_with_cuda() and tf.test.is_gpu_available(): + IDT IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT IDT loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(features, labels) + IDT IDT IDT IDT tf_loss, tf_backprop = sess.run([loss, backprop]) + IDT IDT IDT IDT self.assertAllClose([[np.nan] * 4, [0.25, 0.25, 0.25, -0.75], [-0.968, 0.087, 0.237, 0.6439], [np.nan] * 4], tf_backprop, rtol= , atol= ) IDT IDT IDT IDT self.assertAllClose([np.nan, 1.3862, 3.442, np.nan], tf_loss, rtol= , atol= ) IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(features, labels) + IDT IDT IDT with self.assertRaisesOpError('Received a label value of'): + IDT IDT IDT IDT sess.run([loss, backprop]) + + IDT def testNpXent(self): + IDT IDT features = [[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]] + IDT IDT labels = [3, 0] + IDT IDT np_loss, np_backprop = self._npXent(np.array(features), np.array(labels)) + IDT IDT self.assertAllClose(np.array([[0.25, 0.25, 0.25, -0.75], [-0.968, 0.087, 0.237, 0.6439]]), np_backprop, rtol= , atol= ) IDT IDT self.assertAllClose(np.array([1.3862, 3.442]), np_loss, rtol= , atol= ) + IDT def testShapeMismatch(self): + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT with self.assertRaisesRegexp(ValueError, '.*Rank mismatch:*'): + IDT IDT IDT IDT tf.nn.sparse_softmax_cross_entropy_with_logits([[0.0, 1.0], [2.0, 3.0], [2.0, 3.0]], [[0, 2]]) + + IDT def testScalar(self): + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT with self.assertRaisesRegexp(ValueError, '.*Logits cannot be scalars*'): + IDT IDT IDT IDT tf.nn.sparse_softmax_cross_entropy_with_logits(tf.constant(1.0), tf.constant(0)) + + IDT def testLabelsPlaceholderScalar(self): + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT labels = tf.placeholder(np.int32) + IDT IDT IDT y = tf.nn.sparse_softmax_cross_entropy_with_logits([[7.0]], labels) + IDT IDT IDT with self.assertRaisesOpError('labels must be 1-D'): + IDT IDT IDT IDT y.eval(feed_dict= ) + IDT def testVector(self): + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT loss = tf.nn.sparse_softmax_cross_entropy_with_logits(tf.constant([1.0]), tf.constant(0)) + IDT IDT IDT self.assertAllClose(0.0, loss.eval()) + + IDT def testFloat(self): + IDT IDT for label_dtype in (np.int32, np.int64): + IDT IDT IDT self._testXent(np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]).astype(np.float32), np.array([3, 0]).astype(label_dtype)) + + IDT def testDouble(self): + IDT IDT for label_dtype in (np.int32, np.int64): + IDT IDT IDT self._testXent(np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]).astype(np.float64), np.array([0, 3]).astype(label_dtype)) + + IDT def testHalf(self): + IDT IDT for label_dtype in (np.int32, np.int64): + IDT IDT IDT self._testXent(np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]).astype(np.float16), np.array([3, 0]).astype(label_dtype)) + + IDT def testEmpty(self): + IDT IDT self._testXent(np.zeros((0, 3)), np.zeros((0,), dtype= ) + IDT def testGradient(self): + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT l = , 0, 1], name= ) IDT IDT IDT f = , 0.2, 0.3, 0.4, 0.1, 0.4, 0.9, 1.6, 0.1, 0.8, 2.7, 6.4], shape= , 4], dtype= , name= ) IDT IDT IDT x = , l, name= ) IDT IDT IDT err = tf.test.compute_gradient_error(f, [3, 4], x, [3]) + IDT IDT + IDT IDT self.assertLess(err, 5e-08) + + IDT def testSecondGradient(self): + IDT IDT images_placeholder = , shape= , 2)) ) IDT IDT labels_placeholder = , shape= ) IDT IDT weights = , stddev= ) IDT IDT weights_with_zeros = , weights], axis= ) IDT IDT logits = tf.matmul(images_placeholder, weights_with_zeros) + IDT IDT cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels_placeholder) + IDT IDT loss = tf.reduce_mean(cross_entropy) + IDT IDT with self.assertRaisesRegexp(LookupError, '.*No gradient defined.*PreventGradient.*'): + IDT IDT IDT _ = tf.hessians(loss, [weights]) + + IDT def _testHighDim(self, features, labels): + IDT IDT np_loss, np_backprop = self._npXent(np.array(features), np.array(labels)) + IDT IDT np_loss = np.reshape(np_loss, np.array(labels).shape) + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT loss = tf.nn.sparse_softmax_cross_entropy_with_logits(features, labels) + IDT IDT IDT backprop = loss.op.inputs[0].op.outputs[1] + IDT IDT IDT tf_loss, tf_backprop = sess.run([loss, backprop]) + IDT IDT self.assertAllCloseAccordingToType(np_loss, tf_loss) + IDT IDT self.assertAllCloseAccordingToType(np_backprop, tf_backprop) + + IDT def testHighDim(self): + IDT IDT features = [[[1.0, 1.0, 1.0, 1.0]], [[1.0, 2.0, 3.0, 4.0]]] + IDT IDT labels = [[3], [0]] + IDT IDT self._testHighDim(features, labels) + + IDT def testHighDim2(self): + IDT IDT features = [[[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0]], [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]] + IDT IDT labels = [[3, 2], [0, 3]] + IDT IDT self._testHighDim(features, labels) + + IDT def testScalarHandling(self): + IDT IDT with self.test_session(use_gpu= ) IDT IDT IDT with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, '.*labels must be 1-D.*'): + IDT IDT IDT IDT labels = , shape= , 1]) ) IDT IDT IDT IDT logits = , shape= , 3]) ) IDT IDT IDT IDT ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, tf.squeeze(labels)) + IDT IDT IDT IDT labels_v2 = , 1), dtype= ) IDT IDT IDT IDT logits_v2 = np.random.randn(1, 3) + IDT IDT IDT IDT sess.run([ce], feed_dict= , logits: logits_v2}) ) + +def _sparse_vs_dense_xent_benchmark_dense(labels, logits): + IDT labels = tf.identity(labels) + IDT logits = tf.identity(logits) + IDT with tf.device('/cpu:0'): + IDT IDT batch_size = tf.shape(logits)[0] + IDT IDT num_entries = tf.shape(logits)[1] + IDT IDT length = batch_size * num_entries + IDT IDT labels += num_entries * tf.range(batch_size) + IDT IDT target = sparse_ops.sparse_to_dense(labels, tf.stack([length]), 1.0, 0.0) + IDT target = tf.reshape(target, tf.stack([-1, num_entries])) + IDT crossent = , target, name= ) IDT crossent_sum = tf.reduce_sum(crossent) + IDT grads = tf.gradients([crossent_sum], [logits])[0] + IDT return crossent_sum, grads + + +def _sparse_vs_dense_xent_benchmark_sparse(labels, logits): + IDT labels = labels.astype(np.int64) + IDT labels = tf.identity(labels) + IDT logits = tf.identity(logits) + IDT crossent = , labels, name= ) IDT crossent_sum = tf.reduce_sum(crossent) + IDT grads = tf.gradients([crossent_sum], [logits])[0] + IDT return crossent_sum, grads + + +def sparse_vs_dense_xent_benchmark(batch_size, num_entries, use_gpu): + IDT config = tf.ConfigProto() + IDT config.allow_soft_placement = True + IDT config.gpu_options.per_process_gpu_memory_fraction = 0.3 + IDT labels = , size= ) IDT logits = np.random.randn(batch_size, num_entries).astype(np.float32) + + IDT def _timer(sess, ops): + IDT IDT for _ in range(20): + IDT IDT IDT sess.run(ops) + IDT IDT start = time.time() + IDT IDT for _ in range(20): + IDT IDT IDT sess.run(ops) + IDT IDT end = time.time() + IDT IDT return (end - start) / 20.0 + IDT with tf.Session(config= ) IDT IDT if not use_gpu: + IDT IDT IDT with tf.device('/cpu:0'): + IDT IDT IDT IDT ops = _sparse_vs_dense_xent_benchmark_dense(labels, logits) + IDT IDT else: + IDT IDT IDT ops = _sparse_vs_dense_xent_benchmark_dense(labels, logits) + IDT IDT delta_dense = _timer(sess, ops) + IDT with tf.Session(config= ) IDT IDT if not use_gpu: + IDT IDT IDT with tf.device('/cpu:0'): + IDT IDT IDT IDT ops = _sparse_vs_dense_xent_benchmark_sparse(labels, logits) + IDT IDT else: + IDT IDT IDT ops = _sparse_vs_dense_xent_benchmark_sparse(labels, logits) + IDT IDT delta_sparse = _timer(sess, ops) + IDT ) + + +def main(_): + IDT + IDT \t dt(sparse) \t dt(sparse)/dt(dense)') + IDT for use_gpu in (False, True): + IDT IDT for batch_size in (32, 64, 128): + IDT IDT IDT for num_entries in (100, 1000, 10000): + IDT IDT IDT IDT sparse_vs_dense_xent_benchmark(batch_size, num_entries, use_gpu) + IDT IDT sparse_vs_dense_xent_benchmark(32, 100000, use_gpu) + IDT IDT sparse_vs_dense_xent_benchmark(8, 1000000, use_gpu) + + +if __name__ == '__main__': + IDT if '--benchmarks' in sys.argv: + IDT IDT sys.argv.remove('--benchmarks') + IDT IDT tf.app.run() + IDT else: + IDT IDT tf.test.main() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import load_policy +import math +import logz +import time + + +class Config(object): + IDT n_features = 17 + IDT n_classes = 6 + IDT dropout = 0.5 + IDT hidden_size_1 = 128 + IDT hidden_size_2 = 256 + IDT hidden_size_3 = 64 + IDT batch_size = 256 + IDT lr = 0.0005 + IDT itera = 20 + IDT train_itera = 20 + IDT envname = 'Walker2d-v1' + IDT max_steps = 1000 + + +class NN(object): + + IDT def add_placeholders(self): + IDT IDT self.input_placeholder = , shape= , Config.n_features), name= ) IDT IDT self.labels_placeholder = , shape= , Config.n_classes), name= ) IDT IDT self.dropout_placeholder = , name= ) IDT IDT self.is_training = tf.placeholder(tf.bool) + + IDT def create_feed_dict(self, inputs_batch, labels_batch= , dropout= , is_training= ) IDT IDT if labels_batch is None: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT else: + IDT IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT return feed_dict + + IDT def add_prediction_op(self): + IDT IDT self.global_step = tf.Variable(0) + IDT IDT with tf.name_scope('layer1'): + IDT IDT IDT hidden1 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer2'): + IDT IDT IDT hidden2 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('layer3'): + IDT IDT IDT hidden3 = , num_outputs= , activation_fn= ) IDT IDT with tf.name_scope('output'): + IDT IDT IDT pred = , num_outputs= , activation_fn= ) IDT IDT return pred + + IDT def add_loss_op(self, pred): + IDT IDT loss = , labels= ) IDT IDT tf.summary.scalar('loss', loss) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + IDT IDT with tf.control_dependencies(extra_update_ops): + IDT IDT IDT learning_rate = , self.global_step, 1000, 0.8, staircase= ) IDT IDT IDT train_op = , global_step= ) IDT IDT return train_op + + IDT def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + IDT IDT feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + IDT IDT rs, _, loss = , self.train_op, self.loss], feed_dict= ) IDT IDT train_writer.add_summary(rs, i) + IDT IDT return loss + + IDT def __init__(self, config): + IDT IDT self.config = config + IDT IDT self.build() + + IDT def fit(self, sess, train_x, train_y): + IDT IDT loss = self.train_on_batch(sess, train_x, train_y) + + IDT def build(self): + IDT IDT with tf.name_scope('inputs'): + IDT IDT IDT self.add_placeholders() + IDT IDT with tf.name_scope('predict'): + IDT IDT IDT self.pred = self.add_prediction_op() + IDT IDT with tf.name_scope('loss'): + IDT IDT IDT self.loss = self.add_loss_op(self.pred) + IDT IDT with tf.name_scope('train'): + IDT IDT IDT self.train_op = self.add_training_op(self.loss) + + IDT def get_pred(self, sess, inputs_batch): + IDT IDT feed = , dropout= , is_training= ) IDT IDT p = , feed_dict= ) IDT IDT return p + + +def load(path): + IDT all = np.load(path) + IDT X = all['arr_0'] + IDT y = all['arr_1'] + IDT y1 = y.reshape(y.shape[0], y.shape[2]) + IDT return X, y1 + + +def run_env(env, nn, session): + IDT obs = env.reset() + IDT done = False + IDT totalr = 0.0 + IDT steps = 0 + IDT observations = [] + IDT while not done: + IDT IDT action = nn.get_pred(session, obs[(None), :]) + IDT IDT observations.append(obs) + IDT IDT obs, r, done, _ = env.step(action) + IDT IDT totalr += r + IDT IDT steps += 1 + IDT IDT if steps >= Config.max_steps: + IDT IDT IDT break + IDT return totalr, observations + + +def shuffle(X_train, y_train): + IDT training_data = , y_train), axis= ) IDT np.random.shuffle(training_data) + IDT X = training_data[:, :-Config.n_classes] + IDT y = training_data[:, -Config.n_classes:] + IDT return X, y + + +def main(): + IDT PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + IDT train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + IDT policy_path = os.path.join(PROJECT_ROOT, 'experts/' + Config.envname + '.pkl') + IDT train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + IDT logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_DA_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + IDT X_train, y_train = load(train_path) + IDT + IDT + IDT with tf.Graph().as_default(): + IDT IDT config = Config() + IDT IDT nn = NN(config) + IDT IDT init = tf.global_variables_initializer() + IDT IDT saver = , keep_checkpoint_every_n_hours= ) IDT IDT + IDT IDT policy_fn = load_policy.load_policy(policy_path) + IDT IDT + IDT IDT with tf.Session() as session: + IDT IDT IDT merged = tf.summary.merge_all() + IDT IDT IDT train_writer = tf.summary.FileWriter(train_log_path, session.graph) + IDT IDT IDT session.run(init) + IDT IDT IDT coord = tf.train.Coordinator() + IDT IDT IDT threads = tf.train.start_queue_runners(session, coord) + IDT IDT IDT for j in tqdm.tqdm(range(Config.itera)): + IDT IDT IDT IDT X_train, y_train = shuffle(X_train, y_train) + IDT IDT IDT IDT i = 0 + IDT IDT IDT IDT try: + IDT IDT IDT IDT IDT for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + IDT IDT IDT IDT IDT IDT offset = i * Config.batch_size % X_train.shape[0] + IDT IDT IDT IDT IDT IDT batch_x = X_train[offset:offset + Config.batch_size, :] + IDT IDT IDT IDT IDT IDT batch_y = y_train[offset:offset + Config.batch_size] + IDT IDT IDT IDT IDT IDT loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + IDT IDT IDT IDT IDT IDT i += 1 + IDT IDT IDT IDT IDT + IDT IDT IDT IDT except tf.errors.OutOfRangeError: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT finally: + IDT IDT IDT IDT IDT coord.request_stop() + IDT IDT IDT IDT coord.join(threads) + IDT IDT IDT IDT observations = [] + IDT IDT IDT IDT actions = [] + IDT IDT IDT IDT env = gym.make(Config.envname) + IDT IDT IDT IDT for _ in range(10): + IDT IDT IDT IDT IDT _, o = run_env(env, nn, session) + IDT IDT IDT IDT IDT observations.extend(o) + IDT IDT IDT IDT IDT action = policy_fn(o) + IDT IDT IDT IDT IDT actions.extend(action) + IDT IDT IDT IDT new_x = np.array(observations) + IDT IDT IDT IDT new_y = np.array(actions) + IDT IDT IDT IDT X_train = np.concatenate((X_train, new_x)) + IDT IDT IDT IDT y_train = np.concatenate((y_train, new_y)) + IDT IDT IDT IDT + IDT IDT IDT IDT rollouts = 20 + IDT IDT IDT IDT returns = [] + IDT IDT IDT IDT for _ in range(rollouts): + IDT IDT IDT IDT IDT totalr, _ = run_env(env, nn, session) + IDT IDT IDT IDT IDT returns.append(totalr) + IDT IDT IDT IDT + IDT IDT IDT IDT logz.log_tabular('Iteration', j) + IDT IDT IDT IDT logz.log_tabular('AverageReturn', np.mean(returns)) + IDT IDT IDT IDT logz.log_tabular('StdReturn', np.std(returns)) + IDT IDT IDT IDT logz.dump_tabular() + + +if __name__ == '__main__': + IDT main() +import tensorflow as tf +import numpy as np +from print_ import print_ +from PIL import Image + + +class NN(object): + + IDT def __init__(self, sizes= , learning_rate= , batch_size= , n_epoches= ) IDT IDT self._sizes = sizes + IDT IDT self._learning_rate = learning_rate + IDT IDT self._batch_size = batch_size + IDT IDT self._n_epoches = n_epoches + IDT IDT self.w_list = [] + IDT IDT self.b_list = [] + IDT IDT if self._sizes is None: + IDT IDT IDT return + IDT IDT with tf.Session() as session: + IDT IDT IDT for i, size in enumerate(self._sizes[1:]): + IDT IDT IDT IDT in_size = self._sizes[i] + IDT IDT IDT IDT out_size = size + IDT IDT IDT IDT self.w_list.append(session.run(tf.random_normal([in_size, out_size]))) + IDT IDT IDT IDT self.b_list.append(session.run(tf.random_normal([out_size]))) + + IDT def load_from_dbn_to_reconstructNN(self, dbn): + IDT IDT assert len(self._sizes) = ) IDT IDT for i in range(len(dbn._sizes)): + IDT IDT IDT assert dbn._sizes[i] == self._sizes[i] + IDT IDT IDT assert self._sizes[-i - 1] == dbn._sizes[i] + IDT IDT for i in range(len(dbn._sizes) - 1): + IDT IDT IDT self.w_list[i], _, self.b_list[i] = dbn._rbm_list[i].get_param() + IDT IDT n_dbn_layers = len(dbn._sizes) + IDT IDT for i in range(n_dbn_layers - 1): + IDT IDT IDT w, vb, _ = dbn._rbm_list[i].get_param() + IDT IDT IDT self.w_list[-i - 1] = np.transpose(w) + IDT IDT IDT self.b_list[-i - 1] = vb + + IDT def load_layers_from_NN(self, nnet, start_layer, end_layer): + IDT IDT assert start_layer < end_layer + IDT IDT assert end_layer - start_layer + 1 < len(nnet._sizes) + IDT IDT self._sizes = nnet._sizes[start_layer:end_layer + 1] + IDT IDT self._learning_rate = nnet._learning_rate + IDT IDT self._batch_size = nnet._batch_size + IDT IDT self._n_epoches = nnet._n_epoches + IDT IDT self.w_list = nnet.w_list[start_layer:end_layer] + IDT IDT self.b_list = nnet.b_list[start_layer:end_layer] + + IDT def _load_w_b_from_self(self): + IDT IDT w_list = [] + IDT IDT b_list = [] + IDT IDT for i, size in enumerate(self._sizes[1:]): + IDT IDT IDT in_size = self._sizes[i] + IDT IDT IDT out_size = size + IDT IDT IDT w_list.append(tf.get_variable('nn_weight_' + str(i), [in_size, out_size], initializer= ) IDT IDT IDT b_list.append(tf.get_variable('nn_bias_' + str(i), [out_size], initializer= ) IDT IDT return w_list, b_list + + IDT def _save_w_b_to_self(self, session, w_list, b_list): + IDT IDT self.w_list = session.run(w_list) + IDT IDT self.b_list = session.run(b_list) + + IDT def _MLP(self, x_in, w_list, b_list): + IDT IDT y_out = x_in + IDT IDT for i in range(len(self._sizes) - 2): + IDT IDT IDT y_out = tf.nn.sigmoid(tf.add(tf.matmul(y_out, w_list[i]), b_list[i])) + IDT IDT y_out = tf.add(tf.matmul(y_out, w_list[len(self._sizes) - 2]), b_list[len(self._sizes) - 2]) + IDT IDT return y_out + + IDT def train(self, X, Y): + IDT IDT batch_size = self._batch_size + IDT IDT n_epoches = self._n_epoches + IDT IDT display_epoches = 1 + IDT IDT x_input = tf.placeholder('float', [None, self._sizes[0]]) + IDT IDT y_target = tf.placeholder('float', [None, self._sizes[-1]]) + IDT IDT w_list, b_list = self._load_w_b_from_self() + IDT IDT y_output = self._MLP(x_input, w_list, b_list) + IDT IDT loss_cross_entropy = , labels= ) IDT IDT train_op = ) IDT IDT init = tf.global_variables_initializer() + IDT IDT with tf.Session() as session_t: + IDT IDT IDT session_t.run(init) + IDT IDT IDT for epoch in range(n_epoches): + IDT IDT IDT IDT avg_lost = 0.0 + IDT IDT IDT IDT x_len = len(X) + IDT IDT IDT IDT total_batch = x_len // batch_size if x_len % batch_size == 0 else x_len // batch_size + 1 + IDT IDT IDT IDT for i in range(total_batch): + IDT IDT IDT IDT IDT s_site = i * batch_size + IDT IDT IDT IDT IDT if s_site + batch_size <= len(X): + IDT IDT IDT IDT IDT IDT e_site = s_site + batch_size + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT e_site = len(X) + IDT IDT IDT IDT IDT x_batch = X[s_site:e_site] + IDT IDT IDT IDT IDT y_batch = Y[s_site:e_site] + IDT IDT IDT IDT IDT lost_t, _ = , train_op], feed_dict= , y_target: y_batch}) ) IDT IDT IDT IDT IDT self._save_w_b_to_self(session_t, w_list, b_list) + IDT IDT IDT IDT IDT avg_lost += float(lost_t) / total_batch + IDT IDT IDT IDT if epoch % display_epoches == 0: + IDT IDT IDT IDT IDT print_('NNET Training : Epoch' + ' %04d' % (epoch + 1) + ' Lost ' + str(avg_lost)) + IDT IDT IDT print_('Optimizer Finished') + + IDT def test_linear(self, X, Y): + IDT IDT x_in = tf.placeholder('float', [None, self._sizes[0]]) + IDT IDT y_in = tf.placeholder('float', [None, self._sizes[-1]]) + IDT IDT __predict = tf.nn.sigmoid(self._MLP(x_in, self.w_list, self.b_list)) + IDT IDT error_square = tf.square(tf.subtract(__predict, y_in)) + IDT IDT mean_error_square = tf.reduce_mean(tf.cast(error_square, tf.float32)) + IDT IDT init = tf.global_variables_initializer() + IDT IDT with tf.Session() as session_t: + IDT IDT IDT session_t.run(init) + IDT IDT IDT print_('Error: ' + str(session_t.run(mean_error_square, feed_dict= , y_in: Y}))) ) + IDT def test_logical(self, X, Y): + IDT IDT x_in = tf.placeholder('float', [None, self._sizes[0]]) + IDT IDT y_in = tf.placeholder('float', [None, self._sizes[-1]]) + IDT IDT __predict = tf.nn.softmax(tf.nn.sigmoid(self._MLP(x_in, self.w_list, self.b_list))) + IDT IDT __correct = tf.equal(tf.argmax(__predict, 1), tf.argmax(y_in, 1)) + IDT IDT __accuracy_rate = tf.reduce_mean(tf.cast(__correct, tf.float32)) + IDT IDT init = tf.global_variables_initializer() + IDT IDT with tf.Session() as session_t: + IDT IDT IDT session_t.run(init) + IDT IDT IDT print_('Accuracy: ' + str(session_t.run(__accuracy_rate, feed_dict= , y_in: Y}))) ) + IDT def predict(self, X): + IDT IDT X_t = tf.placeholder('float', [None, self._sizes[0]]) + IDT IDT __predict = tf.nn.sigmoid(self._MLP(X_t, self.w_list, self.b_list)) + IDT IDT init = tf.global_variables_initializer() + IDT IDT with tf.Session() as session_t: + IDT IDT IDT session_t.run(init) + IDT IDT IDT __predict = , feed_dict= ) IDT IDT IDT return __predict +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.optimization = ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.saved_path = './saved_model' + + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + IDT def variable_output(self, session): + IDT IDT form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + IDT IDT return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + IDT def save(self, session): + IDT IDT self.saved_path = self.saver.save(session, './saved_model') + + IDT def restore(self, session): + IDT IDT self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + IDT IDT self.optimization = ) + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + IDT IDT session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + IDT IDT session.run(self.update_op) +import random +import tensorflow as tf +import numpy as np +import gym +policy_network_verbose = False +update_gradients_verbose = False +policy_learning_rate = 0.0001 +baseline_learning_rate = 0.0001 +n_episode = 1000 +gamma = 0.99 +observation_placeholder = , shape= , 4]) )W1_policy = , shape= , 64]) )b1_policy = , shape= )W2_policy = , shape= , 64]) )b2_policy = , shape= )W3_policy = , shape= , 2]) )b3_policy = , shape= )hidden1_policy = tf.nn.relu(tf.matmul(observation_placeholder, W1_policy) + b1_policy) +hidden2_policy = tf.nn.relu(tf.matmul(hidden1_policy, W2_policy) + b2_policy) +scores = tf.nn.relu(tf.matmul(hidden2_policy, W3_policy) + b3_policy) +probs = tf.nn.softmax(scores) +discount_placeholder = tf.placeholder(tf.float32) +gain_placeholder = tf.placeholder(tf.float32) +action_placeholder = , shape= , 2]) )log_probs = tf.log(probs) * action_placeholder +grads_policy = tf.gradients(log_probs, [W1_policy, b1_policy, W2_policy, b2_policy, W3_policy, b3_policy]) +update_W1_policy = W1_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[0]) +update_b1_policy = b1_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[1]) +update_W2_policy = W2_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[2]) +update_b2_policy = b2_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[3]) +update_W3_policy = W3_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[4]) +update_b3_policy = b3_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[5]) +W1_baseline = , shape= , 64]) )b1_baseline = , shape= )W2_baseline = , shape= , 64]) )b2_baseline = , shape= )W3_baseline = , shape= , 1]) )b3_baseline = , shape= )hidden1_baseline = tf.nn.relu(tf.matmul(observation_placeholder, W1_baseline) + b1_baseline) +hidden2_baseline = tf.nn.relu(tf.matmul(hidden1_baseline, W2_baseline) + b2_baseline) +values = tf.matmul(hidden2_baseline, W3_baseline) + b3_baseline +grads_baseline = tf.gradients(values, [W1_baseline, b1_baseline, W2_baseline, b2_baseline, W3_baseline, b3_baseline]) +update_W1_baseline = W1_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[0]) +update_b1_baseline = b1_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[1]) +update_W2_baseline = W2_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[2]) +update_b2_baseline = b2_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[3]) +update_W3_baseline = W3_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[4]) +update_b3_baseline = b3_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[5]) + + +def policy_network(observation, session, verbose= ) IDT probabilities = , feed_dict= ) IDT if verbose: + IDT IDT + IDT if random.random() <= probabilities[0][0]: + IDT IDT return 0 + IDT else: + IDT IDT return 1 + + +def update_policy_gradients(discount, gain, observation, action, session, verbose= ) IDT if action == 0: + IDT IDT mask = [[1, 0]] + IDT else: + IDT IDT mask = [[0, 1]] + IDT result = , log_probs, grads_policy, update_W1_policy, update_b1_policy, update_W2_policy, update_b2_policy, update_W3_policy, update_b3_policy], feed_dict= , discount_placeholder: discount, gain_placeholder: gain, action_placeholder: mask}) ) IDT if verbose: + IDT IDT + + +def update_baseline_gradients(gain, observation, session, verbose= ) IDT result = , update_W1_baseline, update_b1_baseline, update_W2_baseline, update_b2_baseline, update_W3_baseline, update_b3_baseline], feed_dict= , gain_placeholder: gain}) ) IDT if verbose: + IDT IDT + + +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT sess.run(init) + IDT env = gym.make('CartPole-v1') + IDT + IDT for episode in range(n_episode): + IDT IDT observation = env.reset() + IDT IDT reward = 0 + IDT IDT done = False + IDT IDT total_reward = 0.0 + IDT IDT memory = [] + IDT IDT while not done: + IDT IDT IDT action = , sess, verbose= ) IDT IDT IDT next_observation, next_reward, next_done, _ = env.step(action) + IDT IDT IDT if next_done: + IDT IDT IDT IDT next_reward = -100 + IDT IDT IDT memory.append((observation, action, next_reward)) + IDT IDT IDT total_reward += next_reward + IDT IDT IDT observation, reward, done = next_observation, next_reward, next_done + IDT IDT ) + IDT IDT discount = 1.0 + IDT IDT gain = 0 + IDT IDT for observation, action, reward in memory: + IDT IDT IDT gain += discount * reward + IDT IDT IDT discount *= gamma + IDT IDT discount = 1.0 + IDT IDT for t in range(len(memory)): + IDT IDT IDT observation, action, reward = memory[t] + IDT IDT IDT gain_with_baseline = , feed_dict= ) IDT IDT IDT update_policy_gradients(discount= , gain= , observation= , action= , session= , verbose= ) IDT IDT IDT update_baseline_gradients(gain= , observation= , session= , verbose= ) IDT IDT IDT gain -= discount * reward + IDT IDT IDT discount *= gamma + IDT + IDT + IDT input('Press Enter to start testing!') + IDT + IDT for episode in range(5): + IDT IDT observation = env.reset() + IDT IDT reward = 0 + IDT IDT done = False + IDT IDT total_reward = 0.0 + IDT IDT while not done: + IDT IDT IDT env.render() + IDT IDT IDT action = policy_network(observation, sess) + IDT IDT IDT next_observation, next_reward, next_done, _ = env.step(action) + IDT IDT IDT total_reward += next_reward + IDT IDT IDT observation, reward, done = next_observation, next_reward, next_done + IDT IDT + IDT env.close() +import tensorflow as tf + + +class DQAgent: + + IDT def __init__(self): + IDT IDT self.sess = tf.Session() + IDT IDT self.state_placeholder = , 84, 84, 4), dtype= , name= ) IDT IDT self.reward_placeholder = ,), dtype= , name= ) IDT IDT self.action_index = , 2), dtype= , name= ) IDT IDT self.define_variables() + IDT IDT self.action = self.define_graph(self.state_placeholder) + IDT IDT ) + IDT IDT self.optimizer = ) IDT IDT self.q_val = tf.gather_nd(self.action, self.action_index) + IDT IDT ) + IDT IDT self.q_val = , shape= ,)) ) IDT IDT with tf.name_scope('Loss'): + IDT IDT IDT self.loss = tf.losses.mean_squared_error(self.reward_placeholder, self.q_val) + IDT IDT IDT self.opt = self.optimizer.minimize(-1 * self.loss) + IDT IDT init = tf.global_variables_initializer() + IDT IDT self.sess.run(init) + IDT IDT self.train_writer = tf.summary.FileWriter('results/', self.sess.graph) + IDT IDT self.game_len_placeholder = , dtype= , name= ) IDT IDT self.game_len_summary = tf.summary.scalar('Game_length', self.game_len_placeholder) + + IDT def define_variables(self): + IDT IDT self.variables = [] + IDT IDT self.biases = [] + IDT IDT self.fully_connected = [] + IDT IDT with tf.name_scope('Convolutional_weights'): + IDT IDT IDT self.variables.append(tf.Variable(tf.random_normal(shape= , 8, 4, 32), mean= , stddev= , dtype= , name= ) IDT IDT IDT self.variables.append(tf.Variable(tf.random_normal(shape= , 4, 32, 64), mean= , stddev= , dtype= , name= ) IDT IDT IDT self.biases.append(tf.Variable(tf.random_normal(shape= , 20, 32), mean= , stddev= , dtype= ) IDT IDT IDT self.biases.append(tf.Variable(tf.random_normal(shape= , 4, 64), mean= , stddev= , dtype= ) IDT IDT with tf.name_scope('Fully_connected_weights'): + IDT IDT IDT self.fully_connected.append(tf.Variable(tf.random_normal(shape= , 256), mean= , stddev= , dtype= , name= ) IDT IDT IDT self.fully_connected.append(tf.Variable(tf.random_normal(shape= , 3), mean= , stddev= , dtype= , name= ) IDT IDT IDT self.biases.append(tf.Variable(tf.random_normal(shape= ,), mean= , stddev= , dtype= ) + IDT def define_graph(self, state): + IDT IDT with tf.name_scope('Convolutional_layer'): + IDT IDT IDT state = , self.variables[0], strides= , 4, 4, 1], padding= ) IDT IDT IDT state = tf.add(state, self.biases[0]) + IDT IDT IDT state = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT state = , name= ) IDT IDT IDT + IDT IDT with tf.name_scope('Convolutional_layer'): + IDT IDT IDT state = , self.variables[1], strides= , 2, 2, 1], padding= ) IDT IDT IDT state = tf.add(state, self.biases[1]) + IDT IDT IDT state = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT state = , name= ) IDT IDT IDT + IDT IDT state = , shape= , 256]) ) IDT IDT with tf.name_scope('Fully_connected_layer'): + IDT IDT IDT out = state + IDT IDT IDT out = tf.matmul(out, self.fully_connected[0]) + IDT IDT IDT out = tf.add(out, self.biases[2]) + IDT IDT IDT out = , name= ) IDT IDT IDT out = tf.matmul(out, self.fully_connected[1]) + IDT IDT return out + + IDT def get_action(self, observation): + IDT IDT return self.sess.run(self.action, feed_dict= ) + IDT def get_actions_taken(self, observation, actions): + IDT IDT return self.sess.run(self.q_val, feed_dict= , self.action_index: actions}) ) + IDT def train_step(self, inputs_batch, targets_batch, actions_batch): + IDT IDT self.sess.run(self.opt, feed_dict= , self.reward_placeholder: targets_batch, self.action_index: actions_batch}) ) + IDT def restore_session(self, steps): + IDT IDT saver = tf.train.Saver() + IDT IDT saver.restore(self.sess, 'results/dqmodels/model_%d.cpkt' % steps) + + +class StaleAgent: + + IDT def __init__(self): + IDT IDT self.sess = tf.Session() + IDT IDT self.state_placeholder = , 84, 84, 4), dtype= , name= ) IDT IDT self.reward_placeholder = ,), dtype= , name= ) IDT IDT self.action_index = , None), dtype= , name= ) IDT IDT self.define_variables() + IDT IDT self.action = self.define_graph(self.state_placeholder) + IDT IDT self.optimizer = ) IDT IDT self.q_val = tf.gather_nd(self.action, self.action_index) + IDT IDT self.q_val = , shape= ,)) ) IDT IDT with tf.name_scope('Loss'): + IDT IDT IDT self.loss = tf.losses.mean_squared_error(self.reward_placeholder, self.q_val) + IDT IDT IDT self.opt = self.optimizer.minimize(-1 * self.loss) + + IDT def transfer_values(self, dqlearner): + IDT IDT pass + + IDT def define_variables(self): + IDT IDT self.variables = [] + IDT IDT self.biases = [] + IDT IDT self.fully_connected = [] + IDT IDT with tf.name_scope('Convolutional_weights'): + IDT IDT IDT self.variables.append(tf.Variable(tf.random_normal(shape= , 8, 4, 32), mean= , stddev= , dtype= , name= ) IDT IDT IDT self.variables.append(tf.Variable(tf.random_normal(shape= , 4, 32, 64), mean= , stddev= , dtype= , name= ) IDT IDT with tf.name_scope('Fully_connected_weights'): + IDT IDT IDT self.fully_connected.append(tf.Variable(tf.random_normal(shape= , 256), mean= , stddev= , dtype= , name= ) IDT IDT IDT self.fully_connected.append(tf.Variable(tf.random_normal(shape= , 3), mean= , stddev= , dtype= , name= ) IDT IDT for var in self.variables: + IDT IDT IDT + IDT IDT for fc in self.fully_connected: + IDT IDT IDT + + IDT def define_graph(self, state): + IDT IDT with tf.name_scope('Convolutional_layer'): + IDT IDT IDT state = , self.variables[0], strides= , 4, 4, 1], padding= ) IDT IDT IDT state = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT state = , name= ) IDT IDT IDT + IDT IDT with tf.name_scope('Convolutional_layer'): + IDT IDT IDT state = , self.variables[1], strides= , 2, 2, 1], padding= ) IDT IDT IDT state = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT state = , name= ) IDT IDT IDT + IDT IDT + IDT IDT .as_list()) + IDT IDT state = , shape= , 256]) ) IDT IDT .as_list()) + IDT IDT + IDT IDT with tf.name_scope('Fully_connected_layer'): + IDT IDT IDT out = state + IDT IDT IDT out = tf.matmul(out, self.fully_connected[0]) + IDT IDT IDT out = , name= ) IDT IDT IDT out = tf.matmul(out, self.fully_connected[1]) + IDT IDT return out + + IDT def get_action(self, observation): + IDT IDT return self.sess.run(self.action, feed_dict= ) + IDT def train_step(self, inputs_batch, targets_batch, actions_batch): + IDT IDT self.sess.run(self.opt, feed_dict= , self.reward_placeholder: targets_batch, self.action_index: actions_batch}) ) + IDT def restore_session(self, steps): + IDT IDT saver = tf.train.Saver() + IDT IDT saver.restore(self.sess, 'results/dqmodels/model_%d.cpkt' % steps) +def split_data(data, train_frac, val_frac, test_frac, seed): + IDT """ + + IDT Split a numpy array into three parts for training, validation, and testing. + + + + IDT Args: + + IDT IDT - data: numpy array, to be split along the first axis + + IDT IDT - train_frac, fraction of data to be used for training + + IDT IDT - val_frac, fraction of data to be used for validation + + IDT IDT - test_frac, fraction of data to be used for testing + + IDT IDT - seed, random seed for reproducibility + IDT Returns: + + IDT IDT - Training Set + + IDT IDT - Validation Set + + IDT IDT - Testing Set + IDT + IDT + IDT """ + IDT if train_frac + val_frac + test_frac != 1: + IDT IDT + IDT else: + IDT IDT np.random.seed(seed) + IDT IDT size = data.shape[0] + IDT IDT split_train = int(train_frac * size) + IDT IDT split_val = int(val_frac * size) + split_train + IDT IDT np.random.shuffle(data) + IDT IDT return data[:split_train], data[split_train:split_val], data[split_val:] + + +import numpy as np +import tensorflow as tf +import pandas as pd +from random import randint +images = np.load('/work/cse496dl/shared/homework/01/fmnist_train_data.npy') +labels = np.load('/work/cse496dl/shared/homework/01/fmnist_train_labels.npy') +train_images, val_images, test_images = split_data(images, 0.7, 0.1, 0.2, 123) +train_labels, val_labels, test_labels = split_data(labels, 0.7, 0.1, 0.2, 123) +filepath = '$WORK' + '/CSCE_896/HW1/' +hlayers_max = 1 +nodes_min = 10 +nodes_max = 784 +batch_size = 100 +results = , 'Layer_1_Nodes', 'Layer_2_Nodes', 'Layer_3_Nodes', 'Accuracy']) )for run in range(25): + IDT num_hlayers = randint(1, hlayers_max) + IDT if num_hlayers == 1: + IDT IDT h1_nodes = randint(nodes_min, nodes_max) + IDT IDT x = , [None, 784], name= ) IDT IDT with tf.name_scope('linear_model') as scope: + IDT IDT IDT hidden = , h1_nodes, activation= , name= ) IDT IDT IDT output = , 10, name= ) IDT IDT IDT tf.identity(output, name= ) IDT IDT y = , [None, 10], name= ) IDT IDT accuracy = , predictions, name= ) IDT IDT cross_entropy = , logits= , name= ) IDT IDT with tf.Session() as session: + IDT IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT IDT for epoch in range(100): + IDT IDT IDT IDT for i in range(train_num_examples // batch_size): + IDT IDT IDT IDT IDT batch_xs = train_images[i * batch_size:(i + 1) * batch_size, :] + IDT IDT IDT IDT IDT batch_ys = train_labels[i * batch_size:(i + 1) * batch_size, :] + IDT IDT IDT IDT IDT session.run([train_op, tf.reduce_mean(cross_entropy)], {x: batch_xs, y: batch_ys}) + IDT IDT IDT IDT val_acc = session.run(accuracy, {x: val_images, y: val_labels}) + IDT IDT IDT IDT if val_acc < best_val_acc: + IDT IDT IDT IDT IDT best_val_acc = val_acc + IDT IDT IDT IDT IDT counter = 0 + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT counter = counter + 1 + IDT IDT IDT IDT if counter > 5: + IDT IDT IDT IDT IDT results.iloc[run, 0] = num_hlayers + IDT IDT IDT IDT IDT results.iloc[run, 1] = h1_nodes + IDT IDT IDT IDT IDT results.iloc[run, 2] = 'NA' + IDT IDT IDT IDT IDT results.iloc[run, 3] = 'NA' + IDT IDT IDT IDT IDT results.iloc[run, 4] = best_val_acc + IDT IDT IDT IDT IDT break + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT continue + IDT elif num_hlayers == 2: + IDT IDT h1_nodes = randint(nodes_min, nodes_max) + IDT IDT h2_nodes = randint(nodes_min, nodes_max) + IDT IDT x = , [None, 784], name= ) IDT IDT with tf.name_scope('linear_model') as scope: + IDT IDT IDT hidden1 = , h1_nodes, activation= , name= ) IDT IDT IDT hidden2 = , h2_nodes, activation= , name= ) IDT IDT IDT output = , 10, name= ) IDT IDT IDT tf.identity(output, name= ) IDT IDT y = , [None, 10], name= ) IDT IDT accuracy = , predictions, name= ) IDT IDT cross_entropy = , logits= , name= ) IDT IDT with tf.Session() as session: + IDT IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT IDT for epoch in range(100): + IDT IDT IDT IDT for i in range(train_num_examples // batch_size): + IDT IDT IDT IDT IDT batch_xs = train_images[i * batch_size:(i + 1) * batch_size, :] + IDT IDT IDT IDT IDT batch_ys = train_labels[i * batch_size:(i + 1) * batch_size, :] + IDT IDT IDT IDT IDT session.run([train_op, tf.reduce_mean(cross_entropy)], {x: batch_xs, y: batch_ys}) + IDT IDT IDT IDT val_acc = session.run(accuracy, {x: val_images, y: val_labels}) + IDT IDT IDT IDT if val_acc < best_val_acc: + IDT IDT IDT IDT IDT best_val_acc = val_acc + IDT IDT IDT IDT IDT counter = 0 + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT counter = counter + 1 + IDT IDT IDT IDT if counter > 5: + IDT IDT IDT IDT IDT results.iloc[run, 0] = num_hlayers + IDT IDT IDT IDT IDT results.iloc[run, 1] = h1_nodes + IDT IDT IDT IDT IDT results.iloc[run, 2] = h2_nodes + IDT IDT IDT IDT IDT results.iloc[run, 3] = 'NA' + IDT IDT IDT IDT IDT results.iloc[run, 4] = best_val_acc + IDT IDT IDT IDT IDT break + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT continue + IDT elif num_hlayers == 3: + IDT IDT h1_nodes = randint(nodes_min, nodes_max) + IDT IDT h2_nodes = randint(nodes_min, nodes_max) + IDT IDT h3_nodes = randint(nodes_min, nodes_max) + IDT IDT x = , [None, 784], name= ) IDT IDT with tf.name_scope('linear_model') as scope: + IDT IDT IDT hidden1 = , h1_nodes, activation= , name= ) IDT IDT IDT hidden2 = , h2_nodes, activation= , name= ) IDT IDT IDT hidden3 = , h2_nodes, activation= , name= ) IDT IDT IDT output = , 10, name= ) IDT IDT IDT tf.identity(output, name= ) IDT IDT y = , [None, 10], name= ) IDT IDT accuracy = , predictions, name= ) IDT IDT cross_entropy = , logits= , name= ) IDT IDT with tf.Session() as session: + IDT IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT IDT for epoch in range(100): + IDT IDT IDT IDT for i in range(train_num_examples // batch_size): + IDT IDT IDT IDT IDT batch_xs = train_images[i * batch_size:(i + 1) * batch_size, :] + IDT IDT IDT IDT IDT batch_ys = train_labels[i * batch_size:(i + 1) * batch_size, :] + IDT IDT IDT IDT IDT session.run([train_op, tf.reduce_mean(cross_entropy)], {x: batch_xs, y: batch_ys}) + IDT IDT IDT IDT val_acc = session.run(tf.argmax(val_labels, 1), tf.argmax(output, 1), {x: val_images, y: val_labels}) + IDT IDT IDT IDT if val_acc < best_val_acc: + IDT IDT IDT IDT IDT best_val_acc = val_acc + IDT IDT IDT IDT IDT counter = 0 + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT counter = counter + 1 + IDT IDT IDT IDT if counter > 5: + IDT IDT IDT IDT IDT results.iloc[run, 0] = num_hlayers + IDT IDT IDT IDT IDT results.iloc[run, 1] = h1_nodes + IDT IDT IDT IDT IDT results.iloc[run, 2] = h2_nodes + IDT IDT IDT IDT IDT results.iloc[run, 3] = h3_nodes + IDT IDT IDT IDT IDT results.iloc[run, 4] = best_val_acc + IDT IDT IDT IDT IDT break + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT continue + IDT results.to_csv(filepath + 'arc_search_results.csv', index= ) IDT + IDT tf.reset_default_graph() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT + IDT image_size = 28 + IDT num_labels = 10 + + IDT def reformat(dataset, labels): + IDT IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT IDT labels = , (None)]).astype(np.float32) ) IDT IDT return dataset, labels + IDT train_dataset, train_labels = reformat(train_dataset, train_labels) + IDT valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) + IDT test_dataset, test_labels = reformat(test_dataset, test_labels) + IDT + IDT + IDT + + IDT def accuracy(predictions, labels): + IDT IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) IDT batch_size = 128 + IDT num_hidden_nodes1 = 1024 + IDT num_hidden_nodes2 = 256 + IDT num_hidden_nodes3 = 128 + IDT keep_prob = 0.5 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT global_step = tf.Variable(0) + IDT IDT weights1 = , num_hidden_nodes1], stddev= ) IDT IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT IDT weights2 = , num_hidden_nodes2], stddev= ) IDT IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT IDT weights3 = , num_hidden_nodes3], stddev= ) IDT IDT biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + IDT IDT weights4 = , num_labels], stddev= ) IDT IDT biases4 = tf.Variable(tf.zeros([num_labels])) + IDT IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT IDT lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + IDT IDT logits = tf.matmul(lay3_train, weights4) + biases4 + IDT IDT loss = , labels= ) IDT IDT learning_rate = , global_step, 4000, 0.65, staircase= ) IDT IDT optimizer = , global_step= ) IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT IDT lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + IDT IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT IDT lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) + IDT num_steps = 18001 + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W, stride): + IDT return tf.nn.conv2d(x, W, strides= , stride, stride, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +def createNetwork(action_count): + IDT W_conv1 = weight_variable([8, 8, 4, 32]) + IDT b_conv1 = bias_variable([32]) + IDT W_conv2 = weight_variable([4, 4, 32, 64]) + IDT b_conv2 = bias_variable([64]) + IDT W_conv3 = weight_variable([3, 3, 64, 64]) + IDT b_conv3 = bias_variable([64]) + IDT W_fc1 = weight_variable([1600, 512]) + IDT b_fc1 = bias_variable([512]) + IDT W_fc2 = weight_variable([512, action_count]) + IDT b_fc2 = bias_variable([action_count]) + IDT s = tf.placeholder('float', [None, 80, 80, 4]) + IDT h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + IDT h_pool1 = max_pool_2x2(h_conv1) + IDT h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + IDT h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + IDT h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + IDT h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + IDT readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + IDT return s, readout, h_fc1 + + +class DQNAgent(object): + + IDT def __init__(self, action_count): + IDT IDT self.action_count = action_count + IDT IDT self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + IDT IDT self.action_placeholder = tf.placeholder('float', [None, action_count]) + IDT IDT self.reward_placeholder = tf.placeholder('float', [None]) + IDT IDT self.readout_action = , self.action_placeholder), reduction_indices= ) IDT IDT self.losses = tf.square(self.reward_placeholder - self.readout_action) + IDT IDT self.loss = tf.reduce_mean(self.losses) + IDT IDT self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.loss) + IDT IDT tf.summary.scalar('loss', self.loss), + IDT IDT tf.summary.histogram('loss_hist', self.losses), + IDT IDT tf.summary.histogram('q_values_hist', self.readout), + IDT IDT tf.summary.scalar('max_q_value', tf.reduce_max(self.readout)) + IDT IDT self.summaries = tf.summary.merge_all() + + IDT def act(self, session, state): + IDT IDT scores = self.score_actions(session, [state])[0] + IDT IDT return np.argmax(scores) + + IDT def score_actions(self, session, state_batch): + IDT IDT return session.run(self.readout, feed_dict= ) + IDT def train(self, session, reward_batch, action_batch, state_batch): + IDT IDT summaries, _, loss = , self.train_step, self.loss], feed_dict= , self.action_placeholder: action_batch, self.state_placeholder: state_batch}) ) IDT IDT return summaries, loss +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import scipy +from scipy import io +import os +import matplotlib.pyplot as plt +import tensorflow as tf +from sklearn.metrics import confusion_matrix +MAT = scipy.io.loadmat('MAT_Py_TF_Data_test2') +train_dataset = MAT['Train_Paras'] +train_labels = MAT['Label'] +train_labels_1D = MAT['Label_1D'] +train_dataset.astype(np.float32) +train_labels.astype(np.float32) + + + + + +train_labels_1D_argmax = np.array([label.argmax() for label in train_labels]) +train_labels_cls = np.array([label.argmax() for label in train_labels]) +train_labels_1D_argmax[0:18] +x = tf.placeholder(tf.float32, [None, 6]) +y_true = tf.placeholder(tf.float32, [None, 2]) +y_true_cls = tf.placeholder(tf.int64, [None]) +beta_regul = tf.placeholder(tf.float32) +num_hidden_nodes = 5 +weights1 = tf.Variable(tf.truncated_normal([6, num_hidden_nodes])) +biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) +weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, 2])) +biases2 = tf.Variable(tf.zeros([2])) +lay1_train = tf.nn.relu(tf.matmul(x, weights1) + biases1) +logits = tf.matmul(lay1_train, weights2) + biases2 +y_pred = tf.nn.softmax(logits) +y_pred_cls = , dimension= )cost = , labels= )optimizer = )correct_prediction = tf.equal(y_pred_cls, y_true_cls) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +saver = tf.train.Saver() +save_dir = 'FullyConnected_Reg/' +if not os.path.exists(save_dir): + IDT os.makedirs(save_dir) +save_path = os.path.join(save_dir, 'best_validation') +session = tf.Session() +session.run(tf.global_variables_initializer()) + + +def optimize(num_iterations): + IDT for i in range(num_iterations): + IDT IDT feed_dict_train = {x: train_dataset, y_true: train_labels, beta_regul: 0.001} + IDT IDT session.run(optimizer, feed_dict= ) + +feed_dict_test = {x: train_dataset, y_true: train_labels, y_true_cls: train_labels_cls} + + +def print_accuracy(): + IDT acc = , feed_dict= ) IDT ) + + +print_accuracy() +session.run(tf.global_variables_initializer()) +print_accuracy() +w = session.run(weights1) +plt.imshow(w) +plt.show +optimize(num_iterations= )print_accuracy() +w = session.run(weights1) +plt.imshow(w) +plt.show +saver.save(sess= , save_path= )session.run(tf.global_variables_initializer()) +print_accuracy() +saver.restore(sess= , save_path= )print_accuracy() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import pdb +pickle_file = '../1_notmnist/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + IDT b1 = tf.Variable(tf.zeros([hidden_size])) + IDT W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y1 = tf.nn.dropout(y1, 0.5) + IDT logits = tf.matmul(y1, W2) + b2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT valid_logits = tf.matmul(y1_valid, W2) + b2 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT test_logits = tf.matmul(y1_test, W2) + b2 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] + +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + IDT IDT batch_data = small_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = small_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT tf_beta = tf.placeholder(tf.float32) + IDT global_step = tf.Variable(0) + IDT W1 = , fc1_size], stddev= ) IDT b1 = tf.Variable(tf.zeros([fc1_size])) + IDT W2 = , fc2_size], stddev= ) IDT b2 = tf.Variable(tf.zeros([fc2_size])) + IDT W3 = , fc3_size], stddev= ) IDT b3 = tf.Variable(tf.zeros([fc3_size])) + IDT W4 = , num_labels], stddev= ) IDT b4 = tf.Variable(tf.zeros([num_labels])) + IDT y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + IDT y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + IDT y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + IDT logits = tf.matmul(y3, W4) + b4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + IDT learning_rate = , global_step, 1000, 0.7, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + IDT y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + IDT y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + IDT valid_logits = tf.matmul(y3_valid, W4) + b4 + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + IDT y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + IDT y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + IDT test_logits = tf.matmul(y3_test, W4) + b4 + IDT test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf + + +class CNN_NonLinearFC: + + IDT def __init__(self, model_name= , session= , learning_rate= , optimizer= , learning_decay_rate= , filter_sizes= , num_filters= , max_sentence_length= , num_classes= , embeddings= , new_embeddings= , embedding_dim= , vocabulary_size= , static= , max_l2_norm= , regularization_lambda= , dropout_keep_prob= ) IDT IDT if model_name == None: + IDT IDT IDT return + IDT IDT self.model_name = model_name + IDT IDT self.session = session + IDT IDT self.learning_rate = learning_rate + IDT IDT self.optimizer = optimizer + IDT IDT self.dropout_keep_prob_train = dropout_keep_prob + IDT IDT self.regularization_lambda = regularization_lambda + IDT IDT self.input_x = , max_sentence_length), dtype= , name= ) IDT IDT self.input_y = , num_classes), dtype= , name= ) IDT IDT self.dropout_keep_prob = , name= ) IDT IDT self.embeddings_placeholder = , shape= , embedding_dim)) ) IDT IDT self.embeddings = , trainable= ) IDT IDT self.new_embeddings = , trainable= ) IDT IDT self.all_embeddings = , self.new_embeddings], axis= ) IDT IDT self.embedded_words = tf.nn.embedding_lookup(self.all_embeddings, self.input_x) + IDT IDT self.input_x_expanded = tf.expand_dims(self.embedded_words, -1) + IDT IDT self.pool_results = [] + IDT IDT for i, filter_size in enumerate(filter_sizes): + IDT IDT IDT filter = , shape= , embedding_dim, 1, num_filters), dtype= , initializer= ) IDT IDT IDT if max_l2_norm != 0: + IDT IDT IDT IDT filter = tf.clip_by_norm(filter, max_l2_norm) + IDT IDT IDT bias = , shape= ,))) ) IDT IDT IDT conv = , filter= , strides= , 1, 1, 1], padding= ) IDT IDT IDT relu = tf.nn.relu(tf.nn.bias_add(conv, bias)) + IDT IDT IDT conv_dim = max_sentence_length - filter_size + 1 + IDT IDT IDT pooled = , ksize= , conv_dim, 1, 1], strides= , 1, 1, 1], padding= ) IDT IDT IDT self.pool_results.append(pooled) + IDT IDT num_filters_total = num_filters * len(filter_sizes) + IDT IDT self.flat = tf.reshape(tf.concat(self.pool_results, 3), [-1, num_filters_total]) + IDT IDT self.dropout = tf.nn.dropout(self.flat, self.dropout_keep_prob) + IDT IDT hidden_layer_size = num_classes + IDT IDT W_hidden = , shape= , hidden_layer_size), dtype= , initializer= ) IDT IDT b_hidden = , shape= ,))) ) IDT IDT out_hidden = tf.nn.relu(tf.nn.xw_plus_b(self.dropout, W_hidden, b_hidden)) + IDT IDT W_output = , shape= , num_classes), dtype= , initializer= ) IDT IDT b_output = , shape= ,))) ) IDT IDT self.output = , W_output, b_output, name= ) IDT IDT self.predictions = , 1, name= ) IDT IDT losses = , logits= ) IDT IDT if regularization_lambda != 0: + IDT IDT IDT l2_loss = tf.nn.l2_loss(W_hidden) + tf.nn.l2_loss(W_output) + IDT IDT IDT self.loss = , tf.multiply(self.regularization_lambda, l2_loss)), name= ) IDT IDT else: + IDT IDT IDT self.loss = , name= ) IDT IDT global_step = , trainable= ) IDT IDT self.optimizer = , global_step, 1000000, learning_decay_rate, staircase= ) IDT IDT self.train_op = , global_step= ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.session.run(tf.global_variables_initializer(), feed_dict= ) + IDT def train_step(self, input_x, input_y): + IDT IDT _, loss = , self.loss], feed_dict= , self.input_y: input_y, self.dropout_keep_prob: self.dropout_keep_prob_train}) ) IDT IDT return loss + + IDT def feed(self, input_x): + IDT IDT return self.session.run([self.output, self.predictions], feed_dict= , self.dropout_keep_prob: 1}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import logging + + +class CNN: + IDT """ + Convolutional Neural Network model. + """ + + IDT def __init__(self, num_actions, observation_shape, verbose= ) IDT IDT """ + IDT Initialize the CNN model with a set of parameters. + IDT Args: + IDT params: a dictionary containing values of the models' parameters. + IDT """ + IDT IDT self.verbose = verbose + IDT IDT self.num_actions = num_actions + IDT IDT self.observation_shape = observation_shape[0] + IDT IDT self.lr = 0.0001 + IDT IDT self.reg = 0.001 + IDT IDT self.hidden_size = 400 + IDT IDT self.session = self.create_model() + + IDT def add_placeholders(self): + IDT IDT input_placeholder = , shape= , self.observation_shape)) ) IDT IDT labels_placeholder = , shape= ,)) ) IDT IDT actions_placeholder = , shape= , self.num_actions)) ) IDT IDT return input_placeholder, labels_placeholder, actions_placeholder + + IDT def nn(self, input_obs): + IDT IDT with tf.name_scope('Layer1') as scope: + IDT IDT IDT W1shape = [self.observation_shape, self.hidden_size] + IDT IDT IDT W1 = , shape= ) IDT IDT IDT bshape = [1, self.hidden_size] + IDT IDT IDT b1 = , shape= , initializer= ) IDT IDT with tf.name_scope('Layer3') as scope: + IDT IDT IDT W3shape = [self.hidden_size, 12] + IDT IDT IDT W3 = , shape= ) IDT IDT IDT bshape = [1, 12] + IDT IDT IDT b3 = , shape= , initializer= ) IDT IDT with tf.name_scope('OutputLayer') as scope: + IDT IDT IDT Ushape = [12, self.num_actions] + IDT IDT IDT U = , shape= ) IDT IDT IDT b3shape = [1, self.num_actions] + IDT IDT IDT b4 = , shape= , initializer= ) IDT IDT xW = tf.matmul(input_obs, W1) + IDT IDT h = tf.nn.softsign(tf.add(xW, b1)) + IDT IDT xW = tf.matmul(h, W3) + IDT IDT h = tf.nn.softsign(tf.add(xW, b3)) + IDT IDT hU = tf.matmul(h, U) + IDT IDT out = tf.add(hU, b4) + IDT IDT reg = self.reg * (tf.reduce_sum(tf.square(W1)) + tf.reduce_sum(tf.square(W3)) + tf.reduce_sum(tf.square(U))) + IDT IDT return out, reg + + IDT def create_model(self): + IDT IDT """ + IDT The model definition. + IDT """ + IDT IDT self.input_placeholder, self.labels_placeholder, self.actions_placeholder = self.add_placeholders() + IDT IDT outputs, reg = self.nn(self.input_placeholder) + IDT IDT self.predictions = outputs + IDT IDT self.q_vals = tf.reduce_sum(tf.mul(self.predictions, self.actions_placeholder), 1) + IDT IDT self.loss = tf.reduce_sum(tf.square(self.labels_placeholder - self.q_vals)) + reg + IDT IDT optimizer = , use_locking= ) IDT IDT self.train_op = optimizer.minimize(self.loss) + IDT IDT init = tf.initialize_all_variables() + IDT IDT session = tf.Session() + IDT IDT session.run(init) + IDT IDT return session + + IDT def train_step(self, Xs, ys, actions): + IDT IDT """ + IDT Updates the CNN model with a mini batch of training examples. + IDT """ + IDT IDT loss, _, prediction_probs, q_values = , self.train_op, self.predictions, self.q_vals], feed_dict= , self.labels_placeholder: ys, self.actions_placeholder: actions}) ) + IDT def predict(self, observation): + IDT IDT """ + IDT Predicts the rewards for an input observation state. + IDT Args: + IDT observation: a numpy array of a single observation state + IDT """ + IDT IDT loss, prediction_probs = , self.predictions], feed_dict= , self.labels_placeholder: np.zeros(len(observation)), self.actions_placeholder: np.zeros((len(observation), self.num_actions))}) ) IDT IDT return prediction_probs +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.optimization = ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.saved_path = './saved_model' + + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + IDT def variable_output(self, session): + IDT IDT form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + IDT IDT return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + IDT def save(self, session): + IDT IDT self.saved_path = self.saver.save(session, './saved_model') + + IDT def restore(self, session): + IDT IDT self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + IDT IDT session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + IDT IDT session.run(self.update_op) +from plot_functions import * +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt +import time + + +def make_model1(X, isTrain): + IDT W1 = , shape= , 6], dtype= , initializer= , tf.sqrt(2 / 111))) ) IDT L1 = tf.matmul(X, W1) + IDT return L1 + + +def make_model2(X, isTrain): + IDT W1 = , shape= , 256], dtype= , initializer= , tf.sqrt(2 / 111))) ) IDT L1 = tf.matmul(X, W1) + IDT L1 = , training= ) IDT L1 = tf.nn.relu(L1) + IDT W2 = , shape= , 512], dtype= , initializer= , tf.sqrt(2 / 256))) ) IDT L2 = tf.matmul(L1, W2) + IDT L2 = , training= ) IDT L2 = tf.nn.relu(L2) + IDT W3 = , shape= , 1024], dtype= , initializer= , tf.sqrt(2 / 512))) ) IDT L3 = tf.matmul(L2, W3) + IDT L3 = , training= ) IDT L3 = tf.nn.relu(L3) + IDT W4 = , shape= , 3], dtype= , initializer= , tf.sqrt(2 / 1024))) ) IDT L4 = tf.matmul(L3, W4) + IDT return L4 + + +def make_model3(X, isTrain): + IDT W1 = , shape= , 256], dtype= , initializer= , tf.sqrt(2 / 111))) ) IDT L1 = tf.matmul(X, W1) + IDT L1 = , training= ) IDT L1 = tf.nn.relu(L1) + IDT W2 = , shape= , 512], dtype= , initializer= , tf.sqrt(2 / 256))) ) IDT L2 = tf.matmul(L1, W2) + IDT L2 = , training= ) IDT L2 = tf.nn.relu(L2) + IDT W3 = , shape= , 512], dtype= , initializer= , tf.sqrt(2 / 512))) ) IDT L3 = tf.matmul(L2, W3) + IDT L3 = , training= ) IDT L3 = tf.nn.relu(L3) + IDT W4 = , shape= , 512], dtype= , initializer= , tf.sqrt(2 / 512))) ) IDT L4 = tf.matmul(L3, W4) + IDT L4 = , training= ) IDT L4 = tf.nn.relu(L4) + IDT W5 = , shape= , 1024], dtype= , initializer= , tf.sqrt(2 / 512))) ) IDT L5 = tf.matmul(L4, W5) + IDT L5 = , training= ) IDT L5 = tf.nn.relu(L5) + IDT W6 = , shape= , 1024], dtype= , initializer= , tf.sqrt(2 / 1024))) ) IDT L6 = tf.matmul(L5, W6) + IDT L6 = , training= ) IDT L6 = tf.nn.relu(L6) + IDT W7 = , shape= , 512], dtype= , initializer= , tf.sqrt(2 / 1024))) ) IDT L7 = tf.matmul(L6, W7) + IDT L7 = , training= ) IDT L7 = tf.nn.relu(L7) + IDT W8 = , shape= , 3], dtype= , initializer= , tf.sqrt(2 / 512))) ) IDT L8 = tf.matmul(L7, W8) + IDT return L8 + + +def make_model4(X, isTrain): + IDT W1 = , shape= , 256], dtype= , initializer= , tf.sqrt(2 / 111))) ) IDT B1 = , shape= , dtype= , initializer= ) IDT L1 = tf.matmul(X, W1) + IDT L1 = tf.nn.bias_add(L1, B1) + IDT L1 = tf.nn.relu(L1) + IDT W2 = , shape= , 512], dtype= , initializer= , tf.sqrt(2 / 256))) ) IDT B2 = , shape= , dtype= , initializer= ) IDT L2 = tf.matmul(L1, W2) + IDT L2 = tf.nn.bias_add(L2, B2) + IDT L2 = tf.nn.relu(L2) + IDT W3 = , shape= , 1024], dtype= , initializer= , tf.sqrt(2 / 512))) ) IDT B3 = , shape= , dtype= , initializer= ) IDT L3 = tf.matmul(L2, W3) + IDT L3 = tf.nn.bias_add(L3, B3) + IDT L3 = tf.nn.relu(L3) + IDT W4 = , shape= , 3], dtype= , initializer= , tf.sqrt(2 / 1024))) ) IDT L4 = tf.matmul(L3, W4) + IDT return L4 + + +def make_model5(X, isTrain): + IDT W1 = , shape= , 256], dtype= , initializer= , tf.sqrt(2 / 111))) ) IDT B1 = , shape= , dtype= , initializer= ) IDT L1 = tf.matmul(X, W1) + IDT L1 = tf.nn.bias_add(L1, B1) + IDT L1 = tf.nn.relu(L1) + IDT W2 = , shape= , 12], dtype= , initializer= , tf.sqrt(2 / 256))) ) IDT L2 = tf.matmul(L1, W2) + IDT return L2 + + +def make_train_graph(input, label, is_training, gpu_num, split_num): + IDT input_list = tf.split(input, gpu_num * split_num) + IDT label_list = tf.split(label, gpu_num * split_num) + IDT logit_list = [] + IDT sum_L2_list = [] + IDT ED_list = [] + IDT pck_list = [] + IDT iter = -1 + IDT for d in range(gpu_num): + IDT IDT with tf.device('/gpu:' + str(d)): + IDT IDT IDT for i in range(split_num): + IDT IDT IDT IDT iter = iter + 1 + IDT IDT IDT IDT with tf.variable_scope(tf.get_variable_scope(), reuse= ) IDT IDT IDT IDT IDT logit = make_model5(input_list[iter], is_training) + IDT IDT IDT IDT IDT loss_L2 = tf.pow(logit - label_list[iter], 2) + IDT IDT IDT IDT IDT sum_L2 = tf.reduce_sum(loss_L2) + IDT IDT IDT IDT IDT ED = , axis= ) IDT IDT IDT IDT IDT pck = tf.to_float(tf.greater(0.3, ED)) + IDT IDT IDT IDT IDT logit_list.append(logit) + IDT IDT IDT IDT IDT sum_L2_list.append(sum_L2) + IDT IDT IDT IDT IDT ED_list.append(ED) + IDT IDT IDT IDT IDT pck_list.append(pck) + IDT total_logit = , axis= ) IDT total_sum_L2 = tf.reduce_sum(sum_L2_list) + IDT total_ED = , axis= ) IDT total_pck = , axis= ) IDT with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): + IDT IDT train_op = , colocate_gradients_with_ops= ) IDT return train_op, total_sum_L2, total_logit, total_ED, total_pck + + +def train(input, label, max_epoch, gpu_num, split_num): + IDT X = tf.placeholder(tf.float32, [None, 111]) + IDT Y = tf.placeholder(tf.float32, [None, 3]) + IDT is_training = tf.placeholder(tf.bool) + IDT train_op, sum_L2, logit_, loss_ED, pck = make_train_graph(X, Y, is_training, gpu_num, split_num) + IDT mean_ED = tf.reduce_mean(loss_ED) + IDT sess = tf.Session() + IDT saver = tf.train.Saver(tf.global_variables()) + IDT ckpt = tf.train.get_checkpoint_state('./model') + IDT if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): + IDT IDT saver.restore(sess, ckpt.model_checkpoint_path) + IDT else: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT for epoch in range(max_epoch): + IDT IDT _, ED = , mean_ED], feed_dict= , Y: label, is_training: True}) ) IDT IDT if epoch + 1 % 100 == 0: + IDT IDT IDT saver.save(sess, './model/model.ckpt') + IDT IDT IDT + IDT IDT ) + + +def inference(gpu_num= , split_num= ) IDT input_batch = np.load('test_input_batch2.npy') + IDT label_batch = np.load('test_label_batch2.npy') + IDT test_len = len(input_batch) + IDT if test_len % (gpu_num * split_num) != 0: + IDT IDT input_batch = input_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + IDT IDT label_batch = label_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + IDT log_txt = open('inference_loss.txt', 'a') + IDT X = tf.placeholder(tf.float32, [None, 108]) + IDT Y = tf.placeholder(tf.float32, [None, 6]) + IDT is_training = tf.placeholder(tf.bool) + IDT train_op, sum_L2, logit_, loss_ED, pck_ = make_train_graph(X, Y, is_training, gpu_num, split_num) + IDT sess = ) IDT saver = tf.train.Saver(tf.global_variables()) + IDT ckpt = tf.train.get_checkpoint_state('./model') + IDT if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): + IDT IDT saver.restore(sess, ckpt.model_checkpoint_path) + IDT else: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT ED, pck = , pck_], feed_dict= , Y: label_batch, is_training: False}) ) IDT for i in range(len(input_batch)): + IDT IDT log_txt.write('\n[%d] - %.3f / %.3f' % (i, ED[i], pck[i])) + IDT log_txt.close() + IDT , np.mean(pck))) + + +def cross_val(max_epoch, gpu_num= , split_num= , already_done_epoch= ) IDT input_batch = np.load('input_batch3.npy') + IDT label_batch = np.load('label_batch3.npy') + IDT test_input_batch = np.load('test_input_batch3.npy') + IDT test_label_batch = np.load('test_label_batch3.npy') + IDT train_len = len(input_batch) + IDT test_len = len(test_input_batch) + IDT if train_len % (gpu_num * split_num) != 0: + IDT IDT input_batch = input_batch[:train_len // (gpu_num * split_num) * (gpu_num * split_num)] + IDT IDT label_batch = label_batch[:train_len // (gpu_num * split_num) * (gpu_num * split_num)] + IDT if test_len % (gpu_num * split_num) != 0: + IDT IDT test_input_batch = test_input_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + IDT IDT test_label_batch = test_label_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + IDT log_txt = open('loss.txt', 'a') + IDT X = tf.placeholder(tf.float32, [None, 216]) + IDT Y = tf.placeholder(tf.float32, [None, 12]) + IDT is_training = tf.placeholder(tf.bool) + IDT train_op, sum_L2, logit_, loss_ED, pck_ = make_train_graph(X, Y, is_training, gpu_num, split_num) + IDT mean_ED = tf.reduce_mean(loss_ED) + IDT mean_pck = tf.reduce_mean(pck_) + IDT a = time.time() + IDT sess = tf.Session() + IDT b = time.time() + IDT + IDT saver = tf.train.Saver(tf.global_variables()) + IDT ckpt = tf.train.get_checkpoint_state('./model') + IDT if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): + IDT IDT saver.restore(sess, ckpt.model_checkpoint_path) + IDT else: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT start_time = time.time() + IDT for epoch in range(max_epoch - already_done_epoch): + IDT IDT _, ED, pck = , mean_ED, mean_pck], feed_dict= , Y: label_batch, is_training: True}) ) IDT IDT info_txt = '\n[%d/%d] - mean of train ED: %.3f // train PCK: %.3f' % (epoch + 1 + already_done_epoch, max_epoch, ED, pck) + IDT IDT log_txt.write(info_txt) + IDT IDT + IDT IDT ED, pck = , mean_pck], feed_dict= , Y: test_label_batch, is_training: False}) ) IDT IDT info_txt = ' /// mean of test ED: %.3f //// test PCK: %.3f\n' % (ED, pck) + IDT IDT log_txt.write(info_txt) + IDT IDT + IDT IDT if epoch % 100 == 0: + IDT IDT IDT end_time = time.time() + IDT IDT IDT + IDT IDT IDT saver.save(sess, './model/model.ckpt') + IDT IDT IDT start_time = end_time + IDT log_txt.close() + IDT saver.save(sess, './model/model.ckpt') + + +cross_val(100000, 2, 1, 0) +epoch_ED_plot_from_txt('loss.txt') +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT """ + + IDT def __init__(self, hidden_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.dropout = dropout + + IDT def encode(self, inputs, masks, attention_inputs= , model_type= , name= , reuse= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(name, reuse= ) IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell_fw = tf.nn.rnn_cell.GRUCell(self.hidden_size) + IDT IDT IDT IDT IDT cell_bw = tf.nn.rnn_cell.GRUCell(self.hidden_size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT IDT cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT else: + IDT IDT IDT IDT raise Exception('Attention not implemented.') + IDT IDT IDT cell_fw = , output_keep_prob= ) IDT IDT IDT cell_bw = , output_keep_prob= ) IDT IDT IDT outputs, final_state = , cell_bw, inputs, sequence_length= , dtype= ) IDT IDT IDT return outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, hidden_size, output_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.output_size = output_size + IDT IDT self.dropout = dropout + + IDT def decode(self, knowledge_rep, masks, size, model_type= ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT IDT and output a probability estimation over + IDT IDT all paragraph tokens on which token should be + IDT IDT the start of the answer span, and which should be + IDT IDT the end of the answer span. + + IDT IDT :param knowledge_rep: it is a representation of the paragraph and question, + IDT IDT IDT IDT IDT IDT IDT decided by how you choose to implement the encoder + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('decoder'): + IDT IDT IDT rep_reshaped = tf.reshape(knowledge_rep, [-1, size]) + IDT IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT IDT start_probs = , output_size= , bias= ) IDT IDT IDT IDT start_probs = tf.reshape(start_probs, [-1, self.output_size]) + IDT IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT IDT end_probs = , output_size= , bias= ) IDT IDT IDT IDT end_probs = tf.reshape(end_probs, [-1, self.output_size]) + IDT IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT IDT add_mask = -1e+30 * (1.0 - bool_masks) + IDT IDT IDT start_probs = tf.add(start_probs, add_mask) + IDT IDT IDT end_probs = tf.add(end_probs, add_mask) + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, pretrained_embeddings, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.flags = flags + IDT IDT self.h_size = self.flags.state_size + IDT IDT self.p_size = self.flags.output_size + IDT IDT self.q_size = self.flags.question_size + IDT IDT self.embed_size = self.flags.embedding_size + IDT IDT self.dropout = 1.0 - self.flags.dropout + IDT IDT self.encoder = , dropout= ) IDT IDT self.decoder = , output_size= , dropout= ) IDT IDT self.context_placeholder = , shape= , self.p_size), name= ) IDT IDT self.question_placeholder = , shape= , self.q_size), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) IDT IDT self.saver = tf.train.Saver() + + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def filter(self, Q, P): + IDT IDT with vs.variable_scope('filter'): + IDT IDT IDT Qn = , dim= ) IDT IDT IDT Pn = , dim= ) IDT IDT IDT R = , tf.transpose(Pn, perm= , 2, 1])) ) IDT IDT IDT r = , axis= ) IDT IDT IDT r = , axis= ) IDT IDT IDT P_filtered = P * r + IDT IDT IDT return P_filtered + + IDT def matching_layer(self, Q_fw, Q_bw, P_fw, P_bw, num_perspectives): + IDT IDT with vs.variable_scope('matching'): + IDT IDT IDT Q_fw_final = Q_fw[:, (-1), :] + IDT IDT IDT Q_bw_final = Q_bw[:, (-1), :] + IDT IDT IDT Q_fw_n = , dim= ) IDT IDT IDT Q_bw_n = , dim= ) IDT IDT IDT Q_fw_final_n = , dim= ) IDT IDT IDT Q_bw_final_n = , dim= ) IDT IDT IDT P_fw_n = , dim= ) IDT IDT IDT P_bw_n = , dim= ) IDT IDT IDT Q_fw_final_n = tf.expand_dims(Q_fw_final_n, 2) + IDT IDT IDT M_full_fw = batch_matmul(P_fw_n, Q_fw_final_n) + IDT IDT IDT Q_bw_final_n = tf.expand_dims(Q_bw_final_n, 2) + IDT IDT IDT M_full_bw = batch_matmul(P_bw_n, Q_bw_final_n) + IDT IDT IDT M_max_fw = , tf.transpose(Q_fw_n, perm= , 2, 1])), axis= , keep_dims= ) IDT IDT IDT M_max_bw = , tf.transpose(Q_bw_n, perm= , 2, 1])), axis= , keep_dims= ) IDT IDT IDT M_mean_fw = , tf.transpose(Q_fw_n, perm= , 2, 1])), axis= , keep_dims= ) IDT IDT IDT M_mean_bw = , tf.transpose(Q_bw_n, perm= , 2, 1])), axis= , keep_dims= ) IDT IDT IDT M = [] + IDT IDT IDT M.append(M_full_fw) + IDT IDT IDT M.append(M_full_bw) + IDT IDT IDT M.append(M_max_fw) + IDT IDT IDT M.append(M_max_bw) + IDT IDT IDT M.append(M_mean_fw) + IDT IDT IDT M.append(M_mean_bw) + IDT IDT IDT M = tf.concat(2, M) + IDT IDT IDT return P_fw_n + P_bw_n + + IDT def aggregation_layer(self, M, masks, size): + IDT IDT with tf.variable_scope('aggregation'): + IDT IDT IDT cell_fw = tf.nn.rnn_cell.BasicLSTMCell(size) + IDT IDT IDT cell_bw = tf.nn.rnn_cell.BasicLSTMCell(size) + IDT IDT IDT cell_fw = , output_keep_prob= ) IDT IDT IDT cell_bw = , output_keep_prob= ) IDT IDT IDT outputs, final_state = , cell_bw, M, sequence_length= , dtype= ) IDT IDT IDT concat_states = tf.concat(2, outputs) + IDT IDT IDT return concat_states + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT P_filtered = , P= ) IDT IDT question_states, final_question_state = , self.mask_q_placeholder, attention_inputs= , model_type= , reuse= ) IDT IDT question_states_fw, question_states_bw = question_states + IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, attention_inputs= , model_type= , reuse= ) IDT IDT ctx_states_fw, ctx_states_bw = ctx_states + IDT IDT M = , Q_bw= , P_fw= , P_bw= , num_perspectives= ) IDT IDT A = , masks= , size= ) IDT IDT self.start_probs, self.end_probs = , masks= , model_type= , size= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT if self.flags.batch_norm == 0: + IDT IDT IDT IDT self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT IDT self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT else: + IDT IDT IDT IDT question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT IDT context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT IDT q_mean, q_var = , axes= , shift= , keep_dims= ) IDT IDT IDT IDT ctx_mean, ctx_var = , axes= , shift= , keep_dims= ) IDT IDT IDT IDT q_scale = , shape= , self.embed_size], initializer= ) IDT IDT IDT IDT ctx_scale = , shape= , self.embed_size], initializer= ) IDT IDT IDT IDT q_offset = , shape= , self.embed_size], initializer= ) IDT IDT IDT IDT ctx_offset = , shape= , self.embed_size], initializer= ) IDT IDT IDT IDT self.question_embeddings = , q_mean, q_var, q_offset, q_scale, variance_epsilon= ) IDT IDT IDT IDT self.context_embeddings = , ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon= ) + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def answer(self, session, data): + IDT IDT yp_lst = [] + IDT IDT yp2_lst = [] + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle= ) IDT IDT IDT yp, yp2 = self.decode(session, *batch) + IDT IDT IDT yp_lst.append(yp) + IDT IDT IDT yp2_lst.append(yp2) + IDT IDT IDT prog_train.update(i + 1, [('computing F1...', 1)]) + IDT IDT + IDT IDT yp_all = , axis= ) IDT IDT yp2_all = , axis= ) IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(session= , context_batch= , question_batch= , answer_span_batch= , mask_ctx_batch= , mask_q_batch= ) + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset[0]) + IDT IDT else: + IDT IDT IDT inds = np.random.choice(len(dataset[0]), sample) + IDT IDT IDT sampled = [elem[inds] for elem in dataset] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT cur_em = exact_match_score(pred_words, actual_words) + IDT IDT IDT em.append(float(cur_em)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + IDT IDT return np.mean(f1), np.mean(em) + + IDT def run_epoch(self, sess, train_set, val_set, train_context, val_context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT + IDT IDT prog_val = ) IDT IDT for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT + IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) + IDT def train(self, session, dataset, val_dataset, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + IDT IDT train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + IDT IDT val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + IDT IDT val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = [elem[:self.flags.batch_size] for elem in train_dataset] + IDT IDT IDT val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + IDT IDT IDT num_epochs = 100 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , train_context= , val_context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT self.saver.save(session, train_dir + '/qa.ckpt') + + IDT def minibatches(self, data, batch_size, shuffle= ) IDT IDT num_data = len(data[0]) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + IDT IDT indices = np.arange(num_data) + IDT IDT if shuffle: + IDT IDT IDT np.random.shuffle(indices) + IDT IDT for minibatch_start in np.arange(0, num_data, batch_size): + IDT IDT IDT minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + IDT IDT IDT yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from neural.full_connect import load_reformat_not_mnist, accuracy + + +def tf_better_nn(offset_range= , regular= , drop_out= , lrd= ) IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_node_count = 1024 + IDT IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count])) + IDT IDT biases1 = tf.Variable(tf.zeros([hidden_node_count])) + IDT IDT weights2 = tf.Variable(tf.truncated_normal([hidden_node_count, num_labels])) + IDT IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT ys = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT hidden = tf.nn.relu(ys) + IDT IDT h_fc = hidden + IDT IDT valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + IDT IDT valid_hidden1 = tf.nn.relu(valid_y0) + IDT IDT test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + IDT IDT test_hidden1 = tf.nn.relu(test_y0) + IDT IDT keep_prob = tf.placeholder(tf.float32) + IDT IDT if drop_out: + IDT IDT IDT hidden_drop = tf.nn.dropout(hidden, keep_prob) + IDT IDT IDT h_fc = hidden_drop + IDT IDT logits = tf.matmul(h_fc, weights2) + biases2 + IDT IDT logits_predict = tf.matmul(hidden, weights2) + biases2 + IDT IDT valid_predict = tf.matmul(valid_hidden1, weights2) + biases2 + IDT IDT test_predict = tf.matmul(test_hidden1, weights2) + biases2 + IDT IDT l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(biases1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(biases2) + IDT IDT if not regular: + IDT IDT IDT l2_loss = 0 + IDT IDT beta = 0.002 + IDT IDT loss = , labels= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT if lrd: + IDT IDT IDT cur_step = tf.Variable(0) + IDT IDT IDT starter_learning_rate = 0.1 + IDT IDT IDT learning_rate = , cur_step, 10000, 0.96, staircase= ) IDT IDT IDT optimizer = , global_step= ) IDT IDT train_prediction = tf.nn.softmax(logits_predict) + IDT IDT valid_prediction = tf.nn.softmax(valid_predict) + IDT IDT test_prediction = tf.nn.softmax(test_predict) + IDT num_steps = 30001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT if offset_range == -1: + IDT IDT IDT IDT offset_range = train_labels.shape[0] - batch_size + IDT IDT IDT offset = step * batch_size % offset_range + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +def tf_deep_nn(regular= , drop_out= , lrd= , layer_cnt= ) IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_node_count = 1024 + IDT IDT hidden_stddev = np.sqrt(2.0 / 784) + IDT IDT weights1 = , hidden_node_count], stddev= ) IDT IDT biases1 = tf.Variable(tf.zeros([hidden_node_count])) + IDT IDT weights = [] + IDT IDT biases = [] + IDT IDT hidden_cur_cnt = hidden_node_count + IDT IDT for i in range(layer_cnt - 2): + IDT IDT IDT if hidden_cur_cnt > 2: + IDT IDT IDT IDT hidden_next_cnt = int(hidden_cur_cnt / 2) + IDT IDT IDT else: + IDT IDT IDT IDT hidden_next_cnt = 2 + IDT IDT IDT hidden_stddev = np.sqrt(2.0 / hidden_cur_cnt) + IDT IDT IDT weights.append(tf.Variable(tf.truncated_normal([hidden_cur_cnt, hidden_next_cnt], stddev= ) IDT IDT IDT biases.append(tf.Variable(tf.zeros([hidden_next_cnt]))) + IDT IDT IDT hidden_cur_cnt = hidden_next_cnt + IDT IDT y0 = tf.matmul(tf_train_dataset, weights1) + biases1 + IDT IDT hidden = tf.nn.relu(y0) + IDT IDT hidden_drop = hidden + IDT IDT keep_prob = 0.5 + IDT IDT if drop_out: + IDT IDT IDT hidden_drop = tf.nn.dropout(hidden, keep_prob) + IDT IDT valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + IDT IDT valid_hidden = tf.nn.relu(valid_y0) + IDT IDT test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + IDT IDT test_hidden = tf.nn.relu(test_y0) + IDT IDT for i in range(layer_cnt - 2): + IDT IDT IDT y1 = tf.matmul(hidden_drop, weights[i]) + biases[i] + IDT IDT IDT hidden_drop = tf.nn.relu(y1) + IDT IDT IDT if drop_out: + IDT IDT IDT IDT keep_prob += 0.5 * i / (layer_cnt + 1) + IDT IDT IDT IDT hidden_drop = tf.nn.dropout(hidden_drop, keep_prob) + IDT IDT IDT y0 = tf.matmul(hidden, weights[i]) + biases[i] + IDT IDT IDT hidden = tf.nn.relu(y0) + IDT IDT IDT valid_y0 = tf.matmul(valid_hidden, weights[i]) + biases[i] + IDT IDT IDT valid_hidden = tf.nn.relu(valid_y0) + IDT IDT IDT test_y0 = tf.matmul(test_hidden, weights[i]) + biases[i] + IDT IDT IDT test_hidden = tf.nn.relu(test_y0) + IDT IDT weights2 = , num_labels], stddev= ) IDT IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden_drop, weights2) + biases2 + IDT IDT logits_predict = tf.matmul(hidden, weights2) + biases2 + IDT IDT valid_predict = tf.matmul(valid_hidden, weights2) + biases2 + IDT IDT test_predict = tf.matmul(test_hidden, weights2) + biases2 + IDT IDT l2_loss = 0 + IDT IDT if regular: + IDT IDT IDT l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + IDT IDT IDT for i in range(len(weights)): + IDT IDT IDT IDT l2_loss += tf.nn.l2_loss(weights[i]) + IDT IDT IDT beta = 0.25 / batch_size + IDT IDT IDT beta = 1e-05 + IDT IDT IDT l2_loss *= beta + IDT IDT loss = , labels= ) IDT IDT if lrd: + IDT IDT IDT cur_step = , trainable= ) IDT IDT IDT starter_learning_rate = 0.4 + IDT IDT IDT learning_rate = , cur_step, 100000, 0.96, staircase= ) IDT IDT IDT optimizer = , global_step= ) IDT IDT else: + IDT IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits_predict) + IDT IDT valid_prediction = tf.nn.softmax(valid_predict) + IDT IDT test_prediction = tf.nn.softmax(test_predict) + IDT num_steps = 20001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset_range = train_labels.shape[0] - batch_size + IDT IDT IDT offset = step * batch_size % offset_range + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT , test_labels)) + + +if __name__ == '__main__': + IDT image_size = 28 + IDT num_labels = 10 + IDT train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = load_reformat_not_mnist(image_size, num_labels) + IDT tf_deep_nn(layer_cnt= , lrd= , drop_out= , regular= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import dataset +import random +import sys +from tensorflow.python import debug as tf_debug +NUM_CLASSES = len(dataset.CLASSES) +IMAGE_SIZE = 100 + + +def placeholder_inputs(batch_size): + IDT images_placeholder = , shape= , IMAGE_SIZE, IMAGE_SIZE, 3), name= ) IDT labels_placeholder = , shape= ,), name= ) IDT return images_placeholder, labels_placeholder + + +def get_weights(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial, name= ) + +def get_biases(shape): + IDT initial = tf.zeros(shape) + IDT return tf.Variable(initial, name= ) + +def conv_layer(input, filter_size, name, strides= , 1, 1, 1], pool_size= , padding= ) IDT with tf.name_scope(name): + IDT IDT weights = get_weights(filter_size) + IDT IDT biases = get_biases([filter_size[3]]) + IDT IDT conv = tf.nn.leaky_relu(tf.nn.conv2d(input, weights, strides, padding)) + IDT IDT pool = tf.nn.max_pool(conv, [1, pool_size, pool_size, 1], [1, pool_size, pool_size, 1], padding) + IDT IDT return pool + + +def dense_layer(inputs, input_size, output_size, name): + IDT with tf.name_scope(name): + IDT IDT weights = get_weights([input_size, output_size]) + IDT IDT biases = get_biases([output_size]) + IDT IDT h = tf.matmul(inputs, weights) + biases + IDT IDT return tf.nn.leaky_relu(h) + + +def define_model(images): + IDT conv1 = , [20, 20, 3, 10], 'Conv1', pool_size= ) IDT conv2 = conv_layer(conv1, [5, 5, 10, 20], 'Conv2') + IDT conv2 = tf.reshape(conv2, [-1, 13 * 13 * 20]) + IDT dense1 = dense_layer(conv2, 13 * 13 * 20, 1000, 'Dense1') + IDT dense2 = dense_layer(dense1, 1000, 100, 'Dense2') + IDT logits = dense_layer(dense2, 100, NUM_CLASSES, 'Softmax_Linear') + IDT return logits + + +def define_loss(logits, labels): + IDT labels = tf.to_int64(labels) + IDT cross_entropy = , logits= , name= ) IDT loss = , name= ) IDT return loss + + +def training(loss, learning_rate, momentum= , beta1= , beta2= ) IDT tf.summary.scalar('loss', loss) + IDT optimizer = , beta1= , beta2= ) IDT train_op = optimizer.minimize(loss) + IDT return train_op + + +def evaluation(logits, labels): + IDT correct = tf.nn.in_top_k(logits, labels, 1) + IDT return tf.reduce_sum(tf.cast(correct, tf.int32)) + + +def do_evaluation(session, eval_correct, testDataset, batch_size, images_placeholder, labels_placeholder): + IDT training_data, training_labels = testDataset + IDT batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + IDT totalCorrect = 0 + IDT for batch in batches: + IDT IDT image_data = batch[0] + IDT IDT label_data = batch[1] + IDT IDT feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + IDT IDT totalCorrect += , feed_dict= ) IDT precision = float(totalCorrect) / len(training_data) + IDT , totalCorrect, precision)) + IDT sys.stdout.flush() + IDT return precision > 0.95 + + +def run_training(batch_size, learning_rate, epochs, run_number): + IDT with tf.Graph().as_default(): + IDT IDT images_placeholder, labels_placeholder = placeholder_inputs(batch_size) + IDT IDT logits = define_model(images_placeholder) + IDT IDT lossFunction = define_loss(logits, labels_placeholder) + IDT IDT train_op = training(lossFunction, learning_rate) + IDT IDT eval_correct = evaluation(logits, labels_placeholder) + IDT IDT saver = tf.train.Saver() + IDT IDT init = tf.global_variables_initializer() + IDT IDT with tf.Session() as session: + IDT IDT IDT logdir = 'log/' + str(run_number) + IDT IDT IDT session.run(init) + IDT IDT IDT for step in range(epochs): + IDT IDT IDT IDT training_data, training_labels = dataset.get_training_data() + IDT IDT IDT IDT batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + IDT IDT IDT IDT epochLoss = 0 + IDT IDT IDT IDT for batch in batches: + IDT IDT IDT IDT IDT image_data = batch[0] + IDT IDT IDT IDT IDT label_data = batch[1] + IDT IDT IDT IDT IDT feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + IDT IDT IDT IDT IDT activations, loss_value = , lossFunction], feed_dict= ) IDT IDT IDT IDT IDT epochLoss += loss_value + IDT IDT IDT IDT if step % 2 == 0: + IDT IDT IDT IDT IDT ) + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT early_stop = False + IDT IDT IDT IDT if (step + 1) % 5 == 0 or step + 1 == epochs: + IDT IDT IDT IDT IDT validation_data = dataset.get_validation_data(batch_size) + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT IDT early_stop = do_evaluation(session, eval_correct, validation_data, batch_size, images_placeholder, labels_placeholder) + IDT IDT IDT IDT if step + 1 == epochs or early_stop: + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT IDT do_evaluation(session, eval_correct, (training_data, training_labels), batch_size, images_placeholder, labels_placeholder) + IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT sys.stdout.flush() + IDT IDT IDT IDT IDT test_data = dataset.get_test_data(batch_size) + IDT IDT IDT IDT IDT do_evaluation(session, eval_correct, test_data, batch_size, images_placeholder, labels_placeholder) + IDT IDT IDT IDT IDT saver.save(session, 'model.ckpt') + IDT IDT IDT IDT IDT if early_stop: + IDT IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT IDT return + + +run_training(89, 0.001, 40, 13) +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +get_ipython().magic('matplotlib inline') +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT logits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + IDT with tf.Session(graph= ) IDT IDT tf.initialize_all_variables().run() + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (1-layer net)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta_regul = tf.placeholder(tf.float32) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT logits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_bacthes + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT drop1 = tf.nn.dropout(lay1_train, 0.5) + IDT logits = tf.matmul(drop1, weights2) + biases2 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_batches = 3 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_batches + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_labels], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT logits = tf.matmul(lay2_train, weights3) + biases3 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) + IDT learning_rate = , global_step, 1000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_hidden_nodes3], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + IDT weights4 = , num_labels], stddev= ) IDT biases4 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + IDT logits = tf.matmul(lay3_train, weights4) + biases4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT learning_rate = , global_step, 4000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 18001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 512 +num_hidden_nodes3 = 256 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_hidden_nodes3], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + IDT weights4 = , num_labels], stddev= ) IDT biases4 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT drop1 = tf.nn.dropout(lay1_train, 0.5) + IDT lay2_train = tf.nn.relu(tf.matmul(drop1, weights2) + biases2) + IDT drop2 = tf.nn.dropout(lay2_train, 0.5) + IDT lay3_train = tf.nn.relu(tf.matmul(drop2, weights3) + biases3) + IDT drop3 = tf.nn.dropout(lay3_train, 0.5) + IDT logits = tf.matmul(drop3, weights4) + biases4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT learning_rate = , global_step, 5000, 0.8, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 20001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import tensorflow as tf +import math +from tensorflow.examples.tutorials.mnist import input_data +mnist = , one_hot= ) + +def weight_variable(shape, stddev): + IDT x = , stddev= ) IDT return tf.Variable(x) + + +def bias_variable(shape): + IDT x = tf.zeros(shape) + IDT return tf.Variable(x) + + + +train_input = , shape= , 784]) )train_output = , shape= , 10]) )dropout = tf.placeholder(tf.float32) +weight1 = weight_variable([5, 5, 1, 8], 1.0) +bias1 = bias_variable([8]) +train_image = tf.reshape(train_input, [-1, 28, 28, 1]) +hidden1 = , weight1, strides= , 1, 1, 1], padding= )hiddenp1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= )weight2 = weight_variable([5, 5, 8, 32], 0.5) +bias2 = bias_variable([32]) +hidden2 = , weight2, strides= , 1, 1, 1], padding= )hiddenp2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= )weight5 = weight_variable([2, 2, 32, 64], 0.5) +bias5 = bias_variable([64]) +hiddenp3 = , weight5, strides= , 1, 1, 1], padding= )weight3 = weight_variable([7 * 7 * 64, 1024], 1.0) +bias3 = bias_variable([1024]) +hidden3 = tf.reshape(hiddenp3, [-1, 7 * 7 * 64]) +hidden4 = tf.nn.relu(tf.matmul(hidden3, weight3) + bias3) +hidden_drop = tf.nn.dropout(hidden4, dropout) +weight4 = weight_variable([1024, 120], 1.0) +weight6 = weight_variable([120, 10], 1.0) +bias4 = bias_variable([120]) +bias6 = bias_variable([10]) +hidden5 = tf.matmul(hidden_drop, weight4) + bias4 +hidden6 = tf.matmul(hidden5, weight6) + bias6 +output = tf.nn.softmax(hidden6) +learning_rate = tf.placeholder(tf.float32) +cross_entropy = -tf.reduce_sum(train_output * tf.log(output)) +train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) +correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(train_output, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) +saver = tf.train.Saver() +init = tf.initialize_all_variables() +num_steps = 20000 +max_accuracy = 0 +count = 0 +avg_accuracy = 0 + +rate = 0.0001 +session = tf.InteractiveSession() +session.run(init) +iterations = 5 +for i in range(num_steps): + IDT batch = mnist.train.next_batch(50) + IDT feed_dict = {train_input: batch[0], train_output: batch[1], dropout: 0.8, learning_rate: rate} + IDT for j in range(iterations): + IDT IDT train_step.run(feed_dict= ) IDT if i % 50 == 0 and i > 0: + IDT IDT feed_dict[dropout] = 1 + IDT IDT train_accuracy = ) IDT IDT avg_accuracy += train_accuracy + IDT IDT if train_accuracy > max_accuracy: + IDT IDT IDT max_accuracy = train_accuracy + IDT IDT IDT saver.save(session, './mnist_best.ckpt') + IDT IDT if max_accuracy > 0.92: + IDT IDT IDT break + IDT IDT + ' and accuracy : ' + str(train_accuracy) + ' and count : ' + str(count) + '/50') + IDT if i % 500 == 0: + IDT IDT avg_accuracy /= 10 + IDT IDT ) + IDT IDT avg_accuracy = 0 + IDT if i % 1000 == 0 and i > 0: + IDT IDT rate *= 0.8 +feed_dict = {train_input: mnist.test.images, train_output: mnist.test.labels, dropout: 1} +)) +path = saver.save(session, './mnist.ckpt') +) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +import pickle +import os +import matplotlib.pyplot as plt +dirname = os.path.dirname +dataPath = dirname(dirname(os.getcwd())) + '\\DataSets\\' +pickle_file = dataPath + 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +def best_accuracy_id(accuracies): + IDT return accuracies.index(max(accuracies)) + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +accuracies = [] +beta_val = [i for i in np.arange(0, 0.01, 0.0005)] +for reg in beta_val: + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: reg} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracies.append(accuracy(test_prediction.eval(), test_labels)) +]) +plt.semilogx(beta_val, accuracies) +plt.grid(True) +plt.title('Test Accuracy versus regularization Curve') +plt.show() +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: beta_val[best_accuracy_id(accuracies)]} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta = tf.placeholder(tf.float32) + IDT weights_hidden = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases_hidden = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT hidden_layer_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + IDT logits = tf.matmul(hidden_layer_train, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_hidden)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, weights) + biases) + IDT test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden, weights) + biases) +accuracies = [] +for reg in beta_val: + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: reg} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT accuracies.append(accuracy(test_prediction.eval(), test_labels)) +]) +plt.semilogx(beta_val, accuracies) +plt.grid(True) +plt.title('Test Accuracy versus regularization Curve') +plt.show() +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: beta_val[best_accuracy_id(accuracies)]} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta = tf.placeholder(tf.float32) + IDT weights_hidden = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases_hidden = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT hidden_layer_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + IDT logits = tf.matmul(hidden_layer_train, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, weights) + biases) + IDT test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden, weights) + biases) +num_steps = 100 +num_batches = 3 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_batches + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: 0} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT beta = tf.placeholder(tf.float32) + IDT weights_hidden = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases_hidden = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT hidden_layer_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + IDT drop1 = tf.nn.dropout(hidden_layer_train, 0.5) + IDT logits = tf.matmul(drop1, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + IDT valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, weights) + biases) + IDT test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + IDT test_prediction = tf.nn.softmax(tf.matmul(test_hidden, weights) + biases) +num_steps = 100 +num_batches = 3 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step % num_batches + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: 0} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 2 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_hidden_nodes3], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + IDT weights4 = , num_labels], stddev= ) IDT biases4 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + IDT logits = tf.matmul(lay3_train, weights4) + biases4 + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT learning_rate = , global_step, 4000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 18001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +from time import time +from sys import stdout +import os +NUM_EPOCHS = 100000 +BATCH_SIZE = 128 +VALIDATION_SIZE = 4096 +LOG_FREQUENCY = 25 +INITIAL_LEARNING_RATE = 0.01 +DECAY_RATE = 0.1 +NUM_EPOCHS_PER_DECAY = NUM_EPOCHS // 4 +MODEL_CKPT_DIR = os.path.join('.', 'model') +train_data, validation_data, test_data = , one_hot= , reshape= , validation_size= )log_device_placement = False +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0' if log_device_placement else '3' + + +def build_model(images, bIsTraining): + IDT with tf.name_scope('Model'): + IDT IDT nn = , training= ) IDT IDT nn = , filters= , kernel_size= , strides= , 1), padding= ) IDT IDT nn = , pool_size= , strides= , 1), padding= ) IDT IDT nn = tf.nn.relu(nn) + IDT IDT for i in range(3): + IDT IDT IDT nn = , training= ) IDT IDT IDT nn = , filters= , kernel_size= , strides= , 1), padding= ) IDT IDT IDT nn = , pool_size= , strides= , 1), padding= ) IDT IDT IDT nn = tf.nn.relu(nn) + IDT IDT nn = , training= ) IDT IDT nn = , filters= , kernel_size= , strides= , 1), padding= ) IDT IDT nn = , pool_size= , strides= , 1), padding= ) IDT IDT nn = tf.nn.relu(nn) + IDT IDT nn = tf.layers.flatten(nn) + IDT IDT nn = , rate= , training= ) IDT IDT nn = , units= ) IDT IDT nn = tf.nn.relu(nn) + IDT IDT nn = , rate= , training= ) IDT IDT nn = , units= ) IDT IDT nn = tf.nn.relu(nn) + IDT IDT nn = , rate= , training= ) IDT IDT nn = , units= ) IDT IDT logits = tf.nn.softmax(nn) + IDT IDT return tf.cond(bIsTraining, true_fn= , false_fn= ) + +def get_loss_op(labels, logits): + IDT loss_op = , logits= ) IDT return tf.reduce_mean(loss_op) + + +def get_accuracy_op(labels, logits): + IDT prediction = tf.argmax(logits, 1) + IDT correct_prediction = , tf.argmax(labels, axis= ) IDT accuray_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT return tf.multiply(accuray_op, tf.constant(100.0, dtype= ) + +def get_train_op(loss_op, global_step): + IDT decay_steps = NUM_EPOCHS_PER_DECAY + IDT lr = , global_step, decay_steps, DECAY_RATE, staircase= ) IDT update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + IDT opt = tf.train.GradientDescentOptimizer(lr) + IDT with tf.control_dependencies(update_ops): + IDT IDT grads = opt.compute_gradients(loss_op) + IDT IDT apply_gradient_op = , global_step= ) IDT return apply_gradient_op + + +def train(): + IDT images = , shape= , 28, 28, 1]) ) IDT labels = , shape= , 10]) ) IDT bIsTraining = tf.placeholder(tf.bool) + IDT global_step = tf.train.get_or_create_global_step() + IDT logits = build_model(images, bIsTraining) + IDT loss_op = get_loss_op(labels, logits) + IDT train_op = get_train_op(loss_op, global_step) + IDT accuracy_op = get_accuracy_op(labels, logits) + IDT scaffold = , local_init_op= ) + + IDT class _LoggerHook(tf.train.SessionRunHook): + IDT IDT + + IDT IDT def __init__(self, *args, **kwargs): + IDT IDT IDT super(*args, **kwargs) + IDT IDT IDT self._start_time = time() + IDT IDT IDT self._train_losses = , dtype= ) IDT IDT IDT self._train_accuracies = , dtype= ) + IDT IDT def begin(self): + IDT IDT IDT ) + + IDT IDT def before_run(self, run_context): + IDT IDT IDT return tf.train.SessionRunArgs([loss_op, accuracy_op]) + + IDT IDT def after_run(self, run_context, run_values): + IDT IDT IDT loss_value, accuracy_value = run_values.results + IDT IDT IDT self._train_losses = np.append(self._train_losses, loss_value) + IDT IDT IDT self._train_accuracies = np.append(self._train_accuracies, accuracy_value) + IDT IDT IDT sess = run_context.session + IDT IDT IDT step = sess.run(global_step) + IDT IDT IDT if step % LOG_FREQUENCY == 0: + IDT IDT IDT IDT current_time = time() + IDT IDT IDT IDT duration = current_time - self._start_time + IDT IDT IDT IDT self._start_time = current_time + IDT IDT IDT IDT X, Y = validation_data.next_batch(BATCH_SIZE) + IDT IDT IDT IDT avg_validation_acc = , feed_dict= , labels: Y, bIsTraining: False}) ) IDT IDT IDT IDT avg_train_loss = , dtype= ) IDT IDT IDT IDT avg_train_acc = , dtype= ) IDT IDT IDT IDT ) + IDT IDT IDT IDT self._train_losses, self._train_accuracies = np.array([]), np.array([]) + IDT with tf.train.MonitoredTrainingSession(checkpoint_dir= , scaffold= , hooks= , tf.train.NanTensorHook(loss_op), _LoggerHook()]) ) IDT IDT while not mon_sess.should_stop(): + IDT IDT IDT X, Y = train_data.next_batch(BATCH_SIZE) + IDT IDT IDT mon_sess.run(train_op, feed_dict= , labels: Y, bIsTraining: True}) ) + +def load_and_test(): + IDT tf.reset_default_graph() + IDT NUM_TEST_IMGS = test_data.num_examples + IDT images = , shape= , 28, 28, 1]) ) IDT labels = , shape= , 10]) ) IDT bIsTraining = tf.placeholder(tf.bool) + IDT logits = build_model(images, bIsTraining) + IDT saver = tf.train.Saver() + IDT with tf.Session() as sess: + IDT IDT saver.restore(sess, tf.train.latest_checkpoint(MODEL_CKPT_DIR)) + IDT IDT is_correct_prediction = , axis= , tf.argmax(labels, axis= , []), dtype= ) IDT IDT total_correct_predictions = , dtype= , trainable= ) IDT IDT predict_op = tf.assign_add(total_correct_predictions, is_correct_prediction) + IDT IDT sess.run(tf.variables_initializer([total_correct_predictions])) + IDT IDT for i in range(NUM_TEST_IMGS): + IDT IDT IDT stdout.write('\rPredicting %d/%d' % (i + 1, NUM_TEST_IMGS)) + IDT IDT IDT X, Y = test_data.next_batch(1) + IDT IDT IDT sess.run(predict_op, feed_dict= , labels: Y, bIsTraining: False}) ) IDT IDT stdout.write('\n') + IDT IDT num_correct = sess.run(total_correct_predictions) + IDT * 100)) + + +def main(): + IDT train() + IDT load_and_test() + + +if __name__ == '__main__': + IDT main() +import numpy as np +import tensorflow as tf +X = , [10, 10], name= )Y1 = , [10, 20], name= )Y2 = , [10, 20], name= )initial_shared_layer_weights = np.random.rand(10, 20) +initial_Y1_layer_weights = np.random.rand(20, 20) +initial_Y2_layer_weights = np.random.rand(20, 20) +shared_layer_weights = , name= , dtype= )Y1_layer_weights = , name= , dtype= )Y2_layer_weights = , name= , dtype= )shared_layer = tf.nn.relu(tf.matmul(X, shared_layer_weights)) +Y1_layer = tf.nn.relu(tf.matmul(shared_layer, Y1_layer_weights)) +Y2_layer = tf.nn.relu(tf.matmul(shared_layer, Y2_layer_weights)) +Y1_Loss = tf.nn.l2_loss(Y1 - Y1_layer) +Y2_Loss = tf.nn.l2_loss(Y2 - Y2_layer) +Joint_Loss = Y1_Loss + Y2_Loss +Optimiser = tf.train.AdamOptimizer().minimize(Joint_Loss) +Y1_op = tf.train.AdamOptimizer().minimize(Y1_Loss) +Y2_op = tf.train.AdamOptimizer().minimize(Y2_Loss) +with tf.Session() as session: + IDT session.run(tf.initialize_all_variables()) + IDT _, Joint_Loss = session.run([Optimiser, Joint_Loss], {X: np.random.rand(10, 10) * 10, Y1: np.random.rand(10, 20) * 10, Y2: np.random.rand(10, 20) * 10}) + IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) IDT IDT self.weights = , name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.optimization = ) + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT IDT self.tgt_form_emb = , self.form_dim)), name= ) IDT IDT IDT self.tgt_pos_emb = , self.pos_dim)), name= ) IDT IDT IDT self.tgt_deprel_emb = , self.deprel_dim)), name= ) IDT IDT IDT tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT self.tgt_W0 = , self.hidden_dim)), name= ) IDT IDT self.tgt_b0 = , name= ) IDT IDT self.tgt_W1 = , self.output_dim)), name= ) IDT IDT self.tgt_b1 = , name= ) IDT IDT self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + IDT IDT self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1) * actions_one_hot, reduction_indices= ) IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT self.loss = tf.reduce_mean(tf.multiply(self.weights, tf.square(predicted_q - self.output))) + l2 * regularizer + IDT IDT self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + IDT def train(self, session, inputs, action, outputs, weights): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs, self.weights: weights}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def target_policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.tgt_q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def update_target(self, session): + IDT IDT session.run(self.update_op) + + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) +import numpy as np +import random as random +import tensorflow as tf +import pickle +from collections import deque + + +class FNN: + IDT """ + IDT neural network model + IDT """ + + IDT def __init__(self, num_actions, STATES_SHAPE, params= ) IDT IDT self.num_actions = num_actions + IDT IDT self.state_shape = STATES_SHAPE + IDT IDT self.LEARNING_RATE = params['LearningRate'] + IDT IDT self.reg = params['reg'] + IDT IDT self.num_hidden = params['HiddenLayerNum'] + IDT IDT self.hidden_size = params['HiddenLayerSize'] + IDT IDT self.log_file_path = params['log_file_path'] + IDT IDT self.session = self.create_model() + + IDT def define_placeholders(self): + IDT IDT input_placeholder = , shape= , self.state_shape)) ) IDT IDT output_placeholder = , shape= , self.num_actions)) ) IDT IDT return input_placeholder, output_placeholder + + IDT def variable_summaries(self, var, name): + IDT IDT with tf.name_scope('summaries'): + IDT IDT IDT mean = tf.reduce_mean(var) + IDT IDT IDT tf.scalar_summary('mean/' + name, mean) + IDT IDT with tf.name_scope('stddev'): + IDT IDT IDT stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean))) + IDT IDT tf.scalar_summary('sttdev/' + name, stddev) + IDT IDT tf.scalar_summary('max/' + name, tf.reduce_max(var)) + IDT IDT tf.scalar_summary('min/' + name, tf.reduce_min(var)) + + IDT def multilayer_perceptron(self, input_mat): + IDT IDT n_input = self.state_shape + IDT IDT n_hidden_1 = self.hidden_size + IDT IDT n_hidden_2 = self.hidden_size + IDT IDT n_classes = self.num_actions + IDT IDT weights = , n_hidden_1], stddev= , 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2], stddev= , 'out': tf.Variable(tf.truncated_normal([n_hidden_2, n_classes], stddev= ) IDT IDT biases = , stddev= , 'b2': tf.Variable(tf.random_normal([n_hidden_2], stddev= , 'out': tf.Variable(tf.random_normal([n_classes], stddev= ) IDT IDT layer_1 = tf.add(tf.matmul(input_mat, weights['h1']), biases['b1']) + IDT IDT layer_1 = tf.nn.relu(layer_1) + IDT IDT layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) + IDT IDT layer_2 = tf.nn.relu(layer_2) + IDT IDT out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + IDT IDT reg_term = self.reg * (tf.reduce_sum(tf.square(weights['h1'])) + tf.reduce_sum(tf.square(weights['h2'])) + tf.reduce_sum(tf.square(weights['out']))) + IDT IDT return out_layer, reg_term, weights, biases + + IDT def create_model(self): + IDT IDT self.input_placeholder, self.output_placeholder = self.define_placeholders() + IDT IDT output, reg_term, weights, biases = self.multilayer_perceptron(self.input_placeholder) + IDT IDT self.variable_summaries(weights['h1'], 'layer1' + '/weights') + IDT IDT self.variable_summaries(weights['h2'], 'layer2' + '/weights') + IDT IDT self.variable_summaries(weights['out'], 'layer3' + '/weights') + IDT IDT self.NN_output = output + IDT IDT self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, self.output_placeholder)) + IDT IDT tf.scalar_summary('cost', self.cost) + IDT IDT optimizer = ) IDT IDT self.train_op = optimizer.minimize(self.cost) + IDT IDT init = tf.initialize_all_variables() + IDT IDT """ + IDT IDT Default runs at cpu, + + IDT IDT For GPU runnning, it takes 33 percent of entire gpu memory + IDT IDT use this: + IDT IDT gpu_options = ) IDT IDT session = ) IDT IDT """ + IDT IDT session = tf.Session() + IDT IDT session.run(init) + IDT IDT self.saver_op = tf.train.Saver() + IDT IDT self.merged = tf.merge_all_summaries() + IDT IDT self.train_writer = tf.train.SummaryWriter('dqn_log_files/log1/data3', session.graph) + IDT IDT return session + + IDT def init_t(self): + IDT IDT self.t = 0 + + IDT def save_model(self, path): + IDT IDT save_path = self.saver_op.save(self.session, path) + IDT IDT + + IDT def restore_model(self, path): + IDT IDT self.saver_op.restore(self.session, path) + + IDT def train_step(self, Input_mat, actions): + IDT IDT _, cost, prediction_probs, self.summary = , self.cost, self.NN_output, self.merged], feed_dict= , self.output_placeholder: actions}) ) IDT IDT return cost + + IDT def predict(self, states, label): + IDT IDT cost, prediction_probs = , self.NN_output], feed_dict= , self.output_placeholder: label}) ) IDT IDT return prediction_probs +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + IDT data_d = , keep_prob= ) IDT output1 = tf.matmul(data_d, weights1) + biases1 + IDT relu1 = tf.nn.relu(output1) + IDT logits = tf.matmul(relu1, weights2) + biases2 + IDT return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + IDT biases1 = tf.Variable(tf.zeros([hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + IDT test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT t0 = time() + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT + IDT - t0, 3))) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import layers_dihedral_equi as nn + + +def summary_images(x, name): + IDT for i in range(min(4, x.get_shape().as_list()[3])): + IDT IDT tf.summary.image('{}-{}'.format(name, i), x[:, :, :, i:i + 1]) + + +class CNN: + + IDT def __init__(self): + IDT IDT self.tfx = None + IDT IDT self.tfy = None + IDT IDT self.tfp = None + IDT IDT self.xent = None + IDT IDT self.tftrain_step = None + IDT IDT self.tfkp = None + IDT IDT self.tfacc = None + IDT IDT self.train_counter = 0 + IDT IDT self.test = None + IDT IDT self.embedding_input = None + + IDT def NN(self, x): + IDT IDT assert x.get_shape().as_list()[:3] == [None, 101, 101] + IDT IDT summary_images(x, 'layer0') + IDT IDT x = , 8 * 4, w= , input_repr= ) IDT IDT x = nn.convolution(x) + IDT IDT summary_images(x, 'layer2') + IDT IDT x = nn.max_pool(x) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 48, 48, 8 * 4] + IDT IDT x = nn.convolution(x, 8 * 8) + IDT IDT x = nn.convolution(x) + IDT IDT summary_images(x, 'layer4') + IDT IDT x = nn.max_pool(x) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT assert x.get_shape().as_list() == [None, 22, 22, 8 * 8] + IDT IDT x = nn.convolution(x, 8 * 16) + IDT IDT x = nn.convolution(x) + IDT IDT summary_images(x, 'layer6') + IDT IDT x = nn.max_pool(x) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT assert x.get_shape().as_list() == [None, 9, 9, 8 * 16] + IDT IDT x = nn.convolution(x, 8 * 32) + IDT IDT summary_images(x, 'layer7') + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.convolution(x) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT assert x.get_shape().as_list() == [None, 5, 5, 8 * 32] + IDT IDT x = , 8 * 128, w= ) IDT IDT assert x.get_shape().as_list() == [None, 1, 1, 8 * 128] + IDT IDT x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + IDT IDT self.embedding_input = x + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.fullyconnected(x, 8 * 256) + IDT IDT x = tf.nn.dropout(x, self.tfkp) + IDT IDT x = nn.fullyconnected(x, 8 * 256) + IDT IDT x = nn.batch_normalization(x, self.tfacc) + IDT IDT self.test = x + IDT IDT x = , 1, output_repr= , activation= ) IDT IDT return x + + IDT def create_architecture(self, bands): + IDT IDT self.tfkp = , tf.float32), [], name= ) IDT IDT self.tfacc = , tf.float32), [], name= ) IDT IDT x = , [None, 101, 101, bands], name= ) IDT IDT with tf.name_scope('nn'): + IDT IDT IDT x = self.NN(x) + IDT IDT assert x.get_shape().as_list() == [None, 1] + IDT IDT self.tfp = tf.nn.sigmoid(tf.reshape(x, [-1])) + IDT IDT with tf.name_scope('xent'): + IDT IDT IDT self.tfy = tf.placeholder(tf.float32, [None]) + IDT IDT IDT xent = , labels= , [-1, 1])) ) IDT IDT IDT self.xent = tf.reduce_mean(xent) + IDT IDT with tf.name_scope('train'): + IDT IDT IDT self.tftrain_step = tf.train.AdamOptimizer(0.0001).minimize(self.xent) + + IDT @staticmethod + IDT def split_test_train(path): + IDT IDT import os + IDT IDT files = ['{}/{}'.format(path, f) for f in sorted(os.listdir(path))] + IDT IDT return files[:3000], files[3000:] + + IDT @staticmethod + IDT def load(files): + IDT IDT xs = np.stack([np.load(f)['image'] for f in files]) + IDT IDT return CNN.prepare(xs) + + IDT @staticmethod + IDT def prepare(images): + IDT IDT images[images == 100] = 0.0 + IDT IDT if images.shape[-1] == 1: + IDT IDT IDT images = (images - 4.337e-13) / 5.504e-12 + IDT IDT elif images.shape[-1] == 4: + IDT IDT IDT images = (images - 1.685e-12) / 5.122e-11 + IDT IDT else: + IDT IDT IDT + IDT IDT return images + + IDT @staticmethod + IDT def batch(files, labels): + IDT IDT id0 = ) IDT IDT id1 = ) IDT IDT k = 15 + IDT IDT idn = , k, replace= ) IDT IDT idp = , k, replace= ) IDT IDT xs = CNN.load([files[i] for i in idp] + [files[i] for i in idn]) + IDT IDT ys = np.concatenate((labels[idp], labels[idn])) + IDT IDT for i in range(len(xs)): + IDT IDT IDT s = np.random.uniform(0.8, 1.2) + IDT IDT IDT u = np.random.uniform(-0.1, 0.1) + IDT IDT IDT xs[i] = xs[i] * s + u + IDT IDT return xs, ys + + IDT def train(self, session, xs, ys, options= , run_metadata= , tensors= ) IDT IDT if tensors is None: + IDT IDT IDT tensors = [] + IDT IDT acc = 0.6 ** (self.train_counter / 1000.0) + IDT IDT kp = 0.5 + 0.5 * 0.5 ** (self.train_counter / 2000.0) + IDT IDT output = , self.xent] + tensors, feed_dict= , self.tfy: ys, self.tfkp: kp, self.tfacc: acc}, options= , run_metadata= ) IDT IDT self.train_counter += 1 + IDT IDT return output[1], output[2:] + + IDT def predict(self, session, images): + IDT IDT return session.run(self.tfp, feed_dict= ) + IDT def predict_xentropy(self, session, images, labels): + IDT IDT return session.run([self.tfp, self.xent], feed_dict= , self.tfy: labels}) )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +if sys.platform == 'win32': + IDT data_root = 'C:\\data\\' +elif sys.platform == 'linux': + IDT data_root = '/home/jovarty/data' +else: + IDT raise Exception('Unknown OS') +pickle_file = 'notMNIST.pickle' +dest_file_path = os.path.join(data_root, pickle_file) +with open(dest_file_path, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +def LogisticRegression(l2_weight): + IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT IDT loss = , logits= ) IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + IDT IDT num_steps = 3001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT return accuracy(test_prediction.eval(), test_labels) + + +def OneHiddenLayer(l2_weight): + IDT batch_size = 128 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_layer_size = 1024 + IDT IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_size])) + IDT IDT hidden_biases = tf.Variable(tf.zeros([hidden_layer_size])) + IDT IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + IDT IDT weights = tf.Variable(tf.truncated_normal([hidden_layer_size, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden_layer, weights) + biases + IDT IDT loss = , logits= ) IDT IDT +l2_weight * tf.nn.l2_loss(weights) + IDT IDT +l2_weight * tf.nn.l2_loss(hidden_weights) + IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + IDT IDT valid_logits = tf.matmul(valid_hidden, weights) + biases + IDT IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT IDT test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + IDT IDT test_logits = tf.matmul(test_hidden, weights) + biases + IDT IDT test_prediction = tf.nn.softmax(test_logits) + IDT num_steps = 3001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT return accuracy(test_prediction.eval(), test_labels) + + +def OneHiddenLayer_WithSmallBatchSize(l2_weight): + IDT batch_size = 3 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_layer_size = 1024 + IDT IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_size])) + IDT IDT hidden_biases = tf.Variable(tf.zeros([hidden_layer_size])) + IDT IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + IDT IDT weights = tf.Variable(tf.truncated_normal([hidden_layer_size, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden_layer, weights) + biases + IDT IDT loss = , logits= ) IDT IDT +l2_weight * tf.nn.l2_loss(weights) + IDT IDT +l2_weight * tf.nn.l2_loss(hidden_weights) + IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + IDT IDT valid_logits = tf.matmul(valid_hidden, weights) + biases + IDT IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT IDT test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + IDT IDT test_logits = tf.matmul(test_hidden, weights) + biases + IDT IDT test_prediction = tf.nn.softmax(test_logits) + IDT num_steps = 3001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT return accuracy(test_prediction.eval(), test_labels) + + +result = OneHiddenLayer_WithSmallBatchSize(0.0) + + '%') + + +def OneHiddenLayer_Dropout(l2_weight): + IDT batch_size = 10 + IDT graph = tf.Graph() + IDT with graph.as_default(): + IDT IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT IDT tf_train_labels = , shape= , num_labels)) ) IDT IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT IDT tf_test_dataset = tf.constant(test_dataset) + IDT IDT hidden_layer_size = 1024 + IDT IDT hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_size])) + IDT IDT hidden_biases = tf.Variable(tf.zeros([hidden_layer_size])) + IDT IDT hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + IDT IDT keep_prob = tf.placeholder(tf.float32) + IDT IDT hidden_layer_drop = tf.nn.dropout(hidden_layer, keep_prob) + IDT IDT weights = tf.Variable(tf.truncated_normal([hidden_layer_size, num_labels])) + IDT IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT IDT logits = tf.matmul(hidden_layer_drop, weights) + biases + IDT IDT loss = , logits= ) IDT IDT +l2_weight * tf.nn.l2_loss(weights) + IDT IDT +l2_weight * tf.nn.l2_loss(hidden_weights) + IDT IDT optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) + IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + IDT IDT valid_logits = tf.matmul(valid_hidden, weights) + biases + IDT IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT IDT test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + IDT IDT test_logits = tf.matmul(test_hidden, weights) + biases + IDT IDT test_prediction = tf.nn.softmax(test_logits) + IDT num_steps = 5001 + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run(feed_dict= ) IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 1.0} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT ) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT return accuracy(test_prediction.eval(), test_labels) +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +import load_cifer10 +import random +from PIL import Image +np.random.seed(20160612) +tf.set_random_seed(20160612) + + +class layer0: + + IDT def __init__(self): + IDT IDT with tf.Graph().as_default(): + IDT IDT IDT self.prepare_model() + IDT IDT IDT self.prepare_session() + + IDT def prepare_model(self): + IDT IDT with tf.name_scope('input_layer'): + IDT IDT IDT with tf.name_scope('input_size'): + IDT IDT IDT IDT input_size = 3072 + IDT IDT IDT with tf.name_scope('x'): + IDT IDT IDT IDT x = tf.placeholder(tf.float32, [None, input_size]) + IDT IDT with tf.name_scope('layer0_output_fully_connected'): + IDT IDT IDT with tf.name_scope('w3'): + IDT IDT IDT IDT w3 = , 10], mean= , stddev= ) IDT IDT IDT with tf.name_scope('b3'): + IDT IDT IDT IDT b3 = tf.Variable(tf.zeros([10])) + IDT IDT IDT with tf.name_scope('p'): + IDT IDT IDT IDT p = tf.nn.softmax(tf.matmul(x, w3) + b3) + IDT IDT IDT t = tf.placeholder(tf.float32, [None, 10]) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT with tf.name_scope('loss'): + IDT IDT IDT IDT loss = -tf.reduce_sum(t * tf.log(tf.clip_by_value(p, 1e-10, 1.0))) + IDT IDT IDT with tf.name_scope('train_step'): + IDT IDT IDT IDT train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + IDT IDT IDT with tf.name_scope('correct_prediction'): + IDT IDT IDT IDT correct_prediction = tf.equal(tf.argmax(p, 1), tf.argmax(t, 1)) + IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT IDT tf.summary.scalar('loss', loss) + IDT IDT tf.summary.scalar('accuracy', accuracy) + IDT IDT tf.summary.scalar('w3', tf.reduce_sum(w3)) + IDT IDT self.x, self.t, self.p = x, t, p + IDT IDT self.train_step = train_step + IDT IDT self.loss = loss + IDT IDT self.accuracy = accuracy + IDT IDT self.w3 = w3 + + IDT def prepare_session(self): + IDT IDT sess = tf.InteractiveSession() + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT summary = tf.summary.merge_all() + IDT IDT writer = tf.summary.FileWriter('./log/cifar10_logs0', sess.graph) + IDT IDT self.sess = sess + IDT IDT self.summary = summary + IDT IDT self.writer = writer + + +class layer1: + + IDT def __init__(self): + IDT IDT with tf.Graph().as_default(): + IDT IDT IDT self.prepare_model() + IDT IDT IDT self.prepare_session() + + IDT def prepare_model(self): + IDT IDT with tf.name_scope('input_layer'): + IDT IDT IDT with tf.name_scope('input_size'): + IDT IDT IDT IDT input_size = 3072 + IDT IDT IDT with tf.name_scope('x'): + IDT IDT IDT IDT x = tf.placeholder(tf.float32, [None, input_size]) + IDT IDT with tf.name_scope('layer2_fully_connected'): + IDT IDT IDT with tf.name_scope('num_units1'): + IDT IDT IDT IDT num_units2 = 1024 + IDT IDT IDT with tf.name_scope('w2'): + IDT IDT IDT IDT w2 = , num_units2], mean= , stddev= ) IDT IDT IDT with tf.name_scope('b2'): + IDT IDT IDT IDT b2 = , shape= ) IDT IDT IDT with tf.name_scope('hidden2'): + IDT IDT IDT IDT hidden2 = tf.nn.relu(tf.matmul(x, w2) + b2) + IDT IDT IDT with tf.name_scope('keep_prob'): + IDT IDT IDT IDT keep_prob = tf.placeholder(tf.float32) + IDT IDT IDT with tf.name_scope('hidden2_drop'): + IDT IDT IDT IDT hidden2_drop = tf.nn.dropout(hidden2, keep_prob) + IDT IDT with tf.name_scope('layer3_output_fully_connected'): + IDT IDT IDT with tf.name_scope('w3'): + IDT IDT IDT IDT w3 = , 10], mean= , stddev= ) IDT IDT IDT with tf.name_scope('w3_0'): + IDT IDT IDT IDT w3_0 = , 10], mean= , stddev= ) IDT IDT IDT with tf.name_scope('b3'): + IDT IDT IDT IDT b3 = tf.Variable(tf.zeros([10])) + IDT IDT IDT with tf.name_scope('p'): + IDT IDT IDT IDT p = tf.nn.softmax(tf.matmul(hidden2_drop, w3) + tf.matmul(x, w3_0) + b3) + IDT IDT IDT with tf.name_scope('input_placeholder'): + IDT IDT IDT IDT input_placeholder = , shape= , 10]) ) IDT IDT IDT IDT assign_op = w3_0.assign(input_placeholder) + IDT IDT IDT t = tf.placeholder(tf.float32, [None, 10]) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT with tf.name_scope('loss'): + IDT IDT IDT IDT loss = -tf.reduce_sum(t * tf.log(tf.clip_by_value(p, 1e-10, 1.0))) + IDT IDT IDT with tf.name_scope('train_step'): + IDT IDT IDT IDT train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + IDT IDT IDT with tf.name_scope('correct_prediction'): + IDT IDT IDT IDT correct_prediction = tf.equal(tf.argmax(p, 1), tf.argmax(t, 1)) + IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT IDT tf.summary.scalar('loss', loss) + IDT IDT tf.summary.scalar('accuracy', accuracy) + IDT IDT tf.summary.scalar('w3', tf.reduce_sum(w3)) + IDT IDT tf.summary.scalar('w2', tf.reduce_sum(w2)) + IDT IDT self.x, self.t, self.p = x, t, p + IDT IDT self.train_step = train_step + IDT IDT self.loss = loss + IDT IDT self.accuracy = accuracy + IDT IDT self.keep_prob = keep_prob + IDT IDT self.w3 = w3 + IDT IDT self.w3_0 = w3_0 + IDT IDT self.w2 = w2 + IDT IDT self.assign_op = assign_op + IDT IDT self.input_placeholder = input_placeholder + + IDT def prepare_session(self): + IDT IDT sess = tf.InteractiveSession() + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT summary = tf.summary.merge_all() + IDT IDT writer = tf.summary.FileWriter('./log/cifar10_logs1', sess.graph) + IDT IDT self.sess = sess + IDT IDT self.summary = summary + IDT IDT self.writer = writer + + +class layer2: + + IDT def __init__(self): + IDT IDT with tf.Graph().as_default(): + IDT IDT IDT self.prepare_model() + IDT IDT IDT self.prepare_session() + + IDT def prepare_model(self): + IDT IDT with tf.name_scope('input_layer'): + IDT IDT IDT with tf.name_scope('input_size'): + IDT IDT IDT IDT input_size = 3072 + IDT IDT IDT with tf.name_scope('x'): + IDT IDT IDT IDT x = tf.placeholder(tf.float32, [None, input_size]) + IDT IDT with tf.name_scope('layer1_fully_connected'): + IDT IDT IDT with tf.name_scope('num_units1'): + IDT IDT IDT IDT num_units1 = 1024 + IDT IDT IDT with tf.name_scope('w1'): + IDT IDT IDT IDT w1 = , num_units1], mean= , stddev= ) IDT IDT IDT with tf.name_scope('b1'): + IDT IDT IDT IDT b1 = , shape= ) IDT IDT IDT with tf.name_scope('hidden1'): + IDT IDT IDT IDT hidden1 = tf.nn.relu(tf.matmul(x, w1) + b1) + IDT IDT IDT with tf.name_scope('keep_prob'): + IDT IDT IDT IDT keep_prob = tf.placeholder(tf.float32) + IDT IDT IDT with tf.name_scope('hidden1_drop'): + IDT IDT IDT IDT hidden1_drop = tf.nn.dropout(hidden1, keep_prob) + IDT IDT with tf.name_scope('layer2_fully_connected'): + IDT IDT IDT with tf.name_scope('num_units2'): + IDT IDT IDT IDT num_units2 = 1024 + IDT IDT IDT with tf.name_scope('w2'): + IDT IDT IDT IDT w2 = , num_units2], mean= , stddev= ) IDT IDT IDT with tf.name_scope('w2_0'): + IDT IDT IDT IDT w2_0 = , num_units2], mean= , stddev= ) IDT IDT IDT with tf.name_scope('b2'): + IDT IDT IDT IDT b2 = , shape= ) IDT IDT IDT with tf.name_scope('hidden2'): + IDT IDT IDT IDT hidden2 = tf.nn.relu(tf.matmul(hidden1_drop, w2) + tf.matmul(x, w2_0) + b2) + IDT IDT IDT with tf.name_scope('hidden2_drop'): + IDT IDT IDT IDT hidden2_drop = tf.nn.dropout(hidden2, keep_prob) + IDT IDT with tf.name_scope('layer3_output_fully_connected'): + IDT IDT IDT with tf.name_scope('w3'): + IDT IDT IDT IDT w3 = tf.Variable(tf.zeros([num_units2, 10])) + IDT IDT IDT with tf.name_scope('w3_0'): + IDT IDT IDT IDT w3_0 = tf.Variable(tf.zeros([input_size, 10])) + IDT IDT IDT with tf.name_scope('b3'): + IDT IDT IDT IDT b3 = tf.Variable(tf.zeros([10])) + IDT IDT IDT with tf.name_scope('p'): + IDT IDT IDT IDT p = tf.nn.softmax(tf.matmul(hidden2_drop, w3) + tf.matmul(x, w3_0) + b3) + IDT IDT with tf.name_scope('input_placeholder'): + IDT IDT IDT with tf.name_scope('input_placeholder_for_w3_0'): + IDT IDT IDT IDT input_placeholder_for_w3_0 = , shape= , 10]) ) IDT IDT IDT IDT assign_op_for_w3_0 = w3_0.assign(input_placeholder_for_w3_0) + IDT IDT IDT with tf.name_scope('input_placeholder_for_w3'): + IDT IDT IDT IDT input_placeholder_for_w3 = , shape= , 10]) ) IDT IDT IDT IDT assign_op_for_w3 = w3.assign(input_placeholder_for_w3) + IDT IDT IDT with tf.name_scope('input_placeholder_for_w2_0'): + IDT IDT IDT IDT input_placeholder_for_w2_0 = , shape= , num_units2]) ) IDT IDT IDT IDT assign_op_for_w2_0 = w2_0.assign(input_placeholder_for_w2_0) + IDT IDT with tf.name_scope('true'): + IDT IDT IDT t = tf.placeholder(tf.float32, [None, 10]) + IDT IDT with tf.name_scope('optimizer'): + IDT IDT IDT with tf.name_scope('loss'): + IDT IDT IDT IDT loss = -tf.reduce_sum(t * tf.log(tf.clip_by_value(p, 1e-10, 1.0))) - 0.005 * tf.reduce_sum(w3_0) + IDT IDT IDT with tf.name_scope('train_step'): + IDT IDT IDT IDT train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + IDT IDT IDT with tf.name_scope('correct_prediction'): + IDT IDT IDT IDT correct_prediction = tf.equal(tf.argmax(p, 1), tf.argmax(t, 1)) + IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT IDT tf.summary.scalar('loss', loss) + IDT IDT tf.summary.scalar('accuracy', accuracy) + IDT IDT tf.summary.scalar('w1', tf.reduce_sum(w1)) + IDT IDT tf.summary.scalar('w2', tf.reduce_sum(w2)) + IDT IDT tf.summary.scalar('w3', tf.reduce_sum(w3)) + IDT IDT tf.summary.scalar('w2_0', tf.reduce_sum(w2_0)) + IDT IDT tf.summary.scalar('w3_0', tf.reduce_sum(w3_0)) + IDT IDT self.x, self.t, self.p = x, t, p + IDT IDT self.train_step = train_step + IDT IDT self.loss = loss + IDT IDT self.accuracy = accuracy + IDT IDT self.keep_prob = keep_prob + IDT IDT self.w1 = w1 + IDT IDT self.w2 = w2 + IDT IDT self.w3 = w3 + IDT IDT self.w2_0 = w2_0 + IDT IDT self.w3_0 = w3_0 + IDT IDT self.assign_op_for_w3_0 = assign_op_for_w3_0 + IDT IDT self.input_placeholder_for_w3_0 = input_placeholder_for_w3_0 + IDT IDT self.assign_op_for_w3 = assign_op_for_w3 + IDT IDT self.input_placeholder_for_w3 = input_placeholder_for_w3 + IDT IDT self.assign_op_for_w2_0 = assign_op_for_w2_0 + IDT IDT self.input_placeholder_for_w2_0 = input_placeholder_for_w2_0 + + IDT def prepare_session(self): + IDT IDT sess = tf.InteractiveSession() + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT summary = tf.summary.merge_all() + IDT IDT writer = tf.summary.FileWriter('./log/cifar10_logs2', sess.graph) + IDT IDT self.sess = sess + IDT IDT self.summary = summary + IDT IDT self.writer = writer + + +if __name__ == '__main__': + IDT data, labels_non_onehot, test_data, test_labels_non_onehot, label_names = load_cifer10.load_dataset() + IDT + IDT labels = np.mat([[(0) for i in range(10)] for k in range(len(labels_non_onehot))]) + IDT for i in range(len(labels)): + IDT IDT labels[i] = np.eye(10)[labels_non_onehot[i]] + IDT test_labels = np.mat([[(0) for i in range(10)] for k in range(len(test_labels_non_onehot))]) + IDT for i in range(len(test_labels)): + IDT IDT test_labels[i] = np.eye(10)[test_labels_non_onehot[i]] + IDT nn = layer0() + IDT batchsize = 100 + IDT batch_xs = np.mat([[(0.0) for n in range(3072)] for k in range(batchsize)]) + IDT batch_ts = np.mat([[(0.0) for n in range(10)] for k in range(batchsize)]) + IDT + IDT + IDT i = 0 + IDT for _ in range(10000): + IDT IDT i += 1 + IDT IDT for n in range(batchsize): + IDT IDT IDT tmp = int(random.uniform(0, len(data))) + IDT IDT IDT batch_xs[n] = data[tmp].reshape(1, 3072) + IDT IDT IDT batch_xs[n] /= batch_xs[n].max() + IDT IDT IDT batch_ts[n] = labels[tmp].reshape(1, 10) + IDT IDT nn.sess.run(nn.train_step, feed_dict= , nn.t: batch_ts}) ) IDT IDT if i % 100 == 0: + IDT IDT IDT summary, loss_val, acc_val = , nn.loss, nn.accuracy], feed_dict= , nn.t: test_labels}) ) IDT IDT IDT ) + IDT IDT IDT nn.writer.add_summary(summary, i) + IDT w3_0 = nn.sess.run(nn.w3) + IDT nn = layer1() + IDT nn.sess.run(nn.assign_op, feed_dict= ) IDT + IDT ) + IDT i = 0 + IDT for _ in range(10000): + IDT IDT i += 1 + IDT IDT for n in range(batchsize): + IDT IDT IDT tmp = int(random.uniform(0, len(data))) + IDT IDT IDT batch_xs[n] = data[tmp].reshape(1, 3072) + IDT IDT IDT batch_xs[n] /= batch_xs[n].max() + IDT IDT IDT batch_ts[n] = labels[tmp].reshape(1, 10) + IDT IDT nn.sess.run(nn.train_step, feed_dict= , nn.t: batch_ts, nn.keep_prob: 0.5}) ) IDT IDT if i % 100 == 0: + IDT IDT IDT summary, loss_val, acc_val = , nn.loss, nn.accuracy], feed_dict= , nn.t: test_labels, nn.keep_prob: 1.0}) ) IDT IDT IDT ) + IDT IDT IDT nn.writer.add_summary(summary, i) + IDT w2_0 = nn.sess.run(nn.w2) + IDT w3_0 = nn.sess.run(nn.w3_0) + IDT w3 = nn.sess.run(nn.w3) + IDT nn = layer2() + IDT nn.sess.run(nn.assign_op_for_w3_0, feed_dict= ) IDT nn.sess.run(nn.assign_op_for_w3, feed_dict= ) IDT nn.sess.run(nn.assign_op_for_w2_0, feed_dict= ) IDT i = 0 + IDT for _ in range(100000): + IDT IDT i += 1 + IDT IDT for n in range(batchsize): + IDT IDT IDT tmp = int(random.uniform(0, len(data))) + IDT IDT IDT batch_xs[n] = data[tmp].reshape(1, 3072) + IDT IDT IDT batch_xs[n] /= batch_xs[n].max() + IDT IDT IDT batch_ts[n] = labels[tmp].reshape(1, 10) + IDT IDT nn.sess.run(nn.train_step, feed_dict= , nn.t: batch_ts, nn.keep_prob: 0.5}) ) IDT IDT if i % 100 == 0: + IDT IDT IDT summary, loss_val, acc_val = , nn.loss, nn.accuracy], feed_dict= , nn.t: test_labels, nn.keep_prob: 1.0}) ) IDT IDT IDT ) + IDT IDT IDT nn.writer.add_summary(summary, i) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" + Tensor: Multidimensional array + + Scalar: s = 1, 2, 3, ... + Vector: v = [1, 2, 3, ...] + Matrix: m = [[1, 2, 3], [4, 5, 6], [7, 8, 9] , ...] + Tensor: t = [[[[[...]]]]] +""" +import numpy as np +import tensorflow as tf +a = tf.constant([1.0, 2.0]) + +b = tf.constant([3.0, 4.0]) +result = a + b +""" + "add:0": IDT node name + shape=(2,0): 1-dimensional vector, length 2 + dtype=float32: data type +""" + +x1 = tf.constant([[1.0, 2.0]]) + +x2 = tf.constant([1.0, 2.0]) + +w = tf.constant([[3.0], [4.0]]) + +y = tf.matmul(x, w) + +with tf.Session() as sess: + IDT ) + + +def simpleNN(): + IDT + IDT x = tf.constant([[0.7, 0.5]]) + IDT w1 = , 3], stddev= , seed= ) IDT w2 = , 1], stddev= , seed= ) IDT h = tf.matmul(x, w1) + IDT y = tf.matmul(h, w2) + IDT with tf.Session() as sess: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT ) + + +simpleNN() + + +def FeedOneDataPointNN(): + IDT x = , shape= , 2)) ) IDT w1 = , 3], stddev= , seed= ) IDT w2 = , 1], stddev= , seed= ) IDT h = tf.matmul(x, w1) + IDT y = tf.matmul(h, w2) + IDT with tf.Session() as sess: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT ) + + +FeedOneDataPointNN() + + +def FeedMultipleDataNN(): + IDT x = , shape= , 2)) ) IDT w1 = , 3], stddev= , seed= ) IDT w2 = , 1], stddev= , seed= ) IDT h = tf.matmul(x, w1) + IDT y = tf.matmul(h, w2) + IDT with tf.Session() as sess: + IDT IDT sess.run(tf.global_variables_initializer()) + IDT IDT ) + + +FeedMultipleDataNN() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import division, print_function, unicode_literals +import numpy as np +import os +import tensorflow as tf +reset_graph() +n_steps = 28 +n_inputs = 28 +n_neurons = 150 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +basic_cell = )outputs, states = , X, dtype= )logits = tf.layers.dense(states, n_outputs) +xentropy = , logits= )loss = tf.reduce_mean(xentropy) +optimizer = )training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +reset_graph() +n_steps = 28 +n_inputs = 28 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +n_neurons = 100 +n_layers = 3 +layers = , activation= )multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers) +outputs, states = , X, dtype= )layers +states_concat = , values= )logits = tf.layers.dense(states_concat, n_outputs) +xentropy = , logits= )loss = tf.reduce_mean(xentropy) +optimizer = )training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +n_epochs = 10 +batch_size = 150 +with tf.Session() as sess: + IDT init.run() + IDT for epoch in range(n_epochs): + IDT IDT for iteration in range(mnist.train.num_examples // batch_size): + IDT IDT IDT X_batch, y_batch = mnist.train.next_batch(batch_size) + IDT IDT IDT X_batch = X_batch.reshape((-1, n_steps, n_inputs)) + IDT IDT IDT sess.run(training_op, feed_dict= , y: y_batch}) ) IDT IDT acc_train = , y: y_batch}) ) IDT IDT acc_test = , y: y_test}) ) IDT IDT +reset_graph() +sess = tf.Session() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = , activation= )outputs, states = , X, dtype= )reset_graph() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = , activation= , output_size= )outputs, states = , X, dtype= )learning_rate = 0.001 +loss = tf.reduce_mean(tf.square(outputs - y)) +optimizer = )training_op = optimizer.minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver() +n_iterations = 1500 +batch_size = 50 +with tf.Session() as sess: + IDT init.run() + IDT for iteration in range(n_iterations): + IDT IDT X_batch, y_batch = next_batch(batch_size, n_steps) + IDT IDT sess.run(training_op, feed_dict= , y: y_batch}) ) IDT IDT if iteration % 100 == 0: + IDT IDT IDT mse = , y: y_batch}) ) IDT IDT IDT + IDT saver.save(sess, './my_time_series_model') +reset_graph() +n_inputs = 1 +n_neurons = 100 +n_layers = 3 +n_steps = 20 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +keep_prob = 0.5 +cells = )cells_drop = , input_keep_prob= )multi_layer_cell = tf.contrib.rnn.MultiRNNCell(cells_drop) +rnn_outputs, states = , X, dtype= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +train_subset = 10000 +beta = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + IDT tf_train_labels = tf.constant(train_labels[:train_subset]) + IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +import time +batch_size = 128 +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 128 +num_nodes = 1024 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(relu1, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +import time + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 12 +num_nodes = 1024 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(relu1, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT l2_regularizer = tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_relu1) + IDT loss += 0.0005 * l2_regularizer + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +import time + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 128 +num_nodes = 1024 +beta = 0.001 +prob_limit = 0.75 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT keep_prob = tf.placeholder('float') + IDT hidden_layer_drop = tf.nn.dropout(relu1, keep_prob) + IDT logits = tf.matmul(hidden_layer_drop, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +import time + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: prob_limit} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 128 +h1_num_nodes = 1024 +h2_num_nodes = 768 +h3_num_nodes = 512 +h1_stddev = np.sqrt(2.0 / 784) +h2_stddev = np.sqrt(2.0 / h1_num_nodes) +h3_stddev = np.sqrt(2.0 / h2_num_nodes) +logits_stddev = np.sqrt(2.0 / h3_num_nodes) +beta = 1e-05 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT h1_weights = , h1_num_nodes], stddev= ) IDT h1_biases = tf.Variable(tf.zeros([h1_num_nodes])) + IDT h1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, h1_weights) + h1_biases) + IDT + IDT h2_weights = , h2_num_nodes], stddev= ) IDT h2_biases = tf.Variable(tf.zeros([h2_num_nodes])) + IDT h2_layer = tf.nn.relu(tf.matmul(h1_layer, h2_weights) + h2_biases) + IDT + IDT h3_weights = , h3_num_nodes], stddev= ) IDT h3_biases = tf.Variable(tf.zeros([h3_num_nodes])) + IDT h3_layer = tf.nn.relu(tf.matmul(h2_layer, h3_weights) + h3_biases) + IDT + IDT weights = , num_labels], stddev= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(h3_layer, weights) + biases + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(h1_weights) + beta * tf.nn.l2_loss(h2_weights) + beta * tf.nn.l2_loss(h3_weights) + beta * tf.nn.l2_loss(weights)) + IDT global_step = tf.Variable(0) + IDT learning_rate = , global_step, 100000, 0.95, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT h1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, h1_weights) + h1_biases) + IDT h2_valid = tf.nn.relu(tf.matmul(h1_valid, h2_weights) + h2_biases) + IDT h3_valid = tf.nn.relu(tf.matmul(h2_valid, h3_weights) + h3_biases) + IDT valid_logits = tf.matmul(h3_valid, weights) + biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT ->', valid_prediction) + IDT h1_test = tf.nn.relu(tf.matmul(tf_test_dataset, h1_weights) + h1_biases) + IDT h2_test = tf.nn.relu(tf.matmul(h1_test, h2_weights) + h2_biases) + IDT h3_test = tf.nn.relu(tf.matmul(h2_test, h3_weights) + h3_biases) + IDT test_logits = tf.matmul(h3_test, weights) + biases + IDT test_prediction = tf.nn.softmax(test_logits) +import time + +num_steps = 10000 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.initialize_all_variables().run() + IDT ->', valid_prediction.eval()) + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +hello_constant = tf.constant('Hello World!') +with tf.Session() as sess: + IDT output = sess.run(hello_constant) + IDT +A = tf.constant(1234) +B = tf.constant([123, 456, 789]) +C = tf.constant([[123, 456, 789], [222, 333, 444]]) +with tf.Session() as sess2: + IDT output1 = sess2.run(C) + IDT +x = tf.placeholder(tf.string) +y = tf.placeholder(tf.int32) +z = tf.placeholder(tf.float32) + + +def placeHolder(string): + IDT output = None + IDT x = tf.placeholder(tf.string) + IDT with tf.Session() as sess: + IDT IDT output = , feed_dict= ) IDT return + + +placeHolder('Good Morning World!') +x = tf.add(5, 2) +x = tf.subtract(10, 4) +y = tf.multiply(2, 5) +tf.subtract(tf.cast(tf.constant(2.0), tf.int32), tf.constant(1)) +x = tf.constant(10) +y = tf.constant(2) +z = tf.subtract(tf.divide(x, y), tf.cast(tf.constant(1), tf.float64)) +with tf.Session() as sess: + IDT output = sess.run(z) + IDT +matW = tf.constant([[-0.5, 0.2, 0.1], [0.7, -0.8, 0.2]]) +matX = tf.constant([[0.2], [0.5], [0.6]]) +b = tf.constant([[0.1], [0.2]]) +prod = tf.matmul(matW, matX) +sum1 = tf.add(prod, b) +with tf.Session() as sess: + IDT output = sess.run(sum1) + IDT +x = tf.Variable(5) +init = tf.global_variables_initializer() +with tf.Session() as sess: + IDT sess.run(init) +n_features = 120 +n_labels = 5 +weights = tf.Variable(tf.truncated_normal((n_features, n_labels))) +n_labels = 5 +bias = tf.Variable(tf.zeros(n_labels)) + + +def get_weights(n_features, n_labels): + IDT return tf.Variable(tf.truncated_normal((n_features, n_labels))) + + +def get_biases(n_labels): + IDT return tf.Variable(tf.zeros(n_labels)) + + +def linear(input, w, b): + IDT return tf.add(tf.matmul(input, w), b) + + +x = tf.nn.softmax([2.0, 1.0, 0.2]) + + +def run(): + IDT output = None + IDT logit_data = [2.0, 1.0, 0.1] + IDT logits = tf.placeholder(tf.float32) + IDT softmax = tf.nn.softmax(logit_data) + IDT with tf.Session() as sess: + IDT IDT output = , feed_dict= ) IDT return output + + +softmax_data = [0.7, 0.2, 0.1] +one_hot_data = [1.0, 0.0, 0.0] +softmax = tf.placeholder(tf.float32) +one_hot = tf.placeholder(tf.float32) +cross_entropy = -tf.reduce_sum(tf.multiply(one_hot, tf.log(softmax))) +with tf.Session() as sess: + IDT ) +from tensorflow.examples.tutorials.mnist import input_data +import tensorflow as tf +n_input = 784 +n_classes = 10 +mnist = , one_hot= )train_features = mnist.train.images +test_features = mnist.test.images +train_labels = mnist.train.labels.astype(np.float32) +test_labels = mnist.test.labels.astype(np.float32) +weights = tf.Variable(tf.random_normal([n_input, n_classes])) +bias = tf.Variable(tf.random_normal([n_classes])) +features = tf.placeholder(tf.float32, [None, n_input]) +labels = tf.placeholder(tf.float32, [None, n_classes]) +example_features = [['F11', 'F12', 'F13', 'F14'], ['F21', 'F22', 'F23', 'F24'], ['F31', 'F32', 'F33', 'F34'], ['F41', 'F42', 'F43', 'F44']] +example_labels = [['L11', 'L12'], ['L21', 'L22'], ['L31', 'L32'], ['L41', 'L42']] +import math + + +def batches(batch_size, features, labels): + IDT """ + IDT Create batches of features and labels + IDT :param batch_size: The batch size + IDT :param features: List of features + IDT :param labels: List of labels + IDT :return: Batches of (Features, Labels) + IDT """ + IDT assert len(features) = ) IDT output_batches = [] + IDT sample_size = len(features) + IDT for start_i in range(0, sample_size, batch_size): + IDT IDT end_i = start_i + batch_size + IDT IDT batch = [features[start_i:end_i], labels[start_i:end_i]] + IDT IDT output_batches.append(batch) + IDT return output_batches + + +example_batches = batches(batch_size, example_features, example_labels) +hidden_layer = tf.add(tf.matmul(features, hidden_weights), hidden_biases) +hidden_layer = tf.nn.relu(hidden_layer) +output = tf.add(tf.matmul(hidden_layer, output_weights), output_biases) +import tensorflow as tf +output = None +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[1.0, 2.0, 3.0, 4.0], [-1.0, -2.0, -3.0, -4.0], [11.0, 12.0, 13.0, 14.0]]) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT ) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +import tensorflow as tf +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[0.0, 2.0, 3.0, 4.0], [0.1, 0.2, 0.3, 0.4], [11.0, 12.0, 13.0, 14.0]]) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) +) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from unittest import TestCase +import pkg_resources +import logging, warnings +import tensorflow as tf +import numpy as np +from deepexplain.tensorflow import DeepExplain +from deepexplain.tensorflow.methods import original_grad +activations = {'Relu': tf.nn.relu, 'Sigmoid': tf.nn.sigmoid, 'Softplus': tf.nn.softplus, 'Tanh': tf.nn.tanh} + + +def simple_model(activation, session): + IDT X = tf.placeholder('float', [None, 2]) + IDT w1 = , -1.0], [-1.0, 1.0]]) ) IDT b1 = , -1.0]) ) IDT w2 = , 1.4], [-0.5, 1.0]]) ) IDT b2 = , 2.0]) ) IDT layer1 = activation(tf.matmul(X, w1) + b1) + IDT out = tf.matmul(layer1, w2) + b2 + IDT session.run(tf.global_variables_initializer()) + IDT return X, out + + +def simpler_model(session): + IDT """ + IDT Implements ReLU( ReLU(x1 - 1) - ReLU(x2) ) + IDT : + IDT """ + IDT X = tf.placeholder('float', [None, 2]) + IDT w1 = , 0.0], [0.0, 1.0]], trainable= ) IDT b1 = , 0], trainable= ) IDT w2 = , [-1.0]], trainable= ) IDT l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + IDT out = tf.nn.relu(tf.matmul(l1, w2)) + IDT session.run(tf.global_variables_initializer()) + IDT return X, out + + +def min_model(session): + IDT """ + IDT Implements min(xi) + IDT """ + IDT X = tf.placeholder('float', [None, 2]) + IDT out = tf.reduce_min(X, 1) + IDT session.run(tf.global_variables_initializer()) + IDT return X, out + + +def min_model_2d(session): + IDT """ + IDT Implements min(xi) + IDT """ + IDT X = tf.placeholder('float', [None, 2, 2]) + IDT out = tf.reduce_min(tf.reshape(X, (-1, 4)), 1) + IDT session.run(tf.global_variables_initializer()) + IDT return X, out + + +def simple_multi_inputs_model(session): + IDT """ + IDT Implements Relu (3*x1|2*x2) | is a concat op + IDT : + IDT """ + IDT X1 = tf.placeholder('float', [None, 2]) + IDT X2 = tf.placeholder('float', [None, 2]) + IDT w1 = , 0.0], [0.0, 3.0]], trainable= ) IDT w2 = , 0.0], [0.0, 2.0]], trainable= ) IDT out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + IDT session.run(tf.global_variables_initializer()) + IDT return X1, X2, out + + +def simple_multi_inputs_model2(session): + IDT """ + IDT Implements Relu (3*x1|2*x2) | is a concat op + IDT : + IDT """ + IDT X1 = tf.placeholder('float', [None, 2]) + IDT X2 = tf.placeholder('float', [None, 1]) + IDT w1 = , trainable= ) IDT w2 = , trainable= ) IDT out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + IDT session.run(tf.global_variables_initializer()) + IDT return X1, X2, out + + +def train_xor(session): + IDT X = tf.placeholder('float', [None, 2]) + IDT Y = tf.placeholder('float', [None, 1]) + IDT w1 = , -0.0987727], [-1.57625198, 1.34942603]]) ) IDT b1 = , -0.14483099]) ) IDT w2 = , [-0.16255915]]) ) IDT b2 = ) IDT l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + IDT out = tf.matmul(l1, w2) + b2 + IDT session.run(tf.global_variables_initializer()) + IDT loss = tf.reduce_mean(tf.losses.mean_squared_error(Y, out)) + IDT train_step = tf.train.GradientDescentOptimizer(0.05).minimize(loss) + IDT np.random.seed(10) + IDT x = , 2, size= , 2)) ) IDT y = np.expand_dims(np.logical_or(x[:, (0)], x[:, (1)]), -1) + IDT l = None + IDT for _ in range(100): + IDT IDT l, _ = , train_step], feed_dict= , Y: y}) ) IDT return np.abs(l - 0.1) < 0.01 + + +class TestDeepExplainGeneralTF(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_tf_available(self): + IDT IDT try: + IDT IDT IDT pkg_resources.require('tensorflow>= ) IDT IDT except Exception: + IDT IDT IDT try: + IDT IDT IDT IDT pkg_resources.require('tensorflow-gpu>= ) IDT IDT IDT except Exception: + IDT IDT IDT IDT self.fail('Tensorflow requirement not met') + + IDT def test_simple_model(self): + IDT IDT X, out = simple_model(tf.nn.relu, self.session) + IDT IDT xi = np.array([[1, 0]]) + IDT IDT r = self.session.run(out, {X: xi}) + IDT IDT self.assertEqual(r.shape, xi.shape) + IDT IDT np.testing.assert_equal(r[0], [2.75, 5.5]) + + IDT def test_simpler_model(self): + IDT IDT X, out = simpler_model(self.session) + IDT IDT xi = np.array([[3.0, 1.0]]) + IDT IDT r = self.session.run(out, {X: xi}) + IDT IDT self.assertEqual(r.shape, (xi.shape[0], 1)) + IDT IDT np.testing.assert_equal(r[0], [1.0]) + + IDT def test_training(self): + IDT IDT session = tf.Session() + IDT IDT r = train_xor(session) + IDT IDT self.assertTrue(r) + + IDT def test_context(self): + IDT IDT """ + IDT IDT DeepExplain overrides nonlinearity gradient + IDT IDT """ + IDT IDT from deepexplain.tensorflow import DeepExplain + IDT IDT X = tf.placeholder('float', [None, 1]) + IDT IDT for name in activations: + IDT IDT IDT x1 = activations[name](X) + IDT IDT IDT x1_g = tf.gradients(x1, X)[0] + IDT IDT IDT self.assertEqual(x1_g.op.type, '%sGrad' % name) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT for name in activations: + IDT IDT IDT IDT x2 = activations[name](X) + IDT IDT IDT IDT self.assertEqual(x2.op.get_attr('_gradient_op_type').decode('utf-8'), 'DeepExplainGrad') + + IDT def test_mismatch_input_lens(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1 = tf.placeholder('float', [None, 1]) + IDT IDT IDT X2 = tf.placeholder('float', [None, 1]) + IDT IDT IDT w1 = ) IDT IDT IDT w2 = ) IDT IDT IDT out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + IDT IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('grad*input', out, [X1, X2], [[1], [2], [3]]) + IDT IDT IDT self.assertIn('List of input tensors and input data have different lengths', str(cm.exception)) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1 = tf.placeholder('float', [None, 1]) + IDT IDT IDT X2 = tf.placeholder('float', [None, 1]) + IDT IDT IDT w1 = ) IDT IDT IDT w2 = ) IDT IDT IDT out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + IDT IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT IDT attributions = de.explain('grad*input', out, [X1, X2], [[[2]], [[3]]]) + IDT IDT IDT self.assertEqual(len(attributions), 2) + IDT IDT IDT self.assertEqual(attributions[0][0], 20.0) + IDT IDT IDT self.assertEqual(attributions[1][0], 30.0) + + IDT def test_supported_activations(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT xi = [[-1, 0, 1]] + IDT IDT IDT Y = tf.nn.relu(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [0, 0, 1], 7) + IDT IDT IDT Y = tf.nn.elu(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [-0.632120558, 0, 1], 7) + IDT IDT IDT Y = tf.nn.sigmoid(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [0.268941421, 0.5, 0.731058578], 7) + IDT IDT IDT Y = tf.nn.tanh(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [-0.761594155, 0, 0.761594155], 7) + IDT IDT IDT Y = tf.nn.softplus(X) + IDT IDT IDT r = self.session.run(Y, {X: xi}) + IDT IDT IDT np.testing.assert_almost_equal(r[0], [0.313261687, 0.693147181, 1.31326168], 7) + + IDT def test_original_grad(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT for name in activations: + IDT IDT IDT Y = activations[name](X) + IDT IDT IDT grad = original_grad(Y.op, tf.ones_like(X)) + IDT IDT IDT self.assertTrue('Tensor' in str(type(grad))) + + IDT def test_warning_unsupported_activations(self): + IDT IDT with warnings.catch_warnings(record= ) IDT IDT IDT warnings.simplefilter('always') + IDT IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT IDT IDT Y = tf.nn.relu6(X) + IDT IDT IDT IDT xi = [[-1, 0, 1]] + IDT IDT IDT IDT de.explain('elrp', Y, X, xi) + IDT IDT IDT IDT assert any([('unsupported activation' in str(wi.message)) for wi in w]) + + IDT def test_override_as_default(self): + IDT IDT """ + IDT IDT In DeepExplain context, nonlinearities behave as default, including training time + IDT IDT """ + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT r = train_xor(self.session) + IDT IDT IDT self.assertTrue(r) + + IDT def test_explain_not_in_context(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT pass + IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT de.explain('grad*input', None, None, None) + IDT IDT self.assertEqual('Explain can be called only within a DeepExplain context.', str(cm.exception)) + + IDT def test_invalid_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('invalid', None, None, None) + IDT IDT IDT self.assertIn('Method must be in', str(cm.exception)) + + IDT def test_T_is_tensor(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT Y = tf.nn.relu(X) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('grad*input', [Y], X, [[0, 0, 0]]) + IDT IDT IDT IDT self.assertIn('T must be a Tensorflow Tensor object', str(cm.exception)) + + IDT def test_X_is_tensor(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT Y = tf.nn.relu(X) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('grad*input', Y, np.eye(3), [[0, 0, 0]]) + IDT IDT IDT IDT self.assertIn('Tensorflow Tensor object', str(cm.exception)) + + IDT def test_all_in_X_are_tensor(self): + IDT IDT X = tf.placeholder('float', [None, 3]) + IDT IDT Y = tf.nn.relu(X) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('grad*input', Y, [X, np.eye(3)], [[0, 0, 0]]) + IDT IDT IDT IDT self.assertIn('Tensorflow Tensor object', str(cm.exception)) + + IDT def test_X_has_compatible_batch_dim(self): + IDT IDT X = tf.placeholder('float', [10, 3]) + IDT IDT Y = tf.nn.relu(X) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('grad*input', Y, X, [[0, 0, 0]], batch_size= ) IDT IDT IDT IDT self.assertIn('the first dimension of the input tensor', str(cm.exception)) + + IDT def test_T_has_compatible_batch_dim(self): + IDT IDT X, out = simpler_model(self.session) + IDT IDT xi = np.array([[-10, -5]]).repeat(50, 0) + IDT IDT Y = out * np.expand_dims(np.array(range(50)), -1) + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('saliency', Y, X, xi, batch_size= ) IDT IDT IDT IDT self.assertIn('the first dimension of the target tensor', str(cm.exception)) + + IDT def test_use_of_target_weights(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, T = simple_model(tf.identity, self.session) + IDT IDT IDT xi = np.array([[1, 0]]) + IDT IDT IDT yi1 = np.array([[1, 0]]) + IDT IDT IDT yi2 = np.array([[0, 1]]) + IDT IDT IDT yi3 = np.array([[1, 1]]) + IDT IDT IDT yi4 = np.array([[0, 0]]) + IDT IDT IDT a1 = , T, X, xi, ys= ) IDT IDT IDT a2 = , T, X, xi, ys= ) IDT IDT IDT a3 = , T, X, xi, ys= ) IDT IDT IDT a4 = , T, X, xi, ys= ) IDT IDT IDT np.testing.assert_almost_equal(a1 + a2, a3, 10) + IDT IDT IDT np.testing.assert_almost_equal(a4, np.array([[0.0, 0.0]]), 10) + + IDT def test_use_of_target_weights_batch(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, T = simple_model(tf.identity, self.session) + IDT IDT IDT xi = np.array([[1, 0]]).repeat(20, 0) + IDT IDT IDT yi1 = np.array([[1, 0]]).repeat(20, 0) + IDT IDT IDT yi2 = np.array([[0, 1]]).repeat(20, 0) + IDT IDT IDT yi3 = np.array([[1, 1]]).repeat(20, 0) + IDT IDT IDT yi4 = np.array([[0, 0]]).repeat(20, 0) + IDT IDT IDT a1 = , T, X, xi, ys= , batch_size= ) IDT IDT IDT a2 = , T, X, xi, ys= , batch_size= ) IDT IDT IDT a3 = , T, X, xi, ys= , batch_size= ) IDT IDT IDT a4 = , T, X, xi, ys= , batch_size= ) IDT IDT IDT np.testing.assert_almost_equal(a1 + a2, a3, 10) + IDT IDT IDT np.testing.assert_almost_equal(a4, np.array([[0.0, 0.0]]).repeat(20, 0), 10) + + IDT def test_wrong_weight_len(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, T = simple_model(tf.identity, self.session) + IDT IDT IDT xi = np.array([[1, 0]]).repeat(20, 0) + IDT IDT IDT yi1 = np.array([[1, 0]]) + IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('saliency', T, X, xi, ys= , batch_size= ) IDT IDT IDT IDT self.assertIn('the number of elements in ys must equal ', str(cm.exception)) + + IDT def test_explainer_api_memory(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, T = simple_model(tf.identity, self.session) + IDT IDT IDT xi = np.array([[1, 0]]).repeat(20, 0) + IDT IDT IDT prev_ops_count = None + IDT IDT IDT explainer = de.get_explainer('saliency', T, X) + IDT IDT IDT for i in range(10): + IDT IDT IDT IDT explainer.run(xi) + IDT IDT IDT IDT ops_count = len([n.name for n in tf.get_default_graph().as_graph_def().node]) + IDT IDT IDT IDT if prev_ops_count is None: + IDT IDT IDT IDT IDT prev_ops_count = ops_count + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT self.assertEquals(prev_ops_count, ops_count) + + +class TestDummyMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_dummy_zero(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.sigmoid, self.session) + IDT IDT IDT xi = np.array([[10, -10]]) + IDT IDT IDT attributions = de.explain('zero', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [0.0, 0.0], 10) + + IDT def test_gradient_restored(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.sigmoid, self.session) + IDT IDT IDT xi = np.array([[10, -10]]) + IDT IDT IDT de.explain('zero', out, X, xi) + IDT IDT IDT r = train_xor(self.session) + IDT IDT IDT self.assertTrue(r) + + +class TestSaliencyMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_saliency_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('saliency', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, 1.0]], 10) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('saliency', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[2.0, 2.0]], 10) + + IDT def test_multiple_inputs_explainer_api(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT explainer = de.get_explainer('saliency', out, [X1, X2]) + IDT IDT IDT attributions = explainer.run(xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[2.0, 2.0]], 10) + + +class TestGradInputMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_saliency_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('grad*input', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 10) + + IDT def test_saliency_method_explainer_api(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT explainer = de.get_explainer('grad*input', out, X) + IDT IDT IDT attributions = explainer.run(xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 10) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('grad*input', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + +class TestIntegratedGradientsMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_int_grad(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('intgrad', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 10) + + IDT def test_int_grad_higher_precision(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = , out, X, xi, steps= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 10) + + IDT def test_int_grad_baseline(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[2, 0]]) + IDT IDT IDT attributions = , out, X, xi, baseline= , 0])) ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[1.0, 0.0]], 10) + + IDT def test_int_grad_baseline_explainer_api(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[2, 0]]) + IDT IDT IDT explainer = , out, X, baseline= , 0])) ) IDT IDT IDT attributions = explainer.run(xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[1.0, 0.0]], 10) + + IDT def test_int_grad_baseline_2(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[2, 0], [3, 0]]) + IDT IDT IDT attributions = , out, X, xi, baseline= , 0])) ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[1.0, 0.0], [2.0, 0.0]], 10) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('intgrad', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + IDT def test_multiple_inputs_different_sizes(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model2(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3]])] + IDT IDT IDT attributions = de.explain('intgrad', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6]], 10) + + IDT def test_intgrad_targeting_equivalence(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.relu, self.session) + IDT IDT IDT xi = np.array([[5, 3]]) + IDT IDT IDT self.assertEqual(out.shape[1], 2) + IDT IDT IDT a1 = de.explain('intgrad', out * np.array([[1, 0]]), X, xi) + IDT IDT IDT b1 = de.explain('intgrad', out * np.array([[0, 1]]), X, xi) + IDT IDT IDT a2 = , out, X, xi, ys= , 0]])) ) IDT IDT IDT b2 = , out, X, xi, ys= , 1]])) ) IDT IDT IDT np.testing.assert_almost_equal(a1, a2, 1) + IDT IDT IDT np.testing.assert_almost_equal(b1, b2, 1) + + +class TestEpsilonLRPMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_elrp_method(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('elrp', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 3) + + IDT def test_elrp_epsilon(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = , out, X, xi, epsilon= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 7) + + IDT def test_elrp_epsilon_explainer_api(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT explainer = , out, X, epsilon= ) IDT IDT IDT attributions = explainer.run(xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 7) + + IDT def test_elrp_zero_epsilon(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT with self.assertRaises(AssertionError): + IDT IDT IDT IDT de.explain('elrp', out, X, xi, epsilon= ) + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = , out, [X1, X2], xi, epsilon= ) IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + IDT def test_elrp_targeting_equivalence(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.relu, self.session) + IDT IDT IDT xi = np.array([[5, 3]]) + IDT IDT IDT self.assertEqual(out.shape[1], 2) + IDT IDT IDT a1 = de.explain('elrp', out * np.array([[1, 0]]), X, xi) + IDT IDT IDT b1 = de.explain('elrp', out * np.array([[0, 1]]), X, xi) + IDT IDT IDT a2 = , out, X, xi, ys= , 0]])) ) IDT IDT IDT b2 = , out, X, xi, ys= , 1]])) ) IDT IDT IDT np.testing.assert_almost_equal(a1, a2, 1) + IDT IDT IDT np.testing.assert_almost_equal(b1, b2, 1) + + +class TestDeepLIFTMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_deeplift(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('deeplift', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [2.0, -1.0]], 10) + + IDT def test_deeplift_batches(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT xi = np.repeat(xi, 25, 0) + IDT IDT IDT self.assertEqual(xi.shape[0], 50) + IDT IDT IDT attributions = , out, X, xi, batch_size= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, np.repeat([[0.0, 0.0], [2.0, -1.0]], 25, 0), 10) + + IDT def test_deeplift_batches_explainer_api(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT xi = np.repeat(xi, 25, 0) + IDT IDT IDT self.assertEqual(xi.shape[0], 50) + IDT IDT IDT explaoiner = de.get_explainer('deeplift', out, X) + IDT IDT IDT attributions = , batch_size= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, np.repeat([[0.0, 0.0], [2.0, -1.0]], 25, 0), 10) + + IDT def test_deeplift_baseline(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[3, 1]]) + IDT IDT IDT attributions = , out, X, xi, baseline= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0]], 5) + + IDT def test_multiple_inputs(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT attributions = de.explain('deeplift', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + IDT def test_multiple_inputs_different_sizes(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model2(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3]])] + IDT IDT IDT attributions = de.explain('deeplift', out, [X1, X2], xi) + IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], [[6]], 10) + + IDT def test_multiple_inputs_different_sizes_batches(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model2(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]).repeat(50, 0), np.array([[3]]).repeat(50, 0)] + IDT IDT IDT attributions = , out, [X1, X2], xi, batch_size= ) IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], np.repeat([[0.0, 0.0]], 50, 0), 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], np.repeat([[6]], 50, 0), 10) + + IDT def test_multiple_inputs_different_sizes_batches_disable(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model2(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]).repeat(50, 0), np.array([[3]]).repeat(50, 0)] + IDT IDT IDT attributions = , out, [X1, X2], xi, batch_size= ) IDT IDT IDT self.assertEqual(len(attributions), len(xi)) + IDT IDT IDT np.testing.assert_almost_equal(attributions[0], np.repeat([[0.0, 0.0]], 50, 0), 10) + IDT IDT IDT np.testing.assert_almost_equal(attributions[1], np.repeat([[6]], 50, 0), 10) + + IDT def test_deeplift_targeting_equivalence(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.relu, self.session) + IDT IDT IDT xi = np.array([[5, 3]]) + IDT IDT IDT self.assertEqual(out.shape[1], 2) + IDT IDT IDT a1 = de.explain('deeplift', out * np.array([[1, 0]]), X, xi) + IDT IDT IDT b1 = de.explain('deeplift', out * np.array([[0, 1]]), X, xi) + IDT IDT IDT a2 = , out, X, xi, ys= , 0]])) ) IDT IDT IDT b2 = , out, X, xi, ys= , 1]])) ) IDT IDT IDT np.testing.assert_almost_equal(a1, a2, 1) + IDT IDT IDT np.testing.assert_almost_equal(b1, b2, 1) + + +class TestOcclusionMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_occlusion(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = de.explain('occlusion', out, X, xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, -1.0]], 10) + + IDT def test_occlusion_explainer_api(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT explainer = de.get_explainer('occlusion', out, X) + IDT IDT IDT attributions = explainer.run(xi) + IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, -1.0]], 10) + + IDT def test_occlusion_batches(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]).repeat(10, 0) + IDT IDT IDT attributions = , out, X, xi, batch_size= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, np.repeat([[0.0, 0.0], [1.0, -1.0]], 10, 0), 10) + + IDT def test_window_shape(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT attributions = , out, X, xi, window_shape= ,)) ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [0.5, 0.5]], 10) + + IDT def test_nan_warning(self): + IDT IDT with warnings.catch_warnings(record= ) IDT IDT IDT warnings.simplefilter('always') + IDT IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT IDT X, out = simpler_model(self.session) + IDT IDT IDT IDT xi = np.array([[-10, -5], [3, 1]]) + IDT IDT IDT IDT attributions = , out, X, xi, step= ) IDT IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, np.nan], [1.0, np.nan]], 10) + IDT IDT IDT IDT assert any([('nans' in str(wi.message)) for wi in w]) + + IDT def test_multiple_inputs_error(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('occlusion', out, [X1, X2], xi) + IDT IDT IDT self.assertIn('not yet supported', str(cm.exception)) + + IDT def test_occlusion_targeting_equivalence(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.nn.relu, self.session) + IDT IDT IDT xi = np.array([[5, 3]]) + IDT IDT IDT self.assertEqual(out.shape[1], 2) + IDT IDT IDT a1 = de.explain('occlusion', out * np.array([[1, 0]]), X, xi) + IDT IDT IDT b1 = de.explain('occlusion', out * np.array([[0, 1]]), X, xi) + IDT IDT IDT a2 = , out, X, xi, ys= , 0]])) ) IDT IDT IDT b2 = , out, X, xi, ys= , 1]])) ) IDT IDT IDT np.testing.assert_almost_equal(a1, a2, 10) + IDT IDT IDT np.testing.assert_almost_equal(b1, b2, 10) + + +class TestShapleySamplingMethod(TestCase): + + IDT def setUp(self): + IDT IDT self.session = tf.Session() + + IDT def tearDown(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() + + IDT def test_shapley_sampling(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = min_model(self.session) + IDT IDT IDT xi = np.array([[2, -2], [4, 2]]) + IDT IDT IDT attributions = , out, X, xi, samples= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[0.0, -2.0], [1.0, 1.0]], 1) + + IDT def test_shapley_sampling_batches(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = min_model(self.session) + IDT IDT IDT xi = np.array([[2, -2], [4, 2]]).repeat(20, 0) + IDT IDT IDT attributions = , out, X, xi, samples= , batch_size= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, np.repeat([[0.0, -2.0], [1.0, 1.0]], 20, 0), 1) + + IDT def test_shapley_sampling_batches_explainer_api(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = min_model(self.session) + IDT IDT IDT xi = np.array([[2, -2], [4, 2]]).repeat(20, 0) + IDT IDT IDT explainer = , out, X, samples= ) IDT IDT IDT attributions = , batch_size= ) IDT IDT IDT self.assertEqual(attributions.shape, xi.shape) + IDT IDT IDT np.testing.assert_almost_equal(attributions, np.repeat([[0.0, -2.0], [1.0, 1.0]], 20, 0), 1) + + IDT def test_shapley_sampling_dims(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = min_model_2d(self.session) + IDT IDT IDT xi = np.array([[[-1, 4], [-2, 1]]]) + IDT IDT IDT attributions = , out, X, xi, samples= , sampling_dims= ) IDT IDT IDT self.assertEqual(attributions.shape, (1, 2)) + IDT IDT IDT np.testing.assert_almost_equal(attributions, [[-0.5, -1.5]], 1) + + IDT def test_multiple_inputs_error(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X1, X2, out = simple_multi_inputs_model(self.session) + IDT IDT IDT xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + IDT IDT IDT with self.assertRaises(RuntimeError) as cm: + IDT IDT IDT IDT de.explain('shapley_sampling', out, [X1, X2], xi) + IDT IDT IDT self.assertIn('not yet supported', str(cm.exception)) + + IDT def test_shapley_targeting_equivalence(self): + IDT IDT with DeepExplain(graph= , session= ) IDT IDT IDT X, out = simple_model(tf.identity, self.session) + IDT IDT IDT xi = np.array([[5, 3]]) + IDT IDT IDT self.assertEqual(out.shape[1], 2) + IDT IDT IDT np.random.seed(10) + IDT IDT IDT a1 = , out * np.array([[1, 0]]), X, xi, samples= ) IDT IDT IDT np.random.seed(10) + IDT IDT IDT b1 = , out * np.array([[0, 1]]), X, xi, samples= ) IDT IDT IDT np.random.seed(10) + IDT IDT IDT a2 = , out, X, xi, ys= , 0]]), samples= ) IDT IDT IDT np.random.seed(10) + IDT IDT IDT b2 = , out, X, xi, ys= , 1]]), samples= ) IDT IDT IDT np.testing.assert_almost_equal(a1, a2, 3) + IDT IDT IDT np.testing.assert_almost_equal(b1, b2, 3) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +m1 = tf.constant([[2, 2]]) +m2 = tf.constant([[3], [3]]) +dot_operation = tf.matmul(m1, m2) +init = tf.OptimizerOptions() +sess = tf.Session() +result = sess.run(dot_operation) + +sess.close() +with tf.Session() as sess: + IDT result_ = sess.run(dot_operation) + IDT +x1 = , shape= )y1 = , shape= )z1 = x1 + y1 +x2 = , shape= , 1]) )y2 = , shape= , 2]) )z2 = tf.matmul(x2, y2) +with tf.Session() as sess: + IDT z1_value = , feed_dict= , y1: 2}) ) IDT z2_value = , feed_dict= , [2]], y2: [[3, 3]]}) ) IDT + IDT + IDT z1_value, z2_value = , z2], feed_dict= , y1: 2, x2: [[2], [2]], y2: [[3, 3]]}) ) IDT + IDT +var = tf.Variable(0) +add_operation = tf.add(var, 1) +update_operation = tf.assign(var, add_operation) +with tf.Session() as sess: + IDT sess.run(tf.global_variables_initializer()) + IDT for _ in range(3): + IDT IDT sess.run(update_operation) + IDT IDT ) +import numpy as np +import matplotlib.pyplot as plt +x = np.linspace(-5, 5, 200) +y_relu = tf.nn.relu(x) +y_sigmoid = tf.nn.sigmoid(x) +y_tanh = tf.nn.tanh(x) +y_softplus = tf.nn.softplus(x) +y_softmax = tf.nn.softmax(x) +sess = tf.Session() +y_relu, y_sigmoid, y_tanh, y_softplus, y_softmax = sess.run([y_relu, y_sigmoid, y_tanh, y_softplus, y_softmax]) +plt.figure(1, figsize= , 6)) )plt.subplot(221) +plt.plot(x, y_relu, c= , label= )plt.ylim((-1, 5)) +plt.legend(loc= )plt.subplot(222) +plt.plot(x, y_sigmoid, c= , label= )plt.ylim((-0.2, 1.2)) +plt.legend(loc= )plt.subplot(223) +plt.plot(x, y_tanh, c= , label= )plt.ylim((-1.2, 1.2)) +plt.legend(loc= )plt.subplot(224) +plt.plot(x, y_softplus, c= , label= )plt.ylim((-0.2, 6)) +plt.legend(loc= )plt.show() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT reg = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , logits= ) IDT loss += reg * tf.nn.l2_loss(weights) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, reg: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT reg = tf.placeholder(tf.float32) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT lay1_train = tf.nn.relu(logits) + IDT secLogits = tf.matmul(lay1_train, weights2) + biases2 + IDT loss = , logits= ) IDT loss += reg * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(secLogits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights) + biases) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, reg: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +num_steps = 3001 +n_batches = 5 +limited_train_dataset = train_dataset[0:batch_size * n_batches] +limited_train_labels = train_labels[0:batch_size * n_batches] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT reg = tf.placeholder(tf.float32) + IDT W1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT b1 = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT W2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT b2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1), W2) + b2 + IDT loss = , logits= ) IDT loss += reg * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(W2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1), W2) + b2) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1), W2) + b2) +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (limited_train_labels.shape[0] - batch_size) + IDT IDT batch_data = limited_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = limited_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, reg: 0.001} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +num_steps = 3001 +reg = 0.001 +dropout = 0.5 +n_batches = 5 +limited_train_dataset = train_dataset[0:batch_size * n_batches] +limited_train_labels = train_labels[0:batch_size * n_batches] +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + IDT biases = tf.Variable(tf.zeros([num_hidden_nodes])) + IDT weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + IDT biases2 = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT lay1_train = tf.nn.relu(logits) + IDT drop1 = tf.nn.dropout(lay1_train, 0.5) + IDT secLogits = tf.matmul(drop1, weights2) + biases2 + IDT loss = , logits= ) IDT loss += reg * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights2)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(secLogits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights) + biases) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (limited_train_labels.shape[0] - batch_size) + IDT IDT batch_data = limited_train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = limited_train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT global_step = tf.Variable(0) + IDT weights1 = , num_hidden_nodes1], stddev= ) IDT biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + IDT weights2 = , num_hidden_nodes2], stddev= ) IDT biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + IDT weights3 = , num_labels], stddev= ) IDT biases3 = tf.Variable(tf.zeros([num_labels])) + IDT lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + IDT lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + IDT logits = tf.matmul(lay2_train, weights3) + biases3 + IDT loss = , logits= ) IDT learning_rate = , global_step, 1000, 0.65, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + IDT lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + IDT valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + IDT lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + IDT lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + IDT test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(LSTMAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.tanh(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, lstm_state + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT """ + + IDT def __init__(self, hidden_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.dropout = dropout + + IDT def encode(self, inputs, masks, attention_inputs= , initial_state= , None), model_type= , name= , reuse= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(name, reuse= ) IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT else: + IDT IDT IDT IDT assert model_type == 'lstm' + IDT IDT IDT IDT cell = LSTMAttnCell(self.hidden_size, attention_inputs) + IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT outputs, final_state = , cell, inputs, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT final_outputs = outputs[0] + outputs[1] + IDT IDT IDT return final_outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, hidden_size, output_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.output_size = output_size + IDT IDT self.dropout = dropout + + IDT def decode(self, knowledge_rep, masks, initial_state= , None)) ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT IDT and output a probability estimation over + IDT IDT all paragraph tokens on which token should be + IDT IDT the start of the answer span, and which should be + IDT IDT the end of the answer span. + + IDT IDT :param knowledge_rep: it is a representation of the paragraph and question, + IDT IDT IDT IDT IDT IDT IDT decided by how you choose to implement the encoder + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('decoder'): + IDT IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT IDT start_states, start_final_state = , cell, knowledge_rep, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT IDT start_states = start_states[0] + start_states[1] + IDT IDT IDT IDT start_states_reshaped = tf.reshape(start_states, [-1, self.hidden_size]) + IDT IDT IDT IDT start_probs = , output_size= , bias= ) IDT IDT IDT IDT start_probs = tf.reshape(start_probs, [-1, self.output_size]) + IDT IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT IDT end_states, end_final_state = , cell, knowledge_rep, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT IDT end_states = end_states[0] + end_states[1] + IDT IDT IDT IDT end_states_reshaped = tf.reshape(end_states, [-1, self.hidden_size]) + IDT IDT IDT IDT end_probs = , output_size= , bias= ) IDT IDT IDT IDT end_probs = tf.reshape(end_probs, [-1, self.output_size]) + IDT IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT IDT add_mask = -1e+30 * (1.0 - bool_masks) + IDT IDT IDT start_probs = tf.add(start_probs, add_mask) + IDT IDT IDT end_probs = tf.add(end_probs, add_mask) + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, pretrained_embeddings, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.flags = flags + IDT IDT self.h_size = self.flags.state_size + IDT IDT self.p_size = self.flags.output_size + IDT IDT self.q_size = self.flags.question_size + IDT IDT self.embed_size = self.flags.embedding_size + IDT IDT self.dropout = self.flags.dropout + IDT IDT self.encoder = , dropout= ) IDT IDT self.decoder = , output_size= , dropout= ) IDT IDT self.context_placeholder = , shape= , self.p_size), name= ) IDT IDT self.question_placeholder = , shape= , self.q_size), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) IDT IDT self.saver = tf.train.Saver() + + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def mixer(self, q_states, ctx_states): + IDT IDT with vs.variable_scope('mixer'): + IDT IDT IDT A = , tf.transpose(q_states, perm= , 2, 1]))) ) IDT IDT IDT C_P = batch_matmul(A, q_states) + IDT IDT IDT C_P = tf.reshape(C_P, [-1, self.h_size]) + IDT IDT IDT P = tf.reshape(ctx_states, [-1, self.h_size]) + IDT IDT IDT P_final = , P], output_size= , bias= ) IDT IDT IDT P_final = tf.reshape(P_final, [-1, self.p_size, self.h_size]) + IDT IDT IDT return P_final + + IDT def filter(self, Q, P): + IDT IDT with vs.variable_scope('filter'): + IDT IDT IDT Qn = , dim= ) IDT IDT IDT Pn = , dim= ) IDT IDT IDT R = , tf.transpose(Pn, perm= , 2, 1])) ) IDT IDT IDT r = , axis= ) IDT IDT IDT r = , axis= ) IDT IDT IDT P_filtered = P * r + IDT IDT IDT return P_filtered + + IDT def coattention(self, P, Q, masks): + IDT IDT P_t = , perm= , 2, 1]) ) IDT IDT Q_t = , perm= , 2, 1]) ) IDT IDT L = tf.batch_matmul(P, Q_t) + IDT IDT AQ = , dim= ) IDT IDT AD = , perm= , 2, 1]), dim= ) IDT IDT CQ = tf.batch_matmul(P_t, AQ) + IDT IDT contexts = tf.concat(1, [Q_t, CQ]) + IDT IDT CD = tf.batch_matmul(contexts, AD) + IDT IDT lstm_inputs = , [P_t, CD]), perm= , 2, 1]) ) IDT IDT cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_fw = , output_keep_prob= ) IDT IDT cell_bw = , output_keep_prob= ) IDT IDT all_states, _ = , cell_bw, lstm_inputs, sequence_length= , dtype= ) IDT IDT U = tf.concat(2, all_states) + IDT IDT U = U[:, :self.p_size, :] + IDT IDT return U + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT P_filtered = self.context_embeddings + IDT IDT P_filtered = , P= ) IDT IDT question_states, final_question_state = , self.mask_q_placeholder, attention_inputs= , model_type= , reuse= , name= ) IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, attention_inputs= , initial_state= , model_type= , reuse= , name= ) IDT IDT feed_states = , ctx_states= ) IDT IDT self.start_probs, self.end_probs = , masks= , initial_state= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT """ + IDT IDT IDT q_mean,q_var = , axes= , shift= , name= , keep_dims= ) IDT IDT IDT ctx_mean,ctx_var = , axes= , shift= , name= , keep_dims= ) + + + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + + + IDT IDT IDT q_scale = , shape= ,self.embed_size],initializer= ) IDT IDT IDT ctx_scale = , shape= ,self.embed_size],initializer= ) IDT IDT IDT q_offset = , shape= ,self.embed_size],initializer= ) IDT IDT IDT ctx_offset = , shape= ,self.embed_size],initializer= ) + + IDT IDT IDT self.question_embeddings = , q_mean, q_var, q_offset,q_scale, variance_epsilon= ) IDT IDT IDT self.context_embeddings = , ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon= ) IDT IDT IDT """ + + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def answer(self, session, data): + IDT IDT yp_lst = [] + IDT IDT yp2_lst = [] + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle= ) IDT IDT IDT yp, yp2 = self.decode(session, *batch) + IDT IDT IDT yp_lst.append(yp) + IDT IDT IDT yp2_lst.append(yp2) + IDT IDT IDT prog_train.update(i + 1, [('Answering Questions....', 0.0)]) + IDT IDT + IDT IDT yp_all = , axis= ) IDT IDT yp2_all = , axis= ) IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(session= , context_batch= , question_batch= , answer_span_batch= , mask_ctx_batch= , mask_q_batch= ) + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset[0]) + IDT IDT else: + IDT IDT IDT inds = np.random.choice(len(dataset[0]), sample) + IDT IDT IDT sampled = [elem[inds] for elem in dataset] + IDT IDT IDT context = [context[i] for i in inds] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT cur_em = exact_match_score(pred_words, actual_words) + IDT IDT IDT em.append(float(cur_em)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + IDT IDT return np.mean(f1), np.mean(em) + + IDT def run_epoch(self, sess, train_set, val_set, train_context, val_context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT + IDT IDT prog_val = ) IDT IDT for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT + IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) + IDT def train(self, session, dataset, val_dataset, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + IDT IDT train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + IDT IDT val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + IDT IDT val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = [elem[:self.flags.batch_size * 1] for elem in train_dataset] + IDT IDT IDT val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + IDT IDT IDT num_epochs = 100 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , train_context= , val_context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT self.saver.save(session, train_dir + '/' + self.flags.run_name + '.ckpt') + + IDT def minibatches(self, data, batch_size, shuffle= ) IDT IDT num_data = len(data[0]) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + IDT IDT indices = np.arange(num_data) + IDT IDT if shuffle: + IDT IDT IDT np.random.shuffle(indices) + IDT IDT for minibatch_start in np.arange(0, num_data, batch_size): + IDT IDT IDT minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + IDT IDT IDT yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] +import tensorflow as tf +import load_data as ld +import os +import numpy as np + + +class Config(object): + IDT n_features = 36 + IDT n_height = 6 + IDT n_classes = 3 + IDT dropout = 0.5 + IDT hidden_size_1 = 512 + IDT hidden_size_2 = 256 + IDT batch_size = 4096 + IDT n_epochs = 100 + IDT lr = 0.0001 + IDT lamda = 0.01 + IDT trainrate = 0.9 + + +class NN(object): + + IDT def add_placeholders(self): + IDT IDT self.input_placeholder = , shape= , Config.n_height, Config.n_height, 1), name= ) IDT IDT self.labels_placeholder = , shape= , Config.n_classes), name= ) IDT IDT self.dropout_placeholder = , name= ) IDT IDT self.is_training = tf.placeholder(tf.bool) + + IDT def create_feed_dict(self, inputs_batch, labels_batch, dropout= , is_training= ) IDT IDT feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + IDT IDT return feed_dict + + IDT def add_prediction_op(self): + IDT IDT xavier_initializer = tf.contrib.layers.xavier_initializer() + IDT IDT with tf.name_scope('w_variable'): + IDT IDT IDT w1_init = xavier_initializer((3 * 3 * 32, Config.hidden_size_1)) + IDT IDT IDT self.W1 = , name= ) IDT IDT IDT tf.summary.histogram('W1', self.W1) + IDT IDT IDT w2_init = xavier_initializer((Config.hidden_size_1, Config.hidden_size_2)) + IDT IDT IDT self.W2 = , name= ) IDT IDT IDT tf.summary.histogram('W2', self.W2) + IDT IDT IDT w3_init = xavier_initializer((Config.hidden_size_2, Config.n_classes)) + IDT IDT IDT self.W3 = , name= ) IDT IDT IDT tf.summary.histogram('W3', self.W3) + IDT IDT with tf.name_scope('b_variable'): + IDT IDT IDT b1 = , name= ) IDT IDT IDT tf.summary.histogram('b1', b1) + IDT IDT IDT b2 = , name= ) IDT IDT IDT tf.summary.histogram('b2', b2) + IDT IDT IDT b3 = , name= ) IDT IDT IDT tf.summary.histogram('b3', b3) + IDT IDT self.global_step = tf.Variable(0) + IDT IDT with tf.name_scope('cnn'): + IDT IDT IDT layer_cnn = , 32, 3, strides= , padding= ) IDT IDT IDT hidden = tf.nn.relu(layer_cnn) + IDT IDT IDT pool = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT IDT shape = pool.get_shape().as_list() + IDT IDT IDT reshape = tf.reshape(pool, [-1, shape[1] * shape[2] * shape[3]]) + IDT IDT with tf.name_scope('layer1'): + IDT IDT IDT layer1 = tf.matmul(reshape, self.W1) + b1 + IDT IDT IDT h_batch1 = , center= , scale= , training= ) IDT IDT IDT hidden1 = tf.nn.relu(h_batch1) + IDT IDT IDT tf.summary.histogram('hidden1_out', hidden1) + IDT IDT IDT h_drop1 = tf.nn.dropout(hidden1, self.dropout_placeholder) + IDT IDT with tf.name_scope('layer2'): + IDT IDT IDT layer2 = tf.matmul(h_drop1, self.W2) + b2 + IDT IDT IDT h_batch2 = , center= , scale= , training= ) IDT IDT IDT hidden2 = tf.nn.relu(h_batch2) + IDT IDT IDT tf.summary.histogram('hidden2_out', hidden2) + IDT IDT IDT h_drop2 = tf.nn.dropout(hidden2, self.dropout_placeholder) + IDT IDT with tf.name_scope('output'): + IDT IDT IDT pred = tf.matmul(h_drop2, self.W3) + b3 + IDT IDT IDT tf.summary.histogram('out', pred) + IDT IDT return pred + + IDT def add_loss_op(self, pred): + IDT IDT loss = , labels= ) IDT IDT loss += Config.lamda * (tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.W2) + tf.nn.l2_loss(self.W3)) + IDT IDT tf.summary.scalar('loss', loss) + IDT IDT return loss + + IDT def add_training_op(self, loss): + IDT IDT extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + IDT IDT with tf.control_dependencies(extra_update_ops): + IDT IDT IDT learning_rate = , self.global_step, 1000, 0.8, staircase= ) IDT IDT IDT train_op = , global_step= ) IDT IDT return train_op + + IDT def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + IDT IDT feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + IDT IDT rs, _, loss = , self.train_op, self.loss], feed_dict= ) IDT IDT train_writer.add_summary(rs, i) + IDT IDT return loss + + IDT def __init__(self, config): + IDT IDT self.config = config + IDT IDT self.build() + + IDT def fit(self, sess, train_x, train_y): + IDT IDT loss = self.train_on_batch(sess, train_x, train_y) + + IDT def build(self): + IDT IDT with tf.name_scope('inputs'): + IDT IDT IDT self.add_placeholders() + IDT IDT with tf.name_scope('predict'): + IDT IDT IDT self.pred = self.add_prediction_op() + IDT IDT with tf.name_scope('loss'): + IDT IDT IDT self.loss = self.add_loss_op(self.pred) + IDT IDT with tf.name_scope('train'): + IDT IDT IDT self.train_op = self.add_training_op(self.loss) + IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT self.acc_op = self.add_acc_op(self.pred) + IDT IDT self.argmax_op = self.add_argmax_ops(self.pred) + + IDT def add_acc_op(self, pred): + IDT IDT correct_prediction = tf.cast(tf.equal(tf.argmax(pred, 1), tf.argmax(self.labels_placeholder, 1)), tf.float32) + IDT IDT accuracy = tf.reduce_mean(correct_prediction) + IDT IDT tf.summary.scalar('accuracy', accuracy) + IDT IDT return accuracy + + IDT def accuracy(self, sess, inputs_batch, labels_batch, name, merged, writer, i): + IDT IDT feed = self.create_feed_dict(inputs_batch, labels_batch, 1, False) + IDT IDT self.class_accuracy(sess, feed) + IDT IDT rs, acc = , self.acc_op], feed_dict= ) IDT IDT writer.add_summary(rs, i) + IDT IDT return acc + + IDT def add_argmax_ops(self, pred): + IDT IDT op = [] + IDT IDT pred_op = tf.argmax(pred, 1) + IDT IDT op.append(pred_op) + IDT IDT label_op = tf.argmax(self.labels_placeholder, 1) + IDT IDT op.append(label_op) + IDT IDT return op + + IDT def class_accuracy(self, sess, feed): + IDT IDT y_pre_argmax, v_y_argmax = , feed_dict= ) IDT IDT for i in range(Config.n_classes): + IDT IDT IDT y_pre_mask = y_pre_argmax != i + IDT IDT IDT y_pre_class = np.ma.masked_array(y_pre_argmax, y_pre_mask) + IDT IDT IDT v_y_class = np.ma.masked_array(v_y_argmax, y_pre_mask) + IDT IDT IDT correct_prediction = np.equal(y_pre_class, v_y_class) + IDT IDT IDT acc = np.mean(correct_prediction) + IDT IDT IDT + IDT IDT + + +def main(): + IDT PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + IDT train_path = os.path.join(PROJECT_ROOT, 'data/train2.data') + IDT test_path = os.path.join(PROJECT_ROOT, 'data/test2.data') + IDT train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + IDT test_log_path = os.path.join(PROJECT_ROOT, 'log/test/') + IDT dev_log_path = os.path.join(PROJECT_ROOT, 'log/dev/') + IDT X, y = ld.load(train_path) + IDT X = X.reshape([-1, 6, 6, 1]) + IDT train_num = int(X.shape[0] * Config.trainrate) + IDT X_train = X[:train_num] + IDT y_train = y[:train_num] + IDT X_dev = X[train_num:-1] + IDT y_dev = y[train_num:-1] + IDT X_test, y_test = ld.load(test_path) + IDT X_test = X_test.reshape([-1, 6, 6, 1]) + IDT + IDT + IDT + IDT + IDT with tf.Graph().as_default(): + IDT IDT config = Config() + IDT IDT nn = NN(config) + IDT IDT init = tf.global_variables_initializer() + IDT IDT saver = , keep_checkpoint_every_n_hours= ) IDT IDT y_train = , depth= ) IDT IDT y_test = , depth= ) IDT IDT y_dev = , depth= ) IDT IDT shuffle_batch_x, shuffle_batch_y = , y_train], batch_size= , capacity= , min_after_dequeue= , enqueue_many= ) IDT IDT with tf.Session() as session: + IDT IDT IDT merged = tf.summary.merge_all() + IDT IDT IDT train_writer = tf.summary.FileWriter(train_log_path, session.graph) + IDT IDT IDT test_writer = tf.summary.FileWriter(test_log_path) + IDT IDT IDT dev_writer = tf.summary.FileWriter(dev_log_path) + IDT IDT IDT session.run(init) + IDT IDT IDT coord = tf.train.Coordinator() + IDT IDT IDT threads = tf.train.start_queue_runners(session, coord) + IDT IDT IDT y_test, y_dev = session.run([y_test, y_dev]) + IDT IDT IDT i = 0 + IDT IDT IDT try: + IDT IDT IDT IDT while not coord.should_stop(): + IDT IDT IDT IDT IDT batch_x, batch_y = session.run([shuffle_batch_x, shuffle_batch_y]) + IDT IDT IDT IDT IDT loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + IDT IDT IDT IDT IDT i += 1 + IDT IDT IDT IDT IDT if i % 1000 == 0: + IDT IDT IDT IDT IDT IDT dev_acc = nn.accuracy(session, X_dev, y_dev, 'dev', merged, dev_writer, i) + IDT IDT IDT IDT IDT IDT test_acc = nn.accuracy(session, X_test, y_test, 'test', merged, test_writer, i) + IDT IDT IDT IDT IDT IDT + IDT IDT IDT IDT IDT IDT saver.save(session, os.path.join(PROJECT_ROOT, 'model/model_ckpt'), global_step= ) IDT IDT IDT except tf.errors.OutOfRangeError: + IDT IDT IDT IDT + IDT IDT IDT finally: + IDT IDT IDT IDT coord.request_stop() + IDT IDT IDT coord.join(threads) + + +if __name__ == '__main__': + IDT main() +import tensorflow as tf +import numpy as np +import Utility as util +import math + + +class SoftMax(object): + + IDT def __init__(self, d, k): + IDT IDT """ + IDT IDT :param d: dimensionality + IDT IDT :param k: number of classes + IDT IDT """ + IDT IDT self.D = d + IDT IDT self.K = k + IDT IDT self.NR_VALIDATION_DATA = 50 + IDT IDT self.NR_ITERATION = 1000 + IDT IDT self.SHOW_ACC = 100 + IDT IDT self.BATCH_SIZE = 5000 + IDT IDT self.TRAIN_STEP = 0.01 + + IDT def training(self, features, labels): + IDT IDT """ + IDT IDT Training Neural Network + IDT IDT :param features: training data [50000 x 3072] + IDT IDT :param labels: labels for X [50000 x 1] + IDT IDT :return: return a dictionary which contains all learned parameter + IDT IDT """ + IDT IDT features = self.__preprocessing(features) + IDT IDT train_features = features[self.NR_VALIDATION_DATA:] + IDT IDT train_labels = labels[self.NR_VALIDATION_DATA:] + IDT IDT validation_features = features[0:self.NR_VALIDATION_DATA] + IDT IDT validation_labels = labels[0:self.NR_VALIDATION_DATA] + IDT IDT sess = tf.InteractiveSession() + IDT IDT x = , shape= , self.D]) ) IDT IDT y_ = , shape= ) IDT IDT W = , self.K) * math.sqrt(2.0 / self.D * self.K), dtype= ) IDT IDT b = , dtype= ) IDT IDT y = tf.nn.softmax(tf.matmul(x, W) + b) + IDT IDT cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_) + IDT IDT cross_entropy_mean = tf.reduce_mean(cross_entropy) + IDT IDT train_step = tf.train.AdamOptimizer(self.TRAIN_STEP).minimize(cross_entropy_mean) + IDT IDT correct_prediction = tf.equal(tf.argmax(y, 1), y_) + IDT IDT accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + IDT IDT sess.run(tf.initialize_all_variables()) + IDT IDT for i in range(self.NR_ITERATION): + IDT IDT IDT batch = util.generate_batch(train_features, train_labels, self.BATCH_SIZE) + IDT IDT IDT train_step.run(feed_dict= , y_: batch[1]}) ) IDT IDT IDT if i % self.SHOW_ACC == 0: + IDT IDT IDT IDT value = , y_: validation_labels}) ) IDT IDT IDT IDT + IDT IDT W_final = W.eval() + IDT IDT b_final = b.eval() + IDT IDT sess.close() + IDT IDT return {'W': W_final, 'b': b_final} + + IDT def predict(self, test_features, test_labels, nn): + IDT IDT """ + IDT IDT Predict data + IDT IDT :param test_features: testing data + IDT IDT :param test_labels: labels for X + IDT IDT :param nn: it is a dictionary which contains a Neural Network + IDT IDT :return: return the predicted labels + IDT IDT """ + IDT IDT test_features = self.__preprocessing(test_features) + IDT IDT x = , shape= , self.D]) ) IDT IDT W = , shape= , self.K]) ) IDT IDT b = , shape= ) IDT IDT y = tf.nn.softmax(tf.matmul(x, W) + b) + IDT IDT sess = tf.InteractiveSession() + IDT IDT predicted_labels = , feed_dict= , W: nn['W'], b: nn['b']}) ) IDT IDT sess.close() + IDT IDT predicted_labels = , axis= ) IDT IDT acc = ) IDT IDT + IDT IDT return predicted_labels + + IDT def __preprocessing(self, X): + IDT IDT """ + IDT IDT Preprocessing the X data by zero-centered and normalized them. + IDT IDT :param X: the data. + IDT IDT :return: return the new zero-centered and normalized data. + IDT IDT """ + IDT IDT X = X.astype(np.float64) + IDT IDT X = , axis= , dtype= ) IDT IDT X = , axis= , dtype= ) IDT IDT X = X.T + IDT IDT return X + + +if __name__ == '__main__': + IDT learn_data = 'result/SoftMax2/cifar_10' + IDT D = 3072 + IDT K = 10 + IDT softMax = SoftMax(D, K) + IDT X, y, X_test, y_test = util.load_CIFAR10('data/') + IDT if util.file_exist(learn_data): + IDT IDT nn_parameter = util.unpickle(learn_data) + IDT else: + IDT IDT nn_parameter = softMax.training(X, y) + IDT IDT util.pickle_nn(learn_data, nn_parameter) + IDT predicted_labels = softMax.predict(X_test, y_test, nn_parameter) + IDT util.save_predicted_labels('result/SoftMax2/submission.csv', predicted_labels) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +""" +tensorflow policy value net +""" +import numpy as np +import tensorflow as tf + + +class PolicyValueNet: + + IDT def __init__(self, board_width, board_height, model_file= ) IDT IDT self.board_width = board_width + IDT IDT self.board_height = board_height + IDT IDT self.input_states = , shape= , 4, self.board_width, self.board_height]) ) IDT IDT self.input_state = tf.transpose(self.input_states, [0, 2, 3, 1]) + IDT IDT self.conv1 = , filters= , kernel_size= , 3], padding= , activation= ) IDT IDT self.conv2 = , filters= , kernel_size= , 3], padding= , activation= ) IDT IDT self.conv3 = , filters= , kernel_size= , 3], padding= , activation= ) IDT IDT self.action_conv = , filters= , kernel_size= , 1], padding= , activation= ) IDT IDT self.action_conv_flat = tf.reshape(self.action_conv, [-1, 4 * self.board_width * self.board_height]) + IDT IDT self.action_fc = , units= , activation= ) IDT IDT self.evaluation_conv = , filters= , kernel_size= , 1], padding= , activation= ) IDT IDT self.evaluation_conv_flat = tf.reshape(self.evaluation_conv, [-1, 2 * self.board_width * self.board_height]) + IDT IDT self.evaluation_fc1 = , units= , activation= ) IDT IDT self.evaluation_fc2 = , units= , activation= ) IDT IDT self.labels = , shape= , 1]) ) IDT IDT self.value_loss = tf.losses.mean_squared_error(self.labels, self.evaluation_fc2) + IDT IDT self.mcts_probs = , shape= , self.board_width * self.board_height]) ) IDT IDT self.policy_loss = tf.losses.softmax_cross_entropy(self.mcts_probs, self.action_fc) + IDT IDT l2_penalty_beta = 0.0001 + IDT IDT vars = tf.trainable_variables() + IDT IDT self.l2_penalty = l2_penalty_beta * tf.add_n([tf.nn.l2_loss(v) for v in vars if 'bias' not in v.name.lower()]) + IDT IDT self.loss = self.value_loss + self.policy_loss + self.l2_penalty + IDT IDT self.learning_rate = tf.placeholder(tf.float32) + IDT IDT self.optimizer = ) IDT IDT self.session = tf.Session() + IDT IDT self.entropy = tf.losses.softmax_cross_entropy(tf.exp(self.action_fc), self.action_fc) + IDT IDT init = tf.global_variables_initializer() + IDT IDT self.session.run(init) + IDT IDT self.saver = tf.train.Saver() + IDT IDT if model_file is not None: + IDT IDT IDT self.restore_model(model_file) + IDT IDT IDT + + IDT def policy_value(self, state_batch): + IDT IDT """ + IDT IDT value + IDT IDT + IDT IDT softmaxvalue + IDT IDT :return: + IDT IDT """ + IDT IDT log_act_probs, value = , self.evaluation_fc2], feed_dict= ) IDT IDT act_probs = np.exp(log_act_probs) + IDT IDT return act_probs, value + + IDT def policy_value_fn(self, board): + IDT IDT """ + IDT IDT board + IDT IDT action-probability boardvalue + IDT IDT :param board: + IDT IDT :return: + IDT IDT """ + IDT IDT legal_positions = board.availables + IDT IDT current_state = np.ascontiguousarray(board.current_state().reshape(-1, 4, self.board_width, self.board_height)) + IDT IDT act_probs, value = self.policy_value(current_state) + IDT IDT act_probs = zip(legal_positions, act_probs[0][legal_positions]) + IDT IDT return act_probs, value + + IDT def train_step(self, state_batch, mcts_probs, winner_batch, learning_rate): + IDT IDT winner_batch = np.reshape(winner_batch, (-1, 1)) + IDT IDT loss, entropy, _ = , self.entropy, self.optimizer], feed_dict= , self.mcts_probs: mcts_probs, self.labels: winner_batch, self.learning_rate: learning_rate}) ) IDT IDT return loss, entropy + + IDT def save_model(self, model_path): + IDT IDT self.saver.save(self.session, model_path) + + IDT def restore_model(self, model_path): + IDT IDT self.saver.restore(self.session, model_path) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from helpers.custom_rnn_cells import PredictionLSTMStateTuple, GridPredictionLSTMCell, BasicPredictionLSTMCell, _linear, DiscountRNNCell +from helpers.layer_helpers import normalized_columns_initializer, linear, conv2d, flatten, categorical_sample +from helpers.rnn import old_dynamic_rnn +from tensorflow.python.ops.rnn_cell_impl import _RNNCell as RNNCell +from tensorflow.python.ops.math_ops import tanh, sigmoid +import tensorflow as tf +import numpy as np +from tensorflow.python.ops import variable_scope as vs + + +class GridQAnswerNet: + + IDT def __init__(self, ob_space, ac_space, replay_size= , grid_size= ) IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.bs = ) IDT IDT self.replay_memory = [] + IDT IDT self.replay_size = replay_size + IDT IDT self.grid_size = grid_size + IDT IDT self.prob = 1.0 + IDT IDT self.final_prob = 0.1 + IDT IDT self.anneal_rate = 1.8e-07 + IDT IDT self.num_actions = ac_space + IDT IDT x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + IDT IDT x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + IDT IDT x = flatten(x) + IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT x = , values= , self.action, self.reward]) ) IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT size = 256 + IDT IDT lstm = , state_is_tuple= , ac_space= , grid_size= ) IDT IDT self.state_size = lstm.state_size + IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT c_init = np.zeros((1, lstm.state_size.c), np.float32) + IDT IDT h_init = np.zeros((1, lstm.state_size.h), np.float32) + IDT IDT pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + IDT IDT self.state_init = [c_init, h_init, pred_init] + IDT IDT c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + IDT IDT h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + IDT IDT pred_in = tf.placeholder(tf.float32, [1, lstm.state_size.pred]) + IDT IDT self.state_in = [c_in, h_in, pred_in] + IDT IDT state_in = PredictionLSTMStateTuple(c_in, h_in, pred_in) + IDT IDT lstm_outputs, lstm_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT lstm_c, lstm_h, lstm_pred = lstm_state + IDT IDT x = tf.reshape(lstm_outputs, [-1, size]) + IDT IDT self.Q = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + IDT IDT self.vf = , axis= ) IDT IDT self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + IDT IDT self.predictions = , shape= , grid_size, grid_size, ac_space]) ) IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + IDT IDT self.target_weights = [] + + IDT def get_initial_features(self): + IDT IDT return self.state_init + + IDT def act(self, ob, prev_a, prev_r, c, h, pred): + IDT IDT sess = tf.get_default_session() + IDT IDT self.update_exploration() + IDT IDT fetched = sess.run([self.Q, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred}) + IDT IDT qvals = fetched[0] + IDT IDT if np.random.uniform > self.prob: + IDT IDT IDT action = np.argmax(qvals) + IDT IDT else: + IDT IDT IDT action = np.random.choice(range(self.num_actions)) + IDT IDT return action, fetched[1], fetched[2:] + + IDT def value(self, ob, prev_a, prev_r, c, h, pred): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred})[0] + + IDT def update_replay_memory(self, tuple): + IDT IDT self.replay_memory.append(tuple) + IDT IDT if len(self.replay_memory) > self.replay_size: + IDT IDT IDT self.replay_memory.pop(0) + + IDT def update_exploration(self): + IDT IDT if self.prob > self.final_prob: + IDT IDT IDT self.prob -= self.anneal_rate + + IDT def update_target_weights(self): + IDT IDT sess = tf.get_default_session() + IDT IDT self.target_weights = sess.run(self.var_list) + + +class GridQQuestionNet: + + IDT def __init__(self, ob_space, ac_space, grid_size= ) IDT IDT self.pixel_changes = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT self.bs = tf.placeholder(tf.int32) + IDT IDT x = flatten(self.obs) + IDT IDT x = , values= , self.acs, self.rewards]) ) IDT IDT rnn = ) IDT IDT self.state_size = rnn.state_size + IDT IDT step_size = tf.shape(self.obs)[:1] + IDT IDT self.pred_init = np.zeros((1, rnn.state_size), np.float32) + IDT IDT pred_in = tf.placeholder(tf.float32, [1, rnn.state_size]) + IDT IDT outputs, state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT self.pred_targets = , shape= , grid_size * grid_size * ac_space]) ) + IDT def get_initial_features(self): + IDT IDT return self.pred_init + + +class GridQTDNet: + + IDT def __init__(self, ob_space, ac_space, grid_size= ) IDT IDT self.qnet = , ac_space= , grid_size= ) IDT IDT self.anet = , ac_space= , grid_size= ) IDT IDT pass + + +class BasicQAnswerNet: + + IDT def __init__(self, ob_space, ac_space, replay_size= , num_predictions= ) IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.bs = ) IDT IDT self.replay_memory = [] + IDT IDT self.replay_size = replay_size + IDT IDT self.prob = 1.0 + IDT IDT self.final_prob = 0.1 + IDT IDT self.anneal_rate = 1.8e-07 + IDT IDT self.num_actions = ac_space + IDT IDT x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + IDT IDT x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + IDT IDT x = flatten(x) + IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT x = , values= , self.action, self.reward]) ) IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT size = 256 + IDT IDT lstm = , state_is_tuple= , num_pred= ) IDT IDT self.state_size = lstm.state_size + IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT c_init = np.zeros((1, lstm.state_size.c), np.float32) + IDT IDT h_init = np.zeros((1, lstm.state_size.h), np.float32) + IDT IDT pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + IDT IDT self.state_init = [c_init, h_init, pred_init] + IDT IDT c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + IDT IDT h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + IDT IDT pred_in = tf.placeholder(tf.float32, [1, lstm.state_size.pred]) + IDT IDT self.state_in = [c_in, h_in, pred_in] + IDT IDT state_in = PredictionLSTMStateTuple(c_in, h_in, pred_in) + IDT IDT lstm_outputs, lstm_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT lstm_c, lstm_h, lstm_pred = lstm_state + IDT IDT x = tf.reshape(lstm_outputs, [-1, size]) + IDT IDT self.Q = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + IDT IDT self.vf = , axis= ) IDT IDT self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + IDT IDT self.predictions = lstm_pred + IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + IDT IDT self.target_weights = [] + + IDT def get_initial_features(self): + IDT IDT return self.state_init + + IDT def act(self, ob, prev_a, prev_r, c, h, pred): + IDT IDT sess = tf.get_default_session() + IDT IDT self.update_exploration() + IDT IDT fetched = sess.run([self.Q, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred}) + IDT IDT qvals = fetched[0] + IDT IDT if np.random.uniform > self.prob: + IDT IDT IDT action = np.argmax(qvals) + IDT IDT else: + IDT IDT IDT action = np.random.choice(range(self.num_actions)) + IDT IDT return action, fetched[1], fetched[2:] + + IDT def value(self, ob, prev_a, prev_r, c, h, pred): + IDT IDT sess = tf.get_default_session() + IDT IDT return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred})[0] + + IDT def update_replay_memory(self, tuple): + IDT IDT self.replay_memory.append(tuple) + IDT IDT if len(self.replay_memory) > self.replay_size: + IDT IDT IDT self.replay_memory.pop(0) + + IDT def update_exploration(self): + IDT IDT if self.prob > self.final_prob: + IDT IDT IDT self.prob -= self.anneal_rate + + IDT def update_target_weights(self): + IDT IDT sess = tf.get_default_session() + IDT IDT self.target_weights = sess.run(self.var_list) + + +class BasicQQuestionNet: + + IDT def __init__(self, ob_space, ac_space, replay_size= , num_predictions= ) IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT x = , axis= ) IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT self.bs = ) IDT IDT self.replay_memory = [] + IDT IDT self.replay_size = replay_size + IDT IDT self.num_actions = ac_space + IDT IDT x = flatten(x) + IDT IDT x = , values= , tf.reverse(self.action, axis= , tf.reverse(self.reward, axis= ) IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT size = num_predictions + IDT IDT rnn = , activation= ) IDT IDT self.pred_init = np.zeros((1, size), np.float32) + IDT IDT self.pred_in = tf.placeholder(tf.float32, [1, size]) + IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT rnn_output, rnn_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT self.prediction_targs = , shape= , size]) ) IDT IDT self.final_prediction_targs = , begin= , 0], size= , -1]) ) IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + + +class BasicQTDNet: + + IDT def __init__(self, ob_space, ac_space, replay_size= , num_predictions= ) IDT IDT self.num_predictions = num_predictions + IDT IDT with tf.variable_scope('Question'): + IDT IDT IDT self.qnet = , ac_space= ) IDT IDT with tf.variable_scope('Answer'): + IDT IDT IDT self.anet = , ac_space= ) IDT IDT with tf.variable_scope('Question', reuse= ) IDT IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT IDT self.bs = ) IDT IDT IDT self.num_actions = ac_space + IDT IDT IDT x = , axis= ) IDT IDT IDT rev_action = , axis= ) IDT IDT IDT rev_reward = , axis= ) IDT IDT IDT x = flatten(x) + IDT IDT IDT x = , values= , rev_action, rev_reward]) ) IDT IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT IDT size = 100 + IDT IDT IDT rnn = ) IDT IDT IDT self.pred_init = np.zeros((1, size), np.float32) + IDT IDT IDT self.pred_in = tf.placeholder(tf.float32, [1, size]) + IDT IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT IDT rnn_output, rnn_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT IDT self.prediction_targs = , shape= , size]) ) IDT IDT IDT self.final_prediction_targs = , begin= , 0], size= , -1]) ) IDT IDT with tf.variable_scope('Answer', reuse= ) IDT IDT IDT x = self.x + IDT IDT IDT x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + IDT IDT IDT x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + IDT IDT IDT x = flatten(x) + IDT IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT IDT x = , values= , self.action, self.reward]) ) IDT IDT IDT x = tf.expand_dims(x, [0]) + IDT IDT IDT size = 256 + IDT IDT IDT lstm = , state_is_tuple= , num_pred= ) IDT IDT IDT self.state_size = lstm.state_size + IDT IDT IDT step_size = tf.shape(self.x)[:1] + IDT IDT IDT c_init = np.zeros((1, lstm.state_size.c), np.float32) + IDT IDT IDT h_init = np.zeros((1, lstm.state_size.h), np.float32) + IDT IDT IDT pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + IDT IDT IDT self.state_init = [c_init, h_init, pred_init] + IDT IDT IDT c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + IDT IDT IDT h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + IDT IDT IDT self.state_in = [c_in, h_in, self.final_prediction_targs] + IDT IDT IDT state_in = PredictionLSTMStateTuple(c_in, h_in, self.final_prediction_targs) + IDT IDT IDT lstm_outputs, lstm_state = , x, initial_state= , sequence_length= , time_major= ) IDT IDT IDT lstm_c, lstm_h, lstm_pred = lstm_state + IDT IDT IDT x = tf.reshape(lstm_outputs, [-1, size]) + IDT IDT IDT self.Q = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + IDT IDT IDT self.vf = , axis= ) IDT IDT IDT self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + IDT IDT IDT self.predictions = lstm_pred + IDT IDT IDT self.target_weights = [] + IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + + +class BasicRNNCell(RNNCell): + IDT + + IDT def __init__(self, num_units, input_size= , activation= ) IDT IDT self._num_units = num_units + IDT IDT self._activation = activation + + IDT @property + IDT def state_size(self): + IDT IDT return self._num_units + + IDT @property + IDT def output_size(self): + IDT IDT return self._num_units + + IDT def __call__(self, inputs, state, scope= ) IDT IDT + IDT IDT with vs.variable_scope(scope or 'basic_rnn_cell'): + IDT IDT IDT output = , state], self._num_units, True, scope= ) IDT IDT return output, output + + +class ConvTDNet: + + IDT def __init__(self, ob_space, ac_space, future_steps= , pred_per_step= , ckpt= ) IDT IDT self.ckpt_file = ckpt + IDT IDT self.replay_memory = [] + IDT IDT self.future_steps = future_steps + IDT IDT self.bs = ) IDT IDT self.num_actions = ac_space + IDT IDT self.pred_per_step = pred_per_step + IDT IDT self.num_pred = pred_per_step * future_steps + IDT IDT self.use_target = ) IDT IDT self.default_last_prediction = , self.num_pred]) ) IDT IDT self.default_target_obs = ) IDT IDT with tf.variable_scope('Question'): + IDT IDT IDT self.target_obs = tf.placeholder(tf.float32, [None] + [self.future_steps] + list(ob_space)) + IDT IDT IDT feature_list = [] + IDT IDT IDT for i in range(self.future_steps): + IDT IDT IDT IDT obs = , begin= , i, 0, 0, 0], size= , 1, -1, -1, -1]), axis= ) IDT IDT IDT IDT z = tf.nn.relu(conv2d(obs, 16, 'ql1' + str(i), [8, 8], [4, 4])) + IDT IDT IDT IDT z = tf.nn.relu(conv2d(z, 32, 'ql2' + str(i), [4, 4], [2, 2])) + IDT IDT IDT IDT z = flatten(z) + IDT IDT IDT IDT z = tf.nn.relu(linear(z, self.pred_per_step, 'ql3' + str(i), normalized_columns_initializer(0.1))) + IDT IDT IDT IDT feature_list.append(z) + IDT IDT IDT self.target_predictions = , axis= ) IDT IDT with tf.variable_scope('Main'): + IDT IDT IDT self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + IDT IDT IDT self.action = tf.placeholder(tf.float32, [None, ac_space]) + IDT IDT IDT self.reward = tf.placeholder(tf.float32, [None, 1]) + IDT IDT IDT self.last_prediction = tf.placeholder(tf.float32, [None, self.num_pred]) + IDT IDT IDT x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + IDT IDT IDT x = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + IDT IDT IDT x = flatten(x) + IDT IDT IDT x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + IDT IDT IDT p = , dim= ) IDT IDT IDT p = tf.nn.tanh(linear(p, 256, 'encode_pred', normalized_columns_initializer(0.1))) + IDT IDT IDT xmain = , values= , self.action, self.reward]) ) IDT IDT IDT xaux = , values= , self.action, self.reward, p]) ) IDT IDT IDT xmain = tf.nn.relu(linear(xmain, 256, 'l4', normalized_columns_initializer(0.1))) + IDT IDT IDT y = tf.nn.relu(linear(xaux, 256, 'auxbranch_l1', normalized_columns_initializer(0.1))) + IDT IDT IDT self.approx_predictions = linear(y, self.num_pred, 'auxbranch_l2', normalized_columns_initializer(0.1)) + IDT IDT IDT self.predictions = tf.where(self.use_target, self.target_predictions, self.approx_predictions) + IDT IDT IDT x = , values= , self.predictions]) ) IDT IDT IDT val = linear(x, 1, 'value', normalized_columns_initializer(0.01)) + IDT IDT IDT self.val = , shape= ) IDT IDT self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + IDT IDT self.target_weights = [] + + IDT def get_initial_features(self): + IDT IDT return np.zeros((1, self.num_pred), np.float32) + + IDT def restore(self, sess, saver): + IDT IDT if self.ckpt_file is not None: + IDT IDT IDT saver.restore(sess, self.ckpt_file) + IDT IDT IDT self.target_weights = sess.run(self.var_list) + + IDT def targ_value(self, ob, prev_a, prev_r, target_obs, current_session= ) IDT IDT if current_session is not None: + IDT IDT IDT sess = current_session + IDT IDT else: + IDT IDT IDT sess = tf.get_default_session() + IDT IDT reward_value, prediction_values = sess.run((self.val, self.predictions), {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.bs: 1, self.target_obs: target_obs, self.use_target: True}) + IDT IDT return reward_value, prediction_values + + IDT def approx_value(self, ob, prev_a, prev_r, last_pred, current_session= ) IDT IDT if current_session is not None: + IDT IDT IDT sess = current_session + IDT IDT else: + IDT IDT IDT sess = tf.get_default_session() + IDT IDT reward_value, prediction_values = sess.run((self.val, self.predictions), {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.bs: 1, self.last_prediction: last_pred, self.use_target: False}) + IDT IDT return reward_value, prediction_values + + IDT def update_target_weights(self, current_session= ) IDT IDT if current_session is not None: + IDT IDT IDT sess = current_session + IDT IDT else: + IDT IDT IDT sess = tf.get_default_session() + IDT IDT self.target_weights = sess.run(self.var_list) + + IDT def value_with_target(self, ob, prev_a, prev_r, pred, current_session= ) IDT IDT if current_session is not None: + IDT IDT IDT sess = current_session + IDT IDT else: + IDT IDT IDT sess = tf.get_default_session() + IDT IDT feed_dict = , self.action: prev_a, self.reward: np.expand_dims(prev_r, axis= , self.bs: np.shape(ob) ) IDT IDT feed_dict.update(zip(self.var_list, self.target_weights)) + IDT IDT reward_value, prediction_values = , self.predictions), feed_dict= ) IDT IDT return reward_value, prediction_values +import sys +import tensorflow as tf + + +class Model: + + IDT def __init__(self, args, num_words, num_chars, num_tags): + IDT IDT graph = tf.Graph() + IDT IDT self.session = , config= , intra_op_parallelism_threads= ) IDT IDT with self.session.graph.as_default(): + IDT IDT IDT self.sentence_lens = , [None], name= ) IDT IDT IDT self.word_ids = , [None, None], name= ) IDT IDT IDT self.charseqs = , [None, None], name= ) IDT IDT IDT self.charseq_lens = , [None], name= ) IDT IDT IDT self.charseq_ids = , [None, None], name= ) IDT IDT IDT self.tags = , [None, None], name= ) IDT IDT IDT self.is_training = , [], name= ) IDT IDT IDT self.learning_rate = tf.placeholder_with_default(0.01, None) + IDT IDT IDT output_layer, self.predictions = self.build_model(args, num_words, num_chars, num_tags) + IDT IDT IDT weights = , dtype= ) IDT IDT IDT loss = tf.losses.sparse_softmax_cross_entropy(self.tags, output_layer, weights) + IDT IDT IDT global_step = tf.train.create_global_step() + IDT IDT IDT self.training = , global_step= , name= ) IDT IDT IDT self.current_accuracy, self.update_accuracy = , self.predictions, weights= ) IDT IDT IDT self.current_loss, self.update_loss = , weights= ) IDT IDT IDT self.reset_metrics = tf.variables_initializer(tf.get_collection(tf.GraphKeys.METRIC_VARIABLES)) + IDT IDT IDT summary_writer = , flush_millis= ) IDT IDT IDT self.summaries = {} + IDT IDT IDT with summary_writer.as_default(), tf.contrib.summary.record_summaries_every_n_global_steps(10): + IDT IDT IDT IDT self.summaries['train'] = [tf.contrib.summary.scalar('train/loss', self.update_loss), tf.contrib.summary.scalar('train/lr', self.learning_rate), tf.contrib.summary.scalar('train/accuracy', self.update_accuracy)] + IDT IDT IDT with summary_writer.as_default(), tf.contrib.summary.always_record_summaries(): + IDT IDT IDT IDT for dataset in ['dev', 'test']: + IDT IDT IDT IDT IDT self.summaries[dataset] = [tf.contrib.summary.scalar(dataset + '/loss', self.current_loss), tf.contrib.summary.scalar(dataset + '/accuracy', self.current_accuracy)] + IDT IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT IDT with summary_writer.as_default(): + IDT IDT IDT IDT tf.contrib.summary.initialize(session= , graph= ) IDT IDT IDT self.saver = tf.train.Saver() + + IDT def build_model(self, args, num_words, num_chars, num_tags): + IDT IDT raise NotImplemented + + IDT def train_epoch(self, train, batch_size, lr): + IDT IDT while not train.epoch_finished(): + IDT IDT IDT sentence_lens, word_ids, charseq_ids, charseqs, charseq_lens = , including_charseqs= ) IDT IDT IDT self.session.run(self.reset_metrics) + IDT IDT IDT self.session.run([self.training, self.summaries['train']], {self.sentence_lens: sentence_lens, self.charseqs: charseqs[train.FORMS], self.charseq_lens: charseq_lens[train.FORMS], self.word_ids: word_ids[train.FORMS], self.charseq_ids: charseq_ids[train.FORMS], self.tags: word_ids[train.TAGS], self.learning_rate: lr, self.is_training: True}) + + IDT def evaluate(self, dataset_name, dataset, batch_size): + IDT IDT self.session.run(self.reset_metrics) + IDT IDT while not dataset.epoch_finished(): + IDT IDT IDT sentence_lens, word_ids, charseq_ids, charseqs, charseq_lens = , including_charseqs= ) IDT IDT IDT self.session.run([self.update_accuracy, self.update_loss], {self.sentence_lens: sentence_lens, self.charseqs: charseqs[dataset.FORMS], self.charseq_lens: charseq_lens[dataset.FORMS], self.word_ids: word_ids[dataset.FORMS], self.charseq_ids: charseq_ids[dataset.FORMS], self.tags: word_ids[dataset.TAGS], self.is_training: False}) + IDT IDT acc, loss, _ = self.session.run([self.current_accuracy, self.current_loss, self.summaries[dataset_name]]) + IDT IDT return acc, loss + + IDT def predict(self, dataset, batch_size): + IDT IDT tags = [] + IDT IDT while not dataset.epoch_finished(): + IDT IDT IDT sentence_lens, word_ids, charseq_ids, charseqs, charseq_lens = , including_charseqs= ) IDT IDT IDT tags.extend(self.session.run(self.predictions, {self.sentence_lens: sentence_lens, self.charseqs: charseqs[dataset.FORMS], self.charseq_lens: charseq_lens[dataset.FORMS], self.word_ids: word_ids[dataset.FORMS], self.charseq_ids: charseq_ids[dataset.FORMS], self.is_training: False})) + IDT IDT return tags + + IDT def save(self, path): + IDT IDT self.saver.save(self.session, path) + + IDT def restore(self, path): + IDT IDT self.saver.restore(self.session, path) + + +class CLE(Model): + + IDT def build_model(self, args, num_words, num_chars, num_tags): + IDT IDT rnn_cell_types = {'LSTM': tf.nn.rnn_cell.BasicLSTMCell, 'GRU': tf.nn.rnn_cell.GRUCell} + IDT IDT fw_rnn_cell = rnn_cell_types[args.rnn_cell](args.rnn_cell_dim) + IDT IDT bw_rnn_cell = rnn_cell_types[args.rnn_cell](args.rnn_cell_dim) + IDT IDT word_embeddings = , [num_words, args.we_dim], dtype= ) IDT IDT word_embeddings = , rate= , noise_shape= , 1], training= ) IDT IDT word_embedded = tf.nn.embedding_lookup(word_embeddings, self.word_ids) + IDT IDT char_embeddings = , [num_chars, args.cle_dim], dtype= ) IDT IDT char_embeddings = , rate= , noise_shape= , 1], training= ) IDT IDT char_embedded = tf.nn.embedding_lookup(char_embeddings, self.charseqs) + IDT IDT _, char_rnn_final_outputs = , tf.nn.rnn_cell.GRUCell(args.cle_dim), char_embedded, sequence_length= , dtype= , scope= ) IDT IDT char_rnn = char_rnn_final_outputs[0] + char_rnn_final_outputs[1] + IDT IDT cle = tf.nn.embedding_lookup(char_rnn, self.charseq_ids) + IDT IDT embedded = , cle], axis= ) IDT IDT rnn_outputs, _ = , bw_rnn_cell, embedded, dtype= , sequence_length= , scope= ) IDT IDT rnn_output = , axis= ) IDT IDT rnn_output = , rate= , training= ) IDT IDT output_layer = tf.layers.dense(rnn_output, num_tags) + IDT IDT predictions = , axis= ) IDT IDT return output_layer, predictions + + +def get_model(name): + IDT return getattr(sys.modules[__name__], name) +import numpy as np +import os +import matplotlib.pyplot as plt +import tqdm +import tensorflow as tf +from six.moves import cPickle as pickle +import random +import math +pickle_dir = '.' +with open(os.path.join(pickle_dir, 'notMNIST.pickle'), 'rb') as f: + IDT letter_set = pickle.load(f) +train_dataset = letter_set['train_dataset'] +train_labels = letter_set['train_labels'] +valid_dataset = letter_set['valid_dataset'] +valid_labels = letter_set['valid_labels'] +test_dataset = letter_set['test_dataset'] +test_labels = letter_set['test_labels'] +valid_dataset_sani = letter_set['valid_dataset_sani'] +valid_labels_sani = letter_set['valid_labels_sani'] +test_dataset_sani = letter_set['test_dataset_sani'] +test_labels_sani = letter_set['test_labels_sani'] +samples, width, height = train_dataset.shape +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, width * height)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +n_hidden = 1024 +beta_values = [0.0005, 0.1, 1e-05, 1e-06, 0.01] +num_steps = 3001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , width * height)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT minibatch_dataset = , shape= , width * height)) ) IDT minibatch_labels = , shape= , num_labels)) ) IDT l2_regularization_beta = tf.placeholder(tf.float32) + IDT weights = , num_labels], stddev= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT weights_hidden = , n_hidden], stddev= ) IDT biases_hidden = tf.Variable(tf.zeros([n_hidden])) + IDT hidden = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + IDT logits = tf.matmul(hidden, weights) + biases + IDT loss = , logits= ) IDT loss = loss + l2_regularization_beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_hidden)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT hidden_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + IDT valid_prediction = tf.nn.softmax(tf.matmul(hidden_valid, weights) + biases) + IDT hidden_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + IDT test_prediction = tf.nn.softmax(tf.matmul(hidden_test, weights) + biases) +betaplot = [] +accuracyplot = [] +for beta in beta_values: + IDT with tf.Session(graph= ) IDT IDT tf.global_variables_initializer().run() + IDT IDT + IDT IDT for step in tqdm.trange(num_steps): + IDT IDT IDT offset = random.randint(0, int((train_labels.shape[0] - batch_size) / batch_size)) + IDT IDT IDT offset = offset * batch_size + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, l2_regularization_beta: beta} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT betaplot.append(beta) + IDT IDT accuracyplot.append(accuracy(valid_prediction.eval(), valid_labels)) + IDT IDT ) + IDT IDT , valid_labels)) + IDT IDT , test_labels)) +plt.figure(1) +plt.semilogx(betaplot, accuracyplot, '+') +plt.xlabel('Beta') +plt.ylabel('Valid Accuracy') +plt.show() +num_steps = 300 +num_batches = 25 +fixed_offset = random.sample(range(int((train_labels.shape[0] - batch_size) / batch_size)), num_batches) +fixed_offset = [(i * batch_size) for i in fixed_offset] +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = random.choice(fixed_offset) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, l2_regularization_beta: 0} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step == num_steps - 1: + IDT IDT IDT for idx, offset in enumerate(fixed_offset): + IDT IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT IDT predictions = , feed_dict= ) IDT IDT IDT IDT )) + IDT IDT IDT , valid_labels)) + IDT IDT IDT , test_labels)) +batch_size = 128 * 3 +n_hidden = 1024 * 2 +dropout_keep_probablity = 1 +l2_regularization_beta = 0.0005 +num_steps = 9001 +initial_learning_rate = 0.5 +decay_steps = 3500 +decay_size = 0.6 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , width * height)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT minibatch_dataset = , shape= , width * height)) ) IDT minibatch_labels = , shape= , num_labels)) ) IDT global_step = tf.Variable(0) + IDT learning_rate = , global_step, decay_steps, decay_size, staircase= ) IDT weights = , num_labels], stddev= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT weights_hidden = , n_hidden], stddev= ) IDT biases_hidden = tf.Variable(tf.zeros([n_hidden])) + IDT hidden = tf.nn.dropout(tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden), dropout_keep_probablity) + IDT logits = tf.matmul(hidden, weights) + biases + IDT loss = , logits= ) IDT loss = loss + l2_regularization_beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_hidden)) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT hidden_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + IDT valid_prediction = tf.nn.softmax(tf.matmul(hidden_valid, weights) + biases) + IDT hidden_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + IDT test_prediction = tf.nn.softmax(tf.matmul(hidden_test, weights) + biases) +losses = [] +plotsteps = [] +acc = [] +learning_rate_steps = [] +with tf.Session(graph= ) IDT tf.global_variables_initializer().run() + IDT + IDT for step in tqdm.trange(num_steps): + IDT IDT offset = random.randint(0, int((train_labels.shape[0] - batch_size) / batch_size)) + IDT IDT offset = offset * batch_size + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 100 == 0: + IDT IDT IDT acc.append(accuracy(valid_prediction.eval(), valid_labels)) + IDT IDT IDT losses.append(l) + IDT IDT IDT plotsteps.append(step) + IDT IDT IDT learning_rate_steps.append(session.run(learning_rate)) + IDT ) + IDT , valid_labels)) + IDT , test_labels)) +plotoffset = 10 +f, axarr = , sharex= )axarr[0].plot(plotsteps[plotoffset:], acc[plotoffset:]) +axarr[0].set_title('Accuracy [%]') +axarr[1].plot(plotsteps[plotoffset:], losses[plotoffset:]) +axarr[1].set_title('Loss') +axarr[2].plot(plotsteps[plotoffset:], learning_rate_steps[plotoffset:]) +axarr[2].set_title('Learning Rate') +plt.show() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from ModelSkeletons.SupervisedModel import SupervisedModel, define_scope +from LSTM_CTCConfig import LSTM_CTCModelConfig + + +class LSTM_CTCModel(SupervisedModel): + + IDT def __init__(self, config: LSTM_CTCModelConfig): + IDT IDT self.config = config + IDT IDT self.graph = tf.Graph() + IDT IDT self.graph.as_default() + IDT IDT self.build_placeholders() + IDT IDT self.inference + IDT IDT self.loss + IDT IDT self.training + IDT IDT self.decoded_inference + IDT IDT self.evaluation + IDT IDT self.session = None + + IDT def build_placeholders(self): + IDT IDT self.input_placeholder = , shape= , self.config.input_max_time, self.config.input_frame_size], name= ) IDT IDT self.input_lengths_placeholder = , shape= , name= ) IDT IDT self.output_placeholder = , name= ) IDT IDT self.output_lengths_placeholder = , shape= , name= ) + IDT @define_scope + IDT def inference(self): + IDT IDT with tf.name_scope('LSTM'): + IDT IDT IDT cell = tf.nn.rnn_cell.LSTMCell(self.config.cell_size) + IDT IDT IDT cell = tf.nn.rnn_cell.MultiRNNCell([cell] * self.config.cell_count) + IDT IDT IDT cell_initial = cell.zero_state(tf.shape(self.input_placeholder)[0], tf.float32) + IDT IDT IDT rnn, _ = tf.nn.dynamic_rnn(cell, self.input_placeholder, self.output_lengths_placeholder, cell_initial) + IDT IDT IDT flatten_rnn = , shape= , self.config.input_max_time * self.config.cell_size]) ) IDT IDT layer = flatten_rnn + IDT IDT previous_layer_size = int(layer.get_shape()[1]) + IDT IDT for i in range(self.config.fully_connected_count): + IDT IDT IDT with tf.name_scope('FullyConnected_n' + str(i)): + IDT IDT IDT IDT weights = , self.config.fully_connected_size], stddev= , name= ) IDT IDT IDT IDT biases = , name= ) IDT IDT IDT IDT layer = tf.matmul(layer, weights) + biases + IDT IDT IDT IDT layer = tf.nn.relu(layer) + IDT IDT IDT IDT previous_layer_size = int(layer.get_shape()[1]) + IDT IDT with tf.name_scope('FullyConnected_Last'): + IDT IDT IDT weights = , self.config.output_max_time * self.config.output_frame_size], stddev= ) IDT IDT IDT biases = ) IDT IDT IDT layer = tf.matmul(layer, weights) + biases + IDT IDT logits = , shape= , self.config.output_max_time, self.config.output_frame_size]) ) IDT IDT return logits + + IDT @define_scope + IDT def loss(self): + IDT IDT ctc_loss = , self.output_placeholder, self.output_lengths_placeholder, time_major= ) IDT IDT return tf.reduce_mean(ctc_loss, name= ) + IDT @define_scope + IDT def training(self): + IDT IDT tf.summary.scalar('loss', self.loss) + IDT IDT tf.summary.scalar('error', self.evaluation) + IDT IDT global_step = , name= , trainable= ) IDT IDT optimizer = , epsilon= ) IDT IDT return optimizer.minimize(self.loss, global_step= ) + IDT @define_scope + IDT def decoded_inference(self): + IDT IDT inference_timemajor = tf.transpose(self.inference, [1, 0, 2]) + IDT IDT decoded, prob = tf.nn.ctc_greedy_decoder(inference_timemajor, self.output_lengths_placeholder) + IDT IDT return decoded + + IDT @define_scope + IDT def evaluation(self): + IDT IDT average_error = [] + IDT IDT for result in self.decoded_inference: + IDT IDT IDT predictions = tf.cast(result, tf.int32) + IDT IDT IDT error = , self.output_placeholder, normalize= ) IDT IDT IDT average_error.append(tf.reduce_mean(error, name= ) IDT IDT return tf.reduce_mean(average_error) + + IDT @staticmethod + IDT def tokens_for_sparse(sequences): + IDT IDT eos_value = 9632 + IDT IDT tmp = [] + IDT IDT for seq_idx in range(len(sequences)): + IDT IDT IDT seq = sequences[seq_idx] + IDT IDT IDT for i in range(len(seq)): + IDT IDT IDT IDT end_idx = i + IDT IDT IDT IDT if seq[i] == eos_value: + IDT IDT IDT IDT IDT break + IDT IDT IDT tmp.append(seq[:end_idx]) + IDT IDT sequences = tmp + IDT IDT indices = [] + IDT IDT values = [] + IDT IDT for n, seq in enumerate(sequences): + IDT IDT IDT indices.extend(zip([n] * len(seq), range(len(seq)))) + IDT IDT IDT values.extend(seq) + IDT IDT indices = , dtype= ) IDT IDT values = , dtype= ) IDT IDT shape = , np.asarray(indices).max(0)[1] + 1], dtype= ) IDT IDT return indices, values, shape + + IDT def train(self, database, iteration_count: int, batch_size: int): + IDT IDT session_config = tf.ConfigProto() + IDT IDT session_config.gpu_options.allow_growth = True + IDT IDT self.session = ) IDT IDT self.init_model() + IDT IDT train_data = database.train_dataset + IDT IDT for i in tqdm(range(iteration_count)): + IDT IDT IDT feed_dict = self.train_step(train_data, batch_size) + IDT IDT IDT if feed_dict is None: + IDT IDT IDT IDT continue + IDT IDT IDT for key in feed_dict: + IDT IDT IDT IDT if feed_dict[key] is None: + IDT IDT IDT IDT IDT continue + IDT IDT IDT if len(feed_dict) == 0: + IDT IDT IDT IDT continue + IDT IDT IDT if (i + 1) % 10 == 0: + IDT IDT IDT IDT summary_str = , feed_dict= ) IDT IDT IDT IDT self.summary_writer.add_summary(summary_str, i) + IDT IDT IDT IDT self.summary_writer.flush() + IDT IDT IDT if (i + 1) % 100 == 0: + IDT IDT IDT IDT + IDT IDT IDT IDT ) + IDT IDT IDT IDT + IDT IDT IDT if (i + 1) % 1000 == 0: + IDT IDT IDT IDT self.saver.save(self.session, self.config.checkpoints_path + '\\network', global_step= ) IDT IDT IDT IDT self.test(database) + + IDT def init_model(self): + IDT IDT self.summary = tf.summary.merge_all() + IDT IDT self.summary_writer = tf.summary.FileWriter(self.config.checkpoints_path + '/logs', self.session.graph) + IDT IDT self.saver = tf.train.Saver() + IDT IDT checkpoint = tf.train.get_checkpoint_state(self.config.checkpoints_path) + IDT IDT if checkpoint and checkpoint.model_checkpoint_path: + IDT IDT IDT + IDT IDT IDT self.saver.restore(self.session, checkpoint.model_checkpoint_path) + IDT IDT else: + IDT IDT IDT + IDT IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT + + IDT def get_feed_dict(self, dataset, batch_size, one_hot= ) IDT IDT batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = , one_hot= ) IDT IDT tmp = [] + IDT IDT for j in range(len(batch_outputs)): + IDT IDT IDT tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + IDT IDT batch_outputs_feed = np.array(tmp) + IDT IDT try: + IDT IDT IDT batch_outputs_feed = LSTM_CTCModel.tokens_for_sparse(batch_outputs_feed) + IDT IDT except: + IDT IDT IDT return None + IDT IDT tmp = [] + IDT IDT for j in range(len(batch_inputs)): + IDT IDT IDT tmp.append(batch_inputs[j][:self.config.input_max_time]) + IDT IDT batch_inputs = np.array(tmp) + IDT IDT feed_dict = {self.input_placeholder: batch_inputs, self.input_lengths_placeholder: batch_input_lengths, self.output_placeholder: batch_outputs_feed, self.output_lengths_placeholder: batch_output_lengths} + IDT IDT return feed_dict + + IDT def train_step(self, train_data, batch_size: int): + IDT IDT feed_dict = , batch_size, one_hot= ) IDT IDT self.session.run(self.training, feed_dict= ) IDT IDT return feed_dict + + IDT def test(self, database, batch_size= ) IDT IDT if self.session is None: + IDT IDT IDT session_config = tf.ConfigProto() + IDT IDT IDT session_config.gpu_options.allow_growth = True + IDT IDT IDT self.session = ) IDT IDT IDT self.init_model() + IDT IDT test_data = database.test_dataset + IDT IDT feed_dict = , batch_size, one_hot= ) IDT IDT + IDT IDT ) * 100, '%') + IDT IDT +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +mnist = , one_hot= )x = tf.placeholder(tf.float32, [None, 784]) +y_ = tf.placeholder(tf.float32, [None, 10]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) + + +def weight_variable(shape): + IDT initial = , stddev= ) IDT return tf.Variable(initial) + + +def bias_variable(shape): + IDT initial = , shape= ) IDT return tf.Variable(initial) + + +def conv2d(x, W): + IDT return tf.nn.conv2d(x, W, strides= , 1, 1, 1], padding= ) + +def max_pool_2x2(x): + IDT return tf.nn.max_pool(x, ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) + +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder(tf.float32) +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 +cross_entropy = , logits= )optimizer = )correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +session = tf.InteractiveSession() +session.run(tf.global_variables_initializer()) +for i in range(1000): + IDT batch = mnist.train.next_batch(50) + IDT if i % 100 == 0: + IDT IDT train_accuracy = , y_: batch[1], keep_prob: 1.0}) ) IDT IDT ) + IDT optimizer.run(feed_dict= , y_: batch[1], keep_prob: 0.5}) )) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import scipy.io as sio +import sys, os, os.path, inspect +filedir = inspect.getframeinfo(inspect.currentframe())[0] +filedir = os.path.dirname(os.path.abspath(filedir)) +mat_contents = sio.loadmat(filedir + '/sampleData_wNetChangeLabels.mat') +train_dataset = np.array(mat_contents['train_data']).astype(np.float32) +train_labels = np.array(mat_contents['train_label']).astype(np.float32) +valid_dataset = np.array(mat_contents['test_data']).astype(np.float32) +valid_labels = np.array(mat_contents['test_label']).astype(np.float32) + + +sample_size = train_dataset.shape[1] +num_labels = train_labels.shape[1] + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +batch_size = 128 +hidden_size = 512 +hidden_1_size = hidden_size +hidden_2_size = hidden_size +hidden_3_size = hidden_size +beta = 0.01 +SEED = None +dropoutPercent = 0.5 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , sample_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_predict_label = , shape= , sample_size)) ) IDT weights_1 = , hidden_1_size], stddev= , name= ) IDT biases_1 = , name= ) IDT weights_2 = , hidden_2_size]), name= ) IDT biases_2 = , name= ) IDT weights_3 = , hidden_3_size]), name= ) IDT biases_3 = , name= ) IDT weights_o = , num_labels], stddev= , name= ) IDT biases_o = , name= ) + IDT def model(data, train= ) IDT IDT hidden = tf.nn.relu(tf.matmul(data, weights_1) + biases_1) + IDT IDT if train: + IDT IDT IDT hidden = , dropoutPercent, seed= ) IDT IDT hidden = tf.nn.relu(tf.matmul(hidden, weights_2) + biases_2) + IDT IDT if train: + IDT IDT IDT hidden = , dropoutPercent, seed= ) IDT IDT hidden = tf.nn.relu(tf.matmul(hidden, weights_3) + biases_3) + IDT IDT if train: + IDT IDT IDT hidden = , dropoutPercent, seed= ) IDT IDT return tf.matmul(hidden, weights_o) + biases_o + IDT logits = model(tf_train_dataset, True) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + IDT regularized_loss = loss + beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_o)) + IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(0.001, global_step, 250, 0.96) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(model(tf_train_dataset)) + IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + IDT data_prediction = tf.nn.softmax(model(tf_predict_label)) + IDT init_op = tf.initialize_all_variables() + IDT saver = tf.train.Saver() +session = ) + +def train(num_steps= , save= , filename= ) IDT with session.as_default(): + IDT IDT session.run(init_op) + IDT IDT + IDT IDT for step in range(num_steps): + IDT IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT if step % 500 == 0: + IDT IDT IDT IDT )) + IDT IDT IDT IDT ) + IDT IDT IDT IDT , valid_labels)) + IDT IDT IDT IDT save_path = , filedir + '/' + filename, global_step= ) IDT IDT if save: + IDT IDT IDT save_path = saver.save(session, filedir + '/' + filename) + + +def init(): + IDT with session.as_default(): + IDT IDT saver.restore(session, filedir + '/model_directionDNN.ckpt') + + +def predict(input_data): + IDT with session.as_default(): + IDT IDT feed_dict = {tf_predict_label: input_data} + IDT IDT predictionArray = , feed_dict= ) IDT IDT prediction = np.argmax(predictionArray) + IDT IDT ) + IDT IDT return prediction + + +def close(): + IDT session.close() + + +if __name__ == '__main__': + IDT train(save= ) IDT close +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.optimization = ) + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + +class DeepQNetwork(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2, lr): + IDT IDT super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.action = , shape= ,), name= ) IDT IDT self.mle_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.action)) + l2 * regularizer + IDT IDT self.mle_optimization = ) IDT IDT actions_one_hot = , self.output_dim, 1.0, 0.0, name= ) IDT IDT predicted_q = , self.W1), self.b1)) + 1) / 2.0 * actions_one_hot, reduction_indices= ) IDT IDT self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + IDT IDT self.optimization = ) + IDT def train(self, session, inputs, action, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) ) IDT IDT return cost + + IDT def policy(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT return session.run(self.q_function, feed_dict= , self.pos: pos, self.deprel: deprel}) ) + IDT def classify(self, session, inputs): + IDT IDT return self.policy(session, inputs) + + IDT def oracle_train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.mle_loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.action: outputs}) ) IDT IDT return cost +import tensorflow as tf +from bilstm import biLSTM +from utils import feature2cos_sim, max_pooling, cal_loss_and_acc + + +class LSTM_QA(object): + + IDT def __init__(self, batch_size, num_unroll_steps, embeddings, embedding_size, rnn_size, num_rnn_layers, max_grad_norm, l2_reg_lambda= , adjust_weight= , label_weight= , is_training= ) IDT IDT self.batch_size = batch_size + IDT IDT self.embeddings = embeddings + IDT IDT self.embedding_size = embedding_size + IDT IDT self.adjust_weight = adjust_weight + IDT IDT self.label_weight = label_weight + IDT IDT self.rnn_size = rnn_size + IDT IDT self.num_rnn_layers = num_rnn_layers + IDT IDT self.num_unroll_steps = num_unroll_steps + IDT IDT self.max_grad_norm = max_grad_norm + IDT IDT self.l2_reg_lambda = l2_reg_lambda + IDT IDT self.is_training = is_training + IDT IDT self.keep_prob = , name= ) IDT IDT self.lr = , trainable= ) IDT IDT self.new_lr = , shape= , name= ) IDT IDT self._lr_update = tf.assign(self.lr, self.new_lr) + IDT IDT self.ori_input_quests = , shape= , self.num_unroll_steps]) ) IDT IDT self.cand_input_quests = , shape= , self.num_unroll_steps]) ) IDT IDT self.neg_input_quests = , shape= , self.num_unroll_steps]) ) IDT IDT self.test_input_q = , shape= , self.num_unroll_steps]) ) IDT IDT self.test_input_a = , shape= , self.num_unroll_steps]) ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding_layer'): + IDT IDT IDT W = , trainable= , name= ) IDT IDT IDT ori_quests = tf.nn.embedding_lookup(W, self.ori_input_quests) + IDT IDT IDT cand_quests = tf.nn.embedding_lookup(W, self.cand_input_quests) + IDT IDT IDT neg_quests = tf.nn.embedding_lookup(W, self.neg_input_quests) + IDT IDT IDT test_q = tf.nn.embedding_lookup(W, self.test_input_q) + IDT IDT IDT test_a = tf.nn.embedding_lookup(W, self.test_input_a) + IDT IDT with tf.variable_scope('LSTM_scope', reuse= ) IDT IDT IDT ori_q = biLSTM(ori_quests, self.rnn_size) + IDT IDT IDT ori_q_feat = tf.nn.tanh(max_pooling(ori_q)) + IDT IDT with tf.variable_scope('LSTM_scope', reuse= ) IDT IDT IDT cand_a = biLSTM(cand_quests, self.rnn_size) + IDT IDT IDT neg_a = biLSTM(neg_quests, self.rnn_size) + IDT IDT IDT cand_q_feat = tf.nn.tanh(max_pooling(cand_a)) + IDT IDT IDT neg_q_feat = tf.nn.tanh(max_pooling(neg_a)) + IDT IDT IDT test_q_out = biLSTM(test_q, self.rnn_size) + IDT IDT IDT test_q_out = tf.nn.tanh(max_pooling(test_q_out)) + IDT IDT IDT test_a_out = biLSTM(test_a, self.rnn_size) + IDT IDT IDT test_a_out = tf.nn.tanh(max_pooling(test_a_out)) + IDT IDT self.ori_cand = feature2cos_sim(ori_q_feat, cand_q_feat) + IDT IDT self.ori_neg = feature2cos_sim(ori_q_feat, neg_q_feat) + IDT IDT self.loss, self.acc = cal_loss_and_acc(self.ori_cand, self.ori_neg) + IDT IDT self.test_q_a = feature2cos_sim(test_q_out, test_a_out) + + IDT def assign_new_lr(self, session, lr_value): + IDT IDT session.run(self._lr_update, feed_dict= )import random +import time +import math +import gc +import psutil +import tensorflow as tf +import numpy as np +from sklearn import preprocessing +from param import * +from test_funcs import eval_alignment, eval_alignment_mul, eval_alignment_multi_embed +g = 1000000000 + + +def embed_init(mat_x, mat_y, name, is_l2= ) IDT + IDT embeddings = , mat_y], stddev= ) IDT return tf.nn.l2_normalize(embeddings, 1) if is_l2 else embeddings + + +def mul(tensor1, tensor2, session, num, sigmoid): + IDT t = time.time() + IDT if num < 20000: + IDT IDT sim_mat = , tensor2, transpose_b= ) IDT IDT if sigmoid: + IDT IDT IDT res = ) IDT IDT else: + IDT IDT IDT res = ) IDT else: + IDT IDT res = , tensor2.eval(session= ) IDT - t)) + IDT return res + + +class KGE_Model: + + IDT def __init__(self, ent_num, rel_num, seed_sup_ent1, seed_sup_ent2, ref_ent1, ref_ent2, kb1_ents, kb2_ents, dim, embed_size, lr): + IDT IDT self.ent_num = ent_num + IDT IDT self.rel_num = rel_num + IDT IDT self.seed_sup_ent1 = seed_sup_ent1 + IDT IDT self.seed_sup_ent2 = seed_sup_ent2 + IDT IDT self.ref_ent1 = ref_ent1 + IDT IDT self.ref_ent2 = ref_ent2 + IDT IDT self.kb1_ents = kb1_ents + IDT IDT self.kb2_ents = kb2_ents + IDT IDT self.embed_size = embed_size + IDT IDT self.lr = lr + IDT IDT self.dim = dim + IDT IDT config = tf.ConfigProto() + IDT IDT config.gpu_options.allow_growth = True + IDT IDT self.session = ) IDT IDT self._generate_variables() + IDT IDT self._generate_graph() + IDT IDT self._generate_alignment_graph() + IDT IDT self._generate_likelihood_graph() + IDT IDT tf.global_variables_initializer().run(session= ) + IDT def _generate_variables(self): + IDT IDT with tf.variable_scope('relation' + 'embedding'): + IDT IDT IDT self.ent_embeddings = embed_init(self.ent_num, self.embed_size, 'ent_embeds') + IDT IDT IDT self.rel_embeddings = embed_init(self.rel_num, self.embed_size, 'rel_embeds') + IDT IDT IDT self.ent_embeddings = tf.nn.l2_normalize(self.ent_embeddings, 1) + IDT IDT IDT self.rel_embeddings = tf.nn.l2_normalize(self.rel_embeddings, 1) + + IDT def _generate_graph(self): + + IDT IDT def generate_loss(phs, prs, pts, nhs, nrs, nts): + IDT IDT IDT pos_score = tf.reduce_sum(tf.pow(phs + prs - pts, 2), 1) + IDT IDT IDT neg_score = tf.reduce_sum(tf.pow(nhs + nrs - nts, 2), 1) + IDT IDT IDT pos_loss = tf.reduce_sum(tf.maximum(pos_score - tf.constant(P.lambda_1), 0)) + IDT IDT IDT neg_loss = P.mu_1 * tf.reduce_sum(tf.maximum(tf.constant(P.lambda_2) - neg_score, 0)) + IDT IDT IDT return pos_loss, neg_loss + + IDT IDT def generate_optimizer(loss): + IDT IDT IDT opt_vars = [v for v in tf.trainable_variables() if v.name.startswith('relation')] + IDT IDT IDT optimizer = , var_list= ) IDT IDT IDT return optimizer + IDT IDT self.pos_hs = , shape= ) IDT IDT self.pos_rs = , shape= ) IDT IDT self.pos_ts = , shape= ) IDT IDT self.neg_hs = , shape= ) IDT IDT self.neg_rs = , shape= ) IDT IDT self.neg_ts = , shape= ) IDT IDT phs = tf.nn.embedding_lookup(self.ent_embeddings, self.pos_hs) + IDT IDT prs = tf.nn.embedding_lookup(self.rel_embeddings, self.pos_rs) + IDT IDT pts = tf.nn.embedding_lookup(self.ent_embeddings, self.pos_ts) + IDT IDT nhs = tf.nn.embedding_lookup(self.ent_embeddings, self.neg_hs) + IDT IDT nrs = tf.nn.embedding_lookup(self.rel_embeddings, self.neg_rs) + IDT IDT nts = tf.nn.embedding_lookup(self.ent_embeddings, self.neg_ts) + IDT IDT self.pos_loss, self.neg_loss = generate_loss(phs, prs, pts, nhs, nrs, nts) + IDT IDT self.triple_loss = self.pos_loss + self.neg_loss + IDT IDT self.triple_optimizer = generate_optimizer(self.triple_loss) + + IDT def _generate_alignment_graph(self): + + IDT IDT def generate_loss(phs, prs, pts): + IDT IDT IDT pos_loss = tf.reduce_sum(tf.log(tf.sigmoid(-tf.reduce_sum(tf.pow(phs + prs - pts, 2), 1)))) + IDT IDT IDT return -pos_loss + + IDT IDT def generate_optimizer(loss): + IDT IDT IDT opt_vars = [v for v in tf.trainable_variables() if v.name.startswith('relation')] + IDT IDT IDT optimizer = , var_list= ) IDT IDT IDT return optimizer + IDT IDT self.new_h = , shape= ) IDT IDT self.new_r = , shape= ) IDT IDT self.new_t = , shape= ) IDT IDT phs = tf.nn.embedding_lookup(self.ent_embeddings, self.new_h) + IDT IDT prs = tf.nn.embedding_lookup(self.rel_embeddings, self.new_r) + IDT IDT pts = tf.nn.embedding_lookup(self.ent_embeddings, self.new_t) + IDT IDT self.alignment_loss = generate_loss(phs, prs, pts) + IDT IDT self.alignment_optimizer = generate_optimizer(self.alignment_loss) + + IDT def _generate_likelihood_graph(self): + IDT IDT self.ents1 = , shape= ) IDT IDT self.ents2 = , shape= ) IDT IDT self.likelihood_mat = , shape= , self.dim]) ) IDT IDT ent1_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ents1) + IDT IDT ent2_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ents2) + IDT IDT mat = , ent2_embed, transpose_b= ) IDT IDT self.likelihood_loss = -tf.reduce_sum(tf.multiply(mat, self.likelihood_mat)) + IDT IDT self.likelihood_optimizer = tf.train.AdagradOptimizer(self.lr).minimize(self.likelihood_loss) + + IDT def test(self, selected_pairs= ) IDT IDT t1 = time.time() + IDT IDT refs1_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent1) + IDT IDT refs2_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent2) + IDT IDT refs1_embed = tf.nn.l2_normalize(refs1_embed, 1) + IDT IDT refs2_embed = tf.nn.l2_normalize(refs2_embed, 1) + IDT IDT refs1_embed = ) IDT IDT refs2_embed = ) IDT IDT prec_set = , refs2_embed, P.ent_top_k, selected_pairs, mess= ) IDT IDT t2 = time.time() + IDT IDT m1 = psutil.virtual_memory().used + IDT IDT del refs1_embed, refs2_embed + IDT IDT gc.collect() + IDT IDT - t1)) + IDT IDT return prec_set + + IDT def eval_ent_embeddings(self): + IDT IDT return self.ent_embeddings.eval(session= ) + IDT def eval_rel_embeddings(self): + IDT IDT return self.rel_embeddings.eval(session= ) + IDT def eval_ref_sim_mat(self, sigmoid= ) IDT IDT refs1_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent1) + IDT IDT refs2_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent2) + IDT IDT refs1_embeddings = tf.nn.l2_normalize(refs1_embeddings, 1) + IDT IDT refs2_embeddings = tf.nn.l2_normalize(refs2_embeddings, 1) + IDT IDT return mul(refs1_embeddings, refs2_embeddings, self.session, len(self.ref_ent1), sigmoid) + + IDT def eval_kb1_mat(self, sigmoid= ) IDT IDT ent1_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb1_ents) + IDT IDT ent2_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb1_ents) + IDT IDT return mul(ent1_embeddings, ent2_embeddings, self.session, len(self.kb1_ents), sigmoid) + + IDT def eval_kb2_mat(self, sigmoid= ) IDT IDT ent1_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb2_ents) + IDT IDT ent2_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb2_ents) + IDT IDT return mul(ent1_embeddings, ent2_embeddings, self.session, len(self.kb2_ents), sigmoid) + + IDT def eval_sim_mat(self, ent1, ent2, sigmoid= ) IDT IDT embeddings1 = tf.nn.embedding_lookup(self.ent_embeddings, ent1) + IDT IDT embeddings2 = tf.nn.embedding_lookup(self.ent_embeddings, ent2) + IDT IDT return mul(embeddings1, embeddings2, self.session, len(ent1), sigmoid) + + IDT def eval_kb1_embed(self): + IDT IDT return tf.nn.embedding_lookup(self.ent_embeddings, self.kb1_ents).eval(session= ) + IDT def eval_kb2_embed(self): + IDT IDT return tf.nn.embedding_lookup(self.ent_embeddings, self.kb2_ents).eval(session= ) + IDT def save(self, folder, suffix): + IDT IDT np.save(folder + 'ent1_embeds_' + suffix + '.npy', self.eval_kb1_embed()) + IDT IDT np.save(folder + 'ent2_embeds_' + suffix + '.npy', self.eval_kb2_embed()) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT test_phase = tf.placeholder(tf.bool) + IDT logits_out = conv_net(test_x, test_k, test_phase) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network, x, y, keep_prob): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_phase = tf.placeholder(tf.bool) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y, x, y, keep_prob, train_phase= ) IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +try: + IDT import bequick +except ImportError: + IDT import sys + IDT import os + IDT sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + IDT from .tb_parser import Parser +except (ValueError, SystemError) as e: + IDT from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + IDT _indices = [tf.to_int32(i) for i in indices] + IDT session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + IDT form = [_[0] for _ in inputs] + IDT pos = [_[1] for _ in inputs] + IDT deprel = [_[2] for _ in inputs] + IDT return form, pos, deprel + + +class Network(object): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + IDT IDT self.form_size = form_size + IDT IDT self.form_dim = form_dim + IDT IDT self.pos_size = pos_size + IDT IDT self.pos_dim = pos_dim + IDT IDT self.deprel_size = deprel_size + IDT IDT self.deprel_dim = deprel_dim + IDT IDT self.hidden_dim = hidden_dim + IDT IDT self.output_dim = output_dim + IDT IDT self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + IDT IDT self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + IDT IDT self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + IDT IDT self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + IDT IDT self.dropout = dropout + IDT IDT self.l2 = l2 + IDT IDT self.form = , shape= , len(Parser.FORM_NAMES)), name= ) IDT IDT self.pos = , shape= , len(Parser.POS_NAMES)), name= ) IDT IDT self.deprel = , shape= , len(Parser.DEPREL_NAMES)), name= ) + +class Classifier(Network): + + IDT def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2, learning_rate_rl): + IDT IDT super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + IDT IDT self.output = , shape= ,), name= ) IDT IDT self.rewards = , shape= ,), name= ) IDT IDT self.indices = , shape= , 2), name= ) IDT IDT with tf.device('/cpu:0'), tf.name_scope('embedding'): + IDT IDT IDT self.form_emb = , self.form_dim), name= ) IDT IDT IDT self.pos_emb = , self.pos_dim), name= ) IDT IDT IDT self.deprel_emb = , self.deprel_dim), name= ) IDT IDT IDT inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + IDT IDT self.W0 = , self.hidden_dim), name= ) IDT IDT self.b0 = , name= ) IDT IDT self.W1 = , self.output_dim), name= ) IDT IDT self.b1 = , name= ) IDT IDT hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + IDT IDT self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + IDT IDT regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + IDT IDT if self.dropout > 0: + IDT IDT IDT hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + IDT IDT else: + IDT IDT IDT hidden_layer2 = hidden_layer + IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + IDT IDT self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + IDT IDT self.select_prob = tf.gather_nd(self.prob, self.indices) + IDT IDT self.result = tf.multiply(self.select_prob, self.rewards) + IDT IDT self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + IDT IDT self.grad = ) IDT IDT self.pg_optimization = ) IDT IDT self.optimization = ) IDT IDT self.saver = tf.train.Saver() + IDT IDT self.saved_path = './saved_model' + IDT IDT self.count = 0 + + IDT def train(self, session, inputs, outputs): + IDT IDT form, pos, deprel = inputs + IDT IDT _, cost = , self.loss], feed_dict= , self.pos: pos, self.deprel: deprel, self.output: outputs}) ) IDT IDT return cost + + IDT def policy_train(self, session, inputs, outputs, rewards): + IDT IDT form, pos, deprel = inputs + IDT IDT indices = [] + IDT IDT for aid in range(len(outputs)): + IDT IDT IDT indices.append([aid, outputs[aid]]) + IDT IDT _, cost, indices, prob, res = , self.pg_loss, self.indices, self.prob, self.result], feed_dict= , self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) ) IDT IDT self.count += 1 + IDT IDT return cost + + IDT def classify(self, session, inputs): + IDT IDT form, pos, deprel = inputs + IDT IDT prediction = , feed_dict= , self.pos: pos, self.deprel: deprel}) ) IDT IDT return prediction + + IDT def variable_output(self, session): + IDT IDT form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + IDT IDT return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + IDT def save(self, session): + IDT IDT self.saved_path = self.saver.save(session, './saved_model') + + IDT def restore(self, session): + IDT IDT self.saver.restore(session, self.saved_path) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import numpy as np +import tensorflow as tf +from tensorflow.contrib import rnn +from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file + + +class LSTMModel: + + IDT def __init__(self, embedding_dim, vocab_size, ref_size, learning_rate, dropout_prob): + IDT IDT self.embedding_dim = embedding_dim + IDT IDT self.vocab_size = vocab_size + 1 + IDT IDT self.ref_size = ref_size + 1 + IDT IDT self.learning_rate = learning_rate + IDT IDT self.dropout_prob = dropout_prob + IDT IDT self.stddev = 0.001 + IDT IDT self.loss_array = [] + + IDT def create_Graph(self, embedding_matrix, refs_matrix): + IDT IDT tf.reset_default_graph() + IDT IDT self.left_context = , shape= , None]) ) IDT IDT self.right_context = , shape= , None]) ) IDT IDT self.left_len = , shape= ) IDT IDT self.right_len = , shape= ) IDT IDT self.refs = , shape= ) IDT IDT self.labels = , shape= ) IDT IDT self.size_placeholder = , dtype= ) IDT IDT if embedding_matrix is not None: + IDT IDT IDT self.word_embeddings = , shape= , self.embedding_dim], initializer= ) IDT IDT else: + IDT IDT IDT self.word_embeddings = , shape= , self.embedding_dim], initializer= ) IDT IDT if refs_matrix is not None: + IDT IDT IDT self.refs_embeddings = , shape= , self.embedding_dim], initializer= ) IDT IDT else: + IDT IDT IDT self.refs_embeddings = , shape= , self.embedding_dim], initializer= ) IDT IDT self.relu_matrix = , shape= , 2], initializer= ) IDT IDT self.relu_bias = , shape= , initializer= ) IDT IDT self.left_embedded_context = , ids= ) IDT IDT self.right_embedded_context = , ids= ) IDT IDT self.refs_embedded = , ids= ) IDT IDT with tf.variable_scope('left'): + IDT IDT IDT self.lstm_cell_left = rnn.LSTMCell(self.embedding_dim) + IDT IDT IDT self.outputs_left, self.states_left = , inputs= , dtype= , sequence_length= ) IDT IDT IDT self.last_rnn_output_left = self.states_left.h + IDT IDT with tf.variable_scope('right'): + IDT IDT IDT self.lstm_cell_right = rnn.LSTMCell(self.embedding_dim) + IDT IDT IDT self.outputs_right, self.states_right = , inputs= , dtype= , sequence_length= ) IDT IDT IDT self.last_rnn_output_right = self.states_right.h + IDT IDT self.concat_vecs = tf.concat([self.last_rnn_output_left, self.last_rnn_output_right, self.refs_embedded], 1) + IDT IDT self.vecs_dp = , keep_prob= ) IDT IDT self.relu = tf.nn.leaky_relu(tf.matmul(self.vecs_dp, self.relu_matrix) + self.relu_bias) + IDT IDT self.loss_op = , labels= ) IDT IDT self.optimizer = ) IDT IDT self.train_op = self.optimizer.minimize(self.loss_op) + IDT IDT self.stacked_vecs_left = tf.tile(self.last_rnn_output_left, [self.size_placeholder, 1]) + IDT IDT self.stacked_vecs_right = tf.tile(self.last_rnn_output_right, [self.size_placeholder, 1]) + IDT IDT self.train_concat_vecs = tf.concat([self.stacked_vecs_left, self.stacked_vecs_right, self.refs_embedded], 1) + IDT IDT self.train_relu = tf.nn.leaky_relu(tf.matmul(self.train_concat_vecs, self.relu_matrix) + self.relu_bias) + IDT IDT self.prediction = tf.nn.softmax(self.train_relu) + IDT IDT self.session = tf.Session() + IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT self.saver = tf.train.Saver() + + IDT def train_forward(self, left_context_batch, right_context_batch, left_len_batch, right_len_batch, refs_batch, labels_batch): + IDT IDT _, loss = , self.loss_op], feed_dict= , self.right_context: right_context_batch, self.refs: refs_batch, self.left_len: left_len_batch, self.right_len: right_len_batch, self.labels: labels_batch}) ) IDT IDT self.loss_array.append(loss) + IDT IDT return loss + + IDT def eval_avg_loss(self): + IDT IDT avg_loss = sum(self.loss_array) / len(self.loss_array) + IDT IDT return avg_loss + + IDT def reset_avg_loss(self): + IDT IDT self.loss_array = [] + + IDT def save_model(self, path, step): + IDT IDT if step is not None: + IDT IDT IDT self.saver.save(self.session, path, global_step= ) IDT IDT else: + IDT IDT IDT self.saver.save(self.session, path) + + IDT def restore_model(self, path): + IDT IDT self.saver.restore(self.session, path) + + IDT def test_batch(self, left_context_batch, right_context_batch, left_len_batch, right_len_batch, refs_batch): + IDT IDT + IDT IDT p_vec = , feed_dict= , self.right_context: right_context_batch, self.refs: refs_batch, self.left_len: left_len_batch, self.right_len: right_len_batch, self.size_placeholder: len(refs_batch)}) ) IDT IDT p_vec_true = p_vec[0][:, (1)] + IDT IDT return p_vec_true + + IDT def delete_model(self): + IDT IDT self.session.close() + IDT IDT tf.reset_default_graph() +import tensorflow as tf +import numpy as np + + +class Neural_Net: + + IDT def __init__(self, session): + IDT IDT self.session = session + IDT IDT self.error = 0 + IDT IDT self.learning_rate = 0.01 + IDT IDT self.neural_net() + + IDT def neural_net(self): + IDT IDT self.tf_x = , shape= , 84, 84, 4], name= ) IDT IDT self.tf_x2 = , shape= , 1], name= ) IDT IDT self.tf_y = , shape= , 1], name= ) IDT IDT with tf.variable_scope('conv_1'): + IDT IDT IDT w1 = , shape= , 3, 4, 32]) ) IDT IDT IDT b1 = , initializer= ) IDT IDT IDT conv1 = , filter= , strides= , 1, 1, 1], padding= ) IDT IDT IDT conv1 = , b1, name= ) IDT IDT IDT conv1 = , name= ) IDT IDT + IDT IDT + IDT IDT conv1_pool = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= , name= ) IDT IDT + IDT IDT + IDT IDT with tf.variable_scope('conv_2'): + IDT IDT IDT w2 = , shape= , 3, 32, 64]) ) IDT IDT IDT b2 = , initializer= ) IDT IDT IDT conv2 = , filter= , strides= , 1, 1, 1], padding= ) IDT IDT IDT conv2 = , b2, name= ) IDT IDT IDT conv2 = , name= ) IDT IDT + IDT IDT + IDT IDT conv2_pool = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= , name= ) IDT IDT + IDT IDT + IDT IDT with tf.variable_scope('conv_3'): + IDT IDT IDT w3 = , shape= , 5, 64, 128]) ) IDT IDT IDT b3 = , initializer= ) IDT IDT IDT conv3 = , filter= , strides= , 1, 1, 1], padding= ) IDT IDT IDT conv3 = , b3, name= ) IDT IDT IDT conv3 = , name= ) IDT IDT + IDT IDT + IDT IDT conv3_pool = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= , name= ) IDT IDT + IDT IDT + IDT IDT conv3_pool_units = np.prod(conv3_pool.get_shape().as_list()[1:]) + IDT IDT + IDT IDT conv3_pool_reshape = , shape= , conv3_pool_units)) ) IDT IDT + IDT IDT + IDT IDT Conv3WithTf_x2 = tf.concat([conv3_pool_reshape, self.tf_x2], 1) + IDT IDT with tf.variable_scope('fcl_1'): + IDT IDT IDT w4 = , shape= , 8193]) ) IDT IDT IDT b4 = , initializer= ) IDT IDT IDT fcl1 = tf.matmul(Conv3WithTf_x2, w4) + IDT IDT IDT fcl1 = , b4, name= ) IDT IDT IDT fcl1 = , name= ) IDT IDT with tf.variable_scope('fcl_2'): + IDT IDT IDT w5 = , shape= , 4096]) ) IDT IDT IDT b5 = , initializer= ) IDT IDT IDT fcl2 = tf.matmul(fcl1, w5) + IDT IDT IDT fcl2 = , b5, name= ) IDT IDT with tf.variable_scope('fcl_3'): + IDT IDT IDT w6 = , shape= , 1]) ) IDT IDT IDT b6 = , initializer= ) IDT IDT IDT fcl3 = tf.matmul(fcl2, w6) + IDT IDT IDT self.Q_value = , b6, name= ) IDT IDT self.error = tf.reduce_mean(tf.square(self.tf_y - self.Q_value)) + IDT IDT self.optimizer = ) + IDT def train(self, state, action, Q_value): + IDT IDT return self.session.run(self.optimizer, feed_dict= , self.tf_x2: action, self.tf_y: Q_value}) ) + IDT def predict(self, state, action): + IDT IDT return self.session.run(self.Q_value, feed_dict= , self.tf_x2: action}) )import tensorflow as tf +import numpy as np +import datetime +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data/') + + +def discriminator(image, reuse= ) IDT if reuse: + IDT IDT tf.get_variable_scope().reuse_variables() + IDT weights1 = , [5, 5, 1, 32], initializer= ) IDT bais1 = , [32], initializer= ) IDT layer1 = , filter= , strides= , 1, 1, 1], padding= ) IDT layer1 = layer1 + bais1 + IDT layer1 = tf.nn.relu(layer1) + IDT layer1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT weights2 = , [5, 5, 32, 64], initializer= ) IDT bais2 = , [64], initializer= ) IDT layer2 = , filter= , strides= , 1, 1, 1], padding= ) IDT layer2 = layer2 + bais2 + IDT layer2 = tf.nn.relu(layer2) + IDT layer2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT weights3 = , [7 * 7 * 64, 1024], initializer= ) IDT bais3 = , [1024], initializer= ) IDT layer3 = tf.reshape(layer2, [-1, 7 * 7 * 64]) + IDT layer3 = tf.matmul(layer3, weights3) + IDT layer3 = layer3 + bais3 + IDT layer3 = tf.nn.relu(layer3) + IDT weights4 = , [1024, 1], initializer= ) IDT bais4 = , [1], initializer= ) IDT layer4 = tf.matmul(layer3, weights4) + bais4 + IDT return layer4 + + +def generator(batch_size, dimension): + IDT z = , dimension], mean= , stddev= , name= ) IDT weights1 = , [dimension, 3136], dtype= , initializer= ) IDT bais1 = , [3136], initializer= ) IDT layer1 = tf.matmul(z, weights1) + bais1 + IDT layer1 = tf.reshape(layer1, [-1, 56, 56, 1]) + IDT layer1 = , epsilon= , scope= ) IDT layer1 = tf.nn.relu(layer1) + IDT weights2 = , [3, 3, 1, dimension / 2], dtype= , initializer= ) IDT bais2 = , [dimension / 2], initializer= ) IDT layer2 = , weights2, strides= , 2, 2, 1], padding= ) IDT layer2 = layer2 + bais2 + IDT layer2 = , epsilon= , scope= ) IDT layer2 = tf.nn.relu(layer2) + IDT layer2 = tf.image.resize_images(layer2, [56, 56]) + IDT weights3 = , [3, 3, dimension / 2, dimension / 4], dtype= , initializer= ) IDT bais3 = , [dimension / 4], initializer= ) IDT layer3 = , weights3, strides= , 2, 2, 1], padding= ) IDT layer3 = layer3 + bais3 + IDT layer3 = , epsilon= , scope= ) IDT layer3 = tf.nn.relu(layer3) + IDT layer3 = tf.image.resize_images(layer3, [56, 56]) + IDT weights4 = , [1, 1, dimension / 4, 1], dtype= , initializer= ) IDT bais4 = , [1], initializer= ) IDT layer4 = , weights4, strides= , 2, 2, 1], padding= ) IDT layer4 = layer4 + bais4 + IDT layer4 = tf.sigmoid(layer4) + IDT return layer4 + + +session = tf.Session() +batch_size = 50 +dimensions = 100 +x_placeholder = , shape= , 28, 28, 1], name= )generator_z = generator(batch_size, dimensions) +discriminator_x = discriminator(x_placeholder) +discriminator_g = , reuse= )generator_loss = , labels= )discriminator_loss_real = , labels= , 1], 0.9))) )discriminator_loss_fake = , labels= )discriminator_loss = discriminator_loss_real + discriminator_loss_fake +tvars = tf.trainable_variables() +discriminator_vars = [var for var in tvars if 'discriminator_' in var.name] +generator_vars = [var for var in tvars if 'generator_' in var.name] +with tf.variable_scope(tf.get_variable_scope(), reuse= ) IDT discriminator_trainer_fake = , var_list= ) IDT discriminator_trainer_real = , var_list= ) IDT generator_trainer = , var_list= )tf.summary.scalar('Generator_loss', generator_loss) +tf.summary.scalar('Discriminator_loss_real', discriminator_loss_real) +tf.summary.scalar('Discriminator_loss_fake', discriminator_loss_fake) +discriminator_real_count_ph = tf.placeholder(tf.float32) +discriminator_fake_count_ph = tf.placeholder(tf.float32) +generator_count_ph = tf.placeholder(tf.float32) +tf.summary.scalar('d_real_count', discriminator_real_count_ph) +tf.summary.scalar('d_fake_count', discriminator_fake_count_ph) +tf.summary.scalar('g_count', generator_count_ph) +discriminator_on_generated = tf.reduce_mean(discriminator(generator(batch_size, dimensions))) +discriminator_on_real = tf.reduce_mean(discriminator(x_placeholder)) +tf.summary.scalar('d_on_generated_eval', discriminator_on_generated) +tf.summary.scalar('d_on_real_eval', discriminator_on_real) +images_for_tensorboard = generator(batch_size, dimensions) +tf.summary.image('Generated_images', images_for_tensorboard, 10) +merged = tf.summary.merge_all() +logdir = 'tensorboard/gan/' +writer = tf.summary.FileWriter(logdir, session.graph) + +saver = tf.train.Saver() +session.run(tf.global_variables_initializer()) +generatorLoss = 0 +discriminatorLossReal, discriminatorLossFake = 1, 1 +discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 +for i in range(100000): + IDT real_image_batch = mnist.train.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + IDT if discriminatorLossFake > 0.6: + IDT IDT _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_fake, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + IDT IDT discriminator_fake_count += 1 + IDT if generatorLoss > 0.5: + IDT IDT _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([generator_trainer, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + IDT IDT generator_count += 1 + IDT if discriminatorLossReal > 0.45: + IDT IDT _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_real, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + IDT IDT discriminator_real_count += 1 + IDT if i % 10 == 0: + IDT IDT real_image_batch = mnist.validation.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + IDT IDT summary = session.run(merged, {x_placeholder: real_image_batch, discriminator_real_count_ph: discriminator_real_count, discriminator_fake_count_ph: discriminator_fake_count, generator_count_ph: generator_count}) + IDT IDT writer.add_summary(summary, i) + IDT IDT discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 + IDT if i % 1000 == 0: + IDT IDT images = session.run(generator(3, dimensions)) + IDT IDT discriminator_result = session.run(discriminator(x_placeholder), {x_placeholder: images}) + IDT IDT ) + IDT IDT for j in range(3): + IDT IDT IDT + IDT IDT IDT im = images[(j), :, :, (0)] + IDT IDT IDT plt.imsave('imageAt' + str(i) + '.png', im.reshape([28, 28]), cmap= ) IDT if i % 5000 == 0: + IDT IDT save_path = , 'models/pretrained_gan.ckpt', global_step= ) IDT IDT +test_images = session.run(generator(10, 100)) +test_eval = session.run(discriminator(x_placeholder), {x_placeholder: test_images}) +real_images = mnist.validation.next_batch(10)[0].reshape([10, 28, 28, 1]) +real_eval = session.run(discriminator(x_placeholder), {x_placeholder: real_images}) +for i in range(10): + IDT + IDT plt.imsave('generated_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap= )for i in range(10): + IDT + IDT plt.imsave('real_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import argparse +import tensorflow as tf +import numpy as np +import time +import os +from tensorflow.examples.tutorials.mnist import input_data +from tensorflow.examples.tutorials.mnist import mnist +FLAGS = None +IMAGE_WIDTH = 28 +IMAGE_HEIGHT = 28 +LABEL_NUM = 10 + + +def load_data(): + IDT mnist_data = input_data.read_data_sets(FLAGS.data_dir) + IDT return mnist_data + + +def weight_variable(shape): + IDT return tf.Variable(tf.truncated_normal(shape, stddev= , name= ) + +def bias_variable(shape): + IDT return tf.Variable(tf.constant(0.1, shape= , name= ) + +class CNN: + + IDT def __init__(self, session, X_placeholder, Y_placeholder, y_pred, loss): + IDT IDT self.session = session + IDT IDT self.X_placeholder = X_placeholder + IDT IDT self.Y_placeholder = Y_placeholder + IDT IDT self.Y_pred = y_pred + IDT IDT self.loss = loss + IDT IDT self.train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + IDT IDT correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(Y_placeholder, 1)) + IDT IDT self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + + IDT def init(self): + IDT IDT self.session.run(tf.global_variables_initializer()) + + IDT def train(self, X, Y): + IDT IDT self.train_step.run(feed_dict= , self.Y_placeholder: Y}) ) + IDT def test(self, X, Y): + IDT IDT return self.accuracy.eval(feed_dict= , self.Y_placeholder: Y}) ) + +def generate_lenet_graph(inputX): + IDT with tf.device('/cpu:0'): + IDT IDT num_fm_conv1 = 32 + IDT IDT with tf.name_scope('conv1'): + IDT IDT IDT W_conv1 = weight_variable([5, 5, 1, num_fm_conv1]) + IDT IDT IDT b_conv1 = bias_variable([num_fm_conv1]) + IDT IDT IDT conv1 = , W_conv1, strides= , 1, 1, 1], padding= ) IDT IDT IDT h1 = tf.nn.relu(conv1 + b_conv1) + IDT IDT IDT pool1 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT with tf.name_scope('conv2'): + IDT IDT IDT num_fm_conv2 = 64 + IDT IDT IDT W_conv2 = weight_variable([5, 5, num_fm_conv1, num_fm_conv2]) + IDT IDT IDT b_conv2 = bias_variable([num_fm_conv2]) + IDT IDT IDT conv2 = , W_conv2, strides= , 1, 1, 1], padding= ) IDT IDT IDT h2 = tf.nn.relu(conv2 + b_conv2) + IDT IDT IDT pool2 = , ksize= , 2, 2, 1], strides= , 2, 2, 1], padding= ) IDT IDT with tf.name_scope('fc'): + IDT IDT IDT output_size = 7 * 7 * num_fm_conv2 + IDT IDT IDT fc_output_size = 1024 + IDT IDT IDT pool2_reshape = tf.reshape(pool2, [-1, output_size]) + IDT IDT IDT W_fc = weight_variable([output_size, fc_output_size]) + IDT IDT IDT b_fc = weight_variable([fc_output_size]) + IDT IDT IDT h_fc = tf.nn.relu(tf.matmul(pool2_reshape, W_fc) + b_fc) + IDT IDT with tf.name_scope('pred'): + IDT IDT IDT W_pred = weight_variable([fc_output_size, LABEL_NUM]) + IDT IDT IDT b_pred = bias_variable([LABEL_NUM]) + IDT IDT IDT Y_pred_activation = tf.matmul(h_fc, W_pred) + b_pred + IDT IDT return Y_pred_activation + + +def build_train_net(session, X_placeholder, Y_placeholder): + IDT image = tf.reshape(X_placeholder, [-1, IMAGE_WIDTH, IMAGE_HEIGHT, 1]) + IDT Y_pred_activation = generate_lenet_graph(image) + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(Y_pred_activation, Y_placeholder)) + IDT return CNN(session, X_placeholder, Y_placeholder, Y_pred_activation, loss) + + +def simple_lr(X_placeholder, Y_placeholder): + IDT N = IMAGE_WIDTH * IMAGE_HEIGHT + IDT W = weight_variable([N, LABEL_NUM]) + IDT b = bias_variable([LABEL_NUM]) + IDT h = tf.matmul(X_placeholder, W) + b + IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(h, Y_placeholder)) + IDT return CNN(session, X_placeholder, Y_placeholder, h, loss) + + +def main(): + IDT mnist_data = load_data() + IDT batch_size = 32 + IDT X_placeholder = , shape= , IMAGE_WIDTH * IMAGE_HEIGHT], name= ) IDT Y_placeholder = , shape= , LABEL_NUM], name= ) IDT session = tf.InteractiveSession() + IDT net = build_train_net(session, X_placeholder, Y_placeholder) + IDT net.init() + IDT saver = tf.train.Saver() + IDT meta_graph_def = , 'model.meta'), collection_list= ) IDT for i in range(10000): + IDT IDT start_time = time.time() + IDT IDT X, Y = mnist_data.train.next_batch(batch_size) + IDT IDT Y = np.eye(LABEL_NUM)[Y] + IDT IDT net.train(X, Y) + IDT IDT duration = time.time() - start_time + IDT IDT if i % 1000 == 999: + IDT IDT IDT train_accuracy = net.test(X, Y) + IDT IDT IDT ' % (train_accuracy, duration)) + IDT IDT IDT saver.save(session, os.path.join(FLAGS.save_dir, 'model.ckpt'), global_step= ) IDT test_Y = np.eye(LABEL_NUM)[mnist_data.test.labels] + IDT test_accuracy = net.test(mnist_data.test.images, test_Y) + IDT + + +if __name__ == '__main__': + IDT parser = argparse.ArgumentParser() + IDT parser.add_argument('--data_dir', type= , default= , help= ) IDT parser.add_argument('--save_dir', type= , default= , help= ) IDT FLAGS, _ = parser.parse_known_args() + IDT main() +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from ModelSkeletons.SupervisedModel import SupervisedModel, define_scope +from LSTM_CTCConfig import LSTM_CTCModelConfig + + +class LSTM_CTCModel(SupervisedModel): + + IDT def __init__(self, config: LSTM_CTCModelConfig): + IDT IDT self.config = config + IDT IDT self.graph = tf.Graph() + IDT IDT self.graph.as_default() + IDT IDT self.build_placeholders() + IDT IDT self.inference + IDT IDT self.loss + IDT IDT self.training + IDT IDT self.decoded_inference + IDT IDT self.evaluation + IDT IDT self.session = None + + IDT def build_placeholders(self): + IDT IDT self.input_placeholder = , shape= , self.config.input_max_time, self.config.input_frame_size], name= ) IDT IDT self.input_lengths_placeholder = , shape= , name= ) IDT IDT self.output_placeholder = , name= ) IDT IDT self.output_lengths_placeholder = , shape= , name= ) + IDT @define_scope + IDT def inference(self): + IDT IDT with tf.name_scope('LSTM'): + IDT IDT IDT cell = tf.nn.rnn_cell.LSTMCell(self.config.cell_size) + IDT IDT IDT cell = tf.nn.rnn_cell.MultiRNNCell([cell] * self.config.cell_count) + IDT IDT IDT cell_initial = cell.zero_state(tf.shape(self.input_placeholder)[0], tf.float32) + IDT IDT IDT rnn, _ = tf.nn.dynamic_rnn(cell, self.input_placeholder, self.output_lengths_placeholder, cell_initial) + IDT IDT IDT flatten_rnn = , shape= , self.config.input_max_time * self.config.cell_size]) ) IDT IDT layer = flatten_rnn + IDT IDT previous_layer_size = int(layer.get_shape()[1]) + IDT IDT for i in range(self.config.fully_connected_count): + IDT IDT IDT with tf.name_scope('FullyConnected_n' + str(i)): + IDT IDT IDT IDT weights = , self.config.fully_connected_size], stddev= , name= ) IDT IDT IDT IDT biases = , name= ) IDT IDT IDT IDT layer = tf.matmul(layer, weights) + biases + IDT IDT IDT IDT layer = tf.nn.relu(layer) + IDT IDT IDT IDT previous_layer_size = int(layer.get_shape()[1]) + IDT IDT with tf.name_scope('FullyConnected_Last'): + IDT IDT IDT weights = , self.config.output_max_time * self.config.output_frame_size], stddev= ) IDT IDT IDT biases = ) IDT IDT IDT layer = tf.matmul(layer, weights) + biases + IDT IDT logits = , shape= , self.config.output_max_time, self.config.output_frame_size]) ) IDT IDT return logits + + IDT @define_scope + IDT def loss(self): + IDT IDT ctc_loss = , self.output_placeholder, self.output_lengths_placeholder, time_major= ) IDT IDT return tf.reduce_mean(ctc_loss, name= ) + IDT @define_scope + IDT def training(self): + IDT IDT tf.summary.scalar('loss', self.loss) + IDT IDT tf.summary.scalar('error', self.evaluation) + IDT IDT global_step = , name= , trainable= ) IDT IDT optimizer = , epsilon= ) IDT IDT return optimizer.minimize(self.loss, global_step= ) + IDT @define_scope + IDT def decoded_inference(self): + IDT IDT inference_timemajor = tf.transpose(self.inference, [1, 0, 2]) + IDT IDT decoded, prob = tf.nn.ctc_greedy_decoder(inference_timemajor, self.output_lengths_placeholder) + IDT IDT return decoded + + IDT @define_scope + IDT def evaluation(self): + IDT IDT average_error = [] + IDT IDT for result in self.decoded_inference: + IDT IDT IDT predictions = tf.cast(result, tf.int32) + IDT IDT IDT error = , self.output_placeholder, normalize= ) IDT IDT IDT average_error.append(tf.reduce_mean(error, name= ) IDT IDT return tf.reduce_mean(average_error) + + IDT @staticmethod + IDT def tokens_for_sparse(sequences): + IDT IDT eos_value = 9632 + IDT IDT tmp = [] + IDT IDT for seq_idx in range(len(sequences)): + IDT IDT IDT seq = sequences[seq_idx] + IDT IDT IDT for i in range(len(seq)): + IDT IDT IDT IDT end_idx = i + IDT IDT IDT IDT if seq[i] == eos_value: + IDT IDT IDT IDT IDT break + IDT IDT IDT tmp.append(seq[:end_idx]) + IDT IDT sequences = tmp + IDT IDT indices = [] + IDT IDT values = [] + IDT IDT for n, seq in enumerate(sequences): + IDT IDT IDT indices.extend(zip([n] * len(seq), range(len(seq)))) + IDT IDT IDT values.extend(seq) + IDT IDT indices = , dtype= ) IDT IDT values = , dtype= ) IDT IDT shape = , np.asarray(indices).max(0)[1] + 1], dtype= ) IDT IDT return indices, values, shape + + IDT def train(self, database, iteration_count: int, batch_size: int): + IDT IDT session_config = tf.ConfigProto() + IDT IDT session_config.gpu_options.allow_growth = True + IDT IDT self.session = ) IDT IDT self.init_model() + IDT IDT train_data = database.train_dataset + IDT IDT for i in tqdm(range(iteration_count)): + IDT IDT IDT feed_dict = self.train_step(train_data, batch_size) + IDT IDT IDT if feed_dict is None: + IDT IDT IDT IDT continue + IDT IDT IDT for key in feed_dict: + IDT IDT IDT IDT if feed_dict[key] is None: + IDT IDT IDT IDT IDT continue + IDT IDT IDT if len(feed_dict) == 0: + IDT IDT IDT IDT continue + IDT IDT IDT if (i + 1) % 10 == 0: + IDT IDT IDT IDT summary_str = , feed_dict= ) IDT IDT IDT IDT self.summary_writer.add_summary(summary_str, i) + IDT IDT IDT IDT self.summary_writer.flush() + IDT IDT IDT if (i + 1) % 100 == 0: + IDT IDT IDT IDT + IDT IDT IDT IDT ) + IDT IDT IDT IDT + IDT IDT IDT if (i + 1) % 1000 == 0: + IDT IDT IDT IDT self.saver.save(self.session, self.config.checkpoints_path + '\\network', global_step= ) IDT IDT IDT IDT self.test(database) + + IDT def init_model(self): + IDT IDT self.summary = tf.summary.merge_all() + IDT IDT self.summary_writer = tf.summary.FileWriter(self.config.checkpoints_path + '/logs', self.session.graph) + IDT IDT self.saver = tf.train.Saver() + IDT IDT checkpoint = tf.train.get_checkpoint_state(self.config.checkpoints_path) + IDT IDT if checkpoint and checkpoint.model_checkpoint_path: + IDT IDT IDT + IDT IDT IDT self.saver.restore(self.session, checkpoint.model_checkpoint_path) + IDT IDT else: + IDT IDT IDT + IDT IDT IDT self.session.run(tf.global_variables_initializer()) + IDT IDT + + IDT def get_feed_dict(self, dataset, batch_size, one_hot= ) IDT IDT batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = , one_hot= ) IDT IDT tmp = [] + IDT IDT for j in range(len(batch_outputs)): + IDT IDT IDT tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + IDT IDT batch_outputs_feed = np.array(tmp) + IDT IDT try: + IDT IDT IDT batch_outputs_feed = LSTM_CTCModel.tokens_for_sparse(batch_outputs_feed) + IDT IDT except: + IDT IDT IDT return None + IDT IDT tmp = [] + IDT IDT for j in range(len(batch_inputs)): + IDT IDT IDT tmp.append(batch_inputs[j][:self.config.input_max_time]) + IDT IDT batch_inputs = np.array(tmp) + IDT IDT feed_dict = {self.input_placeholder: batch_inputs, self.input_lengths_placeholder: batch_input_lengths, self.output_placeholder: batch_outputs_feed, self.output_lengths_placeholder: batch_output_lengths} + IDT IDT return feed_dict + + IDT def train_step(self, train_data, batch_size: int): + IDT IDT feed_dict = , batch_size, one_hot= ) IDT IDT self.session.run(self.training, feed_dict= ) IDT IDT return feed_dict + + IDT def test(self, database, batch_size= ) IDT IDT if self.session is None: + IDT IDT IDT session_config = tf.ConfigProto() + IDT IDT IDT session_config.gpu_options.allow_growth = True + IDT IDT IDT self.session = ) IDT IDT IDT self.init_model() + IDT IDT test_data = database.test_dataset + IDT IDT feed_dict = , batch_size, one_hot= ) IDT IDT + IDT IDT ) * 100, '%') + IDT IDT +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import pandas as pd +import numpy as np +import pickle +import matplotlib.pyplot as plt +from scipy import stats +import tensorflow as tf +import seaborn as sns +from pylab import rcParams +from sklearn import metrics +from sklearn.model_selection import train_test_split +import tensorflow as tf +import pandas as pd +import numpy as np +from tensorflow.contrib import rnn +from sklearn.model_selection import train_test_split +from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score +import os +vocab_size = 10 + + +def reset_graph(seed= ) IDT tf.reset_default_graph() + IDT tf.set_random_seed(seed) + IDT np.random.seed(seed) + + +reset_graph() + + +def build_modeld(hidden_units= , num_classes= , batch_size= , num_layers= , learning_rate= ) IDT x = , [batch_size, None], name= ) IDT y = , [batch_size, None], name= ) IDT seq_lens = tf.placeholder(tf.int32, [batch_size]) + IDT embeddings = tf.get_variable('embedding_matrix', [num_classes, hidden_units]) + IDT rnn_inputs = tf.nn.embedding_lookup(embeddings, x) + IDT lstm_cell = , state_is_tuple= ) IDT cells = , state_is_tuple= ) IDT init_state = cells.zero_state(batch_size, tf.float32) + IDT rnn_outputs, final_state = , rnn_inputs, initial_state= , sequence_length= ) IDT with tf.variable_scope('softmax'): + IDT IDT W = tf.get_variable('W', [hidden_units, num_classes]) + IDT IDT b = , [num_classes], initializer= ) IDT outputs = tf.reshape(rnn_outputs, [-1, hidden_units]) + IDT logits = tf.matmul(outputs, W) + b + IDT error = , labels= , [-1]))) ) IDT optimizer = tf.train.AdamOptimizer(learning_rate).minimize(error) + IDT correct_predictions = tf.equal(tf.argmax(logits, 1), y) + IDT accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32)) + IDT return dict(x= , y= , init_state= , final_state= , error= , accuracy= , optimizer= , sequence_lengths= ) + +def run_epoch(session, model, data): + IDT err = 0.0 + IDT acc = 0.0 + IDT iters = 0 + IDT training_state = None + IDT for _sequence_length, _input, _target in data: + IDT IDT feed_dict = , dtype= , model['y']: np.array([_target], dtype= , model['sequence_lengths']: np.array(_sequence_length, dtype= ) IDT IDT if training_state is not None: + IDT IDT IDT feed_dict[model['init_state']] = training_state + IDT IDT err, training_state, acc, _ = session.run([model['error'], model['final_state'], model['accuracy'], model['optimizer']], feed_dict) + IDT IDT iters += _sequence_length[0] + IDT return err / iters, acc / iters + + +def train_model(train_data, epochs= ) IDT with tf.Graph().as_default(), tf.Session() as session: + IDT IDT initializer = tf.random_uniform_initializer(-0.1, 0.1) + IDT IDT with tf.variable_scope('model', reuse= , initializer= ) IDT IDT IDT model = build_modeld() + IDT IDT session.run(tf.global_variables_initializer()) + IDT IDT for i in range(epochs): + IDT IDT IDT error, accuracy = run_epoch(session, model, train_data) + IDT IDT IDT ) + + +data = pd.read_csv('TesterHRY.csv') +features = data.iloc[:, 0:7] +labels = data.iloc[:, (-1)] +X_train, X_test, y_train, y_test = , labels, test_size= , shuffle= , random_state= )i = 0 +batch_size = 1 +for i in range(int(len(X_train) / batch_size)): + IDT start = i + IDT end = i + batch_size + IDT batch_x = np.array(X_train[start:end]) + IDT batch_y = np.array(y_train[start:end]) + IDT + IDT + IDT + IDT + +_input = data.iloc[1:2, 0:7] +_target = data.iloc[1:2, (-1)] +x = _input.values + + + +) +y = , dtype= ) + + +) +_sequence_length, _input, _target = len(x[0]), x, y +x[0] = list(map(lambda x: x if x > 0 else x * -1, x[0])) + + +train_data = [([len(x[0])], x[0], y)] +train_data = [([5.8], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6])] +train_model(train_data) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf +import numpy as np +import gym + + +class Actor(object): + + IDT def __init__(self, session, state_space, action_bounds, **options): + IDT IDT self.session = session + IDT IDT self.state_space = state_space + IDT IDT self.action_bounds = action_bounds + IDT IDT try: + IDT IDT IDT self.learning_rate = options['learning_rate'] + IDT IDT except KeyError: + IDT IDT IDT self.learning_rate = 0.0001 + IDT IDT self.global_steps = , trainable= ) IDT IDT self._init_input() + IDT IDT self._init_nn() + IDT IDT self._init_op() + + IDT def _init_input(self): + IDT IDT self.state = tf.placeholder(tf.float32, [1, self.state_space]) + IDT IDT self.action = tf.placeholder(tf.float32, None) + IDT IDT self.td_error = tf.placeholder(tf.float32, None) + + IDT def _init_nn(self): + IDT IDT w_init, b_init = tf.random_normal_initializer(0.0, 0.3), tf.constant_initializer(0.1) + IDT IDT phi_state = , 20, tf.nn.relu, kernel_initializer= , bias_initializer= ) IDT IDT mu = , 1, tf.nn.tanh, kernel_initializer= , bias_initializer= ) IDT IDT sigma = , 1, tf.nn.softplus, kernel_initializer= , bias_initializer= ) IDT IDT self.mu, self.sigma = tf.squeeze(mu * 2), tf.squeeze(sigma + 0.1) + IDT IDT self.gaussian_func = tf.distributions.Normal(self.mu, self.sigma) + IDT IDT gaussian_sample = ) IDT IDT self.action = tf.clip_by_value(gaussian_sample, self.action_bounds[0], self.action_bounds[1]) + + IDT def _init_op(self): + IDT IDT self.loss = self.gaussian_func.log_prob(self.action) * self.td_error + 0.01 * self.gaussian_func.entropy() + IDT IDT self.train_op = tf.train.AdamOptimizer(self.learning_rate).minimize(-self.loss, self.global_steps) + + IDT def train(self, state, action, td_error): + IDT IDT _, loss = , self.loss], feed_dict= , :], self.action: action, self.td_error: td_error}) ) IDT IDT return loss + + IDT def get_next_action(self, state): + IDT IDT return self.session.run(self.action, feed_dict= , :]}) ) + +class Critic(object): + + IDT def __init__(self, session, state_space, **options): + IDT IDT self.session = session + IDT IDT self.state_space = state_space + IDT IDT try: + IDT IDT IDT self.learning_rate = options['learning_rate'] + IDT IDT except KeyError: + IDT IDT IDT self.learning_rate = 0.0001 + IDT IDT try: + IDT IDT IDT self.gamma = options['gamma'] + IDT IDT except KeyError: + IDT IDT IDT self.gamma = 0.9 + IDT IDT self._init_input() + IDT IDT self._init_nn() + IDT IDT self._init_op() + + IDT def _init_input(self): + IDT IDT self.state = tf.placeholder(tf.float32, [1, self.state_space]) + IDT IDT self.reward = tf.placeholder(tf.float32) + IDT IDT self.value_next = tf.placeholder(tf.float32, [1, 1]) + + IDT def _init_nn(self): + IDT IDT w_init, b_init = tf.random_normal_initializer(0.0, 0.3), tf.constant_initializer(0.1) + IDT IDT phi_state = , 30, tf.nn.relu, kernel_initializer= , bias_initializer= ) IDT IDT self.value = , 1, kernel_initializer= , bias_initializer= ) + IDT def _init_op(self): + IDT IDT self.td_error = tf.reduce_mean(self.reward + self.gamma * self.value_next - self.value) + IDT IDT self.loss = tf.square(self.td_error) + IDT IDT self.train_op = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss) + + IDT def train(self, state, reward, state_next): + IDT IDT state, state_next = state[(np.newaxis), :], state_next[(np.newaxis), :] + IDT IDT value_next = , feed_dict= ) IDT IDT td_error, _ = , self.train_op], feed_dict= , self.value_next: value_next, self.reward: reward}) ) IDT IDT return td_error + + +def main(_): + IDT env = gym.make('Pendulum-v0') + IDT env.seed(1) + IDT env = env.unwrapped + IDT session = tf.Session() + IDT actor = Actor(session, env.observation_space.shape[0], [-env.action_space.high, env.action_space.high]) + IDT critic = Critic(session, env.observation_space.shape[0]) + IDT session.run(tf.global_variables_initializer()) + IDT running_reward = None + IDT for episode in range(1000): + IDT IDT state, steps, reward_history = env.reset(), 0, [] + IDT IDT while True: + IDT IDT IDT if episode > 500: + IDT IDT IDT IDT env.render() + IDT IDT IDT action = actor.get_next_action(state) + IDT IDT IDT state_next, reward, done, info = env.step(action) + IDT IDT IDT reward /= 10 + IDT IDT IDT td_error = critic.train(state, reward, state_next) + IDT IDT IDT actor.train(state, action, td_error) + IDT IDT IDT state = state_next + IDT IDT IDT steps += 1 + IDT IDT IDT reward_history.append(reward) + IDT IDT IDT if steps > 200: + IDT IDT IDT IDT if running_reward is None: + IDT IDT IDT IDT IDT running_reward = sum(reward_history) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT running_reward = running_reward * 0.9 + sum(reward_history) * 0.1 + IDT IDT IDT IDT ) + IDT IDT IDT IDT break + + +if __name__ == '__main__': + IDT tf.app.run() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import tensorflow as tf + + + +def add_consts(): + IDT c1 = tf.constant(5.1) + IDT c2 = tf.constant(1.0) + IDT c3 = tf.constant(5.9) + IDT a1 = tf.add(c1, c2) + IDT af = tf.add(a1, c3) + IDT return af + + +def add_consts_with_placeholder(): + IDT c1 = tf.constant(5.1) + IDT c2 = tf.constant(1.0) + IDT c3 = tf.placeholder(tf.float32) + IDT a1 = tf.add(c1, c2) + IDT af = tf.add(a1, c3) + IDT return af, c3 + + +def my_relu(in_value): + IDT out_value = tf.maximum(0.0, in_value) + IDT return out_value + + +def my_perceptron(x): + IDT pf = , shape= ) IDT my_int_variable = , [x], dtype= , initializer= ) IDT mul = pf * my_int_variable + IDT add = tf.reduce_sum(mul) + IDT out_value = tf.maximum(0.0, add) + IDT i = pf + IDT out = out_value + IDT return i, out + + + +fc_count = 0 + + +def input_placeholder(): + IDT return tf.placeholder(dtype= , shape= , 784], name= ) + +def target_placeholder(): + IDT return tf.placeholder(dtype= , shape= , 10], name= ) + +def onelayer(X, Y, layersize= ) IDT w = tf.Variable(tf.zeros([784, layersize])) + IDT b = tf.Variable(tf.zeros([layersize])) + IDT logits1 = tf.matmul(X, w) + IDT logits = logits1 + b + IDT preds = tf.nn.softmax(logits) + IDT batch_xentropy = , logits= ) IDT batch_loss = tf.reduce_mean(batch_xentropy) + IDT return w, b, logits, preds, batch_xentropy, batch_loss + + +def twolayer(X, Y, hiddensize= , outputsize= ) IDT w1 = tf.Variable(tf.truncated_normal([784, hiddensize])) + IDT w2 = tf.Variable(tf.zeros([hiddensize, outputsize])) + IDT b1 = tf.Variable(tf.truncated_normal([hiddensize])) + IDT b2 = tf.Variable(tf.zeros([outputsize])) + IDT logitsmul1 = tf.matmul(X, w1) + IDT logits1 = logitsmul1 + b1 + IDT preds1 = tf.nn.relu(logits1) + IDT logits12 = tf.matmul(preds1, w2) + IDT logits = logits12 + b2 + IDT preds = tf.nn.softmax(logits) + IDT batch_xentropy = , logits= ) IDT batch_loss = tf.reduce_mean(batch_xentropy) + IDT return w1, b1, w2, b2, logits, preds, batch_xentropy, batch_loss + + +def convnet(X, Y, convlayer_sizes= , 10], filter_shape= , 3], outputsize= , padding= ) IDT my_conv = [filter_shape[0], filter_shape[1], 1, convlayer_sizes[0]] + IDT weight_conv1 = , stddev= ) IDT biased_conv1 = , shape= ) IDT conv1 = , weight_conv1, strides= , 1, 1, 1], padding= ) IDT my_conv2 = [filter_shape[0], filter_shape[1], convlayer_sizes[0], convlayer_sizes[1]] + IDT weight_conv2 = , stddev= ) IDT biased_conv2 = , shape= ) IDT conv2 = , weight_conv2, strides= , 1, 1, 1], padding= ) IDT X_layer = tf.reshape(conv2, [-1, 28 * 28 * convlayer_sizes[1]]) + IDT w = tf.Variable(tf.zeros([28 * 28 * convlayer_sizes[1], outputsize])) + IDT b = tf.Variable(tf.zeros([outputsize])) + IDT logits1 = tf.matmul(X_layer, w) + IDT logits = logits1 + b + IDT preds = tf.nn.softmax(logits) + IDT batch_xentropy = , logits= ) IDT batch_loss = tf.reduce_mean(batch_xentropy) + IDT return conv1, conv2, w, b, logits, preds, batch_xentropy, batch_loss + + +def train_step(sess, batch, X, Y, train_op, loss_op, summaries_op): + IDT train_result, loss, summary = , loss_op, summaries_op], feed_dict= , Y: batch[1]}) ) IDT return train_result, loss, summary +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + +batch_size = 128 +num_steps = 3001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT isL2 = tf.placeholder(tf.bool) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + + IDT def prediction(mInput): + IDT IDT logits = tf.matmul(mInput, weights) + biases + IDT IDT return logits + IDT if isL2: + IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset), tf_train_labels)) + IDT else: + IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset), tf_train_labels) + 0.05 * tf.nn.l2_loss(weights)) + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(prediction(tf_train_dataset)) + IDT valid_prediction = tf.nn.softmax(prediction(tf_valid_dataset)) + IDT test_prediction = tf.nn.softmax(prediction(tf_test_dataset)) +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, isL2: False} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +batch_size = 128 +num_steps = 3001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights_h = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + IDT biases_h = tf.Variable(tf.zeros([1024])) + IDT weights = tf.Variable(tf.truncated_normal([1024, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT keep_prob = tf.placeholder(tf.float32) + IDT isDropout = tf.placeholder(tf.bool) + IDT isL2 = tf.placeholder(tf.bool) + + IDT def prediction(mInput, dropout= ) IDT IDT layer1 = tf.nn.relu(tf.matmul(mInput, weights_h) + biases_h) + IDT IDT if dropout: + IDT IDT IDT layer1 = tf.nn.dropout(layer1, keep_prob) + IDT IDT logits = tf.matmul(layer1, weights) + biases + IDT IDT return logits + IDT if isL2: + IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset, isDropout), tf_train_labels)) + IDT else: + IDT IDT loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset, isDropout), tf_train_labels) + 0.01 * tf.nn.l2_loss(weights) + 0.01 * tf.nn.l2_loss(weights_h)) + IDT global_step = tf.Variable(0) + IDT learning_rate = tf.train.exponential_decay(0.5, global_step, 100000, 0.96) + IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(prediction(tf_train_dataset)) + IDT valid_prediction = tf.nn.softmax(prediction(tf_valid_dataset)) + IDT test_prediction = tf.nn.softmax(prediction(tf_test_dataset)) +with tf.Session(graph= ) IDT tf.initialize_all_variables().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5, isL2: False, isDropout: False} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import time +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + IDT save = pickle.load(f) + IDT train_dataset = save['train_dataset'] + IDT train_labels = save['train_labels'] + IDT valid_dataset = save['valid_dataset'] + IDT valid_labels = save['valid_labels'] + IDT test_dataset = save['test_dataset'] + IDT test_labels = save['test_labels'] + IDT del save + IDT + IDT + IDT +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + IDT dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + IDT labels = , (None)]).astype(np.float32) ) IDT return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) + + + + + +def accuracy(predictions, labels): + IDT return 100.0 * np.sum(np.argmax(predictions, 1) = , 1)) ) + +train_subset = 10000 +batch_size = 128 +beta = 0.001 +graph = tf.Graph() +with graph.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(tf_train_dataset, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + IDT + IDT + IDT +batch_size = 128 +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 128 +num_nodes = 1024 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(relu1, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 12 +num_nodes = 1024 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(relu1, weights) + biases + IDT loss = , labels= ) IDT l2_regularizer = tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_relu1) + IDT loss += 0.0005 * l2_regularizer + IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 12 +num_nodes = 1024 +beta = 0.001 +prob_limit = 0.75 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + IDT biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + IDT relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + IDT + IDT weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT keep_prob = tf.placeholder('float') + IDT hidden_layer_drop = tf.nn.dropout(relu1, keep_prob) + IDT logits = tf.matmul(hidden_layer_drop, weights) + biases + IDT loss = , labels= ) IDT optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + IDT train_prediction = tf.nn.softmax(logits) + IDT valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + IDT test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: prob_limit} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +batch_size = 128 +h1_num_nodes = 1024 +h2_num_nodes = 768 +h3_num_nodes = 512 +h1_stddev = np.sqrt(2.0 / 784) +h2_stddev = np.sqrt(2.0 / h1_num_nodes) +h3_stddev = np.sqrt(2.0 / h2_num_nodes) +logits_stddev = np.sqrt(2.0 / h3_num_nodes) +beta = 0.001 + +graph_relu = tf.Graph() +with graph_relu.as_default(): + IDT tf_train_dataset = , shape= , image_size * image_size)) ) IDT tf_train_labels = , shape= , num_labels)) ) IDT tf_valid_dataset = tf.constant(valid_dataset) + IDT tf_test_dataset = tf.constant(test_dataset) + IDT + IDT h1_weights = , h1_num_nodes], stddev= ) IDT h1_biases = tf.Variable(tf.zeros([h1_num_nodes])) + IDT h1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, h1_weights) + h1_biases) + IDT + IDT h2_weights = , h2_num_nodes], stddev= ) IDT h2_biases = tf.Variable(tf.zeros([h2_num_nodes])) + IDT h2_layer = tf.nn.relu(tf.matmul(h1_layer, h2_weights) + h2_biases) + IDT + IDT h3_weights = , h3_num_nodes], stddev= ) IDT h3_biases = tf.Variable(tf.zeros([h3_num_nodes])) + IDT h3_layer = tf.nn.relu(tf.matmul(h2_layer, h3_weights) + h3_biases) + IDT + IDT weights = , num_labels], stddev= ) IDT biases = tf.Variable(tf.zeros([num_labels])) + IDT logits = tf.matmul(h3_layer, weights) + biases + IDT loss = , labels= ) IDT global_step = tf.Variable(0) + IDT learning_rate = , global_step, 100000, 0.95, staircase= ) IDT optimizer = , global_step= ) IDT train_prediction = tf.nn.softmax(logits) + IDT h1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, h1_weights) + h1_biases) + IDT h2_valid = tf.nn.relu(tf.matmul(h1_valid, h2_weights) + h2_biases) + IDT h3_valid = tf.nn.relu(tf.matmul(h2_valid, h3_weights) + h3_biases) + IDT valid_logits = tf.matmul(h3_valid, weights) + biases + IDT valid_prediction = tf.nn.softmax(valid_logits) + IDT ->', valid_prediction) + IDT h1_test = tf.nn.relu(tf.matmul(tf_test_dataset, h1_weights) + h1_biases) + IDT h2_test = tf.nn.relu(tf.matmul(h1_test, h2_weights) + h2_biases) + IDT h3_test = tf.nn.relu(tf.matmul(h2_test, h3_weights) + h3_biases) + IDT test_logits = tf.matmul(h3_test, weights) + biases + IDT test_prediction = tf.nn.softmax(test_logits) + +num_steps = 3001 +with tf.Session(graph= ) IDT t1 = time.time() + IDT tf.global_variables_initializer().run() + IDT ->', valid_prediction.eval()) + IDT + IDT for step in range(num_steps): + IDT IDT offset = step * batch_size % (train_labels.shape[0] - batch_size) + IDT IDT batch_data = train_dataset[offset:offset + batch_size, :] + IDT IDT batch_labels = train_labels[offset:offset + batch_size, :] + IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT if step % 500 == 0: + IDT IDT IDT ) + IDT IDT IDT ) + IDT IDT IDT , valid_labels)) + IDT , test_labels)) + IDT t2 = time.time() + IDT ) +import numpy as np +import tensorflow as tf +import logging +import sys +import os +import math +sys.path.insert(0, os.path.abspath('..')) +from Utility.DataToBinaryVec import DataLoader +from Utility.Configs import fullyNNConfig +import logging +logFormatter = logging.Formatter('%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s') +fileHandler = logging.FileHandler('{0}/{1}.log'.format('./', 'Fullyconnected_NN')) +fileHandler.setFormatter(logFormatter) +consoleHandler = logging.StreamHandler() +consoleHandler.setFormatter(logFormatter) +logger = logging.getLogger() +logger.setLevel(logging.INFO) +logger.addHandler(fileHandler) +logger.addHandler(consoleHandler) + + +class NN(object): + + IDT def __init__(self): + IDT IDT self.d_loader = DataLoader() + IDT IDT self.input_vector_size = self.d_loader.d_handler.get_vocab_size() + IDT IDT self.output_vector_size = self.d_loader.d_handler.get_vocab_size() + IDT IDT self.train_dataset, self.train_labels, self.valid_dataset, self.valid_labels, self.test_dataset, self.test_labels = self.d_loader.get_ttv() + + IDT def simple_NN(self): + IDT IDT logger.info('creating the computational graph...') + IDT IDT graph = tf.Graph() + IDT IDT with graph.as_default(): + IDT IDT IDT with tf.device('/cpu:0'): + IDT IDT IDT IDT tf_train_dataset = , shape= , self.input_vector_size)) ) IDT IDT IDT IDT tf_train_labels = , shape= , self.output_vector_size)) ) IDT IDT IDT IDT tf_valid_dataset = tf.constant(self.valid_dataset) + IDT IDT IDT IDT tf_test_dataset = tf.constant(self.test_dataset) + IDT IDT IDT IDT if fullyNNConfig.regularization: + IDT IDT IDT IDT IDT beta_regul = tf.placeholder(tf.float32) + IDT IDT IDT IDT if fullyNNConfig.learning_rate_decay: + IDT IDT IDT IDT IDT global_step = tf.Variable(0) + + IDT IDT IDT IDT def init_weights(shape): + IDT IDT IDT IDT IDT return tf.Variable(tf.truncated_normal(shape)) + + IDT IDT IDT IDT def init_biases(shape): + IDT IDT IDT IDT IDT return tf.Variable(tf.zeros(shape)) + IDT IDT IDT IDT w_h = init_weights([self.input_vector_size, fullyNNConfig.num_hidden_nodes]) + IDT IDT IDT IDT b_h = init_biases([fullyNNConfig.num_hidden_nodes]) + IDT IDT IDT IDT w_o = init_weights([fullyNNConfig.num_hidden_nodes, self.output_vector_size]) + IDT IDT IDT IDT b_o = init_biases([self.output_vector_size]) + + IDT IDT IDT IDT def model(dataset, w_h, b_h, w_o, b_o, train): + IDT IDT IDT IDT IDT if fullyNNConfig.dropout and train: + IDT IDT IDT IDT IDT IDT drop_i = tf.nn.dropout(dataset, fullyNNConfig.dropout_keep_prob_input) + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + IDT IDT IDT IDT IDT IDT drop_h = tf.nn.dropout(h_lay_train, fullyNNConfig.dropout_keep_prob_hidden) + IDT IDT IDT IDT IDT IDT return tf.matmul(drop_h, w_o) + b_o + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + IDT IDT IDT IDT IDT IDT return tf.matmul(h_lay_train, w_o) + b_o + IDT IDT IDT IDT logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + IDT IDT IDT IDT loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits, tf_train_labels)) + IDT IDT IDT IDT if fullyNNConfig.regularization: + IDT IDT IDT IDT IDT loss += beta_regul * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + IDT IDT IDT IDT if fullyNNConfig.learning_rate_decay: + IDT IDT IDT IDT IDT learning_rate = , global_step, fullyNNConfig.decay_steps, fullyNNConfig.decay_rate, staircase= ) IDT IDT IDT IDT IDT optimizer = , global_step= ) IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT optimizer = tf.train.GradientDescentOptimizer(fullyNNConfig.learning_rate).minimize(loss) + IDT IDT IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT IDT IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset, w_h, b_h, w_o, b_o, False)) + IDT IDT IDT IDT test_prediction = tf.nn.softmax(model(tf_test_dataset, w_h, b_h, w_o, b_o, False)) + IDT IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT IDT pre = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT lbl = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT accuracy = tf.reduce_mean(tf.cast(tf.nn.sigmoid_cross_entropy_with_logits(pre, lbl), 'float')) + IDT IDT logger.info('running the session...') + IDT IDT with tf.Session(graph= , config= ) IDT IDT IDT tf.initialize_all_variables().run() + IDT IDT IDT logger.info('Initialized') + IDT IDT IDT for step in range(fullyNNConfig.num_steps): + IDT IDT IDT IDT offset = step * fullyNNConfig.batch_size % (self.train_labels.shape[0] - fullyNNConfig.batch_size) + IDT IDT IDT IDT batch_data = self.train_dataset[offset:offset + fullyNNConfig.batch_size, :] + IDT IDT IDT IDT batch_labels = self.train_labels[offset:offset + fullyNNConfig.batch_size, :] + IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT IDT if fullyNNConfig.regularization: + IDT IDT IDT IDT IDT feed_dict[beta_regul] = fullyNNConfig.beta_regul + IDT IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT IDT if step % fullyNNConfig.summary_steps == 0: + IDT IDT IDT IDT IDT logger.info('Minibatch loss at step %d: %f' % (step, l)) + IDT IDT IDT IDT IDT logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: batch_labels})) ) IDT IDT IDT IDT IDT logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.valid_labels})) ) IDT IDT IDT IDT IDT logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.test_labels})) ) IDT IDT IDT self.print_words(test_prediction.eval(), self.test_labels) + + IDT def simple_NN_w_CandidateSampling(self): + IDT IDT + IDT IDT graph = tf.Graph() + IDT IDT with graph.as_default(): + IDT IDT IDT with tf.device('/cpu:0'): + IDT IDT IDT IDT tf_train_dataset = , shape= , self.input_vector_size)) ) IDT IDT IDT IDT tf_train_labels = , shape= , self.output_vector_size)) ) IDT IDT IDT IDT tf_valid_dataset = tf.constant(self.valid_dataset) + IDT IDT IDT IDT tf_test_dataset = tf.constant(self.test_dataset) + IDT IDT IDT IDT if fullyNNConfig.regularization: + IDT IDT IDT IDT IDT beta_regul = tf.placeholder(tf.float32) + IDT IDT IDT IDT if fullyNNConfig.learning_rate_decay: + IDT IDT IDT IDT IDT global_step = tf.Variable(0) + + IDT IDT IDT IDT def init_biases(shape): + IDT IDT IDT IDT IDT return tf.Variable(tf.zeros(shape)) + IDT IDT IDT IDT w_h = , fullyNNConfig.num_hidden_nodese], stddev= ) IDT IDT IDT IDT b_h = init_biases([fullyNNConfig.num_hidden_nodes]) + IDT IDT IDT IDT w_o = , self.output_vector_size], stddev= ) IDT IDT IDT IDT b_o = init_biases([self.output_vector_size]) + + IDT IDT IDT IDT def model(dataset, w_h, b_h, w_o, b_o, train): + IDT IDT IDT IDT IDT if fullyNNConfig.dropout and train: + IDT IDT IDT IDT IDT IDT drop_i = tf.nn.dropout(dataset, fullyNNConfig.dropout_keep_prob_input) + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + IDT IDT IDT IDT IDT IDT drop_h = tf.nn.dropout(h_lay_train, fullyNNConfig.dropout_keep_prob_hidden) + IDT IDT IDT IDT IDT IDT return tf.matmul(drop_h, w_o) + b_o + IDT IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT IDT h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + IDT IDT IDT IDT IDT IDT return tf.matmul(h_lay_train, w_o) + b_o + IDT IDT IDT IDT logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + IDT IDT IDT IDT if fullyNNConfig.candidate_sampling == 'nce_loss': + IDT IDT IDT IDT IDT instances_loss = , b_o, tf_train_dataset, tf_train_labels, fullyNNConfig.num_sampled, self.output_vector_siz, num_true= ) IDT IDT IDT IDT elif fullyNNConfig.candidate_sampling == 'softmax_loss': + IDT IDT IDT IDT IDT instances_loss = , b_o, tf_train_dataset, tf_train_labels, fullyNNConfig.num_sampled, self.output_vector_siz, num_true= ) IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT instances_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits, tf_train_labels) + IDT IDT IDT IDT IDT + IDT IDT IDT IDT loss = tf.reduce_mean(instances_loss) + IDT IDT IDT IDT if fullyNNConfig.regularization: + IDT IDT IDT IDT IDT loss += beta_regul * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + IDT IDT IDT IDT if fullyNNConfig.learning_rate_decay: + IDT IDT IDT IDT IDT learning_rate = , global_step, fullyNNConfig.decay_steps, fullyNNConfig.decay_rate, staircase= ) IDT IDT IDT IDT IDT optimizer = , global_step= ) IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT optimizer = tf.train.GradientDescentOptimizer(fullyNNConfig.learning_rate).minimize(loss) + IDT IDT IDT IDT train_prediction = tf.nn.softmax(logits) + IDT IDT IDT IDT valid_prediction = tf.nn.softmax(model(tf_valid_dataset, w_h, b_h, w_o, b_o, False)) + IDT IDT IDT IDT test_prediction = tf.nn.softmax(model(tf_test_dataset, w_h, b_h, w_o, b_o, False)) + IDT IDT IDT IDT with tf.name_scope('accuracy'): + IDT IDT IDT IDT IDT pre = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT lbl = , shape= , self.output_vector_size]) ) IDT IDT IDT IDT IDT accuracy = tf.reduce_mean(tf.cast(tf.nn.sigmoid_cross_entropy_with_logits(pre, lbl), 'float')) + IDT IDT logger.info('running the session...') + IDT IDT with tf.Session(graph= , config= ) IDT IDT IDT tf.initialize_all_variables().run() + IDT IDT IDT logger.info('Initialized') + IDT IDT IDT for step in range(fullyNNConfig.num_steps): + IDT IDT IDT IDT offset = step * fullyNNConfig.batch_size % (self.train_labels.shape[0] - fullyNNConfig.batch_size) + IDT IDT IDT IDT batch_data = self.train_dataset[offset:offset + fullyNNConfig.batch_size, :] + IDT IDT IDT IDT batch_labels = self.train_labels[offset:offset + fullyNNConfig.batch_size, :] + IDT IDT IDT IDT feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + IDT IDT IDT IDT if fullyNNConfig.regularization: + IDT IDT IDT IDT IDT feed_dict[beta_regul] = fullyNNConfig.beta_regul + IDT IDT IDT IDT _, l, predictions = , loss, train_prediction], feed_dict= ) IDT IDT IDT IDT if step % fullyNNConfig.summary_steps == 0: + IDT IDT IDT IDT IDT logger.info('Minibatch loss at step %d: %f' % (step, l)) + IDT IDT IDT IDT IDT logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: batch_labels})) ) IDT IDT IDT IDT IDT logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.valid_labels})) ) IDT IDT IDT logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict= , lbl: self.test_labels})) ) IDT IDT IDT self.print_words(test_prediction.eval(), self.test_labels) + + IDT def print_words(self, preds, labels): + IDT IDT for pred, label in zip(preds, labels): + IDT IDT IDT label_ids = self.d_loader.d_handler.get_ids_from_binary_vector(label)[0] + IDT IDT IDT pred_ids = np.argsort(np.negative(pred))[:label_ids.size] + IDT IDT IDT , '-->', self.d_loader.d_handler.id_list_to_word_list(pred_ids)) + + IDT def get_words(self, vect): + IDT IDT ids = self.d_loader.d_handler.get_ids_from_binary_vector(vect)[0] + IDT IDT return self.d_loader.d_handler.id_list_to_word_list(ids) + + +if __name__ == '__main__': + IDT try: + IDT IDT nn = NN() + IDT IDT nn.simple_NN() + IDT IDT logger.info('done...') + IDT except Exception as e: + IDT IDT logger.exception(e) + IDT IDT raise +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level= ) + +def get_optimizer(opt): + IDT if opt == 'adam': + IDT IDT optfn = tf.train.AdamOptimizer + IDT elif opt == 'sgd': + IDT IDT optfn = tf.train.GradientDescentOptimizer + IDT else: + IDT IDT assert False + IDT return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + IDT """ + IDT Arguments: + IDT IDT -num_units: hidden state dimensions + IDT IDT -encoder_output: hidden states to compute attention over + IDT IDT -scope: lol who knows + IDT """ + + IDT def __init__(self, num_units, encoder_output, scope= ) IDT IDT self.attn_states = encoder_output + IDT IDT super(LSTMAttnCell, self).__init__(num_units) + + IDT def __call__(self, inputs, state, scope= ) IDT IDT lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + IDT IDT with vs.variable_scope(scope or type(self).__name__): + IDT IDT IDT with vs.variable_scope('Attn'): + IDT IDT IDT IDT ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + IDT IDT IDT IDT ht = , axis= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT scores = , reduction_indices= , keep_dims= ) IDT IDT IDT context = , reduction_indices= ) IDT IDT IDT with vs.variable_scope('AttnConcat'): + IDT IDT IDT IDT out = tf.nn.tanh(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + IDT IDT IDT return out, lstm_state + + +class Encoder(object): + IDT """ + IDT Arguments: + IDT IDT -size: dimension of the hidden states + IDT """ + + IDT def __init__(self, hidden_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.dropout = dropout + + IDT def encode(self, inputs, masks, attention_inputs= , initial_state= , None), model_type= , name= , reuse= ) IDT IDT """ + IDT IDT In a generalized encode function, you pass in your inputs, + IDT IDT masks, and an initial + IDT IDT hidden state input into this function. + + IDT IDT :param inputs: Symbolic representations of your input + IDT IDT :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + IDT IDT IDT IDT IDT through masked steps + IDT IDT :param encoder_state_input: (Optional) pass this as initial hidden state + IDT IDT IDT IDT IDT IDT IDT IDT IDT to tf.nn.dynamic_rnn to build conditional representations + IDT IDT :param attention_inputs: (Optional) pass this to compute attention and context + IDT IDT IDT IDT IDT IDT IDT IDT IDT over these encodings + IDT IDT :return: an encoded representation of your input. + IDT IDT IDT IDT It can be context-level representation, word-level representation, + IDT IDT IDT IDT or both. + IDT IDT """ + IDT IDT with tf.variable_scope(name, reuse= ) IDT IDT IDT if attention_inputs is None: + IDT IDT IDT IDT if model_type == 'gru': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + IDT IDT IDT IDT elif model_type == 'lstm': + IDT IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT else: + IDT IDT IDT IDT IDT raise Exception('Must specify model type.') + IDT IDT IDT else: + IDT IDT IDT IDT assert model_type == 'lstm' + IDT IDT IDT IDT cell = LSTMAttnCell(self.hidden_size, attention_inputs) + IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT outputs, final_state = , cell, inputs, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT final_outputs = outputs[0] + outputs[1] + IDT IDT IDT return final_outputs, final_state + + +class Decoder(object): + + IDT def __init__(self, hidden_size, output_size, dropout): + IDT IDT self.hidden_size = hidden_size + IDT IDT self.output_size = output_size + IDT IDT self.dropout = dropout + + IDT def decode(self, knowledge_rep, masks, initial_state= , None)) ) IDT IDT """ + IDT IDT takes in a knowledge representation + IDT IDT and output a probability estimation over + IDT IDT all paragraph tokens on which token should be + IDT IDT the start of the answer span, and which should be + IDT IDT the end of the answer span. + + IDT IDT :param knowledge_rep: it is a representation of the paragraph and question, + IDT IDT IDT IDT IDT IDT IDT decided by how you choose to implement the encoder + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('decoder'): + IDT IDT IDT with vs.variable_scope('answer_start'): + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT IDT start_states, start_final_state = , cell, knowledge_rep, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT IDT start_states = start_states[0] + start_states[1] + IDT IDT IDT IDT start_states_reshaped = tf.reshape(start_states, [-1, self.hidden_size]) + IDT IDT IDT IDT start_probs = , output_size= , bias= ) IDT IDT IDT IDT start_probs = tf.reshape(start_probs, [-1, self.output_size]) + IDT IDT IDT with vs.variable_scope('answer_end'): + IDT IDT IDT IDT cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + IDT IDT IDT IDT cell = , output_keep_prob= ) IDT IDT IDT IDT end_states, end_final_state = , cell, knowledge_rep, sequence_length= , initial_state_fw= , initial_state_bw= , dtype= ) IDT IDT IDT IDT end_states = end_states[0] + end_states[1] + IDT IDT IDT IDT end_states_reshaped = tf.reshape(end_states, [-1, self.hidden_size]) + IDT IDT IDT IDT end_probs = , output_size= , bias= ) IDT IDT IDT IDT end_probs = tf.reshape(end_probs, [-1, self.output_size]) + IDT IDT IDT bool_masks = , maxlen= , tf.float32) ) IDT IDT IDT add_mask = -1e+30 * (1.0 - bool_masks) + IDT IDT IDT start_probs = tf.add(start_probs, add_mask) + IDT IDT IDT end_probs = tf.add(end_probs, add_mask) + IDT IDT return start_probs, end_probs + + +class QASystem(object): + + IDT def __init__(self, pretrained_embeddings, flags): + IDT IDT """ + IDT IDT Initializes your System + + IDT IDT :param args: pass in more arguments as needed + IDT IDT """ + IDT IDT self.pretrained_embeddings = pretrained_embeddings + IDT IDT self.flags = flags + IDT IDT self.h_size = self.flags.state_size + IDT IDT self.p_size = self.flags.output_size + IDT IDT self.q_size = self.flags.question_size + IDT IDT self.embed_size = self.flags.embedding_size + IDT IDT self.dropout = self.flags.dropout + IDT IDT self.encoder = , dropout= ) IDT IDT self.decoder = , output_size= , dropout= ) IDT IDT self.context_placeholder = , shape= , self.p_size), name= ) IDT IDT self.question_placeholder = , shape= , self.q_size), name= ) IDT IDT self.answer_span_placeholder = , shape= , 2), name= ) IDT IDT self.mask_q_placeholder = , shape= ,), name= ) IDT IDT self.mask_ctx_placeholder = , shape= ,), name= ) IDT IDT self.dropout_placeholder = , shape= , name= ) IDT IDT with tf.variable_scope('qa', initializer= ) IDT IDT IDT self.setup_embeddings() + IDT IDT IDT self.setup_system() + IDT IDT IDT self.setup_loss() + IDT IDT self.global_step = , trainable= ) IDT IDT self.starter_learning_rate = self.flags.learning_rate + IDT IDT self.learning_rate = self.starter_learning_rate + IDT IDT self.optimizer = get_optimizer('adam') + IDT IDT if self.flags.grad_clip: + IDT IDT IDT self.optimizer = self.optimizer(self.learning_rate) + IDT IDT IDT grads = self.optimizer.compute_gradients(self.loss) + IDT IDT IDT for i, (grad, var) in enumerate(grads): + IDT IDT IDT IDT if grad is not None: + IDT IDT IDT IDT IDT grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + IDT IDT IDT self.train_op = , global_step= ) IDT IDT else: + IDT IDT IDT self.train_op = , global_step= ) IDT IDT self.saver = tf.train.Saver() + + IDT def pad(self, sequence, max_length): + IDT IDT from qa_data import PAD_ID + IDT IDT padded_sequence = [] + IDT IDT mask = [] + IDT IDT for sentence in sequence: + IDT IDT IDT mask.append(len(sentence)) + IDT IDT IDT sentence.extend([PAD_ID] * (max_length - len(sentence))) + IDT IDT IDT padded_sequence.append(sentence) + IDT IDT return padded_sequence, mask + + IDT def mixer(self, q_states, ctx_states): + IDT IDT with vs.variable_scope('mixer'): + IDT IDT IDT A = , tf.transpose(q_states, perm= , 2, 1]))) ) IDT IDT IDT C_P = batch_matmul(A, q_states) + IDT IDT IDT C_P = tf.reshape(C_P, [-1, self.h_size]) + IDT IDT IDT P = tf.reshape(ctx_states, [-1, self.h_size]) + IDT IDT IDT P_final = , P], output_size= , bias= ) IDT IDT IDT P_final = tf.reshape(P_final, [-1, self.p_size, self.h_size]) + IDT IDT IDT return P_final + + IDT def filter(self, Q, P): + IDT IDT with vs.variable_scope('filter'): + IDT IDT IDT Qn = , dim= ) IDT IDT IDT Pn = , dim= ) IDT IDT IDT R = , tf.transpose(Pn, perm= , 2, 1])) ) IDT IDT IDT r = , axis= ) IDT IDT IDT r = , axis= ) IDT IDT IDT P_filtered = P * r + IDT IDT IDT return P_filtered + + IDT def coattention(self, P, Q, masks): + IDT IDT P_t = , perm= , 2, 1]) ) IDT IDT Q_t = , perm= , 2, 1]) ) IDT IDT L = tf.batch_matmul(P, Q_t) + IDT IDT AQ = , dim= ) IDT IDT AD = , perm= , 2, 1]), dim= ) IDT IDT CQ = tf.batch_matmul(P_t, AQ) + IDT IDT contexts = tf.concat(1, [Q_t, CQ]) + IDT IDT CD = tf.batch_matmul(contexts, AD) + IDT IDT lstm_inputs = , [P_t, CD]), perm= , 2, 1]) ) IDT IDT cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + IDT IDT cell_fw = , output_keep_prob= ) IDT IDT cell_bw = , output_keep_prob= ) IDT IDT all_states, _ = , cell_bw, lstm_inputs, sequence_length= , dtype= ) IDT IDT U = tf.concat(2, all_states) + IDT IDT U = U[:, :self.p_size, :] + IDT IDT return U + + IDT def setup_system(self): + IDT IDT """ + IDT IDT After your modularized implementation of encoder and decoder + IDT IDT you should call various functions inside encoder, decoder here + IDT IDT to assemble your reading comprehension system! + IDT IDT :return: + IDT IDT """ + IDT IDT P_filtered = self.context_embeddings + IDT IDT P_filtered = , P= ) IDT IDT question_states, final_question_state = , self.mask_q_placeholder, attention_inputs= , model_type= , reuse= , name= ) IDT IDT ctx_states, final_ctx_state = , self.mask_ctx_placeholder, attention_inputs= , initial_state= , model_type= , reuse= , name= ) IDT IDT feed_states = , ctx_states= ) IDT IDT self.start_probs, self.end_probs = , masks= , initial_state= ) + IDT def setup_loss(self): + IDT IDT """ + IDT IDT Set up your loss computation here + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('loss'): + IDT IDT IDT self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + IDT def setup_embeddings(self): + IDT IDT """ + IDT IDT Loads distributed word representations based on placeholder tokens + IDT IDT :return: + IDT IDT """ + IDT IDT with vs.variable_scope('embeddings'): + IDT IDT IDT embeddings = , name= , dtype= , trainable= ) IDT IDT IDT self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + IDT IDT IDT self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + IDT IDT IDT """ + IDT IDT IDT q_mean,q_var = , axes= , shift= , name= , keep_dims= ) IDT IDT IDT ctx_mean,ctx_var = , axes= , shift= , name= , keep_dims= ) + + + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + IDT IDT IDT + + + IDT IDT IDT q_scale = , shape= ,self.embed_size],initializer= ) IDT IDT IDT ctx_scale = , shape= ,self.embed_size],initializer= ) IDT IDT IDT q_offset = , shape= ,self.embed_size],initializer= ) IDT IDT IDT ctx_offset = , shape= ,self.embed_size],initializer= ) + + IDT IDT IDT self.question_embeddings = , q_mean, q_var, q_offset,q_scale, variance_epsilon= ) IDT IDT IDT self.context_embeddings = , ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon= ) IDT IDT IDT """ + + IDT def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Takes in actual data to optimize your model + IDT IDT This method is equivalent to a step() function + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.train_op, self.loss] + IDT IDT _, loss = session.run(output_feed, input_feed) + IDT IDT return loss + + IDT def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT in here you should compute a cost for your validation set + IDT IDT and tune your hyperparameters according to the validation set performance + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT input_feed[self.answer_span_placeholder] = answer_span_batch + IDT IDT output_feed = [self.loss] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs[0] + + IDT def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Returns the probability distribution over different positions in the paragraph + IDT IDT so that other methods like self.answer() will be able to work properly + IDT IDT :return: + IDT IDT """ + IDT IDT input_feed = {} + IDT IDT input_feed[self.context_placeholder] = context_batch + IDT IDT input_feed[self.question_placeholder] = question_batch + IDT IDT input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + IDT IDT input_feed[self.mask_q_placeholder] = mask_q_batch + IDT IDT input_feed[self.dropout_placeholder] = self.flags.dropout + IDT IDT output_feed = [self.start_probs, self.end_probs] + IDT IDT outputs = session.run(output_feed, input_feed) + IDT IDT return outputs + + IDT def answer(self, session, data): + IDT IDT yp_lst = [] + IDT IDT yp2_lst = [] + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle= ) IDT IDT IDT yp, yp2 = self.decode(session, *batch) + IDT IDT IDT yp_lst.append(yp) + IDT IDT IDT yp2_lst.append(yp2) + IDT IDT IDT prog_train.update(i + 1, [('Answering Questions....', 0.0)]) + IDT IDT + IDT IDT yp_all = , axis= ) IDT IDT yp2_all = , axis= ) IDT IDT a_s = , axis= ) IDT IDT a_e = , axis= ) IDT IDT return a_s, a_e + + IDT def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + IDT IDT """ + IDT IDT Iterate through the validation dataset and determine what + IDT IDT the validation cost is. + + IDT IDT This method calls self.test() which explicitly calculates validation cost. + + IDT IDT How you implement this function is dependent on how you design + IDT IDT your data iteration function + + IDT IDT :return: + IDT IDT """ + IDT IDT return self.test(session= , context_batch= , question_batch= , answer_span_batch= , mask_ctx_batch= , mask_q_batch= ) + IDT def evaluate_answer(self, session, dataset, context, sample= , log= , eval_set= ) IDT IDT """ + IDT IDT Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + IDT IDT with the set of true answer labels + + IDT IDT This step actually takes quite some time. So we can only sample 100 examples + IDT IDT from either training or testing set. + + IDT IDT :param session: session should always be centrally managed in train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param sample: how many examples in dataset we look at + IDT IDT :param log: whether we print to std out stream + IDT IDT :return: + IDT IDT """ + IDT IDT if sample is None: + IDT IDT IDT sampled = dataset + IDT IDT IDT sample = len(dataset[0]) + IDT IDT else: + IDT IDT IDT inds = np.random.choice(len(dataset[0]), sample) + IDT IDT IDT sampled = [elem[inds] for elem in dataset] + IDT IDT IDT context = [context[i] for i in inds] + IDT IDT a_s, a_e = self.answer(session, sampled) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + IDT IDT f1 = [] + IDT IDT em = [] + IDT IDT for i in range(len(sampled[0])): + IDT IDT IDT pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + IDT IDT IDT actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + IDT IDT IDT f1.append(f1_score(pred_words, actual_words)) + IDT IDT IDT cur_em = exact_match_score(pred_words, actual_words) + IDT IDT IDT em.append(float(cur_em)) + IDT IDT if log: + IDT IDT IDT logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + IDT IDT return np.mean(f1), np.mean(em) + + IDT def run_epoch(self, sess, train_set, val_set, train_context, val_context): + IDT IDT prog_train = ) IDT IDT for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + IDT IDT IDT loss = self.optimize(sess, *batch) + IDT IDT IDT prog_train.update(i + 1, [('train loss', loss)]) + IDT IDT + IDT IDT prog_val = ) IDT IDT for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + IDT IDT IDT val_loss = self.validate(sess, *batch) + IDT IDT IDT prog_val.update(i + 1, [('val loss', val_loss)]) + IDT IDT + IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) IDT IDT self.evaluate_answer(session= , dataset= , context= , sample= , log= , eval_set= ) + IDT def train(self, session, dataset, val_dataset, train_dir): + IDT IDT """ + IDT IDT Implement main training loop + + IDT IDT TIPS: + IDT IDT You should also implement learning rate annealing (look into tf.train.exponential_decay) + IDT IDT Considering the long time to train, you should save your model per epoch. + + IDT IDT More ambitious approach can include implement early stopping, or reload + IDT IDT previous models if they have higher performance than the current one + + IDT IDT As suggested in the document, you should evaluate your training progress by + IDT IDT printing out information every fixed number of iterations. + + IDT IDT We recommend you evaluate your model performance on F1 and EM instead of just + IDT IDT looking at the cost. + + IDT IDT :param session: it should be passed in from train.py + IDT IDT :param dataset: a representation of our data, in some implementations, you can + IDT IDT IDT IDT IDT IDT pass in multiple components (arguments) of one dataset to this function + IDT IDT :param train_dir: path to the directory where you should save the model checkpoint + IDT IDT :return: + IDT IDT """ + IDT IDT tic = time.time() + IDT IDT params = tf.trainable_variables() + IDT IDT num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + IDT IDT toc = time.time() + IDT IDT logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + IDT IDT train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + IDT IDT val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + IDT IDT val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + IDT IDT num_epochs = self.flags.epochs + IDT IDT if self.flags.debug: + IDT IDT IDT train_dataset = [elem[:self.flags.batch_size * 1] for elem in train_dataset] + IDT IDT IDT val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + IDT IDT IDT num_epochs = 100 + IDT IDT for epoch in range(num_epochs): + IDT IDT IDT logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + IDT IDT IDT self.run_epoch(sess= , train_set= , val_set= , train_context= , val_context= ) IDT IDT IDT logging.info('Saving model in %s', train_dir) + IDT IDT IDT self.saver.save(session, train_dir + '/' + self.flags.run_name + '.ckpt') + + IDT def minibatches(self, data, batch_size, shuffle= ) IDT IDT num_data = len(data[0]) + IDT IDT context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + IDT IDT indices = np.arange(num_data) + IDT IDT if shuffle: + IDT IDT IDT np.random.shuffle(indices) + IDT IDT for minibatch_start in np.arange(0, num_data, batch_size): + IDT IDT IDT minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + IDT IDT IDT yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT for one_hot in new_enc_labels: + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT IDT assert (one_hot = )Found {} +""".format(one_hot) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +from __future__ import print_function +from __future__ import absolute_import +from __future__ import division +import sys +if sys.version[0] == '2': + IDT import cPickle as pkl +else: + IDT import pickle as pkl +import numpy as np +import tensorflow as tf +from python import utils +dtype = utils.DTYPE + + +class Model: + + IDT def __init__(self): + IDT IDT self.sess = None + IDT IDT self.X = None + IDT IDT self.y = None + IDT IDT self.layer_keeps = None + IDT IDT self.vars = None + IDT IDT self.keep_prob_train = None + IDT IDT self.keep_prob_test = None + + IDT def run(self, fetches, X= , y= , mode= ) IDT IDT feed_dict = {} + IDT IDT if type(self.X) is list: + IDT IDT IDT for i in range(len(X)): + IDT IDT IDT IDT feed_dict[self.X[i]] = X[i] + IDT IDT else: + IDT IDT IDT feed_dict[self.X] = X + IDT IDT if y is not None: + IDT IDT IDT feed_dict[self.y] = y + IDT IDT if self.layer_keeps is not None: + IDT IDT IDT if mode == 'train': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_train + IDT IDT IDT elif mode == 'test': + IDT IDT IDT IDT feed_dict[self.layer_keeps] = self.keep_prob_test + IDT IDT return self.sess.run(fetches, feed_dict) + + IDT def dump(self, model_path): + IDT IDT var_map = {} + IDT IDT for name, var in self.vars.iteritems(): + IDT IDT IDT var_map[name] = self.run(var) + IDT IDT pkl.dump(var_map, open(model_path, 'wb')) + IDT IDT + + +class LR(Model): + + IDT def __init__(self, input_dim= , output_dim= , init_path= , opt_algo= , learning_rate= , l2_weight= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , logits= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FM(Model): + + IDT def __init__(self, input_dim= , output_dim= , factor_order= , init_path= , opt_algo= , learning_rate= , l2_w= , l2_v= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = tf.sparse_placeholder(dtype) + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = self.vars['w'] + IDT IDT IDT v = self.vars['v'] + IDT IDT IDT b = self.vars['b'] + IDT IDT IDT X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + IDT IDT IDT xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + IDT IDT IDT p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + IDT IDT IDT xw = tf.sparse_tensor_dense_matmul(self.X, w) + IDT IDT IDT logits = tf.reshape(xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class FNN(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT node_in = num_inputs * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class DeepFM(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('weight_%d' % i, [field_sizes[i], 1], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('bias', [1], 'zero', dtype)) + IDT IDT node_in = num_inputs * embed_size + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w = [self.vars['weight_%d' % i] for i in range(num_inputs)] + IDT IDT IDT v = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT b = self.vars['bias'] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xv = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], v[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xv + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT xv = tf.reshape(xv, [-1, num_inputs, embed_size]) + IDT IDT IDT p = 0.5 * tf.reduce_sum(tf.square(tf.reduce_sum(xv, 1)) - tf.reduce_sum(tf.square(xv), 1), 1) + IDT IDT IDT xw = tf.reduce_sum(xw, 1) + IDT IDT IDT logits = tf.reshape(l + xw + b + p, [-1]) + IDT IDT IDT self.y_prob = tf.sigmoid(logits) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class CCPM(Model): + + IDT def __init__(self, field_sizes= , embed_size= , filter_sizes= , layer_acts= , drop_out= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + IDT IDT init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + IDT IDT init_vars.append(('b1', [1], 'zero', dtype)) + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT l = xw + IDT IDT IDT l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + IDT IDT IDT f1 = self.vars['f1'] + IDT IDT IDT l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + IDT IDT IDT f2 = self.vars['f2'] + IDT IDT IDT l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + IDT IDT IDT l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + IDT IDT IDT w1 = self.vars['w1'] + IDT IDT IDT b1 = self.vars['b1'] + IDT IDT IDT l = tf.matmul(l, w1) + b1 + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN1(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + IDT IDT IDT l = tf.concat([xw, ip], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= ) + +class PNN2(Model): + + IDT def __init__(self, field_sizes= , embed_size= , layer_sizes= , layer_acts= , drop_out= , embed_l2= , layer_l2= , init_path= , opt_algo= , learning_rate= , random_seed= , layer_norm= , kernel_type= ) IDT IDT Model.__init__(self) + IDT IDT init_vars = [] + IDT IDT num_inputs = len(field_sizes) + IDT IDT for i in range(num_inputs): + IDT IDT IDT init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + IDT IDT num_pairs = int(num_inputs * (num_inputs - 1) / 2) + IDT IDT node_in = num_inputs * embed_size + num_pairs + IDT IDT if kernel_type == 'mat': + IDT IDT IDT init_vars.append(('kernel', [embed_size, num_pairs, embed_size], 'xavier', dtype)) + IDT IDT elif kernel_type == 'vec': + IDT IDT IDT init_vars.append(('kernel', [num_pairs, embed_size], 'xavier', dtype)) + IDT IDT elif kernel_type == 'num': + IDT IDT IDT init_vars.append(('kernel', [num_pairs, 1], 'xavier', dtype)) + IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + IDT IDT IDT init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + IDT IDT IDT node_in = layer_sizes[i] + IDT IDT self.graph = tf.Graph() + IDT IDT with self.graph.as_default(): + IDT IDT IDT if random_seed is not None: + IDT IDT IDT IDT tf.set_random_seed(random_seed) + IDT IDT IDT self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + IDT IDT IDT self.y = tf.placeholder(dtype) + IDT IDT IDT self.keep_prob_train = 1 - np.array(drop_out) + IDT IDT IDT self.keep_prob_test = np.ones_like(drop_out) + IDT IDT IDT self.layer_keeps = tf.placeholder(dtype) + IDT IDT IDT self.vars = utils.init_var_map(init_vars, init_path) + IDT IDT IDT w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + IDT IDT IDT xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + IDT IDT IDT xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + IDT IDT IDT row = [] + IDT IDT IDT col = [] + IDT IDT IDT for i in range(num_inputs - 1): + IDT IDT IDT IDT for j in range(i + 1, num_inputs): + IDT IDT IDT IDT IDT row.append(i) + IDT IDT IDT IDT IDT col.append(j) + IDT IDT IDT p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + IDT IDT IDT q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + IDT IDT IDT p = tf.reshape(p, [-1, num_pairs, embed_size]) + IDT IDT IDT q = tf.reshape(q, [-1, num_pairs, embed_size]) + IDT IDT IDT k = self.vars['kernel'] + IDT IDT IDT if kernel_type == 'mat': + IDT IDT IDT IDT p = tf.expand_dims(p, 1) + IDT IDT IDT IDT kp = tf.reduce_sum(tf.multiply(tf.transpose(tf.reduce_sum(tf.multiply(p, k), -1), [0, 2, 1]), q), -1) + IDT IDT IDT else: + IDT IDT IDT IDT k = tf.expand_dims(k, 0) + IDT IDT IDT IDT kp = tf.reduce_sum(p * q * k, -1) + IDT IDT IDT l = tf.concat([xw, kp], 1) + IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT bi = self.vars['b%d' % i] + IDT IDT IDT IDT l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + IDT IDT IDT l = tf.squeeze(l) + IDT IDT IDT self.y_prob = tf.sigmoid(l) + IDT IDT IDT self.loss = , labels= ) IDT IDT IDT if layer_l2 is not None: + IDT IDT IDT IDT self.loss += embed_l2 * tf.nn.l2_loss(xw) + IDT IDT IDT IDT for i in range(len(layer_sizes)): + IDT IDT IDT IDT IDT wi = self.vars['w%d' % i] + IDT IDT IDT IDT IDT self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + IDT IDT IDT self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + IDT IDT IDT config = tf.ConfigProto() + IDT IDT IDT config.gpu_options.allow_growth = True + IDT IDT IDT self.sess = ) IDT IDT IDT tf.global_variables_initializer().run(session= )import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + IDT return + + +def test_folder_path(cifar10_dataset_folder_path): + IDT assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + IDT assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + IDT assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + IDT assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + IDT train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + IDT other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + IDT missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + IDT assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + IDT + + +def test_normalize(normalize): + IDT test_shape = np.random.choice(range(1000)), 32, 32, 3 + IDT test_numbers = np.random.choice(range(256), test_shape) + IDT normalize_out = normalize(test_numbers) + IDT assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + IDT assert normalize_out.max() <= , 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) ) IDT _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + IDT test_shape = np.random.choice(range(1000)) + IDT test_numbers = np.random.choice(range(10), test_shape) + IDT one_hot_out = one_hot_encode(test_numbers) + IDT assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + IDT assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + IDT n_encode_tests = 5 + IDT test_pairs = list(zip(test_numbers, one_hot_out)) + IDT test_indices = np.random.choice(len(test_numbers), n_encode_tests) + IDT labels = [test_pairs[test_i][0] for test_i in test_indices] + IDT enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + IDT new_enc_labels = one_hot_encode(labels) + IDT assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + IDT _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + IDT image_shape = 32, 32, 3 + IDT nn_inputs_out_x = neural_net_image_input(image_shape) + IDT assert nn_inputs_out_x.get_shape().as_list() = , image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) ) IDT assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + IDT assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + IDT + + +def test_nn_label_inputs(neural_net_label_input): + IDT n_classes = 10 + IDT nn_inputs_out_y = neural_net_label_input(n_classes) + IDT assert nn_inputs_out_y.get_shape().as_list() = , n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) ) IDT assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + IDT assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + IDT + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + IDT nn_inputs_out_k = neural_net_keep_prob_input() + IDT assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + IDT assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + IDT assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + IDT + + +def test_con_pool(conv2d_maxpool): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + IDT test_num_outputs = 10 + IDT test_con_k = 2, 2 + IDT test_con_s = 4, 4 + IDT test_pool_k = 2, 2 + IDT test_pool_s = 2, 2 + IDT conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + IDT assert conv2d_maxpool_out.get_shape().as_list() = , 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_flatten(flatten): + IDT test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + IDT flat_out = flatten(test_x) + IDT assert flat_out.get_shape().as_list() = , 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_fully_conn(fully_conn): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT fc_out = fully_conn(test_x, test_num_outputs) + IDT assert fc_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_output(output): + IDT test_x = tf.placeholder(tf.float32, [None, 128]) + IDT test_num_outputs = 40 + IDT output_out = output(test_x, test_num_outputs) + IDT assert output_out.get_shape().as_list() = , 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) ) IDT _print_success_message() + + +def test_conv_net(conv_net): + IDT test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + IDT test_k = tf.placeholder(tf.float32) + IDT logits_out = conv_net(test_x, test_k) + IDT assert logits_out.get_shape().as_list() = , 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) ) IDT + + +def test_train_nn(train_neural_network): + IDT mock_session = tf.Session() + IDT test_x = np.random.rand(128, 32, 32, 3) + IDT test_y = np.random.rand(128, 10) + IDT test_k = np.random.rand(1) + IDT test_optimizer = tf.train.AdamOptimizer() + IDT mock_session.run = MagicMock() + IDT train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + IDT assert mock_session.run.called, 'Session not used' + IDT _print_success_message() diff --git a/dataset/preprocessed/0.txt b/dataset/preprocessed/0.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/0.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/1.txt b/dataset/preprocessed/1.txt new file mode 100644 index 0000000..d61803e --- /dev/null +++ b/dataset/preprocessed/1.txt @@ -0,0 +1,130 @@ +import sys +sys.path.insert(0, '..') +import tensorflow as tf +from util import batch_norm_layer +from mnist_gan_layers import feed_forward_layer, transposed_conv_layer, conv_layer +from mnist_gan_helper import MNIST_GAN +import numpy as np +np.random.seed(0) +from matplotlib import pyplot as plt + + +class GAN: + """.. :py:class::``GAN`` + + This class encapsulates both generator and discriminator. + + Attributes + ---------- + input : tf.placeholder + Placeholder of shape (batch_size, 50) of uniform random values from [-1,1] + is_training : tf.placeholder + Bool placeholder for correct batch norm during inference + gen_output : tf.Tensor + Output of the generator portion + dis_output : tf.Tensor + Output of the discriminator portion + loss_dis : Cross-entropy loss of the discriminator + loss_gen : Cross-entropy loss of the generator + input_reals : tf.placeholder + Placeholder for feeding batch_size real input images + """ + + def __init__(self, batch_size=32, learning_rate=0.0004): + with tf.variable_scope('generator') as scope_gen: + self.input = tf.placeholder(tf.float32, shape=(batch_size, 50)) + self.is_training = tf.placeholder(tf.bool, shape=[]) + expanded_input = feed_forward_layer(self.input, 64 * 4 * 4, self.is_training, normalize=True, activation_function=tf.nn.relu) + with tf.variable_scope('layer1'): + layer_1 = tf.reshape(expanded_input, (-1, 4, 4, 64)) + with tf.variable_scope('layer2'): + layer_2 = transposed_conv_layer(layer_1, (5, 5, 32, 64), (batch_size, 7, 7, 32), (1, 2, 2, 1), self.is_training, normalize=True, activation_function=tf.nn.relu) + with tf.variable_scope('layer3'): + layer_3 = transposed_conv_layer(layer_2, (5, 5, 16, 32), (batch_size, 14, 14, 16), (1, 2, 2, 1), self.is_training, normalize=True, activation_function=tf.nn.relu) + with tf.variable_scope('layer4'): + layer_4 = transposed_conv_layer(layer_3, (5, 5, 1, 16), (batch_size, 28, 28, 1), (1, 2, 2, 1), self.is_training, normalize=False, activation_function=tf.nn.tanh) + self.gen_output = layer_4 + variables_gen = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope_gen.name) + with tf.variable_scope('discriminator') as scope_dis: + self.input_reals = tf.placeholder(tf.float32, shape=(batch_size, 28, 28, 1)) + inputs = tf.concat([self.gen_output, self.input_reals], 0) + labels = tf.concat([tf.zeros((batch_size, 1), tf.float32), tf.ones((batch_size, 1), tf.float32)], 0) + act_fn = tf.nn.leaky_relu + with tf.variable_scope('layer1'): + conv1 = conv_layer(inputs, 8, 5, 2, activation_function=act_fn, normalize=False) + with tf.variable_scope('layer2'): + conv2 = conv_layer(conv1, 16, 5, 2, activation_function=act_fn, normalize=False) + with tf.variable_scope('layer3'): + conv3 = conv_layer(conv2, 32, 5, 2, activation_function=act_fn, normalize=False) + with tf.variable_scope('layer4'): + conv3_reshaped = tf.reshape(conv3, shape=(batch_size * 2, 4 * 4 * 32)) + self.dis_output = feed_forward_layer(conv3_reshaped, 1, self.is_training, normalize=False, activation_function=tf.nn.sigmoid) + variables_dis = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope_dis.name) + entropy_dis = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=self.dis_output) + self.loss_dis = tf.reduce_mean(entropy_dis) + entropy_gen = tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones((batch_size, 1), tf.float32), logits=self.dis_output[:batch_size]) + self.loss_gen = tf.reduce_mean(entropy_gen) + self.train_step_gen = tf.train.AdamOptimizer(learning_rate, beta1=0.5).minimize(self.loss_gen, var_list=variables_gen) + self.train_step_dis = tf.train.AdamOptimizer(learning_rate, beta1=0.5).minimize(self.loss_dis, var_list=variables_dis) + + def train_step(self, session, vector, real_images, is_training): + """Run one step of training.""" + fetches = [self.loss_dis, self.loss_gen, self.train_step_dis, self.train_step_gen] + feeds = {self.input: vector, self.input_reals: real_images, self.is_training: is_training} + loss_dis, loss_gen, _, _ = session.run(fetches, feed_dict=feeds) + return loss_dis, loss_gen + + def generate_images(self, session, vectors): + fetches = self.gen_output + feeds = {self.input: vectors, self.is_training: False} + return session.run(fetches, feed_dict=feeds) + + +def plot_images(imgs): + n, h, w, c = imgs.shape + cols = int(np.sqrt(n)) + rows = int(n / cols) + 1 + fig, axarr = plt.subplots(rows, cols, figsize=(20, 20)) + for index in range(n): + row = index // cols + col = index % cols + ax = axarr[row][col] + ax.imshow(imgs[(index), :, :, (0)], cmap='gray') + for index in range(n, rows * cols): + row = index // cols + col = index % cols + fig.delaxes(axarr[row][col]) + plt.show() + + +def main(): + mnist_helper = MNIST_GAN('data') + epochs = 5 + batch_size = 64 + gan = GAN(batch_size) + losses_dis = [] + losses_gen = [] + with tf.Session() as session: + session.run(tf.global_variables_initializer()) + for epoch in range(epochs): + print(f'Starting epoch {(epoch + 1)}/{epochs}') + for batch in mnist_helper.get_batch(batch_size): + z = np.random.uniform(-1, 1, size=(batch_size, 50)) + loss_dis, loss_gen = gan.train_step(session, z, batch, True) + losses_dis.append(loss_dis) + losses_gen.append(loss_gen) + imgs = gan.generate_images(session, np.random.uniform(-1, 1, size=(batch_size, 50))) + plot_images(imgs) + fig = plt.figure() + ax = fig.add_subplot(111) + ax.set_title('Entropy of GAN') + ax.set_xlabel('Training step') + ax.set_ylabel('Sigmoid cross-entropy') + ax.plot(losses_dis, label='Discriminator') + ax.plot(losses_gen, label='Generator') + ax.legend() + plt.show() + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/10.txt b/dataset/preprocessed/10.txt new file mode 100644 index 0000000..f58471f --- /dev/null +++ b/dataset/preprocessed/10.txt @@ -0,0 +1,94 @@ +import sys +import os +SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) +sys.path.append(os.path.join(SCRIPT_PATH, 'models/tutorials/image/cifar10/')) +import cifar10 +import cifar10_input +import tensorflow as tf +import numpy as np +import time +import math +EPOCHES = 3000 +BATCH_SIZE = 128 +DATA_DIR = '/tmp/cifar10_data/cifar-10-batches-bin' + + +def variable_with_weight_loss(shape, stddev, wl): + variable = tf.Variable(tf.truncated_normal(shape, stddev=stddev)) + if wl is not None: + weight_loss = tf.multiply(tf.nn.l2_loss(variable), wl) + tf.add_to_collection('losses', weight_loss) + return variable + + +def loss(logits, labels): + labels = tf.cast(labels, tf.int32) + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, name='cross_entropy_per_example') + cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') + tf.add_to_collection('losses', cross_entropy_mean) + return tf.add_n(tf.get_collection('losses'), name='total_loss') + + +if __name__ == '__main__': + cifar10.maybe_download_and_extract() + images_train, labels_train = cifar10_input.distorted_inputs(data_dir=DATA_DIR, batch_size=BATCH_SIZE) + images_test, labels_test = cifar10_input.inputs(eval_data=True, data_dir=DATA_DIR, batch_size=BATCH_SIZE) + images_placeholder = tf.placeholder(tf.float32, [BATCH_SIZE, 24, 24, 3]) + labels_placeholder = tf.placeholder(tf.int32, [BATCH_SIZE]) + with tf.variable_scope('conv_1'): + weight_1 = variable_with_weight_loss([5, 5, 3, 64], stddev=0.05, wl=None) + kernel_1 = tf.nn.conv2d(images_placeholder, weight_1, strides=[1, 1, 1, 1], padding='SAME') + bias_1 = tf.Variable(tf.zeros([64])) + conv_1 = tf.nn.relu(kernel_1 + bias_1) + pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME') + norm_1 = tf.nn.lrn(pool_1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75) + with tf.variable_scope('conv_2'): + weight_2 = variable_with_weight_loss([5, 5, 64, 64], stddev=0.05, wl=None) + kernel_2 = tf.nn.conv2d(norm_1, weight_2, strides=[1, 1, 1, 1], padding='SAME') + bias_2 = tf.Variable(tf.constant(0.1, shape=[64])) + conv_2 = tf.nn.relu(kernel_2 + bias_2) + norm_2 = tf.nn.lrn(conv_2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75) + pool_2 = tf.nn.max_pool(norm_2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME') + with tf.variable_scope('fc_1'): + reshape = tf.reshape(pool_2, [BATCH_SIZE, -1]) + dim = reshape.get_shape()[1].value + weight_3 = variable_with_weight_loss([dim, 384], stddev=0.04, wl=0.004) + bias_3 = tf.Variable(tf.constant(0.1, shape=[384])) + fc_1 = tf.nn.relu(tf.matmul(reshape, weight_3) + bias_3) + with tf.variable_scope('fc_2'): + weight_4 = variable_with_weight_loss([384, 192], stddev=0.04, wl=0.04) + bias_3 = tf.Variable(tf.constant(0.1, shape=[192])) + fc_2 = tf.nn.relu(tf.matmul(fc_1, weight_4) + bias_3) + with tf.variable_scope('fc_3'): + weight_5 = variable_with_weight_loss([192, 10], stddev=1 / 192.0, wl=None) + bias_5 = tf.Variable(tf.constant(0.0, shape=[10])) + logits = tf.matmul(fc_2, weight_5) + bias_5 + with tf.variable_scope('loss'): + loss = loss(logits, labels_placeholder) + with tf.variable_scope('trainer'): + trainer = tf.train.AdamOptimizer(0.001).minimize(loss) + session = tf.InteractiveSession() + tf.global_variables_initializer().run() + tf.train.start_queue_runners() + for epoch in range(EPOCHES): + start_time = time.time() + image_batch, label_batch = session.run([images_train, labels_train]) + _, loss_value = session.run([trainer, loss], feed_dict={images_placeholder: image_batch, labels_placeholder: label_batch}) + duration = time.time() - start_time + if epoch % 10 == 0: + examples_per_sec = BATCH_SIZE / duration + seconds_per_batch = float(duration) + format_str = 'step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)' + print(format_str % (epoch, loss_value, examples_per_sec, seconds_per_batch)) + num_examples = 10000 + num_iter = int(math.ceil(num_examples / BATCH_SIZE)) + top_k = tf.nn.in_top_k(logits, labels_placeholder, 1) + total_sample_count = num_iter * BATCH_SIZE + true_count = 0 + while num_iter > 0: + image_batch, label_batch = session.run([images_test, labels_test]) + predictions = session.run([top_k], feed_dict={images_placeholder: image_batch, labels_placeholder: label_batch}) + true_count += np.sum(predictions) + num_iter -= 1 + precision = true_count / total_sample_count + print('precision @ 1 = %3.f' % precision) diff --git a/dataset/preprocessed/100.txt b/dataset/preprocessed/100.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/100.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/101.txt b/dataset/preprocessed/101.txt new file mode 100644 index 0000000..ee4ee99 --- /dev/null +++ b/dataset/preprocessed/101.txt @@ -0,0 +1,369 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +get_ipython().magic('matplotlib inline') +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (1-layer net)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_bacthes + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + drop1 = tf.nn.dropout(lay1_train, 0.5) + logits = tf.matmul(drop1, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_batches = 3 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_batches + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + logits = tf.matmul(lay2_train, weights3) + biases3 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_hidden_nodes3], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + weights4 = tf.Variable(tf.truncated_normal([num_hidden_nodes3, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes3))) + biases4 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + logits = tf.matmul(lay3_train, weights4) + biases4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 4000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 18001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 512 +num_hidden_nodes3 = 256 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_hidden_nodes3], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + weights4 = tf.Variable(tf.truncated_normal([num_hidden_nodes3, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes3))) + biases4 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + drop1 = tf.nn.dropout(lay1_train, 0.5) + lay2_train = tf.nn.relu(tf.matmul(drop1, weights2) + biases2) + drop2 = tf.nn.dropout(lay2_train, 0.5) + lay3_train = tf.nn.relu(tf.matmul(drop2, weights3) + biases3) + drop3 = tf.nn.dropout(lay3_train, 0.5) + logits = tf.matmul(drop3, weights4) + biases4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 5000, 0.8, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 20001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/102.txt b/dataset/preprocessed/102.txt new file mode 100644 index 0000000..a87f4ba --- /dev/null +++ b/dataset/preprocessed/102.txt @@ -0,0 +1,165 @@ +import tensorflow as tf +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.sigma = tf.Variable(1.0, name='sigma') + self.mu = tf.Variable(0.0, name='mu') + self.upsilon = tf.Variable(0.1, name='upsilon') + self.beta = tf.Variable(0.0001, name='beta') + self.one = tf.Variable(1.0, name='one') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + self.normalized_update_op = [self.upsilon.assign((self.one - self.beta) * self.upsilon + self.beta * tf.reduce_mean(tf.square(self.output))), self.mu.assign((self.one - self.beta) * self.mu + self.beta * tf.reduce_mean(self.output)), self.sigma.assign(self.upsilon - self.mu * self.mu)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + self.nomalized_q_function = tf.sqrt(self.sigma) * self.q_function + self.mu + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00025) + norm_gradients = self.optimizer.compute_gradients(self.loss, var_list=[self.W0, self.b0]) + norm_gradients = [(tf.reciprocal(self.sigma) * g, v) for g, v in norm_gradients] + gradients = self.optimizer.compute_gradients(self.loss, var_list=[self.W1, self.b1]) + gradients.extend(norm_gradients) + self.apply_gradients = self.optimizer.apply_gradients(gradients) + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def update_normalized_parms(self, session, outputs): + session.run(self.normalized_update_op, feed_dict={self.output: outputs}) + + def norm_train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + self.update_normalized_parms(session, outputs) + _, cost = session.run([self.apply_gradients, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def get_norm_parms(self, session): + return session.run([self.beta, self.upsilon, self.mu, self.sigma]) diff --git a/dataset/preprocessed/103.txt b/dataset/preprocessed/103.txt new file mode 100644 index 0000000..eec94d6 --- /dev/null +++ b/dataset/preprocessed/103.txt @@ -0,0 +1,174 @@ +import tensorflow as tf +import numpy as np +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + self.rewards = tf.placeholder(tf.float32, shape=(None,), name='reward') + self.indices = tf.placeholder(tf.int32, shape=(None, 2), name='indices') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + self.select_prob = tf.gather_nd(self.prob, self.indices) + self.result = tf.multiply(self.select_prob, self.rewards) + self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + self.pg_optimization = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(self.pg_loss) + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + self.saver = tf.train.Saver() + self.saved_path = './saved_model' + self.count = 0 + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def policy_train(self, session, inputs, outputs, rewards): + form, pos, deprel = inputs + indices = [] + for aid in range(len(outputs)): + indices.append([aid, outputs[aid]]) + _, cost, indices, prob, res = session.run([self.pg_optimization, self.pg_loss, self.indices, self.prob, self.result], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) + self.count += 1 + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + def variable_output(self, session): + form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + def save(self, session): + self.saved_path = self.saver.save(session, './saved_model') + + def restore(self, session): + self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + session.run(self.update_op) diff --git a/dataset/preprocessed/104.txt b/dataset/preprocessed/104.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/104.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/105.txt b/dataset/preprocessed/105.txt new file mode 100644 index 0000000..8ffa93d --- /dev/null +++ b/dataset/preprocessed/105.txt @@ -0,0 +1,468 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class GRUAttnCell(tf.nn.rnn_cell.GRUCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(GRUAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(gru_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(tf.nn.rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + return out, gru_state + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(LSTMAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + return out, lstm_state + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + -vocab_dim: dimension of the embeddings + """ + + def __init__(self, size, vocab_dim, name): + self.size = size + self.vocab_dim = vocab_dim + self.name = name + + def encode(self, inputs, masks, encoder_state_input=None, attention_inputs=None, model_type='gru'): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(self.name): + if attention_inputs is None: + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(self.size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(self.size) + else: + raise Exception('Must specify model type.') + elif model_type == 'gru': + cell = GRUAttnCell(self.size, attention_inputs) + elif model_type == 'lstm': + cell = LSTMAttnCell(self.size, attention_inputs) + else: + raise Exception('Must specify model type.') + outputs, final_state = tf.nn.dynamic_rnn(cell, inputs, sequence_length=masks, dtype=tf.float32, initial_state=encoder_state_input) + return outputs, final_state + + +class Decoder(object): + + def __init__(self, output_size, name): + self.output_size = output_size + self.name = name + + def decode(self, knowledge_rep, masks, dropout, state_size, model_type='gru'): + """ + takes in a knowledge representation + # and output a probability estimation over + # all paragraph tokens on which token should be + # the start of the answer span, and which should be + # the end of the answer span. + + # :param knowledge_rep: it is a representation of the paragraph and question, + # decided by how you choose to implement the encoder + # :return: + # """ + with tf.variable_scope(self.name): + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(state_size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(state_size) + else: + raise Exception('Must specify model type.') + knowledge_rep = tf.squeeze(knowledge_rep, -1) + with vs.variable_scope('answer_start'): + start_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer1'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer2'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer3'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer4'): + start_probs = tf.nn.rnn_cell._linear(start_probs, self.output_size, True, 1.0) + with vs.variable_scope('answer_end'): + end_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer1'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer2'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer3'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer4'): + end_probs = tf.nn.rnn_cell._linear(end_probs, self.output_size, True, 1.0) + bool_masks = tf.cast(tf.sequence_mask(masks, maxlen=self.output_size), tf.float32) + a = tf.constant(-1e+30) + b = tf.constant(1.0) + add_mask = a * (b - bool_masks) + start_probs = start_probs + add_mask + end_probs = end_probs + add_mask + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, encoder, decoder, pretrained_embeddings, max_ctx_len, max_q_len, flags): + """ + Initializes your System + + :param encoder: tuple of 2 encoders that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.question_encoder, self.context_encoder = encoder + self.decoder = decoder + self.max_ctx_len = max_ctx_len + self.max_q_len = max_q_len + self.embed_size = encoder[0].vocab_dim + self.flags = flags + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.max_ctx_len), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.max_q_len), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def mixer(self, final_q_state, ctx_states, model_type='gru'): + if model_type == 'gru': + pass + elif model_type == 'lstm': + final_q_state = final_q_state[-1] + else: + raise Exception('Must specify model type.') + with vs.variable_scope('mixer'): + ht = tf.nn.rnn_cell._linear(final_q_state, self.flags.state_size, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(ctx_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + ctx_state_rep = ctx_states * scores + return ctx_state_rep + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + question_states, final_question_state = self.question_encoder.encode(self.question_embeddings, self.mask_q_placeholder, encoder_state_input=None, attention_inputs=None, model_type=self.flags.model_type) + ctx_states, final_ctx_state = self.context_encoder.encode(self.context_embeddings, self.mask_ctx_placeholder, encoder_state_input=None, attention_inputs=None, model_type=self.flags.model_type) + feed_states = batch_matmul(ctx_states, tf.expand_dims(final_question_state, 2)) + self.start_probs, self.end_probs = self.decoder.decode(feed_states, self.mask_ctx_placeholder, self.dropout_placeholder, self.flags.state_size, model_type=self.flags.model_type) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.question_embeddings = tf.reshape(question_embeddings, [-1, self.max_q_len, self.embed_size]) + context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + self.context_embeddings = tf.reshape(context_embeddings, [-1, self.max_ctx_len, self.embed_size]) + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = np.array(map(np.array, context_batch)) + input_feed[self.question_placeholder] = np.array(map(np.array, question_batch)) + input_feed[self.mask_ctx_placeholder] = np.array(map(np.array, mask_ctx_batch)) + input_feed[self.mask_q_placeholder] = np.array(mask_q_batch) + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs + + def answer(self, session, data): + data = np.array(data).T + yp, yp2 = self.decode(session, *data) + a_s = np.argmax(yp, axis=1) + a_e = np.argmax(yp2, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset) + else: + sampled = dataset[np.random.choice(dataset.shape[0], sample)] + a_s, a_e = self.answer(session, sampled) + f1 = [] + em = [] + sampled = sampled.T + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][sampled[2][i][0]:sampled[2][i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + em.append(exact_match_score(pred_words, actual_words)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), None, sample)) + return f1, em + + def run_epoch(self, sess, train_set, val_set, context): + prog_train = Progbar(target=1 + int(len(train_set) / self.flags.batch_size)) + for i, batch in enumerate(minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + print('') + if self.flags.debug == 0: + prog_val = Progbar(target=1 + int(len(val_set) / self.flags.batch_size)) + for i, batch in enumerate(minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + train_f1, train_em = self.evaluate_answer(sess, train_set, context=context[0], sample=100, log=True, eval_set='-Epoch TRAIN-') + val_f1, val_em = self.evaluate_answer(sess, val_set, context=context[1], sample=100, log=True, eval_set='-Epoch VAL-') + + def train(self, session, saver, dataset, contexts, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + train_dataset, val_dataset = dataset + train_mask = [None, None] + val_mask = [None, None] + train_dataset[0], train_mask[0] = self.pad(train_dataset[0], self.max_ctx_len) + train_dataset[1], train_mask[1] = self.pad(train_dataset[1], self.max_q_len) + val_dataset[0], val_mask[0] = self.pad(val_dataset[0], self.max_ctx_len) + val_dataset[1], val_mask[1] = self.pad(val_dataset[1], self.max_q_len) + for i in range(1, len(train_dataset[0])): + assert len(train_dataset[0][i]) == len(train_dataset[0][i - 1]), 'Incorrectly padded train context' + assert len(train_dataset[1][i]) == len(train_dataset[1][i - 1]), 'Incorrectly padded train question' + for i in range(1, len(val_dataset[0])): + assert len(val_dataset[0][i]) == len(val_dataset[0][i - 1]), 'Incorrectly padded val context' + assert len(val_dataset[1][i]) == len(val_dataset[1][i - 1]), 'Incorrectly padded val question' + print('Training/val data padding verification completed.') + train_dataset.extend(train_mask) + val_dataset.extend(val_mask) + train_dataset = np.array(train_dataset).T + val_dataset = np.array(val_dataset).T + train_context = contexts[0] + val_context = contexts[1] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = train_dataset[:self.flags.batch_size] + val_dataset = val_dataset[:self.flags.batch_size] + num_epochs = 20 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, context=val_context) + logging.info('Saving model in %s', train_dir) + saver.save(session, train_dir) + self.evaluate_answer(session, train_dataset, train_context, sample=None, log=True, eval_set='-FINAL TRAIN-') + self.evaluate_answer(session, val_dataset, val_context, sample=None, log=True, eval_set='-FINAL VAL-') diff --git a/dataset/preprocessed/106.txt b/dataset/preprocessed/106.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/106.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/107.txt b/dataset/preprocessed/107.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/107.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/108.txt b/dataset/preprocessed/108.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/108.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/109.txt b/dataset/preprocessed/109.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/109.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/11.txt b/dataset/preprocessed/11.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/11.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/110.txt b/dataset/preprocessed/110.txt new file mode 100644 index 0000000..63710b5 --- /dev/null +++ b/dataset/preprocessed/110.txt @@ -0,0 +1,73 @@ +import pandas as pd +import numpy as np +import pickle +import matplotlib.pyplot as plt +from scipy import stats +import tensorflow as tf +import seaborn as sns +from pylab import rcParams +from sklearn import metrics +from sklearn.model_selection import train_test_split +import tensorflow as tf +import pandas as pd +import numpy as np +from tensorflow.contrib import rnn +from sklearn.model_selection import train_test_split +from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score +import os +vocab_size = 10 + + +def buil_model(hidden_units=100, num_classes=vocab_size, batch_size=1, num_layers=1, learning_rate=0.001): + x = tf.placeholder(tf.int32, [batch_size, None], name='input_placeholder') + y = tf.placeholder(tf.int64, [batch_size, None], name='labels_placeholder') + seq_lens = tf.placeholder(tf.int32, [batch_size]) + embeddings = tf.get_variable('embedding_matrix', [num_classes, hidden_units]) + rnn_inputs = tf.nn.embedding_lookup(embeddings, x) + lstm_cell = tf.nn.rnn_cell.LSTMCell(hidden_units, state_is_tuple=True) + cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * num_layers, state_is_tuple=True) + init_state = cells.zero_state(batch_size, tf.float32) + rnn_outputs, final_state = tf.nn.dynamic_rnn(cells, rnn_inputs, initial_state=init_state, sequence_length=seq_lens) + with tf.variable_scope('softmax'): + W = tf.get_variable('W', [hidden_units, num_classes]) + b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0)) + outputs = tf.reshape(rnn_outputs, [-1, hidden_units]) + logits = tf.matmul(outputs, W) + b + error = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tf.reshape(y, [-1]))) + optimizer = tf.train.AdamOptimizer(learning_rate).minimize(error) + correct_predictions = tf.equal(tf.argmax(logits, 1), y) + accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32)) + return dict(x=x, y=y, init_state=init_state, final_state=final_state, error=error, accuracy=accuracy, optimizer=optimizer, sequence_lengths=seq_lens) + + +def run_epoch(session, model, data): + err = 0.0 + acc = 0.0 + iters = 0 + training_state = None + for _sequence_length, _input, _target in data: + feed_dict = {model['x']: np.array([_input], dtype=np.int32), model['y']: np.array([_target], dtype=np.int64), model['sequence_lengths']: np.array(_sequence_length, dtype=np.int32)} + if training_state is not None: + feed_dict[model['init_state']] = training_state + err, training_state, acc, _ = session.run([model['error'], model['final_state'], model['accuracy'], model['optimizer']], feed_dict) + iters += _sequence_length[0] + return err / iters, acc / iters + + +def train_model(train_data, epochs=10): + WORK_DIR = 'tmp_data' + with tf.Graph().as_default(), tf.Session() as session: + initializer = tf.random_uniform_initializer(-0.1, 0.1) + with tf.variable_scope('model', reuse=None, initializer=initializer): + model = buil_model() + session.run(tf.global_variables_initializer()) + saver = tf.train.Saver(tf.global_variables()) + for i in range(epochs): + error, accuracy = run_epoch(session, model, train_data) + print('Epoch: %d -- Train Error: %.4f -- Accuracy: %.4f ' % (i + 1, error, accuracy)) + ckp_path = os.path.join(WORK_DIR, 'model.ckpt') + saver.save(session, ckp_path, global_step=i) + + +train_data = [([5.8], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6])] +train_model(train_data) diff --git a/dataset/preprocessed/111.txt b/dataset/preprocessed/111.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/111.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/113.txt b/dataset/preprocessed/113.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/113.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/114.txt b/dataset/preprocessed/114.txt new file mode 100644 index 0000000..2c37446 --- /dev/null +++ b/dataset/preprocessed/114.txt @@ -0,0 +1,135 @@ +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt + + +def course1(): + x = np.random.rand(100).astype(np.float32) + y = x * 0.1 + 0.3 + W = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) + b = tf.Variable(tf.zeros([1])) + _y = W * x + b + loss = tf.reduce_mean(tf.square(_y - y)) + optimizer = tf.train.GradientDescentOptimizer(0.5) + train = optimizer.minimize(loss) + init = tf.initialize_all_variables() + sess = tf.Session() + sess.run(init) + for step in range(201): + sess.run(train) + if step % 20 == 0: + print('{}, W={}, b={}'.format(step, sess.run(W), sess.run(b))) + + +def course2(): + m1 = tf.constant([[3, 4]]) + m2 = tf.constant([[2], [2]]) + product = tf.matmul(m1, m2) + with tf.Session() as sess: + result = sess.run(product) + print(result) + sess.close() + + +def course3(): + state = tf.Variable(0, name='counter') + one = tf.constant(1) + new_value = tf.add(state, one) + update = tf.assign(state, new_value) + init = tf.initialize_all_variables() + with tf.Session() as sess: + sess.run(init) + for i in range(10): + sess.run(update) + print(sess.run(state)) + sess.close() + + +def course4(): + input1 = tf.placeholder(tf.float32) + input2 = tf.placeholder(tf.float32) + output = tf.multiply(input1, input2) + with tf.Session() as sess: + print(sess.run(output, feed_dict={input1: [7.0], input2: [5.0]})) + sess.close() + + +def add_layer(input, in_size, out_size, activation_function=None, namespace='Unkown'): + with tf.name_scope(namespace): + W = tf.Variable(tf.random_normal([in_size, out_size]), name='Weights') + tf.summary.histogram('{}::{}'.format(namespace, 'Weights'), W) + b = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='Bias') + tf.summary.histogram('{}::{}'.format(namespace, 'Bias'), b) + with tf.name_scope('Wx_plus_b'): + Wx_plus_b = tf.matmul(input, W) + b + if activation_function is None: + outputs = Wx_plus_b + else: + outputs = activation_function(Wx_plus_b) + tf.summary.histogram('{}::{}'.format(namespace, 'Output'), outputs) + return outputs + + +def course5(): + x = np.linspace(-1, 1, 300)[:, (np.newaxis)] + noise = np.random.normal(0, 0.1, x.shape) + y = np.square(x) - 0.8 + noise + xi = tf.placeholder(tf.float32, [None, 1]) + yi = tf.placeholder(tf.float32, [None, 1]) + l1 = add_layer(xi, 1, 10, activation_function=tf.nn.relu) + prediction = add_layer(l1, 10, 1, activation_function=None) + loss = tf.reduce_mean(tf.reduce_sum(tf.square(yi - prediction), 1)) + train = tf.train.GradientDescentOptimizer(0.4).minimize(loss) + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + fig = plt.figure() + ax = fig.add_subplot(1, 1, 1) + ax.scatter(x, y) + plt.ion() + feed_input = {xi: x, yi: y} + for i in range(1000): + sess.run(train, feed_dict=feed_input) + if i % 50 == 0: + try: + ax.lines.remove(lines[0]) + except Exception: + pass + plt_predict_y = sess.run(prediction, feed_dict=feed_input) + lines = ax.plot(x, plt_predict_y, 'r-', lw=5) + plt.pause(0.1) + sess.close() + + +def course6(): + x = np.linspace(-1, 1, 300)[:, (np.newaxis)] + noise = np.random.normal(0, 0.1, x.shape) + y = np.square(x) - 0.8 + noise + with tf.name_scope('inputs'): + xi = tf.placeholder(tf.float32, [None, 1], name='x_input') + yi = tf.placeholder(tf.float32, [None, 1], name='y_input') + layer = add_layer(xi, 1, 100, activation_function=tf.nn.relu, namespace='Layer0') + layer_1 = add_layer(layer, 100, 10, activation_function=tf.nn.relu, namespace='Layer1') + prediction = add_layer(layer_1, 10, 1, activation_function=tf.nn.relu, namespace='Output_Layer') + with tf.name_scope('loss'): + loss = tf.reduce_mean(tf.reduce_sum(tf.square(yi - prediction), 1)) + tf.summary.scalar('loss', loss) + with tf.name_scope('train'): + train = tf.train.GradientDescentOptimizer(0.4).minimize(loss) + init = tf.global_variables_initializer() + with tf.Session() as sess: + merged = tf.summary.merge_all() + writer = tf.summary.FileWriter('log', sess.graph) + sess.run(init) + feed_input = {xi: x, yi: y} + for i in range(1000): + sess.run(train, feed_dict=feed_input) + if i % 50 == 0: + print(sess.run(loss, feed_dict=feed_input)) + result = sess.run(merged, feed_dict=feed_input) + writer.add_summary(result, i) + sess.close() + + +if __name__ == '__main__': + course6() diff --git a/dataset/preprocessed/115.txt b/dataset/preprocessed/115.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/115.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/116.txt b/dataset/preprocessed/116.txt new file mode 100644 index 0000000..d2e6c71 --- /dev/null +++ b/dataset/preprocessed/116.txt @@ -0,0 +1,127 @@ +import sys +import numpy as np +import tensorflow as tf +from sklearn import cross_validation +from sklearn.cross_validation import KFold +from sklearn import metrics + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +class TextCNN(object): + + def __init__(self, train_dataset, train_labels, valid_dataset, valid_labels, embeddings, vocabulary, l2_reg_lambda, num_steps, batch_size, num_filters, filter_sizes_1, filter_sizes_2, filter_sizes_3, dropout_keep_prob, lexical, shuffling): + vocab_size = len(vocabulary) + sequence_length = train_dataset.shape[1] + train_size = train_dataset.shape[0] + num_classes = 2 + filter_sizes = [filter_sizes_1, filter_sizes_2, filter_sizes_3] + num_filters_total = num_filters * len(filter_sizes) + embedding_size = embeddings.shape[1] + embeddings_number = embeddings.shape[0] + graph = tf.Graph() + with graph.as_default(): + tf.set_random_seed(10) + input_x = tf.placeholder(tf.int32, shape=[batch_size, sequence_length]) + input_y = tf.placeholder(tf.int32, shape=[batch_size, num_classes]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_argmax_dataset = tf.constant(valid_dataset) + reg_coef = tf.placeholder(tf.float32) + l2_loss = tf.constant(0.0) + weights_conv = [tf.Variable(tf.truncated_normal([filter_size, embedding_size, 1, num_filters], stddev=tf.sqrt(2.0 / (filter_size * embedding_size)), seed=filter_size + i * num_filters)) for i, filter_size in enumerate(filter_sizes)] + biases_conv = [tf.Variable(tf.constant(0.01, shape=[num_filters])) for filter_size in filter_sizes] + weight_output = tf.Variable(tf.truncated_normal([num_filters_total, num_classes], stddev=tf.sqrt(2.0 / (num_filters_total + num_classes)), seed=0)) + bias_output = tf.Variable(tf.constant(0.01, shape=[num_classes])) + embeddings_const = tf.placeholder(tf.float32, shape=[embeddings_number, embedding_size]) + embeddings_tuned = tf.Variable(embeddings_const) + embedded_chars = tf.nn.embedding_lookup(embeddings_tuned, input_x) + embedded_chars_expanded = tf.expand_dims(embedded_chars, -1) + embedded_chars_valid = tf.nn.embedding_lookup(embeddings_tuned, tf_valid_dataset) + embedded_chars_expanded_valid = tf.expand_dims(embedded_chars_valid, -1) + embedded_chars_argmax = tf.nn.embedding_lookup(embeddings_tuned, tf_argmax_dataset) + embedded_chars_expanded_argmax = tf.expand_dims(embedded_chars_argmax, -1) + + def model(data, dropout_prob): + pooled_outputs = [] + for i, filter_size in enumerate(filter_sizes): + conv = tf.nn.conv2d(data, weights_conv[i], strides=[1, 1, 1, 1], padding='VALID') + h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + pooled = tf.nn.max_pool(h, ksize=[1, sequence_length - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + pooled_outputs.append(pooled) + h_pool = tf.concat(3, pooled_outputs) + h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + h_drop = tf.nn.dropout(h_pool_flat, dropout_prob) + scores = tf.nn.xw_plus_b(h_drop, weight_output, bias_output) + return scores + + def model_argmax(data, dropout_prob): + argmaxs = [] + maximums = [] + pooled_outputs = [] + for i, filter_size in enumerate(filter_sizes): + conv = tf.nn.conv2d(data, weights_conv[i], strides=[1, 1, 1, 1], padding='VALID') + h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + maximum = tf.reduce_max(h, tf.to_int32(1)) + maximums.append(maximum) + argmax = tf.argmax(h, tf.to_int32(1)) + argmaxs.append(argmax) + return argmaxs, maximums + scores = model(embedded_chars_expanded, dropout_keep_prob) + train_prediction = tf.nn.softmax(scores) + losses = tf.nn.softmax_cross_entropy_with_logits(scores, tf.cast(input_y, tf.float32)) + for i in range(len(weights_conv)): + l2_loss += tf.nn.l2_loss(weights_conv[i]) + l2_loss += tf.nn.l2_loss(weight_output) + loss = tf.reduce_mean(losses) + reg_coef * l2_loss + global_step = tf.Variable(0, trainable=False) + optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss) + argmaxs, maximums = model_argmax(embedded_chars_expanded_argmax, 1.0) + maximum1 = maximums[0] + maximum2 = maximums[1] + maximum3 = maximums[2] + argmax1 = argmaxs[0] + argmax2 = argmaxs[1] + argmax3 = argmaxs[2] + valid_prediction = tf.nn.softmax(model(embedded_chars_expanded_valid, 1.0)) + with tf.Session(graph=graph) as session: + session.run(tf.initialize_all_variables(), feed_dict={embeddings_const: embeddings}) + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size] + batch_labels = train_labels[offset:offset + batch_size] + feed_dict = {input_x: batch_data, input_y: batch_labels, reg_coef: l2_reg_lambda, embeddings_const: embeddings} + _, l, predictions, embeddings = session.run([optimizer, loss, train_prediction, embeddings_tuned], feed_dict) + if not step % 100: + print('Minibatch loss at step', step, ':', l) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('\n') + maximum1 = session.run([maximum1], feed_dict={embeddings_const: embeddings}) + maximum1 = np.asarray(maximum1) + maximum2 = session.run([maximum2], feed_dict={embeddings_const: embeddings}) + maximum2 = np.asarray(maximum2) + maximum3 = session.run([maximum3], feed_dict={embeddings_const: embeddings}) + maximum3 = np.asarray(maximum3) + argmax1 = session.run([argmax1], feed_dict={embeddings_const: embeddings}) + argmax1 = np.asarray(argmax1) + argmax2 = session.run([argmax2], feed_dict={embeddings_const: embeddings}) + argmax2 = np.asarray(argmax2) + argmax3 = session.run([argmax3], feed_dict={embeddings_const: embeddings}) + argmax3 = np.asarray(argmax3) + np.save('argmax_filter_sizes_1_tuned.npy', argmax1) + np.save('argmax_filter_sizes_2_tuned.npy', argmax2) + np.save('argmax_filter_sizes_3_tuned.npy', argmax3) + np.save('maximum_filter_sizes_1_tuned.npy', maximum1) + np.save('maximum_filter_sizes_2_tuned.npy', maximum2) + np.save('maximum_filter_sizes_3_tuned.npy', maximum3) + self.valid_predictions = session.run([valid_prediction], feed_dict={embeddings_const: embeddings}) + self.valid_predictions = np.asarray(self.valid_predictions).reshape(valid_labels.shape) + predictions_label = np.argmax(self.valid_predictions, 1) + labels = ['neg', 'pos'] + self.prediction_labels_char = [labels[i] for i in predictions_label] + self.prediction_labels_char = np.asarray(self.prediction_labels_char) + np.save('gold_labels_tuned.npy', predictions_label) + self.valid_accuracy = accuracy(self.valid_predictions, np.asarray(valid_labels)) + self.embeddings_final = embeddings diff --git a/dataset/preprocessed/117.txt b/dataset/preprocessed/117.txt new file mode 100644 index 0000000..aaa1d54 --- /dev/null +++ b/dataset/preprocessed/117.txt @@ -0,0 +1,263 @@ +import numpy as np +import tensorflow as tf +import neural_network as nn + + +class Generator(nn.Generator): + """ + Example OpenAI-Gym Generator architecture. + """ + + def __init__(self, sess): + """ + Args + ---- + sess : the tensorflow session to be used + """ + self.sess_ = sess + with tf.variable_scope('gen'): + self.input_state_ = tf.placeholder(tf.float32, shape=[None, 16], name='input_state') + self.input_seed_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_seed') + self.concat = tf.concat([self.input_state_, self.input_seed_], 1, name='concat') + self.hidden = tf.layers.dense(self.concat, 8, activation=tf.nn.relu, name='hidden') + self.output_ = tf.layers.dense(self.hidden, 4, name='output') + self.sess.run(tf.global_variables_initializer()) + + @property + def input_state(self): + """ + The input state of shape [None, 4] + + Returns + ------- + A placeholder tensor: the input state's placeholder tensor + """ + return self.input_state_ + + @property + def output(self): + """ + The outputted action distribution of shape [None, 2] + + Returns + ------- + A tensor: the output tensor + """ + return self.output_ + + @property + def sess(self): + """ + The session used to create the graph + + Returns + ------- + A session: the graph's session + """ + return self.sess_ + + @property + def input_seed(self): + """ + The input random seed + + Returns + ------- + A placeholder: the input seed's placeholder tensor + """ + return self.input_seed_ + + @property + def trainable_variables(self): + """ + A list of the trainable variables in our generator + + Returns + ------- + A list of tensors: the trainable variables in this graph + """ + return tf.trainable_variables('gen') + + +class Discriminator(nn.Discriminator): + """ + Example OpenAI-Gym Discriminator Architecture + """ + + def __init__(self, sess): + """ + Args + ---- + sess : the tensorflow session to be used + """ + self.sess_ = sess + with tf.variable_scope('dis'): + self.input_state_ = tf.placeholder(tf.float32, shape=[None, 16], name='input_state') + self.input_reward_ = tf.placeholder(tf.float32, shape=[None], name='input_reward') + self.input_action_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_action') + self.input_reward_exp = tf.expand_dims(self.input_reward_, axis=-1, name='input_reward_expanded') + self.concat = tf.concat([self.input_state_, self.input_reward_exp, self.input_action_], axis=1, name='concat') + self.hidden = tf.layers.dense(self.concat, 8, activation=tf.nn.relu, name='hidden') + self.output_ = tf.layers.dense(self.hidden, 1, activation=tf.sigmoid, name='output') + self.sess.run(tf.global_variables_initializer()) + + @property + def input_state(self): + """ + The input state of shape [None, 4] + + Returns + ------- + A placeholder tensor: the input state's placeholder tensor + """ + return self.input_state_ + + @property + def input_action(self): + """ + The input action of shape [None, 1] + + Returns + ------- + A placeholder tensor: the input action's placeholder tensor + """ + return self.input_action_ + + @property + def output(self): + """ + The probability output of shape [None, 1] + + Returns + ------- + A tensor: the output's tensor + """ + return self.output_ + + @property + def sess(self): + """ + The session used to create a graph + + Returns + ------- + A session: the graph's session + """ + return self.sess_ + + @property + def input_reward(self): + """ + The input reward + + Returns + ------- + A placeholder tensor: the input reward's tensor + """ + return self.input_reward_ + + @property + def trainable_variables(self): + """ + A list of the trainable variables in our generator + + Returns + ------- + A list of tensors: the trainable variables in this graph + """ + return tf.trainable_variables('dis') + + +class Discriminator_copy(nn.Discriminator_copy): + """ + Example OpenAI-Gym Discriminator Copying method + """ + + def __init__(self, dis, new_rew_input): + """ + Initializes a discriminator_copy object + + Args + ---- + dis (Discriminator) : The discriminator to copy + new_rew_input (tf.placeholder) : a new reward input. + """ + self.sess_ = dis.sess + with tf.variable_scope('dis', reuse=tf.AUTO_REUSE): + self.input_state_ = tf.placeholder(tf.float32, shape=[None, 16], name='input_state') + self.input_reward_ = new_rew_input + self.input_action_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_action') + self.input_reward_exp = tf.expand_dims(self.input_reward_, axis=-1, name='input_reward_expanded') + self.concat = tf.concat([self.input_state_, self.input_reward_exp, self.input_action_], axis=1, name='concat_copy') + self.hidden_ker = tf.get_variable('hidden/kernel') + self.hidden_bias = tf.get_variable('hidden/bias') + self.output_ker = tf.get_variable('output/kernel') + self.output_bias = tf.get_variable('output/bias') + self.hidden = tf.matmul(self.concat, self.hidden_ker) + self.hidden_bias + self.output_ = tf.sigmoid(tf.matmul(self.hidden, self.output_ker) + self.output_bias) + + @property + def input_state(self): + """ + The input state of shape [None, 4] + + Returns + ------- + A placeholder tensor: the input state's placeholder tensor + """ + return self.input_state_ + + @property + def input_action(self): + """ + The input action of shape [None, 1] + + Returns + ------- + A placeholder tensor: the input action's placeholder tensor + """ + return self.input_action_ + + @property + def output(self): + """ + The probability output of shape [None, 1] + + Returns + ------- + A tensor: the output's tensor + """ + return self.output_ + + @property + def sess(self): + """ + The session used to create a graph + + Returns + ------- + A session: the graph's session + """ + return self.sess_ + + @property + def input_reward(self): + """ + The input reward + + Returns + ------- + A placeholder tensor: the input reward's tensor + """ + return self.input_reward_ + + @property + def trainable_variables(self): + """ + A list of the trainable variables in our generator + + Returns + ------- + A list of tensors: the trainable variables in this graph + """ + return tf.trainable_variables('dis') diff --git a/dataset/preprocessed/118.txt b/dataset/preprocessed/118.txt new file mode 100644 index 0000000..f5c1f81 --- /dev/null +++ b/dataset/preprocessed/118.txt @@ -0,0 +1,51 @@ +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + data_d = tf.nn.dropout(data, keep_prob=0.9) + output1 = tf.matmul(data_d, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/119.txt b/dataset/preprocessed/119.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/119.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/12.txt b/dataset/preprocessed/12.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/12.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/120.txt b/dataset/preprocessed/120.txt new file mode 100644 index 0000000..9be7263 --- /dev/null +++ b/dataset/preprocessed/120.txt @@ -0,0 +1,130 @@ +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy(predictions, train_labels[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + hidden_1 = tf.nn.relu(logits_1) + logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized, Using Neural Network') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/121.txt b/dataset/preprocessed/121.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/121.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/122.txt b/dataset/preprocessed/122.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/122.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/123.txt b/dataset/preprocessed/123.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/123.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/124.txt b/dataset/preprocessed/124.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/124.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/125.txt b/dataset/preprocessed/125.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/125.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/126.txt b/dataset/preprocessed/126.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/126.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/127.txt b/dataset/preprocessed/127.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/127.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/128.txt b/dataset/preprocessed/128.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/128.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/129.txt b/dataset/preprocessed/129.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/129.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/13.txt b/dataset/preprocessed/13.txt new file mode 100644 index 0000000..747adf8 --- /dev/null +++ b/dataset/preprocessed/13.txt @@ -0,0 +1,68 @@ +""" +目的:tf 常用方法使用. + + +Z = n +- `tf.Graph()` +- `tf.Variable()` +- `tf.placeholder()` +- `tf.nn.sigmoid_cross_entropy_with_logits()` +- `tf.reduce_sum()` +- `tf.nn.l2_loss()` +- `tf.sigmoid()` +- `tf.multiply()` +- `tf.concat()` 在某个维度上连接 tensor. +- `tf.gather()` +- `tf.square()` +- `tf.square()` +- `tf.constant()` +- `tf.transpose()` +- `tf.sparse_to_dense()` +- `tf.matmul()` +- `tf.nn.dropout()` +- `tf.nn.moments()` +- `tf.random_uniform()` +- `tf.random_normal()` +- `tf.train.FtrlOptimizer()` +- `tf.train.GradientDescentOptimizer()` +- `tf.tanh()` +- `tf.nn.relu()` +- `tf.train.AdagradOptimizer()` +- `tf.train.AdamOptimizer()` +- `tf.name_scope()` + + + +""" +import tensorflow as tf +sess = tf.Session() +""" +两步走, +- 第一步,根据类型创建一个 placeholder, +- 第二步,将参数通过 feed_dict 的方式喂给 session 和变量. + +三步走:placeholder + 计算表达式 + run 并且喂数据. +""" +x = tf.placeholder(tf.float32) +y = tf.placeholder(tf.float32) +z = x + y + 1 +z_result = sess.run(z, feed_dict={x: 2.0, y: 3.2}) +print(z_result) +x_constant = tf.constant([[1, 2, 3], [5, 98, 20]]) +r_s_1 = tf.reduce_sum(x_constant, axis=0) +r_s_2 = tf.reduce_sum(x_constant, axis=1) +r_1_result = sess.run(r_s_1) +r_2_result = sess.run(r_s_2) +print(r_1_result) +print(r_2_result) +v_1 = tf.constant(2) +v_2 = tf.constant(3) +mul_12 = tf.multiply(v_1, v_2) +mul_12_result = sess.run(mul_12) +print(mul_12_result) +t_1 = [[1, 4, 3], [2, 5, 6]] +t_2 = [[8, 9, 7], [10, 9, 26]] +concat_1 = tf.concat([t_1, t_2], 0) +concat_2 = tf.concat([t_1, t_2], 1) +print(sess.run(concat_1)) +print(sess.run(concat_2)) diff --git a/dataset/preprocessed/130.txt b/dataset/preprocessed/130.txt new file mode 100644 index 0000000..055c9ae --- /dev/null +++ b/dataset/preprocessed/130.txt @@ -0,0 +1,62 @@ +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +import numpy as np +try: + xrange +except NameError: + xrange = range +mnist = input_data.read_data_sets('data/', one_hot=True) + + +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) + + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +x = tf.placeholder(tf.float32, [None, 784]) +y_ = tf.placeholder(tf.float32, [None, 10]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder(tf.float32) +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1])) +train = tf.train.AdamOptimizer(0.0001).minimize(loss) +correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +session = tf.Session() +session.run(tf.initialize_all_variables()) +for i in xrange(1000): + batch = mnist.train.next_batch(50) + if i % 100 == 0: + train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}, session=session) + print('step %d, training accuracy %g' % (i, train_accuracy)) + session.run(train, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) +final_accuracy = accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}, session=session) +print('test accuracy %g' % final_accuracy) diff --git a/dataset/preprocessed/131.txt b/dataset/preprocessed/131.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/131.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/133.txt b/dataset/preprocessed/133.txt new file mode 100644 index 0000000..e2a47a8 --- /dev/null +++ b/dataset/preprocessed/133.txt @@ -0,0 +1,140 @@ +import tensorflow as tf +import tensorflow.contrib.rnn as rnn +from math import sqrt +learning_rate = 0.01 +gaussian_noise = 0.2 +dropout_prob = 0.7 +regularize_beta = 1e-07 +word_vocab_size = 100004 +subword_vocab_size = 100004 +word_embedding_dim = 64 +subword_embedding_dim = 64 +input_subword_dim = 100 +input_dim = 100 +hidden_dim = 150 +hidden_2_dim = 100 +output_types = ['tag', 'word_freq'] +n_output = dict() +n_output['tag'] = 15 +n_output['word_freq'] = 14 +n_saved_models = 2 +x = dict() +x['words'] = tf.placeholder(tf.int32, [None, None], name='input_words') +x['subwords'] = tf.placeholder(tf.int32, [None, None], name='input_subwords') +x['subwords_len'] = tf.placeholder(tf.int32, [None], name='input_subwords_len') +label = dict() +label['tag'] = tf.placeholder(tf.int32, [None], name='label_tags') +label['word_freq'] = tf.placeholder(tf.int32, [None], name='label_word_freq') +word_embedding = tf.Variable(tf.zeros([word_vocab_size, word_embedding_dim]), trainable=False, name='word_emb') +subword_embedding = tf.Variable(tf.zeros([subword_vocab_size, subword_embedding_dim]), trainable=False, name='sw_emb') + + +def nn_run_subword_processing(subwords): + with tf.variable_scope('subword_lstm'): + mapped_subwords = tf.nn.embedding_lookup(subword_embedding, subwords) + lstm_fw_cell = rnn.BasicLSTMCell(input_subword_dim) + lstm_bw_cell = rnn.BasicLSTMCell(input_subword_dim) + lstm_outputs, _ = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, mapped_subwords, dtype=tf.float32) + fw_outputs, bw_outputs = lstm_outputs + return tf.concat([fw_outputs[:, (-1), :], bw_outputs[:, (-1), :]], 1) + + +def nn_run_input_layer(input_dict): + mapped_words = tf.nn.embedding_lookup(word_embedding, input_dict['words']) + subword_vec = nn_run_subword_processing(input_dict['subwords']) + expanded_subword_vec = tf.expand_dims(subword_vec, 0) + word_subword = tf.concat([mapped_words, expanded_subword_vec], axis=2) + with tf.variable_scope('word_lstm'): + lstm_fw_cell = rnn.BasicLSTMCell(input_dim) + lstm_bw_cell = rnn.BasicLSTMCell(input_dim) + lstm_outputs, _ = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, word_subword, dtype=tf.float32) + concat_data = tf.concat([lstm_outputs[0], lstm_outputs[1]], axis=2) + input_data = tf.reshape(concat_data, [-1, input_dim * 2]) + input_noise = tf.Variable(tf.truncated_normal([1, input_dim * 2], stddev=gaussian_noise), name='input_noise') + return tf.add(input_data, input_noise) + + +def nn_run_hidden_layer(input_vec): + hidden_weight = tf.Variable(tf.truncated_normal([input_dim * 2, hidden_dim], stddev=1.0 / sqrt(hidden_dim)), name='weights/hidden_layer') + hidden_bias = tf.Variable(tf.zeros([hidden_dim]), name='bias/hidden_layer') + hidden_2_weight = tf.Variable(tf.truncated_normal([hidden_dim, hidden_2_dim], stddev=1.0 / sqrt(hidden_2_dim)), name='weights/hidden_2_layer') + hidden_2_bias = tf.Variable(tf.zeros([hidden_2_dim]), name='bias/hidden_2_layer') + hidden_1_output = tf.nn.tanh(tf.add(tf.matmul(input_vec, hidden_weight), hidden_bias)) + hidden_2_output = tf.nn.relu(tf.add(tf.matmul(hidden_1_output, hidden_2_weight), hidden_2_bias)) + return hidden_2_output + + +def nn_classify(hidden_vec): + out_weights = dict() + out_biases = dict() + outputs = dict() + for output_type in output_types: + out_weights[output_type] = tf.Variable(tf.truncated_normal([hidden_2_dim, n_output[output_type]], stddev=1.0 / sqrt(n_output[output_type])), name='weights/out_' + output_type) + out_biases[output_type] = tf.Variable(tf.zeros([n_output[output_type]]), name='bias/out_' + output_type) + outputs[output_type] = tf.matmul(hidden_vec, out_weights[output_type]) + out_biases[output_type] + return outputs + + +def nn_calculate_loss(predicted_outputs): + all_loss = 0.0 + for output_type in output_types: + y = tf.one_hot(label[output_type], n_output[output_type], on_value=1.0, off_value=0.0, axis=-1) + dropped_output = tf.nn.dropout(predicted_outputs[output_type], dropout_prob) + ce_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=dropped_output)) + all_loss += ce_loss + all_weights = [tensor for tensor in tf.global_variables() if 'weights' in tensor.name] + l2_score = regularize_beta * sum([tf.nn.l2_loss(tensor) for tensor in all_weights]) + all_loss += l2_score + return all_loss + + +processed_input = nn_run_input_layer(x) +h = nn_run_hidden_layer(processed_input) +outputs = nn_classify(h) +loss = nn_calculate_loss(outputs) +optimize = tf.train.AdamOptimizer(name='pos_tagger_opt').minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver(max_to_keep=n_saved_models) + + +class TaggerModel: + + def __init__(self, embeddings=None, model_path=None): + self.session = tf.Session() + if model_path is not None: + saver.restore(self.session, model_path) + else: + self.session.run(init) + self.initial_embedding(embeddings[0], embeddings[1]) + + def train(self, input_list, labels): + feed_dict = TaggerModel.get_feed_dict(input_list, labels) + _, iter_loss = self.session.run([optimize, loss], feed_dict=feed_dict) + return iter_loss + + def predict(self, input_list): + feed_dict = TaggerModel.get_feed_dict(input_list) + prediction = self.session.run(outputs, feed_dict=feed_dict) + return prediction['tag'] + + def save_model(self, save_path, global_step): + saver.save(self.session, save_path, global_step=global_step) + print('Model at epoch', global_step, 'is saved.') + + def initial_embedding(self, word_emb_input, subword_emb_input): + word_emb_placeholder = tf.placeholder(tf.float32, [word_vocab_size, word_embedding_dim], name='word_pl') + subword_emb_placeholder = tf.placeholder(tf.float32, [subword_vocab_size, subword_embedding_dim], name='sw_pl') + word_assign = tf.assign(word_embedding, word_emb_placeholder) + subword_assign = tf.assign(subword_embedding, subword_emb_placeholder) + feed_dict = {word_emb_placeholder: word_emb_input, subword_emb_placeholder: subword_emb_input} + self.session.run([word_assign, subword_assign], feed_dict=feed_dict) + + @staticmethod + def get_feed_dict(input_list, labels=None): + feed_dict = dict() + feed_dict[x['words']] = input_list[0] + feed_dict[x['subwords']] = input_list[1] + feed_dict[x['subwords_len']] = input_list[2] + if labels is not None: + feed_dict[label['tag']], feed_dict[label['word_freq']] = labels + return feed_dict diff --git a/dataset/preprocessed/134.txt b/dataset/preprocessed/134.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/134.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/135.txt b/dataset/preprocessed/135.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/135.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/136.txt b/dataset/preprocessed/136.txt new file mode 100644 index 0000000..30b7279 --- /dev/null +++ b/dataset/preprocessed/136.txt @@ -0,0 +1,122 @@ +import tensorflow as tf +import numpy as np +import dihedral as nn + + +class CNN: + + def __init__(self): + self.tfx = None + self.tfp = None + self.tfy = None + self.tftrain_step = None + self.mse = None + self.tfkp = None + self.tfacc = None + self.train_counter = 0 + self.test = None + + def NN(self, x): + assert x.get_shape().as_list() == [None, 424, 424, 3] + x = nn.relu(nn.convolution(x, 8 * 4, w=4, s=2, input_repr='invariant')) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 209, 209, 8 * 4] + x = nn.relu(nn.convolution(x, 8 * 8, w=5, s=2)) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 101, 101, 8 * 8] + x = nn.relu(nn.convolution(x, 8 * 16, w=5, s=2)) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 47, 47, 8 * 16] + x = nn.relu(nn.convolution(x, 8 * 32, w=5, s=2)) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 20, 20, 8 * 32] + x = nn.relu(nn.convolution(x, 8 * 64, w=4, s=2)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 9, 9, 8 * 64] + x = nn.relu(nn.convolution(x, 128, output_repr='invariant')) + x = nn.relu(nn.convolution(x, 8 * 256, w=7, input_repr='invariant')) + x = tf.nn.dropout(x, self.tfkp) + assert x.get_shape().as_list() == [None, 1, 1, 8 * 256] + x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + x = nn.relu(nn.fullyconnected(x, 8 * 256)) + x = tf.nn.dropout(x, self.tfkp) + x = nn.relu(nn.fullyconnected(x, 8 * 256)) + x = nn.batch_normalization(x, self.tfacc) + self.test = x + x = nn.fullyconnected(x, 37, output_repr='invariant') + assert x.get_shape().as_list() == [None, 37] + c1 = tf.nn.softmax(x[:, 0:3]) + c2 = tf.nn.softmax(x[:, 3:5]) * c1[:, 1:2] + c3 = tf.nn.softmax(x[:, 5:7]) * c2[:, 1:2] + c4 = tf.nn.softmax(x[:, 7:9]) * c2[:, 1:2] + c5 = tf.nn.softmax(x[:, 9:13]) * c2[:, 1:2] + c6 = tf.nn.softmax(x[:, 13:15]) + c7 = tf.nn.softmax(x[:, 15:18]) * c1[:, 0:1] + c8 = tf.nn.softmax(x[:, 18:25]) * c6[:, 0:1] + c9 = tf.nn.softmax(x[:, 25:28]) * c2[:, 0:1] + c10 = tf.nn.softmax(x[:, 28:31]) * c4[:, 0:1] + c11 = tf.nn.softmax(x[:, 31:37]) * c4[:, 0:1] + return tf.concat(1, [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11]) + + def create_architecture(self): + self.tfkp = tf.placeholder_with_default(tf.constant(1.0, tf.float32), []) + self.tfacc = tf.placeholder_with_default(tf.constant(0.0, tf.float32), []) + x = self.tfx = tf.placeholder(tf.float32, [None, 424, 424, 3]) + self.tfp = self.NN(x) + self.tfy = tf.placeholder(tf.float32, [None, 37]) + self.mse = tf.reduce_mean(tf.square(self.tfp - self.tfy)) + self.tftrain_step = tf.train.AdamOptimizer(0.001, epsilon=1e-06).minimize(self.mse) + + @staticmethod + def split_test_train(images_path, labels_csv): + import csv + import os + with open(labels_csv) as f: + reader = csv.reader(f) + rows = [row for row in reader] + labels = np.array([[float(x) for x in r[1:]] for r in rows[1:]]).astype(np.float32) + files = [(images_path + '/' + f) for f in sorted(os.listdir(images_path))] + n = 2000 + return (files[:n], labels[:n]), (files[n:], labels[n:]) + + @staticmethod + def load(files): + from scipy.ndimage import imread + n = len(files) + xs = np.zeros((n, 424, 424, 3), dtype=np.float32) + for i in range(n): + xs[i] = imread(files[i], mode='RGB').astype(np.float32) / 256.0 + return CNN.prepare(xs) + + @staticmethod + def prepare(images): + images = images - np.array([0.04543276, 0.04002843, 0.02984124]) + images = images / np.array([0.08930177, 0.0741211, 0.0656323]) + return images + + @staticmethod + def batch(files, labels): + ids = np.random.choice(len(files), 20, replace=False) + xs = CNN.load([files[i] for i in ids]) + ys = labels[ids] + for i in range(len(xs)): + s = np.random.uniform(0.8, 1.2) + u = np.random.uniform(-0.1, 0.1) + xs[i] = xs[i] * s + u + return xs, ys + + def train(self, session, xs, ys, options=None, run_metadata=None): + acc = 0.8 ** (self.train_counter / 1000.0) + _, mse = session.run([self.tftrain_step, self.mse], feed_dict={self.tfx: xs, self.tfy: ys, self.tfkp: 0.5, self.tfacc: acc}, options=options, run_metadata=run_metadata) + self.train_counter += 1 + return mse + + def predict(self, session, xs): + return session.run(self.tfp, feed_dict={self.tfx: xs, self.tfkp: 1.0, self.tfacc: 0.0}) + + def predict_mse(self, session, xs, ys): + return session.run([self.tfp, self.mse], feed_dict={self.tfx: xs, self.tfy: ys, self.tfkp: 1.0, self.tfacc: 0.0}) diff --git a/dataset/preprocessed/137.txt b/dataset/preprocessed/137.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/137.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/138.txt b/dataset/preprocessed/138.txt new file mode 100644 index 0000000..367d926 --- /dev/null +++ b/dataset/preprocessed/138.txt @@ -0,0 +1,376 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +print('Done.') +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +print('Done.') +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +print('Done.') + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +print('Done.') +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + beta = 0.05 + loss_without_reg = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = loss_without_reg + beta * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 + + +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) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer1_size = 1024 + hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + hidden1_biases = bias_variable([hidden_layer1_size]) + hidden1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + output_weights = weight_variable([1024, num_labels]) + output_biases = bias_variable([num_labels]) + logits = tf.matmul(hidden1_layer, output_weights) + output_biases + loss_without_reg = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta = 0.01 + loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 + + +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) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer1_size = 1024 + hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + hidden1_biases = bias_variable([hidden_layer1_size]) + hidden1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + output_weights = weight_variable([1024, num_labels]) + output_biases = bias_variable([num_labels]) + logits = tf.matmul(hidden1_layer, output_weights) + output_biases + loss_without_reg = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta = 0 + loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + test_prediction = tf.nn.softmax(test_logits) +num_steps = 5 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + print('%d : %d' % (offset, offset + batch_size)) + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 + + +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) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer1_size = 1024 + hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + hidden1_biases = bias_variable([hidden_layer1_size]) + keep_prob = 0.5 + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + hidden1_layer = tf.nn.dropout(hidden1, keep_prob) + output_weights = weight_variable([1024, num_labels]) + output_biases = bias_variable([num_labels]) + logits = tf.matmul(hidden1_layer, output_weights) + output_biases + loss_without_reg = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta = 0.01 + loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 + + +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) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer1_size = 1024 + hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + hidden1_biases = bias_variable([hidden_layer1_size]) + keep_prob = 0.5 + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + hidden1_layer = tf.nn.dropout(hidden1, keep_prob) + output_weights = weight_variable([1024, num_labels]) + output_biases = bias_variable([num_labels]) + logits = tf.matmul(hidden1_layer, output_weights) + output_biases + loss_without_reg = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta = 0.01 + loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + valid_logits = tf.matmul(valid_hidden1, output_weights) + output_biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + test_logits = tf.matmul(test_hidden1, output_weights) + output_biases + test_prediction = tf.nn.softmax(test_logits) +num_steps = 5 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.05) + return tf.Variable(initial) + + +def bias_variable(shape): + initial = tf.constant(0.0, shape=shape) + return tf.Variable(initial) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer1_size = 1024 + hidden_layer2_size = 305 + hidden_layer3_size = 75 + hidden1_weights = weight_variable([image_size * image_size, hidden_layer1_size]) + hidden1_biases = bias_variable([hidden_layer1_size]) + hidden2_weights = weight_variable([hidden_layer1_size, hidden_layer2_size]) + hidden2_biases = bias_variable([hidden_layer2_size]) + hidden3_weights = weight_variable([hidden_layer2_size, hidden_layer3_size]) + hidden3_biases = bias_variable([hidden_layer3_size]) + keep_prob = 0.5 + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden1_weights) + hidden1_biases) + hidden1_layer = tf.nn.dropout(hidden1, keep_prob) + hidden2 = tf.nn.relu(tf.matmul(hidden1_layer, hidden2_weights) + hidden2_biases) + hidden2_layer = tf.nn.dropout(hidden2, keep_prob) + hidden3 = tf.nn.relu(tf.matmul(hidden2_layer, hidden3_weights) + hidden3_biases) + hidden3_layer = tf.nn.dropout(hidden3, keep_prob) + output_weights = weight_variable([hidden_layer3_size, num_labels]) + output_biases = bias_variable([num_labels]) + logits = tf.matmul(hidden3_layer, output_weights) + output_biases + loss_without_reg = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta = 0.01 + loss = loss_without_reg + beta * tf.nn.l2_loss(output_weights) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 2000, 0.8, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_hidden1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden1_weights) + hidden1_biases) + valid_hidden2 = tf.nn.relu(tf.matmul(valid_hidden1, hidden2_weights) + hidden2_biases) + valid_hidden3 = tf.nn.relu(tf.matmul(valid_hidden2, hidden3_weights) + hidden3_biases) + valid_logits = tf.matmul(valid_hidden3, output_weights) + output_biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden1_weights) + hidden1_biases) + test_hidden2 = tf.nn.relu(tf.matmul(test_hidden1, hidden2_weights) + hidden2_biases) + test_hidden3 = tf.nn.relu(tf.matmul(test_hidden2, hidden3_weights) + hidden3_biases) + test_logits = tf.matmul(test_hidden3, output_weights) + output_biases + test_prediction = tf.nn.softmax(test_logits) +num_steps = 10001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 1000 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/14.txt b/dataset/preprocessed/14.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/14.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/140.txt b/dataset/preprocessed/140.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/140.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/141.txt b/dataset/preprocessed/141.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/141.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/142.txt b/dataset/preprocessed/142.txt new file mode 100644 index 0000000..a3b73a6 --- /dev/null +++ b/dataset/preprocessed/142.txt @@ -0,0 +1,224 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = '../Lesson 1/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(biases))) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized Stochastic Gradient Descent') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + num_neurons = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_neurons])) + hidden_biases = tf.Variable(tf.zeros([num_neurons])) + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + hidden_out_weights = tf.Variable(tf.truncated_normal([num_neurons, num_labels])) + hidden_out_biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_layer, hidden_out_weights) + hidden_out_biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + beta * (tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(hidden_biases)) + beta * (tf.nn.l2_loss(hidden_out_weights) + tf.nn.l2_loss(hidden_out_biases))) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_relu = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + valid_prediction = tf.nn.softmax(tf.matmul(valid_relu, hidden_out_weights) + hidden_out_biases) + test_relu = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + test_prediction = tf.nn.softmax(tf.matmul(test_relu, hidden_out_weights) + hidden_out_biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized Neural Network for SGD') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 64 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(biases))) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized Stochastic Gradient Descent decreasing batch size') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + num_neurons = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_neurons])) + hidden_biases = tf.Variable(tf.zeros([num_neurons])) + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + keep_prob = tf.placeholder(tf.float32) + hidden_layer_dropout = tf.nn.dropout(hidden_layer, keep_prob) + hidden_out_weights = tf.Variable(tf.truncated_normal([num_neurons, num_labels])) + hidden_out_biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_layer_dropout, hidden_out_weights) + hidden_out_biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + beta * (tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(hidden_biases)) + beta * (tf.nn.l2_loss(hidden_out_weights) + tf.nn.l2_loss(hidden_out_biases))) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_relu = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + valid_prediction = tf.nn.softmax(tf.matmul(valid_relu, hidden_out_weights) + hidden_out_biases) + test_relu = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + test_prediction = tf.nn.softmax(tf.matmul(test_relu, hidden_out_weights) + hidden_out_biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized Neural Network for SGD adding dropouts in hidden layer') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_neurons = 1024 +start_learning_rate = 0.005 +decay_steps = 1000 +decay_size = 0.95 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_neurons])) + hidden_biases_1 = tf.Variable(tf.zeros([num_neurons])) + hidden_layer_1 = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights_1) + hidden_biases_1) + keep_prob = tf.placeholder(tf.float32) + hidden_layer_dropout_1 = tf.nn.dropout(hidden_layer_1, keep_prob) + hidden_weights_2 = tf.Variable(tf.truncated_normal([num_neurons, num_neurons])) + hidden_biases_2 = tf.Variable(tf.zeros([num_neurons])) + hidden_layer_2 = tf.nn.relu(tf.matmul(hidden_layer_dropout_1, hidden_weights_2) + hidden_biases_2) + hidden_layer_dropout_2 = tf.nn.dropout(hidden_layer_2, keep_prob) + hidden_out_weights = tf.Variable(tf.truncated_normal([num_neurons, num_labels])) + hidden_out_biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_layer_dropout_2, hidden_out_weights) + hidden_out_biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + beta * (tf.nn.l2_loss(hidden_weights_1) + tf.nn.l2_loss(hidden_biases_1)) + beta * (tf.nn.l2_loss(hidden_weights_2) + tf.nn.l2_loss(hidden_biases_2)) + beta * (tf.nn.l2_loss(hidden_out_weights) + tf.nn.l2_loss(hidden_out_biases))) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(start_learning_rate, global_step, decay_steps, decay_size) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_relu_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights_1) + hidden_biases_1) + valid_relu_2 = tf.nn.relu(tf.matmul(valid_relu_1, hidden_weights_2) + hidden_biases_2) + valid_prediction = tf.nn.softmax(tf.matmul(valid_relu_2, hidden_out_weights) + hidden_out_biases) + test_relu_1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights_1) + hidden_biases_1) + test_relu_2 = tf.nn.relu(tf.matmul(test_relu_1, hidden_weights_2) + hidden_biases_2) + test_prediction = tf.nn.softmax(tf.matmul(test_relu_2, hidden_out_weights) + hidden_out_biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized Neural Network for SGD adding dropouts in 2 hidden layer and learing rate') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/143.txt b/dataset/preprocessed/143.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/143.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/144.txt b/dataset/preprocessed/144.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/144.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/145.txt b/dataset/preprocessed/145.txt new file mode 100644 index 0000000..54ba6a7 --- /dev/null +++ b/dataset/preprocessed/145.txt @@ -0,0 +1,65 @@ +import tensorflow as tf +import numpy as np + + +class MainModel(object): + + def __init__(self, sentMaxl, sentMaxp, sentMaxr, num_classes, wv, emb_size, l2_reg_lambda, learning_rate): + tf.reset_default_graph() + self.X_lids = tf.placeholder(tf.int32, [None, sentMaxl], name='X_lids') + self.X_pids = tf.placeholder(tf.int32, [None, sentMaxp], name='X_pids') + self.X_rids = tf.placeholder(tf.int32, [None, sentMaxr], name='X_rids') + self.X_llen = tf.placeholder(tf.int32, [None], name='X_llen') + self.X_plen = tf.placeholder(tf.int32, [None], name='X_plen') + self.X_rlen = tf.placeholder(tf.int32, [None], name='X_rlen') + self.input_y = tf.placeholder(tf.float32, [None, num_classes], name='input_y') + self.dropout_keep_prob = tf.placeholder(tf.float32, name='dropout_keep_prob') + W_wemb = tf.Variable(wv) + print('W_wemb, ', W_wemb.get_shape(), '--, self.X_lids ', self.X_lids.get_shape()) + emb_l = tf.nn.embedding_lookup(W_wemb, self.X_lids) + print('emb_l ', emb_l.get_shape()) + emb_p = tf.nn.embedding_lookup(W_wemb, self.X_pids) + emb_r = tf.nn.embedding_lookup(W_wemb, self.X_rids) + emb_l = tf.expand_dims(emb_l, -1) + print('emb_l expanded, ', emb_l.get_shape()) + emb_p = tf.expand_dims(emb_p, -1) + emb_r = tf.expand_dims(emb_r, -1) + pooled_l = tf.nn.avg_pool(emb_l, ksize=[1, sentMaxl, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='pooll') + pooled_p = tf.nn.avg_pool(emb_p, ksize=[1, sentMaxp, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='poolp') + pooled_r = tf.nn.avg_pool(emb_r, ksize=[1, sentMaxr, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='poolr') + print('pooled_l ', pooled_l.get_shape()) + final_l = tf.reshape(pooled_l, [-1, emb_size]) + final_p = tf.reshape(pooled_p, [-1, emb_size]) + final_r = tf.reshape(pooled_r, [-1, emb_size]) + print('final_l is ', final_l.get_shape()) + XX = tf.concat(1, [final_l, final_p, final_r]) + print(XX.get_shape()) + h = tf.nn.dropout(XX, self.dropout_keep_prob) + h = tf.nn.relu(h) + W = tf.Variable(tf.truncated_normal([3 * emb_size, num_classes], stddev=0.1), name='W') + b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name='b') + scores = tf.nn.xw_plus_b(h, W, b, name='scores') + print('score', scores.get_shape()) + self.predictions = tf.argmax(scores, 1, name='predictions') + losses = tf.nn.softmax_cross_entropy_with_logits(scores, self.input_y) + self.loss = tf.reduce_mean(losses) + l2_reg_lambda * (tf.nn.l2_loss(W) + tf.nn.l2_loss(b)) + self.correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) + self.accuracy = tf.reduce_mean(tf.cast(self.correct_predictions, 'float'), name='accuracy') + self.optimizer = tf.train.AdamOptimizer(learning_rate) + self.grads_and_vars = self.optimizer.compute_gradients(self.loss) + self.global_step = tf.Variable(0, name='global_step', trainable=False) + self.train_op = self.optimizer.apply_gradients(self.grads_and_vars, global_step=self.global_step) + session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) + self.sess = tf.Session(config=session_conf) + self.sess.run(tf.global_variables_initializer()) + self.saver = tf.train.Saver(max_to_keep=50) + + def train_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y, drop_out): + feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: drop_out} + _, step, loss, accuracy, predictions = self.sess.run([self.train_op, self.global_step, self.loss, self.accuracy, self.predictions], feed_dict) + return loss, accuracy + + def test_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y): + feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: 1.0} + accuracy = self.sess.run([self.accuracy], feed_dict) + return accuracy diff --git a/dataset/preprocessed/146.txt b/dataset/preprocessed/146.txt new file mode 100644 index 0000000..15937f9 --- /dev/null +++ b/dataset/preprocessed/146.txt @@ -0,0 +1,175 @@ +import tensorflow as tf +import numpy as np +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + self.rewards = tf.placeholder(tf.float32, shape=(None,), name='reward') + self.indices = tf.placeholder(tf.int32, shape=(None, 2), name='indices') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.prob = tf.nn.softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + self.select_prob = tf.gather_nd(self.prob, self.indices) + self.logprob = tf.log(self.select_prob) + self.result = tf.multiply(self.logprob, self.rewards) + self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + self.pg_optimization = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(self.pg_loss) + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + self.saver = tf.train.Saver() + self.saved_path = './saved_model' + self.count = 0 + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def policy_train(self, session, inputs, outputs, rewards): + form, pos, deprel = inputs + indices = [] + for aid in range(len(outputs)): + indices.append([aid, outputs[aid]]) + _, cost, indices, prob, res = session.run([self.pg_optimization, self.pg_loss, self.indices, self.prob, self.result], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) + self.count += 1 + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + def variable_output(self, session): + form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + def save(self, session): + self.saved_path = self.saver.save(session, './saved_model') + + def restore(self, session): + self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + session.run(self.update_op) diff --git a/dataset/preprocessed/147.txt b/dataset/preprocessed/147.txt new file mode 100644 index 0000000..b43dce8 --- /dev/null +++ b/dataset/preprocessed/147.txt @@ -0,0 +1,141 @@ +import tensorflow as tf +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + self.weights = tf.placeholder(tf.float32, name='weights') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.multiply(self.weights, tf.square(predicted_q - self.output))) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs, weights): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs, self.weights: weights}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) diff --git a/dataset/preprocessed/149.txt b/dataset/preprocessed/149.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/149.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/15.txt b/dataset/preprocessed/15.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/15.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/150.txt b/dataset/preprocessed/150.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/150.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/152.txt b/dataset/preprocessed/152.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/152.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/153.txt b/dataset/preprocessed/153.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/153.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/154.txt b/dataset/preprocessed/154.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/154.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/155.txt b/dataset/preprocessed/155.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/155.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/156.txt b/dataset/preprocessed/156.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/156.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/157.txt b/dataset/preprocessed/157.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/157.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/158.txt b/dataset/preprocessed/158.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/158.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/159.txt b/dataset/preprocessed/159.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/159.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/16.txt b/dataset/preprocessed/16.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/16.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/160.txt b/dataset/preprocessed/160.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/160.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/161.txt b/dataset/preprocessed/161.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/161.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/162.txt b/dataset/preprocessed/162.txt new file mode 100644 index 0000000..45eb784 --- /dev/null +++ b/dataset/preprocessed/162.txt @@ -0,0 +1,465 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import os +from datetime import datetime +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.ops.nn import dynamic_rnn, bidirectional_dynamic_rnn +from evaluate import exact_match_score, f1_score +from util import ConfusionMatrix, Progbar, minibatches, get_minibatches +from defs import LBLS +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.LSTMCell): + + def __init__(self, num_units, encoder_output, scope=None): + self.hs = encoder_output + super(LSTMAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.hs * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.hs * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + return out, tf.nn.rnn_cell.LSTMStateTuple(out, out) + + +class Encoder(object): + + def __init__(self, size, vocab_dim): + self.size = size + self.vocab_dim = vocab_dim + + def length(self, mask): + used = tf.cast(mask, tf.int32) + length = tf.reduce_sum(used, reduction_indices=1) + length = tf.cast(length, tf.int32) + return length + + def encode_questions(self, inputs, masks, encoder_state_input): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + :param inputs: Symbolic representations of your input with shape = (batch_size, length/max_length, embed_size) + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + if encoder_state_input == None: + encoder_state_input = tf.zeros([1, self.size]) + cell_size = self.size + inputs_shape = tf.shape(inputs) + batch_size = inputs_shape[0] + cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.size) + cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.size) + with tf.variable_scope('bi_LSTM'): + outputs, final_state = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, dtype=tf.float32, sequence_length=self.length(masks), inputs=inputs, time_major=False) + final_state_fw = final_state[0].h + final_state_bw = final_state[1].h + final_state = tf.concat(1, [final_state_fw, final_state_bw]) + states = tf.concat(2, outputs) + return final_state, states + + def encode_w_attn(self, inputs, masks, prev_states, scope='', reuse=False): + """ + Run a BiLSTM over the context paragraph conditioned on the question representation. + """ + cell_size = self.size + prev_states_fw, prev_states_bw = tf.split(2, 2, prev_states) + attn_cell_fw = LSTMAttnCell(cell_size, prev_states_fw) + attn_cell_bw = LSTMAttnCell(cell_size, prev_states_bw) + with vs.variable_scope(scope, reuse): + outputs, final_state = tf.nn.bidirectional_dynamic_rnn(attn_cell_fw, attn_cell_bw, dtype=tf.float32, sequence_length=self.length(masks), inputs=inputs, time_major=False) + final_state_fw = final_state[0].h + final_state_bw = final_state[1].h + final_state = tf.concat(1, [final_state_fw, final_state_bw]) + states = tf.concat(2, outputs) + return final_state, states + + +class Decoder(object): + + def __init__(self, output_size): + self.output_size = 2 * output_size + + def match_LASTM(self, questions_states, paragraph_states, question_length, paragraph_length): + cell = tf.nn.rnn_cell.LSTMCell(num_units=self.output_size, state_is_tuple=False) + fw_states = [] + with tf.variable_scope('Forward_Match-LSTM'): + W_q = tf.get_variable('W_q', shape=(self.output_size, self.output_size), initializer=tf.contrib.layers.xavier_initializer()) + W_r = tf.get_variable('W_r', shape=(self.output_size, self.output_size), initializer=tf.contrib.layers.xavier_initializer()) + b_p = tf.get_variable('b_p', shape=self.output_size, initializer=tf.contrib.layers.xavier_initializer()) + w = tf.get_variable('w', shape=(self.output_size, 1), initializer=tf.contrib.layers.xavier_initializer()) + b = tf.get_variable('b', shape=(1, 1), initializer=tf.contrib.layers.xavier_initializer()) + state = tf.zeros([1, self.output_size]) + for time_step in range(paragraph_length): + p_state = paragraph_states[:, (time_step), :] + X_ = tf.reshape(questions_states, [-1, self.output_size]) + G = tf.nn.tanh(tf.matmul(X_, W_q) + tf.matmul(p_state, W_r) + tf.matmul(state, W_r) + b_p) + atten = tf.nn.softmax(tf.matmul(G, w) + b) + atten = tf.reshape(atten, [-1, 1, question_length]) + X_ = tf.reshape(questions_states, [-1, question_length, self.output_size]) + p_z = tf.matmul(atten, X_) + p_z = tf.reshape(p_z, [-1, self.output_size]) + z = tf.concat(1, [p_state, p_z]) + state, o = cell(z, state) + fw_states.append(state) + tf.get_variable_scope().reuse_variables() + fw_states = tf.pack(fw_states) + fw_states = tf.transpose(fw_states, perm=(1, 0, 2)) + cell = tf.nn.rnn_cell.LSTMCell(num_units=self.output_size, state_is_tuple=False) + bk_states = [] + with tf.variable_scope('Backward_Match-LSTM'): + W_q = tf.get_variable('W_q', shape=(self.output_size, self.output_size), initializer=tf.contrib.layers.xavier_initializer()) + W_r = tf.get_variable('W_r', shape=(self.output_size, self.output_size), initializer=tf.contrib.layers.xavier_initializer()) + b_p = tf.get_variable('b_p', shape=self.output_size, initializer=tf.contrib.layers.xavier_initializer()) + w = tf.get_variable('w', shape=(self.output_size, 1), initializer=tf.contrib.layers.xavier_initializer()) + b = tf.get_variable('b', shape=(1, 1), initializer=tf.contrib.layers.xavier_initializer()) + state = tf.zeros([1, self.output_size]) + for time_step in range(paragraph_length): + p_state = paragraph_states[:, (time_step), :] + X_ = tf.reshape(questions_states, [-1, self.output_size]) + G = tf.nn.tanh(tf.matmul(X_, W_q) + tf.matmul(p_state, W_r) + tf.matmul(state, W_r) + b_p) + atten = tf.nn.softmax(tf.matmul(G, w) + b) + atten = tf.reshape(atten, [-1, 1, question_length]) + X_ = tf.reshape(questions_states, [-1, question_length, self.output_size]) + p_z = tf.matmul(atten, X_) + p_z = tf.reshape(p_z, [-1, self.output_size]) + z = tf.concat(1, [p_state, p_z]) + state, o = cell(z, state) + bk_states.append(state) + tf.get_variable_scope().reuse_variables() + bk_states = tf.pack(bk_states) + bk_states = tf.transpose(bk_states, perm=(1, 0, 2)) + knowledge_rep = tf.concat(2, [fw_states, bk_states]) + return knowledge_rep + + def decode(self, knowledge_rep, paragraph_length): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + output_size = self.output_size + cell = tf.nn.rnn_cell.LSTMCell(num_units=output_size, state_is_tuple=False) + beta_s = [] + with tf.variable_scope('Boundary-LSTM_start'): + V = tf.get_variable('V', shape=(2 * output_size, output_size), initializer=tf.contrib.layers.xavier_initializer()) + b_a = tf.get_variable('b_a', shape=(1, output_size), initializer=tf.contrib.layers.xavier_initializer()) + W_a = tf.get_variable('W_a', shape=(output_size, output_size), initializer=tf.contrib.layers.xavier_initializer()) + c = tf.get_variable('c', shape=(1, 1), initializer=tf.contrib.layers.xavier_initializer()) + v = tf.get_variable('v', shape=(output_size, 1), initializer=tf.contrib.layers.xavier_initializer()) + state = tf.zeros([1, output_size]) + for time_step in range(paragraph_length): + H_r = tf.reshape(knowledge_rep, [-1, 2 * output_size]) + F_s = tf.nn.tanh(tf.matmul(H_r, V) + tf.matmul(state, W_a) + b_a) + probab_s = tf.reshape(tf.nn.softmax(tf.matmul(F_s, v) + c), shape=[-1, paragraph_length]) + beta_s.append(probab_s) + z = tf.matmul(probab_s, H_r) + state, _ = cell(z, state, scope='Boundary-LSTM_start') + tf.get_variable_scope().reuse_variables() + beta_s = tf.pack(beta_s) + beta_s = tf.transpose(beta_s, perm=(1, 0, 2)) + beta_e = [] + with tf.variable_scope('Boundary-LSTM_end'): + cell = tf.nn.rnn_cell.LSTMCell(num_units=output_size, state_is_tuple=False) + V = tf.get_variable('V', shape=(2 * output_size, output_size), initializer=tf.contrib.layers.xavier_initializer()) + b_a = tf.get_variable('b_a', shape=(1, output_size), initializer=tf.contrib.layers.xavier_initializer()) + W_a = tf.get_variable('W_a', shape=(output_size, output_size), initializer=tf.contrib.layers.xavier_initializer()) + c = tf.get_variable('c', shape=(1, 1), initializer=tf.contrib.layers.xavier_initializer()) + v = tf.get_variable('v', shape=(output_size, 1), initializer=tf.contrib.layers.xavier_initializer()) + state = tf.zeros([1, output_size]) + for time_step in range(paragraph_length): + H_r = tf.reshape(knowledge_rep, [-1, 2 * output_size]) + F_e = tf.nn.tanh(tf.matmul(H_r, V) + tf.matmul(state, W_a) + b_a) + probab_e = tf.reshape(tf.nn.softmax(tf.matmul(F_e, v) + c), shape=[-1, paragraph_length]) + beta_e.append(probab_e) + z = tf.matmul(probab_e, H_r) + state, _ = cell(z, state, scope='Boundary-LSTM_start') + tf.get_variable_scope().reuse_variables() + beta_e = tf.pack(beta_e) + beta_e = tf.transpose(beta_e, perm=(1, 0, 2)) + return beta_s, beta_e + + +class QASystem(object): + + def __init__(self, encoder, decoder, args, pretrained_embeddings): + """ + Initializes your System + :param encoder: an encoder that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + self.encoder = encoder + self.decoder = decoder + self.config = args + self.pretrained_embeddings = pretrained_embeddings + self.p_max_length = self.config.paragraph_size + self.embed_size = encoder.vocab_dim + self.q_max_length = self.config.question_size + self.q_placeholder = tf.placeholder(tf.int32, (None, self.q_max_length)) + self.p_placeholder = tf.placeholder(tf.int32, (None, self.p_max_length)) + self.start_labels_placeholder = tf.placeholder(tf.int32, (None, self.p_max_length)) + self.end_labels_placeholder = tf.placeholder(tf.int32, (None, self.p_max_length)) + self.q_mask_placeholder = tf.placeholder(tf.bool, (None, self.q_max_length)) + self.p_mask_placeholder = tf.placeholder(tf.bool, (None, self.p_max_length)) + self.dropout_placeholder = tf.placeholder(tf.float32, ()) + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.preds = self.decoder.decode(self.knowledge_rep, self.p_max_length) + self.loss = self.setup_loss(self.preds) + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + encoded_q, self.q_states = self.encoder.encode_questions(self.q_embeddings, self.q_mask_placeholder, None) + encoded_p, self.p_states = self.encoder.encode_w_attn(self.p_embeddings, self.p_mask_placeholder, self.q_states, scope='', reuse=False) + self.knowledge_rep = self.decoder.match_LASTM(self.q_states, self.p_states, self.q_max_length, self.p_max_length) + + def setup_loss(self, preds): + """ + Set up your loss computation here + :return: + """ + preds = np.array(preds) + with vs.variable_scope('start_index_loss'): + loss_tensor = tf.boolean_mask(tf.nn.sparse_softmax_cross_entropy_with_logits(preds[:, (0)], self.start_labels_placeholder), self.p_mask_placeholder) + start_index_loss = tf.reduce_mean(loss_tensor, 0) + with vs.variable_scope('end_index_loss'): + loss_tensor = tf.boolean_mask(tf.nn.sparse_softmax_cross_entropy_with_logits(preds[:, (1)], self.end_labels_placeholder), self.p_mask_placeholder) + end_index_loss = tf.reduce_mean(loss_tensor, 0) + self.loss = [start_index_loss, end_index_loss] + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + self.pretrained_embeddings = tf.Variable(self.pretrained_embeddings, trainable=False, dtype=tf.float32) + q_embeddings = tf.nn.embedding_lookup(self.pretrained_embeddings, self.q_placeholder) + self.q_embeddings = tf.reshape(q_embeddings, shape=[-1, self.config.question_size, 1 * self.embed_size]) + p_embeddings = tf.nn.embedding_lookup(self.pretrained_embeddings, self.p_placeholder) + self.p_embeddings = tf.reshape(p_embeddings, shape=[-1, self.config.paragraph_size, 1 * self.embed_size]) + + def optimize(self, session, dataset, mask, dropout=1): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + if train_x is not None: + input_feed[self.q_placeholder] = dataset['Questions'] + input_feed[self.p_placeholder] = dataset['Paragraphs'] + if train_y is not None: + input_feed[self.start_labels_placeholder] = dataset['Labels'][:, (0)] + input_feed[self.end_labels_placeholder] = dataset['Labels'][:, (1)] + if mask is not None: + input_feed[self.q_mask_placeholder] = dataset['Questions_masks'] + input_feed[self.p_mask_placeholder] = dataset['Paragraphs_masks'] + input_feed[self.dropout_placeholder] = dropout + output_feed = [] + train_op_start = tf.train.AdamOptimizer(self.config.learning_rate).minimize(self.start_index_loss) + output_feed = [train_op_start, self.start_index_loss] + start_index_pred = session.run(output_feed, input_feed) + train_op_end = tf.train.AdamOptimizer(self.config.learning_rate).minimize(self.end_index_loss) + output_feed = [train_op_end, self.end_index_loss] + end_index_pred = session.run(output_feed, input_feed) + return start_index_loss, end_index_loss + + def test(self, session, valid_x, valid_y): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + output_feed = [] + outputs = session.run(output_feed, input_feed) + return outputs + + def decode(self, session, train_x, mask): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + if train_x is not None: + input_feed[self.q_placeholder] = train_x['Questions'] + input_feed[self.p_placeholder] = train_x['Paragraphs'] + if mask is not None: + input_feed[self.q_mask_placeholder] = train_x['Questions_masks'] + input_feed[self.p_mask_placeholder] = train_x['Paragraphs_masks'] + output_feed = [self.preds] + outputs = session.run(output_feed, input_feed) + return outputs + + def create_feed_dict(self, question_batch, context_batch, labels_batch=None): + """Creates the feed_dict for the model. + NOTE: You do not have to do anything here. + """ + feed_dict = {} + feed_dict[self.q_placeholder] = question_batch + feed_dict[self.p_placeholder] = context_batch + if labels_batch is not None: + feed_dict[self.start_labels_placeholder] = labels_batch[0] + feed_dict[self.end_labels_placeholder] = labels_batch[1] + return feed_dict + + def train_on_batch(self, session, question_batch, context_batch, label_batch): + feed_dict = self.create_feed_dict(question_batch, context_batch, label_batch) + _, loss = session.run([self.train_op, self.loss], feed_dict=feed_dict) + return loss + + def run_epoch(self, sess, inputs): + """Runs an epoch of training. + Args: + sess: tf.Session() object + inputs: datasets represented as a dictionary + labels: np.ndarray of shape (n_samples, n_classes) + Returns: + average_loss: scalar. Average minibatch loss of model on epoch. + """ + n_minibatches, total_loss = 0, 0 + for [question_batch, context_batch, labels_batch] in get_minibatches([inputs['Questions'], inputs['Paragraphs'], inputs['Labels']], self.config.batch_size): + n_minibatches += 1 + total_loss += self.train_on_batch(sess, question_batch, context_batch, labels_batch) + return total_loss / n_minibatches + + def answer(self, session, test_x, mask): + yp, yp2 = self.decode(session, test_x, mask) + a_s = np.argmax(yp, axis=1) + a_e = np.argmax(yp2, axis=1) + return a_s, a_e + + def validate(self, sess, valid_dataset): + """ + Iterate through the validation dataset and determine what + the validation cost is. + This method calls self.test() which explicitly calculates validation cost. + How you implement this function is dependent on how you design + your data iteration function + :return: + """ + valid_cost = 0 + for valid_x, valid_y in valid_dataset: + valid_cost = self.test(sess, valid_x, valid_y) + return valid_cost + + def evaluate_answer(self, session, dataset, sample=100, log=False): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + idx_sample = np.random.randint(0, dataset['Questions'].shape[0], sample) + examples = {} + examples['Questions'] = dataset['Questions'][idx_sample] + examples['Paragraphs'] = dataset['Paragraphs'][idx_sample] + examples['Questions_masks'] = dataset['Questions'][idx_sample] + examples['Paragraphs_masks'] = dataset['Paragraphs'][idx_sample] + examples['Labels'] = dataset['Labels'][idx_sample] + correct_preds, total_correct, total_preds = 0.0, 0.0, 0.0 + masks = True + for _, labels, labels_ in self.answer(session, examples, masks): + pred = set() + if labels_[0] <= labels_[1]: + pred = set(range(labels_[0], labels_[1] + 1)) + gold = set(range(labels[0], labels[1] + 1)) + correct_preds += len(gold.intersection(pred)) + total_preds += len(pred) + total_correct += len(gold) + p = correct_preds / total_preds if correct_preds > 0 else 0 + r = correct_preds / total_correct if correct_preds > 0 else 0 + f1 = 2 * p * r / (p + r) if correct_preds > 0 else 0 + em = correct_preds + if log: + logging.info('F1: {}, EM: {}, for {} samples'.format(f1, em, sample)) + return f1, em + + def train(self, session, dataset, train_dir): + """ + Implement main training loop + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + More ambitious appoarch can include implement early stopping, or reload + previous models if they have higher performance than the current one + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + results_path = os.path.join(train_dir, '{:%Y%m%d_%H%M%S}'.format(datetime.now())) + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + best_score = 0.0 + for epoch in range(self.config.epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.config.epochs) + logging.info('Best score so far: ' + str(best_score)) + loss = self.run_epoch(session, dataset) + f1, em = self.evaluate_answer(session, dataset, sample=800, log=True) + logging.info('loss: ' + str(loss) + ' f1: ' + str(f1) + ' em:' + str(em)) + if f1 > best_score: + best_score = f1 + logging.info('New best score! Saving model in %s', results_path) + if self.saver: + self.saver.save(session, results_path) + print('') + return best_score diff --git a/dataset/preprocessed/163.txt b/dataset/preprocessed/163.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/163.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/164.txt b/dataset/preprocessed/164.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/164.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/165.txt b/dataset/preprocessed/165.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/165.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/166.txt b/dataset/preprocessed/166.txt new file mode 100644 index 0000000..cff092f --- /dev/null +++ b/dataset/preprocessed/166.txt @@ -0,0 +1,148 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +image_size = 28 +num_labels = 10 +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + layer1_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + layer1_biases = tf.Variable(tf.zeros([hidden_nodes])) + layer2_weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + layer2_biases = tf.Variable(tf.zeros([num_labels])) + regularizers = tf.nn.l2_loss(layer1_weights) + tf.nn.l2_loss(layer1_biases) + tf.nn.l2_loss(layer2_weights) + tf.nn.l2_loss(layer2_biases) + + def model(data): + hidden = tf.nn.relu(tf.matmul(data, layer1_weights) + layer1_biases) + return tf.matmul(hidden, layer2_weights) + layer2_biases + logits = model(tf_train_dataset) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss += 0.001 * regularizers + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + test_prediction = tf.nn.softmax(model(tf_test_dataset)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 5 +train_dataset_small = train_dataset[:batch_size * num_steps] +train_labels_small = train_labels[:batch_size * num_steps] +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels_small.shape[0] - batch_size) + batch_data = train_dataset_small[offset:offset + batch_size, :] + batch_labels = train_labels_small[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +image_size = 28 +num_labels = 10 +batch_size = 128 +l1_hidden_nodes = 2048 +l2_hidden_nodes = 64 +l3_hidden_nodes = 128 +num_steps = 4001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_keep_prob = tf.placeholder('float') + global_step = tf.Variable(0) + layer1_weights = tf.Variable(tf.truncated_normal([image_size * image_size, l1_hidden_nodes])) + layer1_biases = tf.Variable(tf.zeros([l1_hidden_nodes])) + layer2_weights = tf.Variable(tf.truncated_normal([l1_hidden_nodes, l2_hidden_nodes])) + layer2_biases = tf.Variable(tf.zeros([l2_hidden_nodes])) + layer3_weights = tf.Variable(tf.truncated_normal([l2_hidden_nodes, l3_hidden_nodes])) + layer3_biases = tf.Variable(tf.zeros([l3_hidden_nodes])) + layer4_weights = tf.Variable(tf.truncated_normal([l1_hidden_nodes, num_labels])) + layer4_biases = tf.Variable(tf.zeros([num_labels])) + regularizers = tf.nn.l2_loss(layer1_weights) + tf.nn.l2_loss(layer1_biases) + tf.nn.l2_loss(layer2_weights) + tf.nn.l2_loss(layer2_biases) + tf.nn.l2_loss(layer3_weights) + tf.nn.l2_loss(layer3_biases) + tf.nn.l2_loss(layer4_weights) + tf.nn.l2_loss(layer4_biases) + + def model(data): + l1 = tf.nn.relu(tf.matmul(data, layer1_weights) + layer1_biases) + d1 = tf.nn.dropout(l1, tf_keep_prob) + return tf.matmul(d1, layer4_weights) + layer4_biases + logits = model(tf_train_dataset) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss += 0.001 * regularizers + learning_rate = tf.train.exponential_decay(0.1, global_step, 100, 0.99) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + test_prediction = tf.nn.softmax(model(tf_test_dataset)) +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + batch_mask = np.random.randint(train_labels.shape[0], size=batch_size) + batch_data = train_dataset[(batch_mask), :] + batch_labels = train_labels[(batch_mask), :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(feed_dict={tf_keep_prob: 1.0}), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(feed_dict={tf_keep_prob: 1.0}), test_labels)) diff --git a/dataset/preprocessed/167.txt b/dataset/preprocessed/167.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/167.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/168.txt b/dataset/preprocessed/168.txt new file mode 100644 index 0000000..7af294b --- /dev/null +++ b/dataset/preprocessed/168.txt @@ -0,0 +1,331 @@ +""" +@Author:Charles Van +@E-mail: williananjhon@hotmail.com +@Time:2019/5/14 10:53 +@Project:CTR +@Filename:models.py +""" +import sys +if sys.version[0] == '2': + import cProfile as pkl +else: + import pickle as pkl +import numpy as np +import tensorflow as tf +import utils +dtype = utils.DTYPE + + +class model: + + def __init__(self): + self.sess = None + self.X = None + self.y = None + self.layer_keeps = None + self.vars = None + self.keep_prob_train = None + self.keep_prob_test = None + + def run(self, fetches, X=None, y=None, mode='train'): + feed_dict = {} + if type(self.X) is list: + for i in range(len(X)): + feed_dict[self.X[i]] = X[i] + else: + feed_dict[self.X] = X + if y is not None: + feed_dict[self.y] = y + if self.layer_keeps is not None: + if mode == 'train': + feed_dict[self.layer_keeps] = self.keep_prob_train + elif mode == 'test': + feed_dict[self.layer_keeps] = self.keep_prob_test + return self.sess.run(fetches, feed_dict) + + def dump(self, model_path): + var_map = {} + for name, var in self.vars.iteritems(): + var_map[name] = self.run(var) + pkl.dump(var_map, open(model_path, 'wb')) + print('model dumped at', model_path) + + +class LR(model): + + def __init__(self, input_dim=None, output_dim=1, init_path=None, opt_algo='gd', learning_rate=0.01, l2_weight=0, random_seed=None): + model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + b = self.vars['b'] + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=logits)) + l2_weight * tf.nn.l2_loss(xw) + self, optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FM(Model): + + def __init__(self, input_dim=None, output_dim=1, factor_order=10, init_path=None, opt_algo='gd', learning_rate=0.01, l2_w=0, l2_v=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('v', [input_dim, factor_order], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + v = self.vars['v'] + b = self.vars['b'] + X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + l2_w * tf.nn.l2_loss(xw) + l2_v * tf.nn.l2_loss(xv) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FNN(model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + factor_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = factor_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + for i in range(2, len(layer_sizes) - 1): + layer_input = layer_sizes[i] + layer_output = layer_sizes[i + 1] + init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class CCPM(model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + embedding_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = embedding_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('f1', [embedding_order, layer_sizes[2], 1, 2], 'tnormal', dtype)) + init_vars.append(('f2', [embedding_order, layer_sizes[3], 2, 2], 'tnormal', dtype)) + init_vars.append(('w1', [2 * 3 * embedding_order, 1], 'tnormal', dtype)) + init_vars.append(('b1', [1], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + l = tf.nn.dropout(utils.activate(tf.concat([(tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) + b0[i]) for i in range(num_inputs)], 1), layer_acts[0]), self.layer_keeps[0]) + l = tf.transpose(tf.reshape(l, [-1, num_inputs, embedding_order, 1]), [0, 2, 1, 3]) + f1 = self.vars['f1'] + l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), num_inputs / 2), [0, 1, 3, 2]) + f2 = self.vars['f2'] + l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embedding_order * 3 * 2]), layer_acts[1]), self.layer_keeps[1]) + w1 = self.vars['w1'] + b1 = self.vars['b1'] + l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1, layer_acts[2]), self.layer_keeps[2]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN1(model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, layer_l2=None, kernel_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + factor_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = factor_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('k1', [num_inputs, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + for i in range(2, len(layer_sizes) - 1): + layer_input = layer_sizes[i] + layer_output = layer_sizes[i + 1] + init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + k1 = self.vars['k1'] + b1 = self.vars['b1'] + p = tf.reduce_sum(tf.reshape(tf.matmul(tf.reshape(tf.transpose(tf.reshape(l, [-1, num_inputs, factor_order]), [0, 2, 1]), [-1, num_inputs]), k1), [-1, factor_order, layer_sizes[2]]), 1) + l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1 + p, layer_acts[1]), self.layer_keeps[1]) + for i in range(2, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + if kernel_l2 is not None: + self.loss += kernel_l2 * tf.nn.l2_loss(k1) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN2(model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, layer_l2=None, kernel_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + factor_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = factor_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('k1', [factor_order * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + for i in range(2, len(layer_sizes) - 1): + layer_input = layer_sizes[i] + layer_output = layer_sizes[i + 1] + init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + k1 = self.vars['k1'] + b1 = self.vars['b1'] + z = tf.reduce_sum(tf.reshape(l, [-1, num_inputs, factor_order]), 1) + p = tf.reshape(tf.matmul(tf.reshape(z, [-1, factor_order, 1]), tf.reshape(z, [-1, 1, factor_order])), [-1, factor_order * factor_order]) + l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + tf.matmul(p, k1) + b1, layer_acts[1]), self.layer_keeps[1]) + for i in range(2, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + if kernel_l2 is not None: + self.loss += kernel_l2 * tf.nn.l2_loss(k1) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) diff --git a/dataset/preprocessed/169.txt b/dataset/preprocessed/169.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/169.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/170.txt b/dataset/preprocessed/170.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/170.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/171.txt b/dataset/preprocessed/171.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/171.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/172.txt b/dataset/preprocessed/172.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/172.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/173.txt b/dataset/preprocessed/173.txt new file mode 100644 index 0000000..77b11fa --- /dev/null +++ b/dataset/preprocessed/173.txt @@ -0,0 +1,264 @@ +import os, sys +sys.path.append(os.pardir) +from savefig import * +import tensorflow as tf +reset_graph() +n_inputs = 3 +n_neurons = 5 +X0 = tf.placeholder(tf.float32, [None, n_inputs]) +X1 = tf.placeholder(tf.float32, [None, n_inputs]) +Wx = tf.Variable(tf.random_normal(shape=[n_inputs, n_neurons], dtype=tf.float32)) +Wy = tf.Variable(tf.random_normal(shape=[n_neurons, n_neurons], dtype=tf.float32)) +b = tf.Variable(tf.zeros([1, n_neurons], dtype=tf.float32)) +Y0 = tf.tanh(tf.matmul(X0, Wx) + b) +Y1 = tf.tanh(tf.matmul(Y0, Wy) + tf.matmul(X1, Wx) + b) +init = tf.global_variables_initializer() +import numpy as np +X0_batch = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 1]]) +X1_batch = np.array([[9, 8, 7], [0, 0, 0], [6, 5, 4], [3, 2, 1]]) +with tf.Session() as sess: + init.run() + Y0_val, Y1_val = sess.run([Y0, Y1], feed_dict={X0: X0_batch, X1: X1_batch}) +n_inputs = 3 +n_neurons = 5 +reset_graph() +X0 = tf.placeholder(tf.float32, [None, n_inputs]) +X1 = tf.placeholder(tf.float32, [None, n_inputs]) +basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) +output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell, [X0, X1], dtype=tf.float32) +Y0, Y1 = output_seqs +init = tf.global_variables_initializer() +X0_batch = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 1]]) +X1_batch = np.array([[9, 8, 7], [0, 0, 0], [6, 5, 4], [3, 2, 1]]) +with tf.Session() as sess: + init.run() + Y0_val, Y1_val = sess.run([Y0, Y1], feed_dict={X0: X0_batch, X1: X1_batch}) +n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +X_seqs = tf.unstack(tf.transpose(X, perm=[1, 0, 2])) +basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) +output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell, X_seqs, dtype=tf.float32) +outputs = tf.transpose(tf.stack(output_seqs), perm=[1, 0, 2]) +init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +print(outputs_val) +print(np.transpose(outputs_val, axes=[1, 0, 2])[1]) +n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) +init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +print(outputs_val) +n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) +seq_length = tf.placeholder(tf.int32, [None]) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32, sequence_length=seq_length) +init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +seq_length_batch = np.array([2, 1, 2, 2]) +with tf.Session() as sess: + init.run() + outputs_val, states_val = sess.run([outputs, states], feed_dict={X: X_batch, seq_length: seq_length_batch}) +print(outputs_val) +print(states_val) +reset_graph() +n_steps = 28 +n_inputs = 28 +n_neurons = 150 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) +logits = tf.layers.dense(states, n_outputs) +xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits) +loss = tf.reduce_mean(xentropy) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('/tmp/data/') +X_test = mnist.test.images.reshape((-1, n_steps, n_inputs)) +y_test = mnist.test.labels +n_epochs = 100 +batch_size = 150 +with tf.Session() as sess: + init.run() + for epoch in range(n_epochs): + for iteration in range(mnist.train.num_examples // batch_size): + X_batch, y_batch = mnist.train.next_batch(batch_size) + X_batch = X_batch.reshape((-1, n_steps, n_inputs)) + sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) + acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch}) + acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test}) + print(epoch, 'Train accuracy:', acc_train, 'Test accuracy:', acc_test) +reset_graph() +n_steps = 28 +n_inputs = 28 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +n_neurons = 100 +n_layers = 3 +layers = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu) for layer in range(n_layers)] +multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers) +outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32) +states_concat = tf.concat(axis=1, values=states) +logits = tf.layers.dense(states_concat, n_outputs) +xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits) +loss = tf.reduce_mean(xentropy) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +n_epochs = 10 +batch_size = 150 +with tf.Session() as sess: + init.run() + for epoch in range(n_epochs): + for iteration in range(mnist.train.num_examples // batch_size): + X_batch, y_batch = mnist.train.next_batch(batch_size) + X_batch = X_batch.reshape((-1, n_steps, n_inputs)) + sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) + acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch}) + acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test}) + print(epoch, 'Train accuracy:', acc_train, 'Test accuracy:', acc_test) +t_min, t_max = 0, 30 +resolution = 0.1 + + +def time_series(t): + return t * np.sin(t) / 3 + 2 * np.sin(t * 5) + + +def next_batch(batch_size, n_steps): + t0 = np.random.rand(batch_size, 1) * (t_max - t_min - n_steps * resolution) + Ts = t0 + np.arange(0.0, n_steps + 1) * resolution + ys = time_series(Ts) + return ys[:, :-1].reshape(-1, n_steps, 1), ys[:, 1:].reshape(-1, n_steps, 1) + + +t = np.linspace(t_min, t_max, int((t_max - t_min) / resolution)) +n_steps = 20 +t_instance = np.linspace(12.2, 12.2 + resolution * (n_steps + 1), n_steps + 1) +plt.figure(figsize=(11, 4)) +plt.subplot(121) +plt.title('A time series (generated)', fontsize=14) +plt.plot(t, time_series(t), label='$t . \\sin(t) / 3 + 2 . \\sin(5t)$') +plt.plot(t_instance[:-1], time_series(t_instance[:-1]), 'b-', linewidth=3, label='A training instance') +plt.legend(loc='lower left', fontsize=14) +plt.axis([0, 30, -17, 13]) +plt.xlabel('Time') +plt.ylabel('Value') +plt.subplot(122) +plt.title('A training instance', fontsize=14) +plt.plot(t_instance[:-1], time_series(t_instance[:-1]), 'bo', markersize=10, label='instance') +plt.plot(t_instance[1:], time_series(t_instance[1:]), 'w*', markersize=10, label='target') +plt.legend(loc='upper left') +plt.xlabel('Time') +plt.show() +X_batch, y_batch = next_batch(1, n_steps) +np.c_[X_batch[0], y_batch[0]] +reset_graph() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = tf.contrib.rnn.OutputProjectionWrapper(tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu), output_size=n_outputs) +outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) +learning_rate = 0.001 +loss = tf.reduce_mean(tf.square(outputs - y)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +training_op = optimizer.minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver() +n_iterations = 1500 +batch_size = 50 +with tf.Session() as sess: + init.run() + for iteration in range(n_iterations): + X_batch, y_batch = next_batch(batch_size, n_steps) + sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) + if iteration % 100 == 0: + mse = loss.eval(feed_dict={X: X_batch, y: y_batch}) + print(iteration, '\tMSE:', mse) + saver.save(sess, './my_time_series_model') +with tf.Session() as sess: + saver.restore(sess, './my_time_series_model') + X_new = time_series(np.array(t_instance[:-1].reshape(-1, n_steps, n_inputs))) + y_pred = sess.run(outputs, feed_dict={X: X_new}) +plt.title('Testing the model', fontsize=14) +plt.plot(t_instance[:-1], time_series(t_instance[:-1]), 'bo', markersize=10, label='instance') +plt.plot(t_instance[1:], time_series(t_instance[1:]), 'w*', markersize=10, label='target') +plt.plot(t_instance[1:], y_pred[(0), :, (0)], 'r.', markersize=10, label='prediction') +plt.legend(loc='upper left') +plt.xlabel('Time') +plt.show() +reset_graph() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu) +rnn_outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) +n_outputs = 1 +learning_rate = 0.001 +stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons]) +stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs) +outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs]) +loss = tf.reduce_mean(tf.square(outputs - y)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +training_op = optimizer.minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver() +n_iterations = 1500 +batch_size = 50 +with tf.Session() as sess: + init.run() + for iteration in range(n_iterations): + X_batch, y_batch = next_batch(batch_size, n_steps) + sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) + if iteration % 100 == 0: + mse = loss.eval(feed_dict={X: X_batch, y: y_batch}) + print(iteration, '\tMSE:', mse) + X_new = time_series(np.array(t_instance[:-1].reshape(-1, n_steps, n_inputs))) + y_pred = sess.run(outputs, feed_dict={X: X_new}) + saver.save(sess, './my_time_series_model') +with tf.Session() as sess: + saver.restore(sess, './my_time_series_model') + sequence = [0.0] * n_steps + for iteration in range(300): + X_batch = np.array(sequence[-n_steps:]).reshape(1, n_steps, 1) + y_pred = sess.run(outputs, feed_dict={X: X_batch}) + sequence.append(y_pred[0, -1, 0]) +plt.figure(figsize=(8, 4)) +plt.plot(np.arange(len(sequence)), sequence, 'b-') +plt.plot(t[:n_steps], sequence[:n_steps], 'b-', linewidth=3) +plt.xlabel('Time') +plt.ylabel('Value') +plt.show() diff --git a/dataset/preprocessed/174.txt b/dataset/preprocessed/174.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/174.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/175.txt b/dataset/preprocessed/175.txt new file mode 100644 index 0000000..e8c5d97 --- /dev/null +++ b/dataset/preprocessed/175.txt @@ -0,0 +1,83 @@ +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from SpeechDataUtils import SpeechDataSet +timit_database = TimitDatabase('G:\\TIMIT') +max_input_sequence_length = timit_database.get_max_mfcc_features_length() +max_output_sequence_length = timit_database.get_max_phonemes_length() +dictionary_size = timit_database.phonemes_dictionary_size +train_data = timit_database.train_dataset +eval_data = timit_database.test_dataset +input_placeholder = tf.placeholder(tf.float32, shape=[None, max_input_sequence_length, 40]) +input_lengths_placeholder = tf.placeholder(tf.int32, shape=[None]) +output_placeholder = tf.sparse_placeholder(tf.int32) +output_lengths_placeholder = tf.placeholder(tf.int32, shape=[None]) +cell = tf.nn.rnn_cell.LSTMCell(128) +cell = tf.nn.rnn_cell.MultiRNNCell([cell] * 2) +rnn, _ = tf.nn.dynamic_rnn(cell, input_placeholder, input_lengths_placeholder, dtype=tf.float32) +rnn = tf.reshape(rnn, shape=[-1, max_input_sequence_length * 128]) +weights = tf.Variable(tf.random_normal(shape=[max_input_sequence_length * 128, 256], stddev=0.1)) +biases = tf.Variable(tf.zeros(shape=[256])) +layer = tf.matmul(rnn, weights) + biases +layer = tf.nn.relu(layer) +weights = tf.Variable(tf.random_normal(shape=[256, max_output_sequence_length * dictionary_size], stddev=0.1)) +biases = tf.Variable(tf.zeros(shape=[max_output_sequence_length * dictionary_size])) +layer = tf.matmul(layer, weights) + biases +layer = tf.nn.relu(layer) +logits = tf.reshape(layer, shape=[-1, max_output_sequence_length, dictionary_size]) +logits_timemajor = tf.transpose(logits, [1, 0, 2]) +loss = tf.nn.ctc_loss(logits_timemajor, output_placeholder, output_lengths_placeholder) +loss = tf.reduce_mean(loss) +optimizer = tf.train.AdamOptimizer() +train = optimizer.minimize(loss) +session_config = tf.ConfigProto() +session_config.gpu_options.allow_growth = True +with tf.Session(config=session_config) as session: + init = tf.global_variables_initializer() + session.run(init) + for i in tqdm(range(1000)): + batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(11, False) + tmp = [] + for j in range(len(batch_outputs)): + tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + batch_outputs = np.array(tmp) + batch_outputs = SpeechDataSet.tokens_for_sparse(batch_outputs) + feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths, output_placeholder: batch_outputs, output_lengths_placeholder: batch_output_lengths} + session.run(train, feed_dict=feed_dict) + if i % 100 == 0: + print('######') + print(session.run(loss, feed_dict=feed_dict)) + print('######') + batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(1, False) + feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths} + test = session.run(logits, feed_dict=feed_dict) + test = session.run(tf.transpose(test, [1, 0, 2])) + decoded, prob = tf.nn.ctc_beam_search_decoder(test, batch_output_lengths, 100, 5) + decoded = session.run(decoded) + prob = session.run(prob) + results = [] + for decoded_path in decoded: + phonemes_ids = decoded_path.values + result_words = '' + for idx in phonemes_ids: + word = timit_database.id_to_phoneme_dictionary[idx] + if word != '': + result_words += word + ' ' + results += [result_words] + target = batch_outputs[0] + target_words = '' + for idx in target: + word = timit_database.id_to_phoneme_dictionary[idx] + if word != '': + target_words += word + ' ' + print('--------------') + print('--------------') + print('--------------') + print('Target : \n', target_words, '\n') + print('Results') + for result_sentence in results: + print(result_sentence) + print('--------------') + print('--------------') + print('--------------') diff --git a/dataset/preprocessed/176.txt b/dataset/preprocessed/176.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/176.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/177.txt b/dataset/preprocessed/177.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/177.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/178.txt b/dataset/preprocessed/178.txt new file mode 100644 index 0000000..73abe47 --- /dev/null +++ b/dataset/preprocessed/178.txt @@ -0,0 +1,384 @@ +import os +import getpass +import sys +import time +import numpy as np +import tensorflow as tf +from q2_initialization import xavier_weight_init +import data_utils.utils as du +import data_utils.ner as ner +from utils import data_iterator +from model import LanguageModel + + +def variable_summaries(variable, name): + with tf.name_scope('summaries'): + mean = tf.reduce_mean(variable) + tf.summary.scalar('mean/' + name, mean) + with tf.name_scope('stddev'): + stddev = tf.sqrt(tf.reduce_sum(tf.square(variable - mean))) + tf.summary.scalar('stddev/' + name, stddev) + tf.summary.scalar('max/' + name, tf.reduce_max(variable)) + tf.summary.scalar('min/' + name, tf.reduce_min(variable)) + tf.summary.histogram(name, variable) + + +class Config(object): + """Holds model hyperparams and data information. + + The config class is used to store various hyperparameters and dataset + information parameters. Model objects are passed a Config() object at + instantiation. + """ + embed_size = 50 + batch_size = 64 + label_size = 5 + hidden_size = 100 + max_epochs = 10 + early_stopping = 5 + dropout = 0.9 + lr = 0.001 + l2 = 0.001 + window_size = 3 + + +class NERModel(LanguageModel): + """Implements a NER (Named Entity Recognition) model. + + This class implements a deep network for named entity recognition. It + inherits from LanguageModel, which has an add_embedding method in addition to + the standard Model method. + """ + + def load_data(self, debug=False): + """Loads starter word-vectors and train/dev/test data.""" + self.wv, word_to_num, num_to_word = ner.load_wv('data/ner/vocab.txt', 'data/ner/wordVectors.txt') + tagnames = ['O', 'LOC', 'MISC', 'ORG', 'PER'] + self.num_to_tag = dict(enumerate(tagnames)) + tag_to_num = {v: k for k, v in self.num_to_tag.items()} + docs = du.load_dataset('data/ner/train') + self.X_train, self.y_train = du.docs_to_windows(docs, word_to_num, tag_to_num, wsize=self.config.window_size) + if debug: + self.X_train = self.X_train[:1024] + self.y_train = self.y_train[:1024] + docs = du.load_dataset('data/ner/dev') + self.X_dev, self.y_dev = du.docs_to_windows(docs, word_to_num, tag_to_num, wsize=self.config.window_size) + if debug: + self.X_dev = self.X_dev[:1024] + self.y_dev = self.y_dev[:1024] + docs = du.load_dataset('data/ner/test.masked') + self.X_test, self.y_test = du.docs_to_windows(docs, word_to_num, tag_to_num, wsize=self.config.window_size) + + def add_placeholders(self): + """Generate placeholder variables to represent the input tensors + + These placeholders are used as inputs by the rest of the model building + code and will be fed data during training. Note that when "None" is in a + placeholder's shape, it's flexible + + Adds following nodes to the computational graph + + input_placeholder: Input placeholder tensor of shape + (None, window_size), type tf.int32 + labels_placeholder: Labels placeholder tensor of shape + (None, label_size), type tf.float32 + dropout_placeholder: Dropout value placeholder (scalar), + type tf.float32 + + Add these placeholders to self as the instance variables + + self.input_placeholder + self.labels_placeholder + self.dropout_placeholder + + (Don't change the variable names) + """ + self.input_placeholder = tf.placeholder(tf.int32, shape=[None, self.config.window_size]) + self.labels_placeholder = tf.placeholder(tf.float32, shape=[None, self.config.label_size]) + self.dropout_placeholder = tf.placeholder(tf.float32, name='dropout_keep_prob') + + def create_feed_dict(self, input_batch, dropout, label_batch=None): + """Creates the feed_dict for softmax classifier. + + A feed_dict takes the form of: + + feed_dict = { + : , + .... + } + + + Hint: The keys for the feed_dict should be a subset of the placeholder + tensors created in add_placeholders. + Hint: When label_batch is None, don't add a labels entry to the feed_dict. + + Args: + input_batch: A batch of input data. + label_batch: A batch of label data. + Returns: + feed_dict: The feed dictionary mapping from placeholders to values. + """ + feed_dict = {self.input_placeholder: input_batch, self.dropout_placeholder: dropout} + if label_batch is not None: + feed_dict[self.labels_placeholder] = label_batch + return feed_dict + + def add_embedding(self): + """Add embedding layer that maps from vocabulary to vectors. + + Creates an embedding tensor (of shape (len(self.wv), embed_size). Use the + input_placeholder to retrieve the embeddings for words in the current batch. + + (Words are discrete entities. They need to be transformed into vectors for use + in deep-learning. Although we won't do so in this problem, in practice it's + useful to initialize the embedding with pre-trained word-vectors. For this + problem, using the default initializer is sufficient.) + + Hint: This layer should use the input_placeholder to index into the + embedding. + Hint: You might find tf.nn.embedding_lookup useful. + Hint: See following link to understand what -1 in a shape means. + https://www.tensorflow.org/versions/r0.8/api_docs/python/array_ops.html#reshape + Hint: Check the last slide from the TensorFlow lecture. + Hint: Here are the dimensions of the variables you will need to create: + + L: (len(self.wv), embed_size) + + Returns: + window: tf.Tensor of shape (-1, window_size*embed_size) + """ + with tf.variable_scope('embedding_layer') as scope: + embedding = tf.get_variable('embedding', [len(self.wv), self.config.embed_size], initializer=xavier_weight_init()) + window = tf.nn.embedding_lookup(params=embedding, ids=self.input_placeholder) + window = tf.reshape(window, shape=[-1, self.config.window_size * self.config.embed_size], name='window') + variable_summaries(window, window.name) + return window + + def add_model(self, window): + """Adds the 1-hidden-layer NN. + + Hint: Use a variable_scope (e.g. "Layer") for the first hidden layer, and + another variable_scope (e.g. "Softmax") for the linear transformation + preceding the softmax. Make sure to use the xavier_weight_init you + defined in the previous part to initialize weights. + Hint: Make sure to add in regularization and dropout to this network. + Regularization should be an addition to the cost function, while + dropout should be added after both variable scopes. + Hint: You might consider using a tensorflow Graph Collection (e.g + "total_loss") to collect the regularization and loss terms (which you + will add in add_loss_op below). + Hint: Here are the dimensions of the various variables you will need to + create + + W: (window_size*embed_size, hidden_size) + b1: (hidden_size,) + U: (hidden_size, label_size) + b2: (label_size) + + https://www.tensorflow.org/versions/r0.7/api_docs/python/framework.html#graph-collections + Args: + window: tf.Tensor of shape (-1, window_size*embed_size) + Returns: + output: tf.Tensor of shape (batch_size, label_size) + """ + with tf.variable_scope('layer') as layer_scope: + W = tf.get_variable('W_l', shape=[self.config.window_size * self.config.embed_size, self.config.hidden_size], initializer=xavier_weight_init()) + b1 = tf.get_variable('b1', shape=[self.config.hidden_size], initializer=tf.constant_initializer(0.0)) + variable_summaries(W, W.name) + variable_summaries(b1, b1.name) + with tf.variable_scope('hidden_layer') as hidden_layer: + U = tf.get_variable('U_h', shape=[self.config.hidden_size, self.config.label_size], initializer=xavier_weight_init()) + b2 = tf.get_variable('b2', shape=[self.config.label_size], initializer=tf.constant_initializer(0.0)) + variable_summaries(U, U.name) + variable_summaries(b2, b2.name) + h_fc1 = tf.nn.relu(tf.matmul(window, W) + b1) + h_fc1 = tf.nn.dropout(h_fc1, self.dropout_placeholder) + h_fc2 = tf.matmul(h_fc1, U) + b2 + h_fc2 = tf.nn.dropout(h_fc2, self.dropout_placeholder) + l2_loss = tf.nn.l2_loss(W) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(U) + tf.nn.l2_loss(b2) + tf.add_to_collection(name='l2_loss', value=l2_loss) + output = h_fc2 + return output + + def add_loss_op(self, y): + """Adds cross_entropy_loss ops to the computational graph. + + Hint: You can use tf.nn.softmax_cross_entropy_with_logits to simplify your + implementation. You might find tf.reduce_mean useful. + Args: + y (pred): A tensor of shape (batch_size, n_classes) + Returns: + loss: A 0-d tensor (scalar) + """ + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=self.labels_placeholder)) + loss += self.config.l2 * tf.get_collection('l2_loss')[0] + return loss + + def add_training_op(self, loss): + """Sets up the training Ops. + + Creates an optimizer and applies the gradients to all trainable variables. + The Op returned by this function is what must be passed to the + `sess.run()` call to cause the model to train. See + + https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html#Optimizer + + for more information. + + Hint: Use tf.train.AdamOptimizer for this model. + Calling optimizer.minimize() will return a train_op object. + + Args: + loss: Loss tensor, from cross_entropy_loss. + Returns: + train_op: The Op for training. + """ + train_op = tf.train.AdamOptimizer(self.config.lr).minimize(loss) + tf.summary.scalar('loss', loss) + return train_op + + def __init__(self, config): + """Constructs the network using the helper functions defined above.""" + self.config = config + self.load_data(debug=False) + self.add_placeholders() + window = self.add_embedding() + y = self.add_model(window) + self.loss = self.add_loss_op(y) + self.predictions = tf.nn.softmax(y) + one_hot_prediction = tf.argmax(self.predictions, 1) + correct_prediction = tf.equal(tf.argmax(self.labels_placeholder, 1), one_hot_prediction) + self.correct_predictions = tf.reduce_sum(tf.cast(correct_prediction, 'int32')) + self.train_op = self.add_training_op(self.loss) + self.merged_summaries = tf.summary.merge_all() + self.summary_writer = None + + def run_epoch(self, session, input_data, input_labels, shuffle=True, verbose=True, epoch=0): + orig_X, orig_y = input_data, input_labels + dp = self.config.dropout + total_loss = [] + total_correct_examples = 0 + total_processed_examples = 0 + total_steps = len(orig_X) / self.config.batch_size + for step, (x, y) in enumerate(data_iterator(orig_X, orig_y, batch_size=self.config.batch_size, label_size=self.config.label_size, shuffle=shuffle)): + feed = self.create_feed_dict(input_batch=x, dropout=dp, label_batch=y) + loss, total_correct, _, merged = session.run([self.loss, self.correct_predictions, self.train_op, self.merged_summaries], feed_dict=feed) + if step % 50 == 0: + self.summary_writer.add_summary(merged, epoch * total_steps + step) + total_processed_examples += len(x) + total_correct_examples += total_correct + total_loss.append(loss) + if verbose and step % verbose == 0: + sys.stdout.write('\r{} / {} : loss = {}'.format(step, total_steps, np.mean(total_loss))) + sys.stdout.flush() + if verbose: + sys.stdout.write('\r') + sys.stdout.flush() + return np.mean(total_loss), total_correct_examples / float(total_processed_examples) + + def predict(self, session, X, y=None): + """Make predictions from the provided model.""" + dp = 1 + losses = [] + results = [] + if np.any(y): + data = data_iterator(X, y, batch_size=self.config.batch_size, label_size=self.config.label_size, shuffle=False) + else: + data = data_iterator(X, batch_size=self.config.batch_size, label_size=self.config.label_size, shuffle=False) + for step, (x, y) in enumerate(data): + feed = self.create_feed_dict(input_batch=x, dropout=dp) + if np.any(y): + feed[self.labels_placeholder] = y + loss, preds = session.run([self.loss, self.predictions], feed_dict=feed) + losses.append(loss) + else: + preds = session.run(self.predictions, feed_dict=feed) + predicted_indices = preds.argmax(axis=1) + results.extend(predicted_indices) + if len(losses) == 0: + return 0, results + return np.mean(losses), results + + +def print_confusion(confusion, num_to_tag): + """Helper method that prints confusion matrix.""" + total_guessed_tags = confusion.sum(axis=0) + total_true_tags = confusion.sum(axis=1) + print() + print(confusion) + for i, tag in sorted(num_to_tag.items()): + prec = confusion[i, i] / float(total_guessed_tags[i]) + recall = confusion[i, i] / float(total_true_tags[i]) + print('Tag: {} - P {:2.4f} / R {:2.4f}'.format(tag, prec, recall)) + + +def calculate_confusion(config, predicted_indices, y_indices): + """Helper method that calculates confusion matrix.""" + confusion = np.zeros((config.label_size, config.label_size), dtype=np.int32) + for i in range(len(y_indices)): + correct_label = y_indices[i] + guessed_label = predicted_indices[i] + confusion[correct_label, guessed_label] += 1 + return confusion + + +def save_predictions(predictions, filename): + """Saves predictions to provided file.""" + with open(filename, 'wb') as f: + for prediction in predictions: + f.write(bytes(prediction)) + + +def test_NER(): + """Test NER model implementation. + + You can use this function to test your implementation of the Named Entity + Recognition network. When debugging, set max_epochs in the Config object to 1 + so you can rapidly iterate. + """ + config = Config() + with tf.Graph().as_default(): + model = NERModel(config) + init = tf.initialize_all_variables() + saver = tf.train.Saver() + with tf.Session() as session: + best_val_loss = float('inf') + best_val_epoch = 0 + model.summary_writer = tf.summary.FileWriter('NER_train_log/', session.graph) + session.run(init) + for epoch in range(config.max_epochs): + print('Epoch {}'.format(epoch)) + start = time.time() + train_loss, train_acc = model.run_epoch(session, model.X_train, model.y_train, epoch=epoch) + val_loss, predictions = model.predict(session, model.X_dev, model.y_dev) + print('Training loss: {}'.format(train_loss)) + print('Training acc: {}'.format(train_acc)) + print('Validation loss: {}'.format(val_loss)) + if val_loss < best_val_loss: + best_val_loss = val_loss + best_val_epoch = epoch + if not os.path.exists('./weights'): + os.makedirs('./weights') + saver.save(session, './weights/ner.weights') + if epoch - best_val_epoch > config.early_stopping: + break + confusion = calculate_confusion(config, predictions, model.y_dev) + cm = confusion.copy() + cm = cm.astype(np.float32) / cm.sum(axis=1, keepdims=True) + cm = cm[(np.newaxis), :, :, (np.newaxis)].astype(np.float32) + cm_tf_image = tf.convert_to_tensor(cm) + cm_is = tf.summary.image('confusion_matrix', cm_tf_image) + cm_current_epoch = session.run(cm_is) + model.summary_writer.add_summary(cm_current_epoch, epoch) + print_confusion(confusion, model.num_to_tag) + print('Total time: {}'.format(time.time() - start)) + saver.restore(session, './weights/ner.weights') + print('Test') + print('=-=-=') + print('Writing predictions to q2_test.predicted') + _, predictions = model.predict(session, model.X_test, model.y_test) + save_predictions(predictions, 'q2_test.predicted') + + +if __name__ == '__main__': + test_NER() diff --git a/dataset/preprocessed/179.txt b/dataset/preprocessed/179.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/179.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/18.txt b/dataset/preprocessed/18.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/18.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/180.txt b/dataset/preprocessed/180.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/180.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/181.txt b/dataset/preprocessed/181.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/181.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/182.txt b/dataset/preprocessed/182.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/182.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/183.txt b/dataset/preprocessed/183.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/183.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/184.txt b/dataset/preprocessed/184.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/184.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/185.txt b/dataset/preprocessed/185.txt new file mode 100644 index 0000000..399a477 --- /dev/null +++ b/dataset/preprocessed/185.txt @@ -0,0 +1,253 @@ +from __future__ import print_function +import imageio +import matplotlib.pyplot as plt +import numpy as np +import os +import sys +import tarfile +from IPython.display import display, Image +from sklearn.linear_model import LogisticRegression +from six.moves.urllib.request import urlretrieve +from six.moves import cPickle as pickle +import tensorflow as tf +from six.moves import range +url = 'https://commondatastorage.googleapis.com/books1000/' +last_percent_reported = None +data_root = '/Users/rakadalal/Desktop/Deep_learning/' +num_classes = 10 +np.random.seed(133) +image_size = 28 +pixel_depth = 255.0 + + +def download_progress_hook(count, blockSize, totalSize): + """A hook to report the progress of a download. This is mostly intended for users with + slow internet connections. Reports every 5% change in download progress. + """ + global last_percent_reported + percent = int(count * blockSize * 100 / totalSize) + if last_percent_reported != percent: + if percent % 5 == 0: + sys.stdout.write('%s%%' % percent) + sys.stdout.flush() + else: + sys.stdout.write('.') + sys.stdout.flush() + last_percent_reported = percent + + +def maybe_download(filename, expected_bytes, force=False): + """Download a file if not present, and make sure it's the right size.""" + dest_filename = os.path.join(data_root, filename) + if force or not os.path.exists(dest_filename): + print('Attempting to download:', filename) + filename, _ = urlretrieve(url + filename, dest_filename, reporthook=download_progress_hook) + print('\nDownload Complete!') + statinfo = os.stat(dest_filename) + if statinfo.st_size == expected_bytes: + print('Found and verified', dest_filename) + else: + raise Exception('Failed to verify ' + dest_filename + '. Can you get to it with a browser?') + return dest_filename + + +def maybe_extract(filename, force=False): + root = os.path.splitext(os.path.splitext(filename)[0])[0] + if os.path.isdir(root) and not force: + print('%s already present - Skipping extraction of %s.' % (root, filename)) + else: + print('Extracting data for %s. This may take a while. Please wait.' % root) + tar = tarfile.open(filename) + sys.stdout.flush() + tar.extractall(data_root) + tar.close() + data_folders = [os.path.join(root, d) for d in sorted(os.listdir(root)) if os.path.isdir(os.path.join(root, d))] + if len(data_folders) != num_classes: + raise Exception('Expected %d folders, one per class. Found %d instead.' % (num_classes, len(data_folders))) + print(data_folders) + return data_folders + + +def load_letter(folder, min_num_images): + """Load the data for a single letter label.""" + image_files = os.listdir(folder) + dataset = np.ndarray(shape=(len(image_files), image_size, image_size), dtype=np.float32) + print(folder) + num_images = 0 + for image in image_files: + image_file = os.path.join(folder, image) + try: + image_data = (imageio.imread(image_file).astype(float) - pixel_depth / 2) / pixel_depth + if image_data.shape != (image_size, image_size): + raise Exception('Unexpected image shape: %s' % str(image_data.shape)) + dataset[(num_images), :, :] = image_data + num_images = num_images + 1 + except (IOError, ValueError) as e: + print('Could not read:', image_file, ':', e, "- it's ok, skipping.") + dataset = dataset[0:num_images, :, :] + if num_images < min_num_images: + raise Exception('Many fewer images than expected: %d < %d' % (num_images, min_num_images)) + print('Full dataset tensor:', dataset.shape) + print('Mean:', np.mean(dataset)) + print('Standard deviation:', np.std(dataset)) + return dataset + + +def maybe_pickle(data_folders, min_num_images_per_class, force=False): + dataset_names = [] + for folder in data_folders: + set_filename = folder + '.pickle' + dataset_names.append(set_filename) + if os.path.exists(set_filename) and not force: + print('%s already present - Skipping pickling.' % set_filename) + else: + print('Pickling %s.' % set_filename) + dataset = load_letter(folder, min_num_images_per_class) + try: + with open(set_filename, 'wb') as f: + pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL) + except Exception as e: + print('Unable to save data to', set_filename, ':', e) + return dataset_names + + +def make_arrays(nb_rows, img_size): + if nb_rows: + dataset = np.ndarray((nb_rows, img_size, img_size), dtype=np.float32) + labels = np.ndarray(nb_rows, dtype=np.int32) + else: + dataset, labels = None, None + return dataset, labels + + +def merge_datasets(pickle_files, train_size, valid_size=0): + num_classes = len(pickle_files) + valid_dataset, valid_labels = make_arrays(valid_size, image_size) + train_dataset, train_labels = make_arrays(train_size, image_size) + vsize_per_class = valid_size // num_classes + tsize_per_class = train_size // num_classes + start_v, start_t = 0, 0 + end_v, end_t = vsize_per_class, tsize_per_class + end_l = vsize_per_class + tsize_per_class + for label, pickle_file in enumerate(pickle_files): + try: + with open(pickle_file, 'rb') as f: + letter_set = pickle.load(f) + np.random.shuffle(letter_set) + if valid_dataset is not None: + valid_letter = letter_set[:vsize_per_class, :, :] + valid_dataset[start_v:end_v, :, :] = valid_letter + valid_labels[start_v:end_v] = label + start_v += vsize_per_class + end_v += vsize_per_class + train_letter = letter_set[vsize_per_class:end_l, :, :] + train_dataset[start_t:end_t, :, :] = train_letter + train_labels[start_t:end_t] = label + start_t += tsize_per_class + end_t += tsize_per_class + except Exception as e: + print('Unable to process data from', pickle_file, ':', e) + raise + return valid_dataset, valid_labels, train_dataset, train_labels + + +train_filename = maybe_download('notMNIST_large.tar.gz', 247336696) +test_filename = maybe_download('notMNIST_small.tar.gz', 8458043) +train_folders = maybe_extract(train_filename) +test_folders = maybe_extract(test_filename) +train_datasets = maybe_pickle(train_folders, 45000) +test_datasets = maybe_pickle(test_folders, 1800) +train_size = 200000 +valid_size = 10000 +test_size = 10000 +valid_dataset, valid_labels, train_dataset, train_labels = merge_datasets(train_datasets, train_size, valid_size) +_, _, test_dataset, test_labels = merge_datasets(test_datasets, test_size) + + +def randomize(dataset, labels): + permutation = np.random.permutation(labels.shape[0]) + shuffled_dataset = dataset[(permutation), :, :] + shuffled_labels = labels[permutation] + return shuffled_dataset, shuffled_labels + + +train_dataset, train_labels = randomize(train_dataset, train_labels) +test_dataset, test_labels = randomize(test_dataset, test_labels) +valid_dataset, valid_labels = randomize(valid_dataset, valid_labels) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +image_size = 28 +num_labels = 10 +num_channels = 1 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size, image_size, num_channels)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +batch_size = 16 +patch_size = 5 +depth = 32 +num_hidden = 64 +num_steps = 30001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + layer1_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, depth], stddev=0.1)) + layer1_biases = tf.Variable(tf.zeros([depth])) + layer2_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth, depth], stddev=0.1)) + layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth])) + layer3_weights = tf.Variable(tf.truncated_normal([image_size // 4 * image_size // 4 * depth, num_hidden], stddev=0.1)) + layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden])) + layer4_weights = tf.Variable(tf.truncated_normal([num_hidden, num_labels], stddev=0.1)) + layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels])) + + def model(data, keep_prob): + conv1 = tf.nn.relu(tf.nn.conv2d(data, layer1_weights, [1, 1, 1, 1], padding='SAME') + layer1_biases) + pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + conv2 = tf.nn.relu(tf.nn.conv2d(pool1, layer2_weights, [1, 1, 1, 1], padding='SAME') + layer2_biases) + pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + shape = pool2.get_shape().as_list() + reshape = tf.reshape(pool2, [shape[0], shape[1] * shape[2] * shape[3]]) + fc1 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases) + fc1_drop = tf.nn.dropout(fc1, keep_prob) + y_conv = tf.matmul(fc1_drop, layer4_weights) + layer4_biases + return y_conv + logits = model(tf_train_dataset, 0.5) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = loss + 0.001 * (tf.nn.l2_loss(layer3_weights) + tf.nn.l2_loss(layer3_biases) + tf.nn.l2_loss(layer4_weights) + tf.nn.l2_loss(layer4_biases)) + learning_rate = tf.train.exponential_decay(0.1, global_step, num_steps, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset, 1)) + test_prediction = tf.nn.softmax(model(tf_test_dataset, 1)) +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :, :, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 300 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Lenet 5 Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/186.txt b/dataset/preprocessed/186.txt new file mode 100644 index 0000000..87c45c3 --- /dev/null +++ b/dataset/preprocessed/186.txt @@ -0,0 +1,53 @@ +""" +@author sourabhxiii + +""" +import numpy as np +import tensorflow as tf +from tensorflow import layers +from collections import namedtuple + + +class Actor: + + def __init__(self, batch_size, input_dim, output_dim, action_low, action_high, np_index, seed): + tf.set_random_seed(seed) + self.lr = 0.01 + bounds = namedtuple('AxnBound', field_names=['low', 'high']) + self.action_bound = bounds(action_low, action_high) + self.batch_size = batch_size + with tf.name_scope('input'): + self.features = tf.placeholder(dtype=tf.float32, shape=(None, input_dim), name='features') + self.grads = tf.placeholder(dtype=tf.float32, shape=(None, output_dim), name='grads') + with tf.name_scope('output'): + self.y = tf.placeholder(dtype=tf.float32, shape=(None, 1), name='action') + with tf.name_scope('actor_net'): + nn = layers.Dense(8, kernel_initializer=tf.random_normal_initializer(), bias_initializer=tf.random_normal_initializer(), activation=tf.nn.relu, name='AM_D1')(self.features) + nn = layers.Dense(4, kernel_initializer=tf.random_normal_initializer(), bias_initializer=tf.random_normal_initializer(), activation=tf.nn.relu, name='AM_D2')(nn) + self.axn = layers.Dense(output_dim, kernel_initializer=tf.random_normal_initializer(), bias_initializer=tf.random_normal_initializer(), activation=tf.nn.tanh, name='AM_D4')(nn) + self.network_params = tf.trainable_variables()[np_index:] + with tf.name_scope('actor_gradient'): + self.unnormalized_actor_gradients = tf.gradients(self.axn, self.network_params, -self.grads) + with tf.name_scope('actor_train'): + self.train_op = tf.train.AdamOptimizer(self.lr).apply_gradients(zip(self.unnormalized_actor_gradients, self.network_params)) + with tf.name_scope('init'): + self.init = tf.global_variables_initializer() + + def set_session(self, sess): + self.session = sess + self.session.run(self.init) + + def model_session(self): + return self.session + + def get_action(self, obs): + axn = self.session.run([self.axn], feed_dict={self.features: obs}) + return axn + + def update_weight(self, obs, grads): + feed_dict = {self.features: obs, self.grads: grads} + _ = self.session.run([self.train_op], feed_dict) + return + + def get_weights(self): + return self.network_params diff --git a/dataset/preprocessed/188.txt b/dataset/preprocessed/188.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/188.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/189.txt b/dataset/preprocessed/189.txt new file mode 100644 index 0000000..0d7fb90 --- /dev/null +++ b/dataset/preprocessed/189.txt @@ -0,0 +1,121 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +import os +import matplotlib.pyplot as plt +from skimage import io +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f, encoding='latin1') + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +plt.imshow(test_dataset[233], cmap='gray') +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +num_nodes_1 = 3048 +num_nodes_2 = 2048 +num_nodes_3 = 1024 +num_nodes_4 = 500 +batch_size = 250 +hidden_layer_1_keep_prob = 0.5 +hidden_layer_2_keep_prob = 0.6 +hidden_layer_3_keep_prob = 0.7 +hidden_layer_4_keep_prob = 0.8 +beta_1 = 1e-05 +beta_2 = 1e-05 +beta_3 = 1e-05 +beta_4 = 1e-05 +hidden_layer_1_stddev = np.sqrt(5.5 / 784) +hidden_layer_2_stddev = np.sqrt(3.5 / (num_nodes_1 + 784)) +hidden_layer_3_stddev = np.sqrt(2.5 / (num_nodes_2 + num_nodes_1)) +hidden_layer_4_stddev = np.sqrt(1.5 / (num_nodes_3 + num_nodes_2)) +output_layer_stddev = np.sqrt(0.5 / (num_nodes_4 + num_nodes_3)) +print(hidden_layer_1_stddev, hidden_layer_2_stddev, hidden_layer_3_stddev, hidden_layer_4_stddev) +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(None, image_size * image_size), name='train_to_restore') + tf_train_labels = tf.placeholder(tf.float32, shape=(None, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes_1], stddev=hidden_layer_1_stddev)) + biases_1 = tf.Variable(tf.zeros([num_nodes_1])) + weights_2 = tf.Variable(tf.truncated_normal([num_nodes_1, num_nodes_2], stddev=hidden_layer_2_stddev)) + biases_2 = tf.Variable(tf.zeros([num_nodes_2])) + weights_3 = tf.Variable(tf.truncated_normal([num_nodes_2, num_nodes_3], stddev=hidden_layer_3_stddev)) + biases_3 = tf.Variable(tf.zeros([num_nodes_3])) + weights_4 = tf.Variable(tf.truncated_normal([num_nodes_3, num_nodes_4], stddev=hidden_layer_4_stddev)) + biases_4 = tf.Variable(tf.zeros([num_nodes_4])) + weights_5 = tf.Variable(tf.truncated_normal([num_nodes_4, num_labels], stddev=output_layer_stddev)) + biases_5 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + relu_layer = tf.nn.dropout(tf.nn.relu(logits_1), hidden_layer_1_keep_prob) + logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + relu_layer_2 = tf.nn.dropout(tf.nn.relu(logits_2), hidden_layer_2_keep_prob) + logits_3 = tf.matmul(relu_layer_2, weights_3) + biases_3 + relu_layer_3 = tf.nn.dropout(tf.nn.relu(logits_3), hidden_layer_3_keep_prob) + logits_4 = tf.matmul(relu_layer_3, weights_4) + biases_4 + relu_layer_4 = tf.nn.dropout(tf.nn.relu(logits_4), hidden_layer_4_keep_prob) + out = tf.matmul(relu_layer_4, weights_5) + biases_5 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=tf_train_labels)) + global_step = tf.Variable(0, trainable=False) + starter_learning_rate = 0.3 + learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, 100000, 0.96, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(out, name='op_to_restore') + validation_hidden_layer_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1) + validation_hidden_layer_2 = tf.nn.relu(tf.matmul(validation_hidden_layer_1, weights_2) + biases_2) + validation_hidden_layer_3 = tf.nn.relu(tf.matmul(validation_hidden_layer_2, weights_3) + biases_3) + validation_hidden_layer_4 = tf.nn.relu(tf.matmul(validation_hidden_layer_3, weights_4) + biases_4) + validation_logits = tf.matmul(validation_hidden_layer_4, weights_5) + biases_5 + valid_prediction = tf.nn.softmax(validation_logits) + test_hidden_layer_1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1) + test_hidden_layer_2 = tf.nn.relu(tf.matmul(test_hidden_layer_1, weights_2) + biases_2) + test_hidden_layer_3 = tf.nn.relu(tf.matmul(test_hidden_layer_2, weights_3) + biases_3) + test_hidden_layer_4 = tf.nn.relu(tf.matmul(test_hidden_layer_3, weights_4) + biases_4) + test_logits = tf.matmul(test_hidden_layer_4, weights_5) + biases_5 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 20000 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step {}: {}'.format(step, l)) + print('Minibatch accuracy: {:.1f}'.format(accuracy(predictions, batch_labels))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) diff --git a/dataset/preprocessed/19.txt b/dataset/preprocessed/19.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/19.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/190.txt b/dataset/preprocessed/190.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/190.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/191.txt b/dataset/preprocessed/191.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/191.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/192.txt b/dataset/preprocessed/192.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/192.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/193.txt b/dataset/preprocessed/193.txt new file mode 100644 index 0000000..2254f30 --- /dev/null +++ b/dataset/preprocessed/193.txt @@ -0,0 +1,290 @@ +import pandas as pd +import numpy as np +import pickle +import matplotlib.pyplot as plt +from scipy import stats +import tensorflow as tf +import seaborn as sns +from pylab import rcParams +from sklearn import metrics +from sklearn.model_selection import train_test_split +import tensorflow as tf +import pandas as pd +import numpy as np +from tensorflow.contrib import rnn +from sklearn.model_selection import train_test_split +from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score +"""' + +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = tf.keras.layers.SimpleRNNCell(units=n_neurons) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) +init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2], [9, 8, 7]], [[3, 4, 5], [0, 0, 0]], [[6, 7, 8], [6, 5, 4]], [[9, 0, 1], [3, 2, 1]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +""" +######################################################################################## +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +n_steps = 1 +n_inputs = 3 +n_neurons = 5 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = tf.keras.layers.SimpleRNNCell(units=n_neurons) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) +init = tf.global_variables_initializer() +X_batch = np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]], [[9, 0, 1]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +""" +######################################################################################## +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 1 +reset_graph() +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +basic_cell = tf.keras.layers.SimpleRNNCell(units=n_neurons) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) +init = tf.global_variables_initializer() +X_batch = np.array([[[0, 8, 6], [4, 6, 7]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +""" +######################################################################################## +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 1 +reset_graph() +X = tf.placeholder(tf.float32, [5, 1, 3]) +basic_cell = tf.keras.layers.SimpleRNNCell(units=n_neurons) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) +init = tf.global_variables_initializer() +X_batch = np.array([[[0, 8, 6]], [[0, 8, 6]], [[0, 8, 6]], [[0, 8, 6]], [[0, 8, 6]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +""" + ######################################################################################## + +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +n_steps = 2 +n_inputs = 3 +n_neurons = 1 +reset_graph() +X = tf.placeholder(tf.float32, [None, 1, 7]) +basic_cell = tf.keras.layers.SimpleRNNCell(units=2) +lstm_cell = tf.nn.rnn_cell.LSTMCell(2, state_is_tuple=True) +outputs, states = tf.nn.dynamic_rnn(lstm_cell, X, dtype=tf.float32) +init = tf.global_variables_initializer() +X_batch = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +""" + ######################################################################################## + +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +reset_graph() +X = tf.placeholder(tf.float32, [None, 1, 7]) +lstm_cell = tf.nn.rnn_cell.LSTMCell(3, state_is_tuple=True) +cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * 1, state_is_tuple=True) +init_state = cells.zero_state(1, tf.float32) +outputs, states = tf.nn.dynamic_rnn(cells, X, initial_state=init_state, dtype=tf.float32) +init = tf.global_variables_initializer() +X_batch = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) +with tf.Session() as sess: + init.run() + outputs_val = outputs.eval(feed_dict={X: X_batch}) +""" + ######################################################################################## + +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +reset_graph() +X = tf.placeholder(tf.float32, [None, 1, 7], name='input_placeholder') +y = tf.placeholder(tf.float32, [None, 1, 7], name='labels_placeholder') +lstm_cell = tf.nn.rnn_cell.LSTMCell(3, state_is_tuple=True) +cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * 1, state_is_tuple=True) +init_state = cells.zero_state(1, tf.float32) +outputs, states = tf.nn.dynamic_rnn(cells, X, initial_state=init_state, dtype=tf.float32) +outputs = tf.transpose(outputs, [1, 0, 2]) +last = tf.gather(outputs, int(outputs.get_shape()[0]) - 1) +weight = tf.get_variable(name='weights', initializer=tf.truncated_normal([3, int(y.get_shape()[1])])) +bias = tf.get_variable(name='bias', initializer=tf.constant(0.1, shape=[y.get_shape()[1]])) +prediction = tf.nn.softmax(tf.matmul(last, weight) + bias) +cross_entropy = -tf.reduce_sum(y * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0))) +optimizer = tf.train.AdamOptimizer() +minimize = optimizer.minimize(cross_entropy) +mistakes = tf.not_equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) +error = tf.reduce_mean(tf.cast(mistakes, tf.float32)) +init = tf.global_variables_initializer() +with tf.Session() as sess: + init.run() + X_train = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + y_train = np.array([[[1535.0, -335353.68, -55.04, 2.32, -550.1531, -155.1523, 0.111]]]) + X_test = np.array([[[41.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + y_test = np.array([[[14535.0, -335353.68, -55.04, 2.32, -550.1531, -155.1523, 0.111]]]) + print('X_train.shape') + outputs_val = outputs.eval(feed_dict={X: X_train, y: y_train}) + minimize_val = sess.run(minimize, feed_dict={X: X_train, y: y_train}) + incorrect_val = sess.run(error, feed_dict={X: X_test, y: y_test}) + prediction_val = sess.run(prediction, feed_dict={X: X_test, y: y_test}) +""" +2do : seq_lens checks gather loops change the data shape +""" +""" + ######################################################################################## + +""" + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() +X = tf.placeholder(tf.float32, [None, 1, 7], name='input_placeholder') +y = tf.placeholder(tf.float32, [None, 1, 7], name='labels_placeholder') +lstm_cell = tf.nn.rnn_cell.LSTMCell(3, state_is_tuple=True) +cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * 1, state_is_tuple=True) +init_state = cells.zero_state(1, tf.float32) +outputs, states = tf.nn.dynamic_rnn(cells, X, initial_state=init_state, dtype=tf.float32) +outputs = tf.transpose(outputs, [1, 0, 2]) +X_train = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) +print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^') +sess = tf.Session() +print('used') +used = sess.run(tf.sign(tf.reduce_max(tf.abs(X_train), 2))) +print(used) +length = tf.reduce_sum(used, 1) +length = sess.run(tf.cast(length, tf.int32)) +print('length') +print(length) +batch_size = sess.run(tf.shape(outputs)[0]) +print('batch_size') +print(batch_size) +max_length = sess.run(tf.shape(outputs)[1]) +print('max_length') +print(max_length) +out_size = int(outputs.get_shape()[2]) +print('out_size') +print(out_size) +index = tf.range(0, batch_size) * max_length + (length - 1) +flat = tf.reshape(outputs, [-1, out_size]) +relevant = tf.gather(flat, index) +out_size = int(outputs.get_shape()[2]) +index = tf.range(0, 1) * 2 +flat = tf.reshape(outputs, [-1, out_size]) +relevant = tf.gather(flat, index) +sess.close() +print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^') +weight = tf.get_variable(name='weights', initializer=tf.truncated_normal([3, int(y.get_shape()[1])])) +bias = tf.get_variable(name='bias', initializer=tf.constant(0.1, shape=[y.get_shape()[1]])) +prediction = tf.nn.softmax(tf.matmul(relevant, weight) + bias) +cross_entropy = -tf.reduce_sum(y * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0))) +optimizer = tf.train.AdamOptimizer() +minimize = optimizer.minimize(cross_entropy) +mistakes = tf.not_equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) +error = tf.reduce_mean(tf.cast(mistakes, tf.float32)) +init = tf.global_variables_initializer() +with tf.Session() as sess: + init.run() + X_train = np.array([[[1.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + y_train = np.array([[[1, 1, 1, 1, 1, 1, 1]]]) + X_test = np.array([[[41.0, -3.68, -3.04, 2.32, -0.1531, -1.1523, 0.111]]]) + y_test = np.array([[[0, 0, 0, 0, 0, 0, 0]]]) + print('X_train.shape') + print(X_train.shape) + print('outputs') + print(outputs) + outputs_val = outputs.eval(feed_dict={X: X_train, y: y_train}) + print('outputs_val') + print(outputs_val) + minimize_val = sess.run(minimize, feed_dict={X: X_train, y: y_train}) + incorrect_val = sess.run(error, feed_dict={X: X_test, y: y_test}) + print('incorrect_val') + print(incorrect_val) + prediction_val = sess.run(prediction, feed_dict={X: X_test, y: y_test}) + print(prediction_val) +""" +2do : seq_lens checks gather loops understand the prediction matrix label shapes tensorboarc +""" +""" + ######################################################################################## + +""" diff --git a/dataset/preprocessed/194.txt b/dataset/preprocessed/194.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/194.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/195.txt b/dataset/preprocessed/195.txt new file mode 100644 index 0000000..f5197a9 --- /dev/null +++ b/dataset/preprocessed/195.txt @@ -0,0 +1,47 @@ +import tensorflow as tf +import numpy as np +ni = 3 +nn = 5 +X0 = tf.placeholder(tf.float32, [None, ni]) +X1 = tf.placeholder(tf.float32, [None, ni]) +Wx = tf.Variable(tf.random_normal(shape=[ni, nn], dtype=tf.float32)) +Wy = tf.Variable(tf.random_normal(shape=[nn, nn], dtype=tf.float32)) +b = tf.Variable(tf.zeros([1, nn], dtype=tf.float32)) +Y0 = tf.tanh(tf.matmul(X0, Wx) + b) +Y1 = tf.tanh(tf.matmul(Y0, Wy) + tf.matmul(X1, Wx) + b) +init = tf.global_variables_initializer() +X0_batch = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 1]]) +X1_batch = np.array([[9, 8, 7], [0, 0, 0], [6, 5, 4], [3, 2, 1]]) +print('Test 1') +with tf.Session() as sess: + init.run() + Y0_val, Y1_val = sess.run([Y0, Y1], feed_dict={X0: X0_batch, X1: X1_batch}) + print(Y0_val) + print(Y1_val) +print('Test 2') +tf.reset_default_graph() +X0 = tf.placeholder(tf.float32, [None, ni]) +X1 = tf.placeholder(tf.float32, [None, ni]) +basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=nn) +output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell, [X0, X1], dtype=tf.float32) +Y0, Y1 = output_seqs +init = tf.global_variables_initializer() +with tf.Session() as sess: + init.run() + Y0_val, Y1_val = sess.run([Y0, Y1], feed_dict={X0: X0_batch, X1: X1_batch}) + print(Y0_val) + print(Y1_val) +print('Test 3') +tf.reset_default_graph() +n_steps = 2 +X = tf.placeholder(tf.float32, [None, n_steps, ni]) +X_seqs = tf.unstack(tf.transpose(X, perm=[1, 0, 2])) +basic_cell1 = tf.contrib.rnn.BasicRNNCell(num_units=nn) +output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell1, X_seqs, dtype=tf.float32) +outputs = tf.transpose(tf.stack(output_seqs), perm=[1, 0, 2]) +X_batch = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 0, 1]], [[9, 8, 7], [0, 0, 0]], [[6, 5, 4], [3, 2, 1]]]) +init = tf.global_variables_initializer() +with tf.Session() as sess: + init.run() + output_val = sess.run(outputs, feed_dict={X: X_batch}) + print(output_val) diff --git a/dataset/preprocessed/196.txt b/dataset/preprocessed/196.txt new file mode 100644 index 0000000..b50c69d --- /dev/null +++ b/dataset/preprocessed/196.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net, n_layers, n_fc_layers): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k, n_layers, n_fc_layers) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/197.txt b/dataset/preprocessed/197.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/197.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/198.txt b/dataset/preprocessed/198.txt new file mode 100644 index 0000000..6c8b8a9 --- /dev/null +++ b/dataset/preprocessed/198.txt @@ -0,0 +1,73 @@ +import tensorflow as tf +import time +from tensorflow.examples.tutorials.mnist import input_data +RESTORE_CHECKPOINT = False +x = tf.placeholder(tf.float32, shape=[None, 784], name='x') +x_ = tf.reshape(x, [-1, 28, 28, 1]) +y = tf.placeholder(tf.float32, shape=[None, 10], name='y') +is_training = tf.placeholder(tf.bool) +with tf.name_scope('conv1'): + W1 = tf.Variable(tf.truncated_normal([5, 5, 1, 4], 0, 1), name='W1') + b1 = tf.Variable(tf.constant(0.1, tf.float32, [4]), name='b1') + l1 = tf.nn.lrn(tf.nn.relu(tf.nn.conv2d(x_, W1, [1, 1, 1, 1], 'SAME') + b1)) +with tf.name_scope('conv2'): + W2 = tf.Variable(tf.truncated_normal([5, 5, 4, 8], 0, 1), name='W2') + b2 = tf.Variable(tf.constant(0.1, tf.float32, [8]), name='b2') + l2 = tf.nn.lrn(tf.nn.relu(tf.nn.conv2d(l1, W2, [1, 2, 2, 1], 'SAME') + b2)) +with tf.name_scope('conv3'): + W3 = tf.Variable(tf.truncated_normal([4, 4, 8, 12], 0, 1), name='W3') + b3 = tf.Variable(tf.constant(0.1, tf.float32, [12]), name='b3') + l3 = tf.nn.lrn(tf.nn.relu(tf.nn.conv2d(l2, W3, [1, 2, 2, 1], 'SAME') + b3)) +with tf.name_scope('dense1'): + l3_ = tf.reshape(l3, [-1, 7 * 7 * 12]) + W4 = tf.Variable(tf.truncated_normal([7 * 7 * 12, 200], 0, 1), name='W4') + b4 = tf.Variable(tf.constant(0.1, tf.float32, [200]), name='b4') + l4 = tf.nn.relu(tf.matmul(l3_, W4) + b4) + l4 = tf.layers.dropout(l4, rate=0.5, training=is_training) +with tf.name_scope('out'): + W5 = tf.Variable(tf.truncated_normal([200, 10], 0, 1), name='W5') + b5 = tf.Variable(tf.constant(0.1, tf.float32, [10]), name='b5') + l5 = tf.matmul(l4, W5) + b5 +out = tf.nn.softmax(l5) +with tf.name_scope('loss'): + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=l5)) + train_loss_stat = tf.summary.scalar('Train loss', loss) + test_loss_stat = tf.summary.scalar('Test loss', loss) +with tf.name_scope('accuracy'): + correct_predictions = tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(y, 1)), 'float') + class_accuracy_op = tf.cast(tf.reduce_mean(correct_predictions), 'float') + train_accuracy_stat = tf.summary.scalar('Train accuracy', class_accuracy_op) + test_accuracy_stat = tf.summary.scalar('Test accuracy', class_accuracy_op) +train_step = tf.train.AdamOptimizer(0.01).minimize(loss) +train_stats = tf.summary.merge([train_loss_stat, train_accuracy_stat]) +test_stats = tf.summary.merge([test_loss_stat, test_accuracy_stat]) +batch_size = 100 +epochs = 200 +num_step = 0 +last_epoch = 0 +ckpt_path = 'models/conv.ckpt' +init = tf.global_variables_initializer() +session = tf.Session() +saver = tf.train.Saver() +if RESTORE_CHECKPOINT and tf.train.checkpoint_exists(ckpt_path): + saver.restore(session, ckpt_path) +else: + session.run(init) +log_dir = time.strftime('logs/conv/%Y-%m-%d-%H:%M:%S', time.gmtime()) +summary_writer = tf.summary.FileWriter(log_dir, session.graph) +mnist = input_data.read_data_sets('data/', one_hot=True) +while mnist.train.epochs_completed < epochs: + batch_xs, batch_ys = mnist.train.next_batch(batch_size) + session.run(train_step, feed_dict={x: batch_xs, y: batch_ys, is_training: True}) + num_step += 1 + if num_step % 200 == 0: + tb_test = session.run(test_stats, feed_dict={x: mnist.test.images, y: mnist.test.labels, is_training: False}) + summary_writer.add_summary(tb_test, global_step=num_step) + tb_train = session.run(train_stats, feed_dict={x: mnist.train.images, y: mnist.train.labels, is_training: False}) + summary_writer.add_summary(tb_train, global_step=num_step) + if last_epoch != mnist.train.epochs_completed: + last_epoch = mnist.train.epochs_completed + saver.save(session, ckpt_path) + train_loss, train_accuracy = session.run([loss, class_accuracy_op], feed_dict={x: mnist.train.images, y: mnist.train.labels, is_training: False}) + test_loss, test_accuracy = session.run([loss, class_accuracy_op], feed_dict={x: mnist.test.images, y: mnist.test.labels, is_training: False}) + print('epoch: %3d\ttrain accuracy: %2.5f\ttest accuracy: %2.5f\ttrain loss: %2.5f\ttest loss: %2.5f' % (last_epoch, train_accuracy, test_accuracy, train_loss, test_loss)) diff --git a/dataset/preprocessed/199.txt b/dataset/preprocessed/199.txt new file mode 100644 index 0000000..9339e4f --- /dev/null +++ b/dataset/preprocessed/199.txt @@ -0,0 +1,245 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +""" +Reformat into a shape that's more adapted to the models we're going to train: + + data as a flat matrix, + labels as float 1-hot encodings. + +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +""" +Problem 1 + +Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor t using nn.l2_loss(t). The right amount of regularization should improve your validation / test accuracy. +""" +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('L2 regularization(beta=%.5f) Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +print('Best beta=%f, accuracy=%.1f%%' % (beta_val[np.argmax(accuracy_val)], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Overfitting with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y1 = tf.nn.dropout(y1, 0.5) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + global_step = tf.Variable(0) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, fc1_size], stddev=np.sqrt(2.0 / (image_size * image_size)))) + b1 = tf.Variable(tf.zeros([fc1_size])) + W2 = tf.Variable(tf.truncated_normal([fc1_size, fc2_size], stddev=np.sqrt(2.0 / fc1_size))) + b2 = tf.Variable(tf.zeros([fc2_size])) + W3 = tf.Variable(tf.truncated_normal([fc2_size, fc3_size], stddev=np.sqrt(2.0 / fc2_size))) + b3 = tf.Variable(tf.zeros([fc3_size])) + W4 = tf.Variable(tf.truncated_normal([fc3_size, num_labels], stddev=np.sqrt(2.0 / fc3_size))) + b4 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + logits = tf.matmul(y3, W4) + b4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + valid_logits = tf.matmul(y3_valid, W4) + b4 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + test_logits = tf.matmul(y3_test, W4) + b4 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) +print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/2.txt b/dataset/preprocessed/2.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/2.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/20.txt b/dataset/preprocessed/20.txt new file mode 100644 index 0000000..37e587e --- /dev/null +++ b/dataset/preprocessed/20.txt @@ -0,0 +1,94 @@ +import tensorflow as tf +import input_data +import cv2 +import numpy as np +import math +from scipy import ndimage +import os + + +def train(iterations): + mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) + x_input = tf.placeholder('float', [None, 784], name='x_input') + y_target = tf.placeholder('float', [None, 10], name='y_target') + x_input_image = tf.reshape(x_input, [-1, 28, 28, 1]) + init_random_dist_1 = tf.truncated_normal([6, 6, 1, 32], stddev=0.1) + W_layer_1 = tf.get_variable('W_layer_1', shape=[6, 6, 1, 32], initializer=tf.contrib.layers.xavier_initializer()) + b_layer_1 = tf.get_variable('b_layer_1', shape=[32], initializer=tf.contrib.layers.xavier_initializer()) + convolution_layaer_1 = tf.nn.relu(tf.nn.conv2d(x_input_image, W_layer_1, strides=[1, 1, 1, 1], padding='SAME') + b_layer_1) + convolution_pooling_1 = tf.nn.max_pool(convolution_layaer_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + init_random_dist_2 = tf.truncated_normal([6, 6, 32, 64], stddev=0.1) + W_layer_2 = tf.get_variable('W_layer_2', shape=[6, 6, 32, 64], initializer=tf.contrib.layers.xavier_initializer()) + b_layer_2 = tf.get_variable('b_layer_2', shape=[64], initializer=tf.contrib.layers.xavier_initializer()) + convolution_layaer_2 = tf.nn.relu(tf.nn.conv2d(convolution_pooling_1, W_layer_2, strides=[1, 1, 1, 1], padding='SAME') + b_layer_2) + convolution_pooling_2 = tf.nn.max_pool(convolution_layaer_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + covolution_layer_2_flat = tf.reshape(convolution_pooling_2, [-1, 7 * 7 * 64]) + init_random_dist_fully_layer = tf.truncated_normal([int(covolution_layer_2_flat.get_shape()[1]), 1024], stddev=0.1) + W_fully_layer = tf.get_variable('W_fully_layer', shape=[int(covolution_layer_2_flat.get_shape()[1]), 1024], initializer=tf.contrib.layers.xavier_initializer()) + b_fully_layer = tf.get_variable('b_fully_layer', shape=[1024], initializer=tf.contrib.layers.xavier_initializer()) + fully_connected_layer = tf.nn.relu(tf.matmul(covolution_layer_2_flat, W_fully_layer) + b_fully_layer) + hold_prob = tf.placeholder(tf.float32) + fully_connected_layer_dropout = tf.nn.dropout(fully_connected_layer, keep_prob=hold_prob) + init_random_dist_final = tf.truncated_normal([int(fully_connected_layer_dropout.get_shape()[1]), 10], stddev=0.1) + W_final = tf.get_variable('W_final', shape=[int(fully_connected_layer_dropout.get_shape()[1]), 10], initializer=tf.contrib.layers.xavier_initializer()) + b_final = tf.get_variable('b_final', shape=[10], initializer=tf.contrib.layers.xavier_initializer()) + y_prediction = tf.nn.relu(tf.matmul(fully_connected_layer_dropout, W_final) + b_final) + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_target, logits=y_prediction)) + optimizer = tf.train.AdamOptimizer(learning_rate=0.0001) + train = optimizer.minimize(cross_entropy) + saver = tf.train.Saver(save_relative_paths=True) + config = tf.ConfigProto(device_count={'GPU': 0}) + session = tf.Session(config=config) + with session: + session.run(tf.global_variables_initializer()) + for i in range(iterations): + batch_xs, batch_ys = mnist.train.next_batch(100) + session.run(train, feed_dict={x_input: batch_xs, y_target: batch_ys, hold_prob: 0.2}) + correct_prediction = tf.equal(tf.argmax(y_prediction, 1), tf.argmax(y_target, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + if i % 100 == 0: + print(session.run(accuracy, feed_dict={x_input: mnist.test.images, y_target: mnist.test.labels, hold_prob: 1.0})) + saver.save(session, './model') + + +def make_prediction(image_1): + print('making a prediction....') + tf.reset_default_graph() + x_input = tf.placeholder('float', [None, 784], name='x_input') + y_target = tf.placeholder('float', [None, 10], name='y_target') + x_input_image = tf.reshape(x_input, [-1, 28, 28, 1]) + init_random_dist_1 = tf.truncated_normal([6, 6, 1, 32], stddev=0.1) + W_layer_1 = tf.get_variable('W_layer_1', shape=[6, 6, 1, 32], initializer=tf.contrib.layers.xavier_initializer()) + b_layer_1 = tf.get_variable('b_layer_1', shape=[32], initializer=tf.contrib.layers.xavier_initializer()) + convolution_layaer_1 = tf.nn.relu(tf.nn.conv2d(x_input_image, W_layer_1, strides=[1, 1, 1, 1], padding='SAME') + b_layer_1) + convolution_pooling_1 = tf.nn.max_pool(convolution_layaer_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + init_random_dist_2 = tf.truncated_normal([6, 6, 32, 64], stddev=0.1) + W_layer_2 = tf.get_variable('W_layer_2', shape=[6, 6, 32, 64], initializer=tf.contrib.layers.xavier_initializer()) + b_layer_2 = tf.get_variable('b_layer_2', shape=[64], initializer=tf.contrib.layers.xavier_initializer()) + convolution_layaer_2 = tf.nn.relu(tf.nn.conv2d(convolution_pooling_1, W_layer_2, strides=[1, 1, 1, 1], padding='SAME') + b_layer_2) + convolution_pooling_2 = tf.nn.max_pool(convolution_layaer_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + covolution_layer_2_flat = tf.reshape(convolution_pooling_2, [-1, 7 * 7 * 64]) + init_random_dist_fully_layer = tf.truncated_normal([int(covolution_layer_2_flat.get_shape()[1]), 1024], stddev=0.1) + W_fully_layer = tf.get_variable('W_fully_layer', shape=[int(covolution_layer_2_flat.get_shape()[1]), 1024], initializer=tf.contrib.layers.xavier_initializer()) + b_fully_layer = tf.get_variable('b_fully_layer', shape=[1024], initializer=tf.contrib.layers.xavier_initializer()) + fully_connected_layer = tf.nn.relu(tf.matmul(covolution_layer_2_flat, W_fully_layer) + b_fully_layer) + hold_prob = tf.placeholder(tf.float32) + fully_connected_layer_dropout = tf.nn.dropout(fully_connected_layer, keep_prob=hold_prob) + init_random_dist_final = tf.truncated_normal([int(fully_connected_layer_dropout.get_shape()[1]), 10], stddev=0.1) + W_final = tf.get_variable('W_final', shape=[int(fully_connected_layer_dropout.get_shape()[1]), 10], initializer=tf.contrib.layers.xavier_initializer()) + b_final = tf.get_variable('b_final', shape=[10], initializer=tf.contrib.layers.xavier_initializer()) + y_prediction = tf.nn.relu(tf.matmul(fully_connected_layer_dropout, W_final) + b_final) + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_target, logits=y_prediction)) + optimizer = tf.train.AdamOptimizer(learning_rate=0.0001) + train = optimizer.minimize(cross_entropy) + flatten = image_1.flatten() / 255.0 + images = np.zeros((1, 784)) + correct_val = np.zeros((1, 10)) + images[0] = flatten + new_saver = tf.train.Saver() + with tf.Session() as sess: + new_saver = tf.train.Saver(tf.all_variables()) + new_saver = tf.train.import_meta_graph('model.meta') + new_saver.restore(sess, 'model') + prediction = tf.argmax(y_prediction, 1) + print('Model predicted...', sess.run(prediction, feed_dict={x_input: images, hold_prob: 1.0})) diff --git a/dataset/preprocessed/200.txt b/dataset/preprocessed/200.txt new file mode 100644 index 0000000..3e85392 --- /dev/null +++ b/dataset/preprocessed/200.txt @@ -0,0 +1,78 @@ +import sys +import numpy as np +import tensorflow as tf +import cv2 +NUM_CLASSES = 9 +IMAGE_SIZE = 28 +IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE * 3 + + +def inference(images_placeholder, keep_prob): + """ モデルを作成する関数 + + 引数: + images_placeholder: inputs()で作成した画像のplaceholder + keep_prob: dropout率のplace_holder + + 返り値: + cross_entropy: モデルの計算結果 + """ + + 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) + + def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + x_image = tf.reshape(images_placeholder, [-1, IMAGE_SIZE, IMAGE_SIZE, 3]) + with tf.name_scope('conv1') as scope: + W_conv1 = weight_variable([5, 5, 3, 32]) + b_conv1 = bias_variable([32]) + h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + with tf.name_scope('pool1') as scope: + h_pool1 = max_pool_2x2(h_conv1) + with tf.name_scope('conv2') as scope: + W_conv2 = weight_variable([5, 5, 32, 64]) + b_conv2 = bias_variable([64]) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + with tf.name_scope('pool2') as scope: + h_pool2 = max_pool_2x2(h_conv2) + with tf.name_scope('fc1') as scope: + W_fc1 = weight_variable([7 * 7 * 64, 1024]) + b_fc1 = bias_variable([1024]) + h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + with tf.name_scope('fc2') as scope: + W_fc2 = weight_variable([1024, NUM_CLASSES]) + b_fc2 = bias_variable([NUM_CLASSES]) + with tf.name_scope('softmax') as scope: + y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) + return y_conv + + +if __name__ == '__main__': + test_image = [] + for i in range(1, len(sys.argv)): + img = cv2.imread(sys.argv[i]) + img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE)) + test_image.append(img.flatten().astype(np.float32) / 255.0) + test_image = np.asarray(test_image) + images_placeholder = tf.placeholder('float', shape=(None, IMAGE_PIXELS)) + labels_placeholder = tf.placeholder('float', shape=(None, NUM_CLASSES)) + keep_prob = tf.placeholder('float') + logits = inference(images_placeholder, keep_prob) + session = tf.InteractiveSession() + saver = tf.train.Saver() + session.run(tf.global_variables_initializer()) + saver.restore(session, 'model.ckpt') + for i in range(len(test_image)): + pred = np.argmax(logits.eval(feed_dict={images_placeholder: [test_image[i]], keep_prob: 1.0})[0]) + print(pred) diff --git a/dataset/preprocessed/201.txt b/dataset/preprocessed/201.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/201.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/202.txt b/dataset/preprocessed/202.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/202.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/203.txt b/dataset/preprocessed/203.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/203.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/204.txt b/dataset/preprocessed/204.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/204.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/205.txt b/dataset/preprocessed/205.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/205.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/206.txt b/dataset/preprocessed/206.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/206.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/207.txt b/dataset/preprocessed/207.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/207.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/209.txt b/dataset/preprocessed/209.txt new file mode 100644 index 0000000..d34d2fd --- /dev/null +++ b/dataset/preprocessed/209.txt @@ -0,0 +1,181 @@ +""" +Created on Fri Jun 16 17:42:27 2017 + +@author: kashu +""" +import os +import numpy as np +import pandas as pd +import tensorflow as tf +filenameTrain = 'all_train.csv' +filenameTest = 'all_test.csv' +data_root = 'C:/MASTERS/AdvancedML/Exercise/all_massData' +dest_filenameTrain = os.path.join(data_root, filenameTrain) +dest_filenameTest = os.path.join(data_root, filenameTest) +dataTrain = pd.read_csv(dest_filenameTrain) +dataTest = pd.read_csv(dest_filenameTest) +completeDataTrain = np.array(dataTrain, dtype='float32') +completeDataTest = np.array(dataTest, dtype='float32') +lablesTrain = completeDataTrain[:, 0:1] +completeDataTrain = completeDataTrain[:, 1:29] +lablesTest = completeDataTest[:, 0:1] +completeDataTest = completeDataTest[:, 1:29] +num_labels = 1 +num_features = 28 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(predictions == labels) / predictions.shape[0] + + +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(completeDataTrain[:train_subset, :]) + tf_train_labels = tf.constant(lablesTrain[:train_subset]) + tf_valid_dataset = tf.constant(completeDataTrain[train_subset:20000, :]) + tf_test_dataset = tf.constant(completeDataTest[:train_subset, :]) + valid_labels = lablesTrain[train_subset:20000] + test_labels = lablesTest[:train_subset] + weights = tf.Variable(tf.truncated_normal([num_features, num_labels], mean=0.0, stddev=1.0, dtype=tf.float32)) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.sigmoid(logits) + valid_prediction = tf.nn.sigmoid(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.sigmoid(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 10001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy(predictions, lablesTrain[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +twoThird = int(2 / 3 * completeDataTrain.shape[0]) +trainingData = completeDataTrain[:twoThird] +trainingLabels = lablesTrain[:twoThird] +validData = completeDataTrain[twoThird:] +validLabels = lablesTrain[twoThird:] +del dest_filenameTest, dest_filenameTrain, filenameTest, filenameTrain +del dataTest, dataTrain, data_root, lablesTrain, completeDataTrain +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, num_features)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(validData) + tf_test_dataset = tf.constant(completeDataTest) + weights = tf.Variable(tf.truncated_normal([num_features, num_labels], mean=0.0, stddev=1.0, dtype=tf.float32)) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) + train_prediction = tf.nn.sigmoid(logits) + valid_prediction = tf.nn.sigmoid(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.sigmoid(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 5500001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (trainingLabels.shape[0] - batch_size) + batch_data = trainingData[offset:offset + batch_size, :] + batch_labels = trainingLabels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('offset', offset) + print(trainingLabels.shape[0]) + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), validLabels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), lablesTest)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, num_features)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(validData) + tf_test_dataset = tf.constant(completeDataTest) + weights = tf.Variable(tf.truncated_normal([num_features, num_labels], mean=0.0, stddev=1.0, dtype=tf.float32)) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.AdamOptimizer(0.05).minimize(loss) + train_prediction = tf.nn.sigmoid(logits) + valid_prediction = tf.nn.sigmoid(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.sigmoid(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 2000001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (trainingLabels.shape[0] - batch_size) + batch_data = trainingData[offset:offset + batch_size, :] + batch_labels = trainingLabels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('offset', offset) + print(trainingLabels.shape[0]) + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), validLabels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), lablesTest)) +twoThird = int(2 / 3 * completeDataTrain.shape[0]) +trainingData = completeDataTrain[:5000] +trainingLabels = lablesTrain[:5000] +validData = completeDataTrain[twoThird:twoThird + 1000] +validLabels = lablesTrain[twoThird:twoThird + 1000] +testData = completeDataTest[:1000] +testLabels = lablesTest[:1000] +del dest_filenameTest, dest_filenameTrain, filenameTest, filenameTrain +del dataTest, dataTrain, data_root, lablesTrain, completeDataTrain, completeDataTest, lablesTest +num_nodes = 3 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, num_features)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(validData) + tf_test_dataset = tf.constant(testData) + weights_1 = tf.Variable(tf.zeros([num_features, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable(tf.zeros([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + sigmoid_layer = tf.nn.sigmoid(logits_1) + logits_2 = tf.matmul(sigmoid_layer, weights_2) + biases_2 + loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_2)) + optimizer = tf.train.AdadeltaOptimizer(0.05).minimize(loss) + train_prediction = tf.nn.sigmoid(logits_2) + logits_1 = tf.matmul(tf_valid_dataset, weights_1) + biases_1 + sigmoid_layer = tf.nn.sigmoid(logits_1) + logits_2 = tf.matmul(sigmoid_layer, weights_2) + biases_2 + valid_prediction = tf.nn.sigmoid(logits_2) + logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + sigmoid_layer = tf.nn.sigmoid(logits_1) + logits_2 = tf.matmul(sigmoid_layer, weights_2) + biases_2 + test_prediction = tf.nn.sigmoid(logits_2) +num_steps = 300000 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (trainingLabels.shape[0] - batch_size) + batch_data = trainingData[offset:offset + batch_size, :] + batch_labels = trainingLabels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 1000 == 0: + print('offset', offset) + print(trainingLabels.shape[0]) + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), validLabels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), testLabels)) diff --git a/dataset/preprocessed/21.txt b/dataset/preprocessed/21.txt new file mode 100644 index 0000000..3087bda --- /dev/null +++ b/dataset/preprocessed/21.txt @@ -0,0 +1,51 @@ +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + data_do = tf.nn.dropout(data, keep_prob=0.9) + output1 = tf.matmul(data_do, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 28 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 2001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/210.txt b/dataset/preprocessed/210.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/210.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/211.txt b/dataset/preprocessed/211.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/211.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/212.txt b/dataset/preprocessed/212.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/212.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/214.txt b/dataset/preprocessed/214.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/214.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/215.txt b/dataset/preprocessed/215.txt new file mode 100644 index 0000000..3087bda --- /dev/null +++ b/dataset/preprocessed/215.txt @@ -0,0 +1,51 @@ +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + data_do = tf.nn.dropout(data, keep_prob=0.9) + output1 = tf.matmul(data_do, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 28 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 2001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/216.txt b/dataset/preprocessed/216.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/216.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/217.txt b/dataset/preprocessed/217.txt new file mode 100644 index 0000000..ecfd668 --- /dev/null +++ b/dataset/preprocessed/217.txt @@ -0,0 +1,111 @@ +import tensorflow as tf +import numpy as np +from tensorflow.examples.tutorials.mnist import input_data + + +def add_layer(inputs, in_size, out_size, activation_function=None): + Weights = tf.Variable(tf.random_normal([in_size, out_size])) + biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) + Wx_plus_b = tf.matmul(inputs, Weights) + biases + if activation_function is None: + outputs = Wx_plus_b + else: + outputs = activation_function(Wx_plus_b) + return outputs + + +def compute_accuracy(v_xs, v_ys): + global prediction + y_pre = session.run(prediction, feed_dict={xs: v_xs, keep_prob: 1}) + correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + result = session.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1}) + return result + + +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) + + +def conv2d(x, filter): + return tf.nn.conv2d(x, filter, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +mnist = input_data.read_data_sets('MNIST_data', one_hot=True) +xs = tf.placeholder(tf.float32, [None, 28 * 28]) / 255.0 +ys = tf.placeholder(tf.float32, [None, 10]) +keep_prob = tf.placeholder(tf.float32) +x_image = tf.reshape(xs, [-1, 28, 28, 1]) +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1])) +train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) +session = tf.Session() +if int(tf.VERSION.split('.')[1]) < 12 and int(tf.VERSION.split('.')[0]) < 1: + init = tf.initialize_all_variables() +else: + init = tf.global_variables_initializer() +session.run(init) +for i in range(500): + batch_xs, batch_ys = mnist.train.next_batch(100) + session.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5}) + if i % 50 == 0: + print(compute_accuracy(mnist.test.images, mnist.test.labels)) +_W_conv1 = W_conv1.eval(session) +_b_conv1 = b_conv1.eval(session) +_W_conv2 = W_conv2.eval(session) +_b_conv2 = b_conv2.eval(session) +_W_fc1 = W_fc1.eval(session) +_b_fc1 = b_fc1.eval(session) +_W_fc2 = W_fc2.eval(session) +_b_fc2 = b_fc2.eval(session) +session.close() +g_2 = tf.Graph() +with g_2.as_default(): + x_export = tf.placeholder(tf.float32, [None, 28 * 28], name='input') + x_image_export = tf.reshape(x_export, [-1, 28, 28, 1]) + W_conv1_export = tf.constant(_W_conv1, name='constant_W_conv1') + b_conv1_export = tf.constant(_b_conv1, name='constant_b_conv1') + h_conv1_export = tf.nn.relu(conv2d(x_image_export, W_conv1_export) + b_conv1_export) + h_pool1_export = max_pool_2x2(h_conv1_export) + W_conv2_export = tf.constant(_W_conv2, name='constant_W_conv2') + b_conv2_export = tf.constant(_b_conv2, name='constant_b_conv2') + h_conv2_export = tf.nn.relu(conv2d(h_pool1_export, W_conv2_export) + b_conv2_export) + h_pool2_export = max_pool_2x2(h_conv2_export) + W_fc1_export = tf.constant(_W_fc1, name='constant_W_fc1') + b_fc1_export = tf.constant(_b_fc1, name='constant_b_fc1') + h_pool2_flat_export = tf.reshape(h_pool2_export, [-1, 7 * 7 * 64]) + h_fc1_export = tf.nn.relu(tf.matmul(h_pool2_flat_export, W_fc1_export) + b_fc1_export) + W_fc2_export = tf.constant(_W_fc2, name='constant_W_fc2') + b_fc2_export = tf.constant(_b_fc2, name='constant_b_fc2') + prediction = tf.nn.softmax(tf.matmul(h_fc1_export, W_fc2_export) + b_fc2_export, name='output') + sess_2 = tf.Session() + sess_2.run(tf.global_variables_initializer()) + graph_def = g_2.as_graph_def() + tf.train.write_graph(graph_def, './model/beginner-export', 'beginner-graph-CNN.pb', as_text=False) + sess_2.close() + print('export data') diff --git a/dataset/preprocessed/218.txt b/dataset/preprocessed/218.txt new file mode 100644 index 0000000..eec94d6 --- /dev/null +++ b/dataset/preprocessed/218.txt @@ -0,0 +1,174 @@ +import tensorflow as tf +import numpy as np +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + self.rewards = tf.placeholder(tf.float32, shape=(None,), name='reward') + self.indices = tf.placeholder(tf.int32, shape=(None, 2), name='indices') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + self.select_prob = tf.gather_nd(self.prob, self.indices) + self.result = tf.multiply(self.select_prob, self.rewards) + self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + self.pg_optimization = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(self.pg_loss) + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + self.saver = tf.train.Saver() + self.saved_path = './saved_model' + self.count = 0 + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def policy_train(self, session, inputs, outputs, rewards): + form, pos, deprel = inputs + indices = [] + for aid in range(len(outputs)): + indices.append([aid, outputs[aid]]) + _, cost, indices, prob, res = session.run([self.pg_optimization, self.pg_loss, self.indices, self.prob, self.result], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) + self.count += 1 + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + def variable_output(self, session): + form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + def save(self, session): + self.saved_path = self.saver.save(session, './saved_model') + + def restore(self, session): + self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + session.run(self.update_op) diff --git a/dataset/preprocessed/22.txt b/dataset/preprocessed/22.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/22.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/220.txt b/dataset/preprocessed/220.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/220.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/221.txt b/dataset/preprocessed/221.txt new file mode 100644 index 0000000..68d3f86 --- /dev/null +++ b/dataset/preprocessed/221.txt @@ -0,0 +1,153 @@ +import numpy as np +import tensorflow as tf +from src.nn.config import NnConfig as config +from src.nn.config import NnConst as const +from src.nn.config import setup_training_environment, load_training_data_set + + +def create_fully_connected_layer(name, in_node, in_size, num_of_neurons, is_output_layer, dropout): + """ + Create fully connected layer's variables and use them to transform in-node + into out-node with optional activation and specified dropout. There is no + need to add activation rescaling for no-dropout case, because tensorflow + rescales outputs with dropout automatically (scales up by 1 / keep_prob). + + :param name: name which will be given to the layer + :param in_node: input tensorflow graph node (vector) + :param in_size: size of the input vector + :param num_of_neurons: number of neurons for created layer + :param is_output_layer: states whether created layer is network's last + layer. In this case no activation function is + used + :param dropout: probability of dropout for this layer (used in training) + :return: + """ + weights = tf.get_variable(name + '/weights', shape=[in_size, num_of_neurons], initializer=tf.contrib.layers.xavier_initializer()) + biases = tf.get_variable(name + '/biases', shape=[1, num_of_neurons], initializer=tf.constant_initializer()) + out = tf.matmul(in_node, weights) + biases + tf.summary.histogram(weights.op.name, weights) + tf.summary.histogram(biases.op.name, biases) + tf.summary.histogram(out.op.name, out) + if is_output_layer: + return tf.nn.dropout(out, 1 - dropout) + else: + return tf.nn.dropout(tf.maximum(out, out * 0.1), 1 - dropout) + + +def create_graph(in_placeholder, in_size, layer_sizes, labels, learning_rate_placeholder, dropout_placeholder): + """ + Creates graph for basic neural network both for training and teest case. + + :param in_placeholder: placeholder for input data batch + :param in_size: size of input vector + :param layer_sizes: list of neurons count for each layer + :param labels: set of labels used to evaluate network + :param learning_rate_placeholder: placeholder for network run's learning + rate + :param dropout_placeholder: placeholder for network's dropout probability + :return: created out, loss, train and accuracy nodes + """ + previous_in = in_placeholder + previous_in_size = in_size + for layer_size, i in zip(layer_sizes, range(len(layer_sizes))): + previous_in = create_fully_connected_layer('layer_{}'.format(i), previous_in, previous_in_size, layer_size, i == len(layer_sizes) - 1, dropout_placeholder) + previous_in_size = layer_size + out = tf.nn.softmax(previous_in) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=previous_in)) + train = tf.train.AdamOptimizer(learning_rate_placeholder).minimize(loss) + accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(previous_in, axis=1), tf.argmax(labels, axis=1)), tf.float32)) + return out, loss, train, accuracy + + +def perform_usage_run(run_index, data_set_path, out_path): + """ + Perform single network's forward pass by using best model saved for given + run index. Save results to output file on given path. + + :param run_index: index of run which will be source for used model + :param data_set_path: path to data set file which will be processed + :param out_path: path to file which will contain forward pass results + :return: - + """ + usage_model_path = const.USAGE_MODEL_PATH_PATTERN.format(run_index) + data = np.load(data_set_path) + in_placeholder = tf.placeholder(tf.float32, shape=[None, config.IN_SIZE]) + labels_placeholder = tf.placeholder(tf.float32, shape=[None, config.NUM_OF_LABELS]) + learning_rate_placeholder = tf.placeholder(tf.float32, shape=[]) + dropout_placeholder = tf.placeholder(tf.float32, shape=[]) + out, _, _, _ = create_graph(in_placeholder, config.IN_SIZE, config.LAYER_SIZES, labels_placeholder, learning_rate_placeholder, dropout_placeholder) + session = tf.Session() + session.run(tf.global_variables_initializer()) + saver = tf.train.Saver() + saver.restore(session, usage_model_path) + calculated_out = session.run([out], feed_dict={in_placeholder: data, dropout_placeholder: config.NO_DROPOUT}) + np.save(out_path, calculated_out) + + +def train_network(run_index, set_index, mode, learning_rate): + """ + Start/continue basic nn training for given run index and with data set + described by set index. + + :param run_index: + :param set_index: + :param mode: + :param learning_rate: + :return: + """ + data_set_dir, train_logs_dir, validation_logs_dir, checkpoints_dir, best_model_dir = setup_training_environment(const, run_index, set_index, mode) + train_data, train_labels, validation_data, validation_labels, test_data, test_labels = load_training_data_set(data_set_dir) + in_placeholder = tf.placeholder(tf.float32, shape=[None, config.IN_SIZE]) + labels_placeholder = tf.placeholder(tf.float32, shape=[None, config.NUM_OF_LABELS]) + learning_rate_placeholder = tf.placeholder(tf.float32, shape=[]) + dropout_placeholder = tf.placeholder(tf.float32, shape=[]) + out, loss, train, accuracy = create_graph(in_placeholder, config.IN_SIZE, config.LAYER_SIZES, labels_placeholder, learning_rate_placeholder, dropout_placeholder) + tf.summary.scalar('loss', loss) + tf.summary.scalar('accuracy', accuracy) + merged_summary = tf.summary.merge_all() + train_logger = tf.summary.FileWriter(train_logs_dir) + validation_logger = tf.summary.FileWriter(validation_logs_dir) + session = tf.Session() + session.run(tf.global_variables_initializer()) + lowest_loss = None + if config.MODE == config.CONTINUE_MODE: + pass + saver = tf.train.Saver(max_to_keep=config.MAX_CHECKPOINTS) + for i in range(config.MAX_ITERATIONS): + batch_indices = np.random.permutation(train_data.shape[0]) + batch_indices = batch_indices[:config.TRAIN_BATCH_SIZE] + batch_data = train_data[batch_indices] + batch_labels = train_labels[batch_indices] + summary, _ = session.run([merged_summary, train], feed_dict={in_placeholder: batch_data, labels_placeholder: batch_labels, learning_rate_placeholder: learning_rate, dropout_placeholder: config.DROPOUT}) + train_logger.add_summary(summary, i) + if i % config.CHECKPOINT_INTERVAL == 0: + saver.save(session, checkpoints_dir + '/model_checkpoint', i) + if i % config.VALIDATION_INTERVAL == 0: + summary, loss_value, accuracy_value = session.run([merged_summary, loss, accuracy], feed_dict={in_placeholder: validation_data, labels_placeholder: validation_labels, dropout_placeholder: config.NO_DROPOUT}) + if lowest_loss is None or loss_value < lowest_loss: + lowest_loss = loss_value + saver.save(session, best_model_dir + '/best_model') + validation_logger.add_summary(summary, i) + print('Iteration {}:\n\tLoss: {}\n\tAccuracy: {}\n'.format(i, loss_value, accuracy_value)) + if i % config.DECAY_INTERVAL == 0 and i != 0: + learning_rate *= config.LEARNING_RATE_DECAY + saver.save(session, checkpoints_dir + '/model_checkpoint_last') + saver.restore(session, best_model_dir + '/best_model') + loss_value, accuracy_value = session.run([loss, accuracy], feed_dict={in_placeholder: test_data, labels_placeholder: test_labels, dropout_placeholder: config.NO_DROPOUT}) + print('Final Loss: {}\n\tFinal Accuracy: {}\n'.format(loss_value, accuracy_value)) + + +def main(MODE): + """ + Main function of this script. + :param MODE: run mode + :return: - + """ + if MODE == const.USE_MODE: + perform_usage_run(const.RUN_INDEX, const.USAGE_DATA_SET_PATH, const.USAGE_OUT_PATH) + else: + train_network(const.RUN_INDEX, const.SET_INDEX, const.MODE, config.LEARNING_RATE) + + +if __name__ == '__main__': + main(const.USE_MODE) diff --git a/dataset/preprocessed/222.txt b/dataset/preprocessed/222.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/222.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/223.txt b/dataset/preprocessed/223.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/223.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/224.txt b/dataset/preprocessed/224.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/224.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/225.txt b/dataset/preprocessed/225.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/225.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/226.txt b/dataset/preprocessed/226.txt new file mode 100644 index 0000000..a24e052 --- /dev/null +++ b/dataset/preprocessed/226.txt @@ -0,0 +1,134 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy(predictions, train_labels[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/227.txt b/dataset/preprocessed/227.txt new file mode 100644 index 0000000..87312a2 --- /dev/null +++ b/dataset/preprocessed/227.txt @@ -0,0 +1,68 @@ +""" Uses Tensorflow to identify digits in the MNIST hand-written digit data set """ +""" Uses multiple fully-connected layers """ +import tensorflow as tf +import math +from tensorflow.examples.tutorials.mnist import input_data +IMAGE_PIXEL_COUNT = 784 +layer_sizes = [400, 200, 100, 60, 30, 10] +weights = [tf.Variable(tf.truncated_normal([IMAGE_PIXEL_COUNT, layer_sizes[0]], stddev=0.2)), tf.Variable(tf.truncated_normal([layer_sizes[0], layer_sizes[1]], stddev=0.2)), tf.Variable(tf.truncated_normal([layer_sizes[1], layer_sizes[2]], stddev=0.2)), tf.Variable(tf.truncated_normal([layer_sizes[2], layer_sizes[3]], stddev=0.2)), tf.Variable(tf.truncated_normal([layer_sizes[3], layer_sizes[4 + ]], stddev=0.2)), tf.Variable(tf.truncated_normal([layer_sizes[4], layer_sizes[5]], stddev=0.2))] +biases = [tf.zeros([layer_sizes[0]]), tf.zeros([layer_sizes[1]]), tf.zeros([layer_sizes[2]]), tf.zeros([layer_sizes[3]]), tf.zeros([layer_sizes[4]]), tf.zeros([layer_sizes[5]])] +learning_rate_placeholder = tf.placeholder(tf.float32) +keep_probability = tf.placeholder(tf.float32) +TRAINING_KEEP_PROBABILITY = 0.75 +TEST_KEEP_PROBABILITY = 1.0 +mnist_data = input_data.read_data_sets('mnist_data/', reshape=False, one_hot=True) +images = tf.placeholder(tf.float32, [None, 28, 28, 1]) +flattened_images = tf.reshape(images, [-1, IMAGE_PIXEL_COUNT]) +layer1 = tf.nn.relu(tf.matmul(flattened_images, weights[0]) + biases[0]) +layer1_dropout = tf.nn.dropout(layer1, keep_probability) +layer2 = tf.nn.relu(tf.matmul(layer1_dropout, weights[1]) + biases[1]) +layer2_dropout = tf.nn.dropout(layer2, keep_probability) +layer3 = tf.nn.relu(tf.matmul(layer2_dropout, weights[2]) + biases[2]) +layer3_dropout = tf.nn.dropout(layer3, keep_probability) +layer4 = tf.nn.relu(tf.matmul(layer3_dropout, weights[3]) + biases[3]) +layer4_dropout = tf.nn.dropout(layer4, keep_probability) +layer5 = tf.nn.relu(tf.matmul(layer4_dropout, weights[4]) + biases[4]) +layer5_dropout = tf.nn.dropout(layer5, keep_probability) +logits = tf.matmul(layer5_dropout, weights[5]) + biases[5] +layer6 = tf.nn.softmax(logits) +model_answers = tf.placeholder(tf.float32, [None, 10]) +cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, model_answers) +cross_entropy = tf.reduce_mean(cross_entropy) * 100 +is_correct = tf.equal(tf.argmax(layer6, 1), tf.argmax(model_answers, 1)) +accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) +optimizer = tf.train.AdamOptimizer(learning_rate_placeholder) +training_step = optimizer.minimize(cross_entropy) +init = tf.global_variables_initializer() +session = tf.Session() +session.run(init) +training_results = [] +test_results = [] + + +def run_training_step(results, lr): + curr_images, currmodel_answers = mnist_data.train.next_batch(100) + training_data = {images: curr_images, model_answers: currmodel_answers, learning_rate_placeholder: lr, keep_probability: TRAINING_KEEP_PROBABILITY} + session.run(training_step, feed_dict=training_data) + results.append(session.run([accuracy, cross_entropy], feed_dict=training_data)) + + +def run_test_step(results, lr): + print('.', end='', flush=True) + results.append(session.run([accuracy, cross_entropy], feed_dict={images: mnist_data.test.images, model_answers: mnist_data.test.labels, learning_rate_placeholder: lr, keep_probability: TEST_KEEP_PROBABILITY})) + + +for i in range(10000): + min_learning_rate = 0.0001 + max_learning_rate = 0.004 + decay_speed = 1800.0 + lr = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i / decay_speed) + run_training_step(training_results, lr) + if i % 100 == 0: + run_test_step(test_results, lr) +run_test_step(test_results, lr) +for i, step in enumerate(training_results): + print('Training Step #' + str(i) + ': ' + str(step)) +for i, step in enumerate(test_results): + print('Test Step #' + str(i) + ': ' + str(step)) diff --git a/dataset/preprocessed/228.txt b/dataset/preprocessed/228.txt new file mode 100644 index 0000000..be5e1ab --- /dev/null +++ b/dataset/preprocessed/228.txt @@ -0,0 +1,514 @@ +import numpy as np +import tensorflow as tf +import os +import argparse +import struct +import cv2 as cv +from tensorflow.python.tools import optimize_for_inference_lib +from tensorflow.tools.graph_transforms import TransformGraph +np.random.seed(2701) + + +def gen_data(placeholder): + shape = placeholder.shape.as_list() + shape[0] = shape[0] if shape[0] else 1 + return np.random.standard_normal(shape).astype(placeholder.dtype.as_numpy_dtype()) + + +def prepare_for_dnn(sess, graph_def, in_node, out_node, out_graph, dtype, optimize=True, quantize=False): + graph_def = tf.graph_util.convert_variables_to_constants(sess, graph_def, [out_node]) + if optimize: + graph_def = optimize_for_inference_lib.optimize_for_inference(graph_def, [in_node], [out_node], dtype.as_datatype_enum) + transforms = ['fold_constants(ignore_errors=True)'] + if quantize: + transforms += ['quantize_weights(minimum_size=0)'] + transforms += ['sort_by_execution_order'] + graph_def = TransformGraph(graph_def, [in_node], [out_node], transforms) + with tf.gfile.FastGFile(out_graph, 'wb') as f: + f.write(graph_def.SerializeToString()) + + +tf.reset_default_graph() +tf.Graph().as_default() +tf.set_random_seed(324) +sess = tf.Session() +isTraining = tf.placeholder(tf.bool, name='isTraining') + + +def writeBlob(data, name): + if data.ndim == 4: + np.save(name + '.npy', data.transpose(0, 3, 1, 2).astype(np.float32)) + elif data.ndim == 5: + np.save(name + '.npy', data.transpose(0, 4, 1, 2, 3).astype(np.float32)) + else: + np.save(name + '.npy', data.astype(np.float32)) + + +def runModel(inp, out, name): + with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + with tf.gfile.FastGFile(name + '_net.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + tf.import_graph_def(graph_def, name='') + inputData = gen_data(inp) + outputData = localSession.run(localSession.graph.get_tensor_by_name(out.name), feed_dict={localSession.graph.get_tensor_by_name(inp.name): inputData}) + writeBlob(inputData, name + '_in') + writeBlob(outputData, name + '_out') + + +def save(inp, out, name, quantize=False, optimize=True): + sess.run(tf.global_variables_initializer()) + inputData = gen_data(inp) + outputData = sess.run(out, feed_dict={inp: inputData, isTraining: False}) + writeBlob(inputData, name + '_in') + writeBlob(outputData, name + '_out') + prepare_for_dnn(sess, sess.graph.as_graph_def(), inp.name[:inp.name.rfind(':')], out.name[:out.name.rfind(':')], name + '_net.pb', inp.dtype, optimize, quantize) + with tf.gfile.FastGFile(name + '_net.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + for node in graph_def.node: + if 'value' in node.attr: + halfs = node.attr['value'].tensor.half_val + if not node.attr['value'].tensor.tensor_content and halfs: + node.attr['value'].tensor.tensor_content = struct.pack('H' * len(halfs), *halfs) + node.attr['value'].tensor.ClearField('half_val') + tf.train.write_graph(graph_def, '', name + '_net.pb', as_text=False) + + +for dtype, prefix in zip([tf.float32, tf.float16], ['', 'fp16_']): + inp = tf.placeholder(dtype, [1, 6, 5, 3], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=3, kernel_size=[1, 1], activation=tf.nn.relu, bias_initializer=tf.random_normal_initializer()) + save(inp, conv, prefix + 'single_conv') + inp = tf.placeholder(dtype, [3, 7, 5, 4], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=5, kernel_size=[5, 3], padding='SAME', use_bias=False) + activation_abs = tf.abs(conv) + save(inp, activation_abs, prefix + 'padding_same') + inp = tf.placeholder(dtype, [2, 4, 6, 5], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=4, kernel_size=[3, 5], padding='VALID', activation=tf.nn.elu, bias_initializer=tf.random_normal_initializer()) + save(inp, conv, prefix + 'padding_valid') + inp = tf.placeholder(dtype, [3, 2, 3, 4], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=4, kernel_size=[1, 1], activation=tf.nn.tanh, bias_initializer=tf.random_uniform_initializer(0, 1)) + conv2 = tf.layers.conv2d(inputs=inp, filters=4, kernel_size=[1, 1], activation=tf.nn.sigmoid, bias_initializer=None) + eltwise_add_mul = (inp * 0.31 + 2 * conv) * conv2 + save(inp, eltwise_add_mul, prefix + 'eltwise_add_mul') + inp = tf.placeholder(dtype, [1, 4, 5, 1], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=4, kernel_size=[3, 1], padding='VALID') + padded = tf.pad(conv, [[0, 0], [0, 2], [0, 0], [0, 0]]) + merged = tf.concat([padded, inp], axis=3) + save(inp, merged, prefix + 'pad_and_concat') + inp = tf.placeholder(dtype, [1, 6, 6, 2], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=3, kernel_size=[3, 3], padding='SAME') + pool = tf.layers.max_pooling2d(inputs=conv, pool_size=2, strides=2) + save(inp, pool, prefix + 'max_pool_even') + inp = tf.placeholder(dtype, [1, 7, 7, 2], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=3, kernel_size=[3, 3], padding='SAME') + pool = tf.layers.max_pooling2d(inputs=conv, pool_size=3, strides=2, padding='VALID') + save(inp, pool, prefix + 'max_pool_odd_valid') + inp = tf.placeholder(dtype, [1, 7, 7, 2], 'input') + conv = tf.layers.conv2d(inputs=inp, filters=3, kernel_size=[3, 3], padding='SAME') + relu = tf.nn.relu6(conv * 10) + pool = tf.layers.max_pooling2d(inputs=relu, pool_size=2, strides=2, padding='SAME') + save(inp, pool, prefix + 'max_pool_odd_same') + inp = tf.placeholder(dtype, [1, 5, 6, 2], 'input') + deconv_weights = tf.Variable(tf.random_normal([5, 3, 4, 2], dtype=dtype), name='deconv_weights') + deconv = tf.nn.conv2d_transpose(value=inp, filter=deconv_weights, output_shape=[1, 9, 8, 4], strides=[1, 1, 1, 1], padding='VALID') + deconv_bias = tf.contrib.layers.bias_add(deconv, activation_fn=tf.nn.relu, initializer=tf.random_normal_initializer()) + save(inp, deconv_bias, prefix + 'deconvolution') +inp = tf.placeholder(tf.float32, [2, 5, 4, 3], 'input') +bn = tf.contrib.layers.batch_norm(inputs=inp, fused=True, is_training=False, scale=True, param_initializers={'beta': tf.random_normal_initializer(), 'gamma': tf.random_normal_initializer(), 'moving_mean': tf.random_uniform_initializer(-2, -1), 'moving_variance': tf.random_uniform_initializer(1, 2)}) +save(inp, bn, 'fused_batch_norm') +inp = tf.placeholder(tf.float32, [2, 5, 6, 3], 'input') +weights = tf.Variable(tf.random_normal([3, 3, 3, 4]), name='weights') +conv = tf.nn.atrous_conv2d(inp, weights, rate=2, padding='VALID') +relu = tf.nn.relu(conv) +save(inp, relu, 'atrous_conv2d_valid') +inp = tf.placeholder(tf.float32, [2, 5, 10, 3], 'input') +weights = tf.Variable(tf.random_normal([3, 5, 3, 4]), name='weights') +conv = tf.nn.atrous_conv2d(inp, weights, rate=2, padding='SAME') +relu = tf.nn.relu(conv) +save(inp, relu, 'atrous_conv2d_same') +inp = tf.placeholder(tf.float32, [2, 5, 4, 3], 'input') +bn = tf.contrib.layers.batch_norm(inputs=inp, is_training=False, scale=True, param_initializers={'beta': tf.random_normal_initializer(), 'gamma': tf.random_normal_initializer(), 'moving_mean': tf.random_uniform_initializer(-2, -1), 'moving_variance': tf.random_uniform_initializer(1, 2)}) +save(inp, bn, 'batch_norm') +inp = tf.placeholder(tf.float32, [2, 10, 9, 6], 'input') +weights = tf.Variable(tf.random_normal([5, 3, 6, 4]), name='weights') +conv = tf.nn.depthwise_conv2d(input=inp, filter=weights, strides=[1, 1, 1, 1], padding='SAME') +save(inp, conv, 'depthwise_conv2d') +inp = tf.placeholder(tf.float32, [2, 3], 'input') +biases = tf.Variable(tf.random_normal([4]), name='matmul_biases') +weights = tf.Variable(tf.random_normal([3, 4]), name='matmul_weights') +mm = tf.matmul(inp, weights) + biases +save(inp, mm, 'matmul') +from tensorflow.python.framework import function + + +@function.Defun(tf.float32, func_name='Dropout') +def my_dropout(x): + return tf.layers.dropout(x, rate=0.1, training=isTraining) + + +inp = tf.placeholder(tf.float32, [1, 10, 10, 3], 'input') +conv = tf.layers.conv2d(inp, filters=3, kernel_size=[1, 1]) +dropout = my_dropout(conv) +relu = tf.nn.relu(dropout) +save(inp, relu, 'defun_dropout') +inp = tf.placeholder(tf.float32, [2, 3, 4], 'input') +shift = tf.Variable(tf.random_normal([2, 3, 4]), name='shift') +shifted = tf.add(inp, shift, name='shifted') +reshape = tf.reshape(shifted, [4, 3, 2], 'reshaped') +save(inp, reshape, 'shift_reshape_no_reorder') +inp = tf.placeholder(tf.float32, [1, 2, 3], 'input') +reshape = tf.reshape(inp, [3, 1, 2], 'reshaped') +save(inp, reshape, 'reshape_no_reorder') +inp = tf.placeholder(tf.float32, [2, 10, 10, 3], 'input') +pad = tf.pad(inp, [[0, 0], [3, 3], [3, 3], [0, 0]]) +conv = tf.layers.conv2d(inp, filters=4, kernel_size=[5, 5], strides=(2, 2), bias_initializer=tf.random_normal_initializer()) +save(inp, conv, 'spatial_padding') +inp = tf.placeholder(tf.float32, [1, 2, 3], 'input') +bn = tf.add(inp, tf.Variable(tf.random_normal(inp.shape))) +reshape = tf.reshape(bn, [-1, 3]) +save(inp, reshape, 'reshape_reduce') +times = 4 +batch_size = 2 +input_size = 5 * 6 * 3 +output_size = 10 +inp = tf.placeholder(tf.float32, [times, batch_size, input_size], 'input') +lstm_cell = tf.contrib.rnn.LSTMBlockFusedCell(output_size, forget_bias=0.9, cell_clip=0.4, use_peephole=True) +outputs, state = lstm_cell(inp, dtype=tf.float32) +last_output = tf.slice(outputs, [times - 1, 0, 0], [1, -1, output_size]) +last_output = tf.reshape(last_output, [-1, 10]) +weights = tf.Variable(tf.random_normal([10, 2])) +biases = tf.Variable(tf.random_normal([2])) +sigmoid = tf.nn.sigmoid(tf.matmul(last_output, weights) + biases) +save(inp, sigmoid, 'lstm') +bgr = tf.placeholder(tf.float32, [4, 5, 6, 3], 'input') +b, g, r = tf.split(bgr, num_or_size_splits=3, axis=3) +rgb = tf.concat([r, g, b], axis=3) +alpha, beta = tf.split(rgb, num_or_size_splits=2, axis=0) +res = tf.layers.conv2d(alpha, filters=1, kernel_size=[1, 1]) + tf.layers.conv2d(beta, filters=1, kernel_size=[1, 1]) +save(bgr, res, 'split_equals') +inp = tf.placeholder(tf.float32, [2, 10, 11, 3], 'input') +conv = tf.layers.conv2d(inp, filters=7, kernel_size=[1, 1]) +scaled = tf.image.resize_nearest_neighbor(conv, size=(15, 8)) +scaled = tf.image.resize_nearest_neighbor(scaled, size=(9, 12)) +save(inp, scaled, 'resize_nearest_neighbor') +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = tf.layers.batch_normalization(inp, training=isTraining, fused=False, name='batch_norm', beta_initializer=tf.random_normal_initializer(), gamma_initializer=tf.random_normal_initializer(), moving_mean_initializer=tf.random_uniform_initializer(-2, 1), moving_variance_initializer=tf.random_uniform_initializer(0.1, 2)) +save(inp, bn, 'batch_norm_text') +inp = tf.placeholder(tf.float32, [2, 4, 5], 'input') +flatten = tf.contrib.layers.flatten(inp) +save(inp, flatten, 'flatten') +with tf.gfile.FastGFile('../ssd_mobilenet_v1_coco.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) +with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + tf.import_graph_def(graph_def, name='') + inp = cv.imread('../street.png') + inp = cv.resize(inp, (300, 300)) + inp = inp[:, :, ([2, 1, 0])] + out = localSession.run([localSession.graph.get_tensor_by_name('concat:0'), localSession.graph.get_tensor_by_name('concat_1:0'), localSession.graph.get_tensor_by_name('num_detections:0'), localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0')], feed_dict={ + 'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)}) + np.save('ssd_mobilenet_v1_coco.concat.npy', out[0]) + np.save('ssd_mobilenet_v1_coco.concat_1.npy', out[1]) + num_detections = int(out[2][0]) + detections = np.zeros([1, 1, num_detections, 7], np.float32) + detections[(0), (0), :, (0)] = 0 + detections[(0), (0), :, (1)] = out[5][0] + detections[(0), (0), :, (2)] = out[3][0] + detections[(0), (0), :, 3:] = out[4][0][:, ([1, 0, 3, 2])] + detections = sorted(detections[(0), (0), :, :], cmp=lambda x, y: -1 if x[1] < y[1] and x[2] < y[2] else 0) + np.save('ssd_mobilenet_v1_coco.detection_out.npy', detections) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +conv = tf.layers.conv2d(inp, filters=5, kernel_size=[1, 1], activation=tf.nn.relu, bias_initializer=tf.random_normal_initializer()) +flattened = tf.reshape(conv, [1, -1], 'reshaped') +biases = tf.Variable(tf.random_normal([10]), name='matmul_biases') +weights = tf.Variable(tf.random_normal([2 * 3 * 5, 10]), name='matmul_weights') +mm = tf.matmul(flattened, weights) + biases +save(inp, mm, 'nhwc_reshape_matmul') +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +conv = tf.layers.conv2d(inp, filters=5, kernel_size=[1, 1], activation=tf.nn.relu, bias_initializer=tf.random_normal_initializer()) +transposed = tf.transpose(conv, [0, 1, 2, 3]) +flattened = tf.reshape(transposed, [1, -1], 'reshaped') +biases = tf.Variable(tf.random_normal([10]), name='matmul_biases') +weights = tf.Variable(tf.random_normal([2 * 3 * 5, 10]), name='matmul_weights') +mm = tf.matmul(flattened, weights) + biases +save(inp, flattened, 'nhwc_transpose_reshape_matmul') +inp = tf.placeholder(tf.float32, [1, 6, 5, 3], 'input') +conv = tf.layers.conv2d(inputs=inp, filters=3, kernel_size=[1, 1], activation=tf.nn.relu, bias_initializer=tf.random_normal_initializer()) +save(inp, conv, 'uint8_single_conv', quantize=True) +runModel(inp, conv, 'uint8_single_conv') +inp = tf.placeholder(tf.float32, [1, 4, 4, 1], 'input') +conv = tf.layers.conv2d(inp, filters=3, kernel_size=[3, 3], padding='SAME') +pool = tf.layers.average_pooling2d(conv, pool_size=3, strides=1, padding='SAME') +save(inp, pool, 'ave_pool_same') +inp = tf.placeholder(tf.float32, [1, 4, 6, 1], 'input') +conv = tf.layers.conv2d(inp, filters=3, kernel_size=[1, 1], padding='SAME') +sliced = tf.slice(conv, [0, 1, 2, 0], [-1, 3, 4, 1]) +save(inp, sliced, 'slice_4d') +inp = tf.placeholder(tf.float32, [1, 4, 4, 1], 'input') +deconv_weights = tf.Variable(tf.random_normal([3, 3, 2, 1], dtype=tf.float32)) +deconv = tf.nn.conv2d_transpose(inp, deconv_weights, output_shape=[1, 4, 4, 2], strides=[1, 1, 1, 1], padding='SAME') +leakyRelu = tf.nn.leaky_relu(deconv, alpha=0.2) +save(inp, leakyRelu, 'deconvolution_same') +inp = tf.placeholder(tf.float32, [1, 3, 3, 1], 'input') +deconv_weights = tf.Variable(tf.random_normal([3, 3, 2, 1], dtype=tf.float32)) +deconv = tf.nn.conv2d_transpose(inp, deconv_weights, output_shape=[1, 5, 5, 2], strides=[1, 2, 2, 1], padding='SAME') +save(inp, deconv, 'deconvolution_stride_2_same') +inp = tf.placeholder(tf.float32, [1, 3, 2, 1], 'input') +deconv_weights = tf.Variable(tf.random_normal([3, 3, 2, 1], dtype=tf.float32)) +deconv = tf.nn.conv2d_transpose(inp, deconv_weights, output_shape=[1, 8, 6, 2], strides=[1, 2, 2, 1], padding='VALID') +save(inp, deconv, 'deconvolution_adj_pad_valid') +inp = tf.placeholder(tf.float32, [1, 2, 2, 1], 'input') +deconv_weights = tf.Variable(np.ones([3, 3, 1, 1]), dtype=tf.float32) +deconv = tf.nn.conv2d_transpose(inp, deconv_weights, output_shape=[1, 4, 4, 1], strides=[1, 2, 2, 1], padding='SAME') +save(inp, deconv, 'deconvolution_adj_pad_same') +inp = tf.placeholder(tf.float32, [1, 3, 4, 5], 'input') +gamma = tf.Variable(tf.random_normal([5], dtype=tf.float32)) +beta = tf.Variable(tf.random_normal([5], dtype=tf.float32)) +bn = tf.nn.fused_batch_norm(inp, gamma, beta, epsilon=1e-05, is_training=True)[0] +save(inp, bn, 'mvn_batch_norm') +inp = tf.placeholder(tf.float32, [1, 1, 1, 5], 'input') +gamma = tf.Variable(tf.random_normal([5], dtype=tf.float32)) +beta = tf.Variable(tf.random_normal([5], dtype=tf.float32)) +bn = tf.nn.fused_batch_norm(inp, gamma, beta, epsilon=1e-05, is_training=True)[0] +save(inp, bn, 'mvn_batch_norm_1x1') +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = tf.layers.batch_normalization(inp, training=False, fused=False, name='unfused_batch_norm', beta_initializer=tf.random_normal_initializer(), gamma_initializer=tf.random_normal_initializer(), moving_mean_initializer=tf.random_uniform_initializer(-2, 1), moving_variance_initializer=tf.random_uniform_initializer(0.1, 2)) +save(inp, bn, 'unfused_batch_norm', optimize=False) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = tf.layers.batch_normalization(inp, training=False, fused=True, name='fused_batch_norm_no_gamma', beta_initializer=tf.random_normal_initializer(), scale=False, moving_mean_initializer=tf.random_uniform_initializer(-2, 1), moving_variance_initializer=tf.random_uniform_initializer(0.1, 2)) +save(inp, bn, 'fused_batch_norm_no_gamma', optimize=False) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +bn = tf.layers.batch_normalization(inp, training=False, fused=False, name='unfused_batch_norm_no_gamma', beta_initializer=tf.random_normal_initializer(), scale=False, moving_mean_initializer=tf.random_uniform_initializer(-2, 1), moving_variance_initializer=tf.random_uniform_initializer(0.1, 2)) +save(inp, bn, 'unfused_batch_norm_no_gamma', optimize=False) +inp = tf.placeholder(tf.float32, [1, 2, 3], 'input') +flatten = tf.contrib.layers.flatten(inp) +save(inp, flatten, 'unfused_flatten', optimize=False) +inp = tf.placeholder(tf.float32, [None, 2, 3], 'input') +flatten = tf.contrib.layers.flatten(inp) +save(inp, flatten, 'unfused_flatten_unknown_batch', optimize=False) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +relu = tf.maximum(inp, 0.01 * inp, name='leaky_relu') * 2 +save(inp, relu, 'leaky_relu_order1', optimize=False) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +relu = tf.maximum(inp, inp * 0.01, name='leaky_relu') * 2 +save(inp, relu, 'leaky_relu_order2', optimize=False) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +relu = tf.maximum(0.01 * inp, inp, name='leaky_relu') * 2 +save(inp, relu, 'leaky_relu_order3', optimize=False) +from tensorflow import keras as K +model = K.models.Sequential() +model.add(K.layers.Softmax(name='keras_softmax', input_shape=(2, 3, 4))) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_softmax_input:0'), sess.graph.get_tensor_by_name('keras_softmax/truediv:0'), 'keras_softmax', optimize=False) +model = K.models.Sequential() +model.add(K.layers.Conv2D(filters=4, kernel_size=1, data_format='channels_last', name='keras_mobilenet_head_conv', input_shape=(2, 3, 4))) +model.add(K.layers.GlobalAveragePooling2D(name='keras_mobilenet_head_pool')) +model.add(K.layers.Reshape((1, 1, 4), name='keras_mobilenet_head_reshape')) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_mobilenet_head_conv_input:0'), sess.graph.get_tensor_by_name('keras_mobilenet_head_reshape/Reshape:0'), 'keras_mobilenet_head', optimize=False) + + +def keras_relu6(x): + return K.activations.relu(x, max_value=6) + + +inp = K.Input(shape=(2, 3, 4), name='keras_relu6_input') +relu = K.layers.Activation(keras_relu6, name='keras_relu6')(inp) +model = K.Model(inp, relu) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_relu6_input:0'), sess.graph.get_tensor_by_name('keras_relu6/clip_by_value:0'), 'keras_relu6', optimize=False) +inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +reduced = tf.reduce_mean(inp, axis=[1, 2], keepdims=True) +save(inp, reduced, 'reduce_mean') +inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +pool = tf.layers.average_pooling2d(inp, pool_size=1, strides=1, padding='SAME') +l2norm = tf.nn.l2_normalize(pool, axis=-1) +l2norm = tf.nn.l2_normalize(l2norm, axis=[2, 3, 1]) +save(inp, l2norm, 'l2_normalize') +inp = tf.placeholder(tf.float32, [2, 3, 4], 'input') +l2norm = tf.nn.l2_normalize(inp, axis=1) +l2norm = tf.nn.l2_normalize(l2norm, axis=-1) +l2norm = tf.nn.l2_normalize(l2norm, axis=[0, 1]) +save(inp, l2norm, 'l2_normalize_3d') +model = K.models.Sequential() +model.add(K.layers.Conv2DTranspose(filters=4, kernel_size=3, strides=(2, 2), data_format='channels_last', name='keras_deconv_valid', input_shape=(4, 5, 2))) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_deconv_valid_input:0'), sess.graph.get_tensor_by_name('keras_deconv_valid/BiasAdd:0'), 'keras_deconv_valid', optimize=True) +model = K.models.Sequential() +model.add(K.layers.Conv2DTranspose(filters=4, kernel_size=3, strides=(2, 2), data_format='channels_last', name='keras_deconv_same', input_shape=(4, 5, 2), padding='same')) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_deconv_same_input:0'), sess.graph.get_tensor_by_name('keras_deconv_same/BiasAdd:0'), 'keras_deconv_same', optimize=True) +inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +resized = tf.image.resize_bilinear(inp, size=[9, 8], name='resize_bilinear') +save(inp, resized, 'resize_bilinear') +inp = tf.placeholder(tf.float32, [None, 3, 4, 5], 'input') +resized = tf.image.resize_bilinear(inp, size=[tf.shape(inp)[1] * 2, tf.shape(inp)[2] * 3], name='resize_bilinear_factor') +sub_add = resized - 0.3 + 0.3 +save(inp, sub_add, 'resize_bilinear_factor', optimize=False) +model = K.models.Sequential() +model.add(K.layers.SeparableConv2D(filters=4, kernel_size=3, strides=(1, 1), dilation_rate=(2, 3), name='keras_atrous_conv2d_same', input_shape=(11, 12, 2), padding='same')) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_atrous_conv2d_same_input:0'), sess.graph.get_tensor_by_name('keras_atrous_conv2d_same/BiasAdd:0'), 'keras_atrous_conv2d_same', optimize=True) +for name in ['faster_rcnn_inception_v2_coco_2018_01_28', 'faster_rcnn_resnet50_coco_2018_01_28']: + with tf.gfile.FastGFile(os.path.join('..', name + '.pb')) as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + tf.import_graph_def(graph_def, name='') + inp = cv.imread('../dog416.png') + inp = cv.resize(inp, (800, 600)) + inp = inp[:, :, ([2, 1, 0])] + out = localSession.run([localSession.graph.get_tensor_by_name('num_detections:0'), localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0')], feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)}) + num_detections = int(out[0][0]) + detections = np.zeros([1, 1, num_detections, 7], np.float32) + detections[(0), (0), :, (1)] = out[3][(0), :num_detections] - 1 + detections[(0), (0), :, (2)] = out[1][(0), :num_detections] + detections[(0), (0), :, 3:] = out[2][:, :num_detections, ([1, 0, 3, 2])] + np.save(name + '.detection_out.npy', detections) +inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input') +conv1 = tf.layers.conv2d(inp, filters=4, kernel_size=[1, 1]) +conv2 = tf.layers.conv2d(inp, filters=4, kernel_size=[1, 1]) +flatten1 = tf.contrib.layers.flatten(conv1) +flatten2 = tf.contrib.layers.flatten(conv2) +concat = tf.concat([flatten1, flatten2], axis=1) +bias = tf.contrib.layers.bias_add(concat) +save(inp, bias, 'concat_axis_1') +inp = tf.placeholder(tf.float32, [1, 3, 5, 8], 'input') +conv = tf.layers.conv2d(inp, filters=4, kernel_size=[2, 3], data_format='channels_first') +pool = tf.layers.max_pooling2d(conv, pool_size=2, strides=2, data_format='channels_first') +save(inp, pool, 'conv_pool_nchw') +for name in ['conv_pool_nchw_in.npy', 'conv_pool_nchw_out.npy']: + np.save(name, np.load(name).transpose(0, 2, 3, 1)) +model = K.models.Sequential() +model.add(K.layers.UpSampling2D(size=(3, 2), data_format='channels_last', name='keras_upsampling2d', input_shape=(2, 3, 4))) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_upsampling2d_input:0'), sess.graph.get_tensor_by_name('keras_upsampling2d/ResizeNearestNeighbor:0'), 'keras_upsampling2d') +with tf.gfile.FastGFile('../ssd_mobilenet_v1_ppn_coco.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) +with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + tf.import_graph_def(graph_def, name='') + img = cv.imread('../dog416.png') + inp = cv.resize(img, (300, 300)) + inp = inp[:, :, ([2, 1, 0])] + out = localSession.run([localSession.graph.get_tensor_by_name('num_detections:0'), localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0')], feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)}) + num_detections = int(out[0][0]) + detections = np.zeros([1, 1, num_detections, 7], np.float32) + detections[(0), (0), :, (1)] = out[3][(0), :num_detections] + detections[(0), (0), :, (2)] = out[1][(0), :num_detections] + detections[(0), (0), :, 3:] = out[2][:, :num_detections, ([1, 0, 3, 2])] + np.save('ssd_mobilenet_v1_ppn_coco.detection_out.npy', detections) +inp = tf.placeholder(tf.float32, [None, 2, 3], 'input') +flatten = tf.reshape(inp, [-1, 2 * 3], 'planar') +reshaped = tf.reshape(flatten, tf.shape(inp), 'reshape') +save(inp, reshaped, 'reshape_as_shape', optimize=False) +with tf.gfile.FastGFile('../mask_rcnn_inception_v2_coco_2018_01_28.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) +with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + tf.import_graph_def(graph_def, name='') + img = cv.imread('../street.png') + inp = cv.resize(img, (800, 800)) + inp = inp[:, :, ([2, 1, 0])] + out = localSession.run([localSession.graph.get_tensor_by_name('num_detections:0'), localSession.graph.get_tensor_by_name('detection_scores:0'), localSession.graph.get_tensor_by_name('detection_boxes:0'), localSession.graph.get_tensor_by_name('detection_classes:0'), localSession.graph.get_tensor_by_name('detection_masks:0')], feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp. + shape[1], 3)}) + num_detections = int(out[0][0]) + detections = np.zeros([1, 1, num_detections, 7], np.float32) + detections[(0), (0), :, (1)] = out[3][(0), :num_detections] - 1 + detections[(0), (0), :, (2)] = out[1][(0), :num_detections] + detections[(0), (0), :, 3:] = out[2][:, :num_detections, ([1, 0, 3, 2])] + np.save('mask_rcnn_inception_v2_coco_2018_01_28.detection_out.npy', detections) + np.save('mask_rcnn_inception_v2_coco_2018_01_28.detection_masks.npy', out[4]) +inp = K.Input(shape=(2, 3, 4), name='keras_pad_concat_input', batch_size=1) +conv = K.layers.Conv2D(filters=4, kernel_size=1, data_format='channels_last', name='keras_pad_concat_conv', input_shape=(2, 3, 4))(inp) + + +def pad_depth(x, desired_channels): + y = K.backend.random_uniform_variable(x.shape.as_list()[:-1] + [desired_channels], low=0, high=1) + return K.layers.concatenate([x, y]) + + +pad = K.layers.Lambda(pad_depth, arguments={'desired_channels': 5}, name='keras_pad_concat')(conv) +sess = K.backend.get_session() +sess.as_default() +save(sess.graph.get_tensor_by_name('keras_pad_concat_input:0'), sess.graph.get_tensor_by_name('keras_pad_concat/concatenate/concat:0'), 'keras_pad_concat', optimize=False) +inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input') +conv = tf.layers.conv2d(inp, filters=5, kernel_size=[1, 1], bias_initializer=tf.random_normal_initializer()) +sub = conv - inp +save(inp, sub, 'eltwise_sub') +inp = tf.placeholder(tf.float32, [None, 2, 3, 4], 'input') +conv = tf.layers.conv2d(inp, filters=3, kernel_size=[1, 1]) +softmax = tf.contrib.slim.softmax(conv) +save(inp, softmax, 'slim_softmax') +inp_node = 'img_inputs' +out_node = 'MobileFaceNet/MobileFaceNet/Conv2d_0/add' +with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + with tf.gfile.FastGFile('frozen_model.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + for node in graph_def.node: + if node.name == inp_node: + del node.attr['shape'] + tf.import_graph_def(graph_def, name='') + inputData = gen_data(tf.placeholder(tf.float32, [1, 4, 5, 3], inp_node)) + outputData = localSession.run(localSession.graph.get_tensor_by_name(out_node + ':0'), feed_dict={(inp_node + ':0'): inputData}) + writeBlob(inputData, 'slim_batch_norm_in') + writeBlob(outputData, 'slim_batch_norm_out') + graph_def = TransformGraph(graph_def, [inp_node], [out_node], ['fold_constants', 'strip_unused_nodes']) + with tf.gfile.FastGFile('slim_batch_norm_net.pb', 'wb') as f: + f.write(graph_def.SerializeToString()) +inp_node = 'PNet/conv3/add' +out_node = 'PNet/cls_prob' +with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + with tf.gfile.FastGFile('PNet_pnet.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + graph_def = TransformGraph(graph_def, [inp_node], [out_node], ['strip_unused_nodes']) + tf.import_graph_def(graph_def, name='') + inputData = gen_data(tf.placeholder(tf.float32, [1, 4, 5, 16], inp_node)) + outputData = localSession.run(localSession.graph.get_tensor_by_name(out_node + ':0'), feed_dict={(inp_node + ':0'): inputData}) + writeBlob(inputData, 'slim_softmax_v2_in') + writeBlob(outputData, 'slim_softmax_v2_out') + with tf.gfile.FastGFile('slim_softmax_v2_net.pb', 'wb') as f: + f.write(graph_def.SerializeToString()) +inp = tf.placeholder(tf.float32, [1, 4, 6, 5, 3], 'input') +conv3d = tf.layers.conv3d(inputs=inp, filters=2, kernel_size=[2, 3, 4], use_bias=True, padding='same') +save(inp, conv3d, 'conv3d') +inp = tf.placeholder(tf.float32, [1, 4, 6, 5, 3], 'input') +maxpool3d = tf.layers.max_pooling3d(inputs=inp, pool_size=(3, 2, 3), strides=(1, 2, 1), padding='same') +save(inp, maxpool3d, 'max_pool3d') +inp = tf.placeholder(tf.float32, [1, 5, 4, 5, 2], 'input') +avepool3d = tf.layers.average_pooling3d(inputs=inp, pool_size=(3, 3, 2), strides=(2, 1, 1), padding='valid') +save(inp, avepool3d, 'ave_pool3d') +inp_node = 'input_image' +out_node = 'SUBPIXEL/SUBPIXEL/subpixel_image/Identity' +with tf.Session(graph=tf.Graph()) as localSession: + localSession.graph.as_default() + with tf.gfile.FastGFile('simple_subpixel.optimized.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + tf.import_graph_def(graph_def, name='') + inputData = gen_data(tf.placeholder(tf.float32, [1, 1, 1, 4], inp_node)) + outputData = localSession.run(localSession.graph.get_tensor_by_name(out_node + ':0'), feed_dict={(inp_node + ':0'): inputData}) + writeBlob(inputData, 'subpixel_in') + writeBlob(outputData, 'subpixel_out') + for node in graph_def.node: + if node.op == 'Placeholder': + node.attr['data_format'].s = 'NHWC' + with tf.gfile.FastGFile('subpixel_net.pb', 'wb') as f: + f.write(graph_def.SerializeToString()) diff --git a/dataset/preprocessed/229.txt b/dataset/preprocessed/229.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/229.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/23.txt b/dataset/preprocessed/23.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/23.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/230.txt b/dataset/preprocessed/230.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/230.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/231.txt b/dataset/preprocessed/231.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/231.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/232.txt b/dataset/preprocessed/232.txt new file mode 100644 index 0000000..8d93c6c --- /dev/null +++ b/dataset/preprocessed/232.txt @@ -0,0 +1,224 @@ +import numpy as np +import os +import tensorflow as tf +from six.moves import urllib +import errno +import os +import zipfile +from collections import Counter +from collections import deque +from sklearn.manifold import TSNE + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +import matplotlib +import matplotlib.pyplot as plt +plt.rcParams['axes.labelsize'] = 14 +plt.rcParams['xtick.labelsize'] = 12 +plt.rcParams['ytick.labelsize'] = 12 +PROJECT_ROOT_DIR = '.' +CHAPTER_ID = 'rnn' + + +def save_fig(fig_id, tight_layout=True): + path = os.path.join(PROJECT_ROOT_DIR, 'images', CHAPTER_ID, fig_id + '.png') + print('Saving figure', fig_id) + if tight_layout: + plt.tight_layout() + plt.savefig(path, format='png', dpi=300) + + +reset_graph() +(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data() +X_train = X_train.astype(np.float32).reshape(-1, 28 * 28) / 255.0 +X_test = X_test.astype(np.float32).reshape(-1, 28 * 28) / 255.0 +y_train = y_train.astype(np.int32) +y_test = y_test.astype(np.int32) +X_valid, X_train = X_train[:5000], X_train[5000:] +y_valid, y_train = y_train[:5000], y_train[5000:] + + +def shuffle_batch(X, y, batch_size): + rnd_idx = np.random.permutation(len(X)) + n_batches = len(X) // batch_size + for batch_idx in np.array_split(rnd_idx, n_batches): + X_batch, y_batch = X[batch_idx], y[batch_idx] + yield X_batch, y_batch + + +t_min, t_max = 0, 30 +resolution = 0.1 + + +def time_series(t): + return t * np.sin(t) / 3 + 2 * np.sin(5 * t) + + +def next_batch(batch_size, n_steps): + t0 = np.random.rand(batch_size, 1) * (t_max - t_min - n_steps * resolution) + Ts = t0 + np.arange(0.0, n_steps + 1) * resolution + ys = time_series(Ts) + return ys[:, :-1].reshape(-1, n_steps, 1), ys[:, 1:].reshape(-1, n_steps, 1) + + +t = np.linspace(t_min, t_max, int((t_max - t_min) / resolution)) +n_steps = 20 +t_instance = np.linspace(12.2, 12.2 + resolution * (n_steps + 1), n_steps + 1) + + +class DeviceCellWrapper(tf.nn.rnn_cell.RNNCell): + + def __init__(self, device, cell): + self._cell = cell + self._device = device + + @property + def state_size(self): + return self._cell.state_size + + @property + def output_size(self): + return self._cell.output_size + + def __call__(self, inputs, state, scope=None): + with tf.device(self._device): + return self._cell(inputs, state, scope) + + +reset_graph() +WORDS_PATH = 'datasets/words' +WORDS_URL = 'http://mattmahoney.net/dc/text8.zip' + + +def mkdir_p(path): + try: + os.makedirs(path) + except OSError as exc: + if exc.errno == errno.EEXIST and os.path.isdir(path): + pass + else: + raise + + +def fetch_word_data(words_url=WORDS_URL, words_path=WORDS_PATH): + os.makedirs(words_path, exist_ok=True) + zip_path = os.path.join(words_path, 'words.zip') + if not os.path.exists(zip_path): + urllib.request.urlretrieve(words_url, zip_path) + with zipfile.ZipFile(zip_path) as f: + data = f.read(f.namelist()[0]) + return data.decode('ascii').split() + + +words = fetch_word_data() +vocabulary_size = 50000 +vocabulary = [('UNK', None)] + Counter(words).most_common(vocabulary_size - 1) +vocabulary = np.array([word for word, _ in vocabulary]) +dictionary = {word: code for code, word in enumerate(vocabulary)} +data = np.array([dictionary.get(word, 0) for word in words]) + + +def generate_batch(batch_size, num_skips, skip_window): + global data_index + assert batch_size % num_skips == 0 + assert num_skips <= 2 * skip_window + batch = np.ndarray(shape=[batch_size], dtype=np.int32) + labels = np.ndarray(shape=[batch_size, 1], dtype=np.int32) + span = 2 * skip_window + 1 + buffer = deque(maxlen=span) + for _ in range(span): + buffer.append(data[data_index]) + data_index = (data_index + 1) % len(data) + for i in range(batch_size // num_skips): + target = skip_window + targets_to_avoid = [skip_window] + for j in range(num_skips): + while target in targets_to_avoid: + target = np.random.randint(0, span) + targets_to_avoid.append(target) + batch[i * num_skips + j] = buffer[skip_window] + labels[i * num_skips + j, 0] = buffer[target] + buffer.append(data[data_index]) + data_index = (data_index + 1) % len(data) + return batch, labels + + +data_index = 0 +batch_size = 128 +embedding_size = 128 +skip_window = 1 +num_skips = 2 +valid_size = 16 +valid_window = 100 +valid_examples = np.random.choice(valid_window, valid_size, replace=False) +num_sampled = 64 +learning_rate = 0.01 +reset_graph() +train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1]) +valid_dataset = tf.constant(valid_examples, dtype=tf.int32) +vocabulary_size = 50000 +embedding_size = 150 +init_embeds = tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0) +embeddings = tf.Variable(init_embeds) +train_inputs = tf.placeholder(tf.int32, shape=[None]) +embed = tf.nn.embedding_lookup(embeddings, train_inputs) +nce_weights = tf.Variable(tf.truncated_normal([vocabulary_size, embedding_size], stddev=1.0 / np.sqrt(embedding_size))) +nce_biases = tf.Variable(tf.zeros([vocabulary_size])) +loss = tf.reduce_mean(tf.nn.nce_loss(nce_weights, nce_biases, train_labels, embed, num_sampled, vocabulary_size)) +optimizer = tf.train.AdamOptimizer(learning_rate) +trianing_op = optimizer.minimize(loss) +norm = tf.sqrt(tf.reduce_mean(tf.square(embeddings), axis=1, keepdims=True)) +normalized_embeddings = embeddings / norm +valid_embeddings = tf.nn.embedding_lookup(normalized_embeddings, valid_dataset) +similarity = tf.matmul(valid_embeddings, normalized_embeddings, transpose_b=True) +init = tf.global_variables_initializer() +num_steps = 10001 +with tf.Session() as sess: + init.run() + average_loss = 0 + for step in range(num_steps): + print('\rIteration:{}'.format(step), end='\t') + batch_inputs, batch_labels = generate_batch(batch_size, num_skips, skip_window) + feed_dict = {train_inputs: batch_inputs, train_labels: batch_labels} + _, loss_val = sess.run([trianing_op, loss], feed_dict=feed_dict) + average_loss += loss_val + if step % 2000 == 0: + if step > 0: + average_loss /= 2000 + print('Average loss at step', step, ':', average_loss) + average_loss = 0 + if step % 10000 == 0: + sim = similarity.eval() + for i in range(valid_size): + valid_word = vocabulary[valid_examples[i]] + top_k = 8 + nearst = (-sim[(i), :]).argsort()[1:top_k + 1] + log_str = 'Nearest to %s:' % valid_word + for k in range(top_k): + close_word = vocabulary[nearst[k]] + log_str = '%s %s,' % (log_str, close_word) + print(log_str) + final_embedding = normalized_embeddings.eval() +np.save('./my_final_embeddings.npy', final_embedding) + + +def plot_with_labels(low_dim_embs, labels): + assert low_dim_embs.shape[0] >= len(labels), 'More labels than embeddings' + plt.figure(figsize=(18, 18)) + for i, label in enumerate(labels): + x, y = low_dim_embs[(i), :] + plt.scatter(x, y) + plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom') + plt.show() + + +tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000) +plot_only = 500 +low_dim_embs = tsne.fit_transform(final_embedding[:plot_only, :]) +labels = [vocabulary[i] for i in range(plot_only)] +plot_with_labels(low_dim_embs, labels) diff --git a/dataset/preprocessed/233.txt b/dataset/preprocessed/233.txt new file mode 100644 index 0000000..b76cc03 --- /dev/null +++ b/dataset/preprocessed/233.txt @@ -0,0 +1,161 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from neural.full_connect import load_reformat_not_mnist, accuracy + + +def tf_better_nn(offset_range=-1, regular=False, drop_out=False, lrd=False): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_node_count = 1024 + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count])) + biases1 = tf.Variable(tf.zeros([hidden_node_count])) + weights2 = tf.Variable(tf.truncated_normal([hidden_node_count, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + ys = tf.matmul(tf_train_dataset, weights1) + biases1 + hidden = tf.nn.relu(ys) + h_fc = hidden + valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + valid_hidden1 = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + test_hidden1 = tf.nn.relu(test_y0) + keep_prob = tf.placeholder(tf.float32) + if drop_out: + hidden_drop = tf.nn.dropout(hidden, keep_prob) + h_fc = hidden_drop + logits = tf.matmul(h_fc, weights2) + biases2 + logits_predict = tf.matmul(hidden, weights2) + biases2 + valid_predict = tf.matmul(valid_hidden1, weights2) + biases2 + test_predict = tf.matmul(test_hidden1, weights2) + biases2 + l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(biases1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(biases2) + if not regular: + l2_loss = 0 + beta = 0.002 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta * l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + if lrd: + cur_step = tf.Variable(0) + starter_learning_rate = 0.1 + learning_rate = tf.train.exponential_decay(starter_learning_rate, cur_step, 10000, 0.96, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=cur_step) + train_prediction = tf.nn.softmax(logits_predict) + valid_prediction = tf.nn.softmax(valid_predict) + test_prediction = tf.nn.softmax(test_predict) + num_steps = 30001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + if offset_range == -1: + offset_range = train_labels.shape[0] - batch_size + offset = step * batch_size % offset_range + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +def tf_deep_nn(regular=False, drop_out=False, lrd=False, layer_cnt=2): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_node_count = 1024 + hidden_stddev = np.sqrt(2.0 / 784) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count], stddev=hidden_stddev)) + biases1 = tf.Variable(tf.zeros([hidden_node_count])) + weights = [] + biases = [] + hidden_cur_cnt = hidden_node_count + for i in range(layer_cnt - 2): + if hidden_cur_cnt > 2: + hidden_next_cnt = int(hidden_cur_cnt / 2) + else: + hidden_next_cnt = 2 + hidden_stddev = np.sqrt(2.0 / hidden_cur_cnt) + weights.append(tf.Variable(tf.truncated_normal([hidden_cur_cnt, hidden_next_cnt], stddev=hidden_stddev))) + biases.append(tf.Variable(tf.zeros([hidden_next_cnt]))) + hidden_cur_cnt = hidden_next_cnt + y0 = tf.matmul(tf_train_dataset, weights1) + biases1 + hidden = tf.nn.relu(y0) + hidden_drop = hidden + keep_prob = 0.5 + if drop_out: + hidden_drop = tf.nn.dropout(hidden, keep_prob) + valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + valid_hidden = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + test_hidden = tf.nn.relu(test_y0) + for i in range(layer_cnt - 2): + y1 = tf.matmul(hidden_drop, weights[i]) + biases[i] + hidden_drop = tf.nn.relu(y1) + if drop_out: + keep_prob += 0.5 * i / (layer_cnt + 1) + hidden_drop = tf.nn.dropout(hidden_drop, keep_prob) + y0 = tf.matmul(hidden, weights[i]) + biases[i] + hidden = tf.nn.relu(y0) + valid_y0 = tf.matmul(valid_hidden, weights[i]) + biases[i] + valid_hidden = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(test_hidden, weights[i]) + biases[i] + test_hidden = tf.nn.relu(test_y0) + weights2 = tf.Variable(tf.truncated_normal([hidden_cur_cnt, num_labels], stddev=hidden_stddev / 2)) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_drop, weights2) + biases2 + logits_predict = tf.matmul(hidden, weights2) + biases2 + valid_predict = tf.matmul(valid_hidden, weights2) + biases2 + test_predict = tf.matmul(test_hidden, weights2) + biases2 + l2_loss = 0 + if regular: + l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + for i in range(len(weights)): + l2_loss += tf.nn.l2_loss(weights[i]) + beta = 0.25 / batch_size + beta = 1e-05 + l2_loss *= beta + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + l2_loss + if lrd: + cur_step = tf.Variable(0, trainable=False) + starter_learning_rate = 0.4 + learning_rate = tf.train.exponential_decay(starter_learning_rate, cur_step, 100000, 0.96, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=cur_step) + else: + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_predict) + valid_prediction = tf.nn.softmax(valid_predict) + test_prediction = tf.nn.softmax(test_predict) + num_steps = 20001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset_range = train_labels.shape[0] - batch_size + offset = step * batch_size % offset_range + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +if __name__ == '__main__': + image_size = 28 + num_labels = 10 + train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = load_reformat_not_mnist(image_size, num_labels) + tf_deep_nn(layer_cnt=6, lrd=True, drop_out=True, regular=True) diff --git a/dataset/preprocessed/234.txt b/dataset/preprocessed/234.txt new file mode 100644 index 0000000..618dd38 --- /dev/null +++ b/dataset/preprocessed/234.txt @@ -0,0 +1,194 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import os +import numpy as np +import tensorflow as tf + + +class MLP(object): + + def __init__(self, is_training, n_layers, keep_prob, last_nl_fun, loss_fun, top_k, aux_classes=0, flag_with_saver=False, gpu=1): + nb_feature = n_layers[0] + nb_classes = n_layers[-1] + if not is_training: + keep_prob = 1.0 + with tf.device('/gpu:%d' % gpu): + with tf.name_scope('input'): + self._input_data = x = tf.placeholder(tf.float32, [None, nb_feature], name='x-input') + self._targets = y_ = tf.placeholder(tf.float32, [None, nb_classes], name='y-input') + self._aux_targets = y2_ = tf.placeholder(tf.float32, [None, aux_classes], name='y2-input') if aux_classes else None + + def weight_variable(shape): + """Create a weight variable with appropriate initialization.""" + initial = tf.truncated_normal(shape, stddev=0.1) + return tf.Variable(initial) + + def bias_variable(shape): + """Create a bias variable with appropriate initialization.""" + initial = tf.constant(0.1, shape=shape) + return tf.Variable(initial) + + def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu): + """Reusable code for making a simple neural net layer. + + It does a matrix multiply, bias add, and then uses relu to nonlinearize. + It also sets up name scoping so that the resultant graph is easy to read, + and adds a number of summary ops. + """ + with tf.name_scope(layer_name): + with tf.name_scope('weights'): + weights = weight_variable([input_dim, output_dim]) + with tf.name_scope('biases'): + biases = bias_variable([output_dim]) + with tf.name_scope('Wx_plus_b'): + preactivate = tf.matmul(input_tensor, weights) + biases + if act is not None: + activations = act(preactivate, name='activation') + return activations + else: + return preactivate + if len(n_layers) == 3: + hidden1 = nn_layer(x, n_layers[0], n_layers[1], 'layer1') + with tf.name_scope('dropout'): + dropped = tf.nn.dropout(hidden1, keep_prob) + elif len(n_layers) == 4: + hidden1 = nn_layer(x, n_layers[0], n_layers[1], 'hidden_layer_1') + with tf.name_scope('dropout'): + dropped1 = tf.nn.dropout(hidden1, keep_prob) + hidden2 = nn_layer(dropped1, n_layers[1], n_layers[2], 'hidden_layer_2') + with tf.name_scope('dropout'): + dropped = tf.nn.dropout(hidden2, keep_prob) + elif len(n_layers) == 5: + hidden1 = nn_layer(x, n_layers[0], n_layers[1], 'hidden_layer_1') + with tf.name_scope('dropout'): + self._keep_prob = keep_prob = tf.placeholder(tf.float32) + dropped1 = tf.nn.dropout(hidden1, keep_prob) + hidden2 = nn_layer(dropped1, n_layers[1], n_layers[2], 'hidden_layer_2') + with tf.name_scope('dropout'): + dropped2 = tf.nn.dropout(hidden2, keep_prob) + hidden3 = nn_layer(dropped2, n_layers[2], n_layers[3], 'hidden_layer_3') + with tf.name_scope('dropout'): + dropped = tf.nn.dropout(hidden3, keep_prob) + self._logit = logit = nn_layer(dropped, n_layers[-2], n_layers[-1], 'layer2', act=None) + if aux_classes > 0: + self._aux_logit = aux_logit = nn_layer(dropped, n_layers[-2], aux_classes, 'layer3', act=None) + if last_nl_fun == 'softmax': + self._outputs = y = tf.nn.softmax(logit) + if aux_classes > 0: + self._aux_outputs = y2 = tf.nn.softmax(aux_logit) + if loss_fun == 'cross_entropy': + with tf.name_scope('cross_entropy'): + self._loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logit, y_)) + if aux_classes > 0: + self._loss += tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(aux_logit, y2_)) + if loss_fun == 'logistic': + with tf.name_scope('logistic'): + self._loss = tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y * y_)), 1)) + if aux_classes > 0: + self._loss += tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y2 * y2_)), 1)) + elif last_nl_fun == 'sigmoid': + self._outputs = y = tf.sigmoid(logit) + if aux_classes > 0: + self._aux_outputs = y2 = tf.sigmoid(aux_logit) + if loss_fun == 'cross_entropy': + with tf.name_scope('cross_entropy'): + self._loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logit, labels=y_)) + if aux_classes > 0: + self._loss += tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=aux_logit, labels=y2_)) + if loss_fun == 'logistic': + with tf.name_scope('logistic'): + self._loss = tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y * y_)), 1)) + if aux_classes > 0: + self._loss += tf.reduce_mean(tf.reduce_sum(tf.log(1 + tf.exp(-y2 * y2_)), 1)) + with tf.name_scope('accuracy'): + correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y * y_, 1)) + self._accuracy = accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + self._pred_top_values, self._pred_top_indices = tf.nn.top_k(y, k=top_k) + if flag_with_saver: + self.saver = tf.train.Saver(max_to_keep=None) + else: + self.saver = None + if not is_training: + return + self._lr = tf.Variable(0.0, trainable=False) + optimizer = tf.train.AdamOptimizer(self.lr) + self._train_op = optimizer.minimize(self._loss) + + def assign_lr(self, session, lr_value): + session.run(tf.assign(self.lr, lr_value)) + + @property + def input_data(self): + return self._input_data + + @property + def targets(self): + return self._targets + + @property + def aux_targets(self): + return self._aux_targets + + @property + def outputs(self): + return self._outputs + + @property + def logit(self): + return self._logit + + @property + def loss(self): + return self._loss + + @property + def train_op(self): + return self._train_op + + @property + def lr(self): + return self._lr + + @property + def pred_top_values(self): + return self._pred_top_values + + @property + def pred_top_indices(self): + return self._pred_top_indices + + @property + def target_top_values(self): + return self._target_top_values + + @property + def target_top_indices(self): + return self._target_top_indices + + @property + def inter_indices(self): + return self._inter_indices + + +class MLPPredictor(object): + """The sentence decoder (generator) for LSTMModel.""" + + def __init__(self, n_layers, last_nl_fun='softmax', loss_fun='cross_entropy', top_k=10, aux_classes=0, ses_threads=2, gpu_memory_fraction=0.2, gpu=1): + gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction) + config_proto = tf.ConfigProto(intra_op_parallelism_threads=ses_threads, gpu_options=gpu_options, allow_soft_placement=True) + self.session = session = tf.Session(config=config_proto) + with tf.variable_scope('MLPModel'): + self.model = MLP(is_training=False, n_layers=n_layers, keep_prob=0.6, last_nl_fun=last_nl_fun, loss_fun=loss_fun, top_k=top_k, aux_classes=aux_classes, flag_with_saver=True, gpu=gpu) + self.model_ready = False + + def load_model(self, model_path): + self.model.saver.restore(self.session, model_path) + self.model_path = model_path + self.model_ready = True + + def predict(self, input_data): + assert self.model_ready + model = self.model + pred_top_values, pred_top_indices = self.session.run([model.pred_top_values, model.pred_top_indices], feed_dict={model.input_data: input_data}) + return pred_top_values, pred_top_indices diff --git a/dataset/preprocessed/235.txt b/dataset/preprocessed/235.txt new file mode 100644 index 0000000..a8f8d5d --- /dev/null +++ b/dataset/preprocessed/235.txt @@ -0,0 +1,375 @@ +import getpass +import sys +import time +import numpy as np +from copy import deepcopy +from utils import calculate_perplexity, get_ptb_dataset, Vocab +from utils import ptb_iterator, sample +import tensorflow as tf +from tensorflow.contrib.seq2seq import sequence_loss +from model import LanguageModel + + +class Config(object): + """Holds model hyperparams and data information. + + The config class is used to store various hyperparameters and dataset + information parameters. Model objects are passed a Config() object at + instantiation. + """ + batch_size = 64 + embed_size = 50 + hidden_size = 128 + num_steps = 10 + max_epochs = 20 + early_stopping = 2 + dropout = 0.9 + lr = 0.001 + + +class RNNLM_Model(LanguageModel): + + def load_data(self, debug=False): + """Loads starter word-vectors and train/dev/test data.""" + self.vocab = Vocab() + self.vocab.construct(get_ptb_dataset('train')) + self.encoded_train = np.array([self.vocab.encode(word) for word in get_ptb_dataset('train')], dtype=np.int32) + self.encoded_valid = np.array([self.vocab.encode(word) for word in get_ptb_dataset('valid')], dtype=np.int32) + self.encoded_test = np.array([self.vocab.encode(word) for word in get_ptb_dataset('test')], dtype=np.int32) + if debug: + num_debug = 1024 + self.encoded_train = self.encoded_train[:num_debug] + self.encoded_valid = self.encoded_valid[:num_debug] + self.encoded_test = self.encoded_test[:num_debug] + + def add_placeholders(self): + """Generate placeholder variables to represent the input tensors + + These placeholders are used as inputs by the rest of the model building + code and will be fed data during training. Note that when "None" is in a + placeholder's shape, it's flexible + + Adds following nodes to the computational graph. + (When None is in a placeholder's shape, it's flexible) + + input_placeholder: Input placeholder tensor of shape + (None, num_steps), type tf.int32 + labels_placeholder: Labels placeholder tensor of shape + (None, num_steps), type tf.float32 + dropout_placeholder: Dropout value placeholder (scalar), + type tf.float32 + + Add these placeholders to self as the instance variables + + self.input_placeholder + self.labels_placeholder + self.dropout_placeholder + + (Don't change the variable names) + """ + self.input_placeholder = tf.placeholder(tf.int32, shape=[None, self.config.num_steps]) + self.labels_placeholder = tf.placeholder(tf.int32, shape=[None, self.config.num_steps]) + self.loss_weights = tf.ones([self.config.batch_size, self.config.num_steps]) + self.dropout_placeholder = tf.placeholder(tf.float32, shape=()) + + def add_embedding(self): + """Add embedding layer. + + Hint: This layer should use the input_placeholder to index into the + embedding. + Hint: You might find tf.nn.embedding_lookup useful. + Hint: You might find tf.split, tf.squeeze useful in constructing tensor inputs + Hint: Check the last slide from the TensorFlow lecture. + Hint: Here are the dimensions of the variables you will need to create: + + L: (len(self.vocab), embed_size) + + Returns: + inputs: List of length num_steps, each of whose elements should be + a tensor of shape (batch_size, embed_size). + """ + with tf.variable_scope('RNN', reuse=tf.AUTO_REUSE): + L = tf.get_variable('L', shape=[len(self.vocab), self.config.embed_size], initializer=tf.random_uniform_initializer) + inputs = tf.nn.embedding_lookup(L, self.input_placeholder) + return inputs + """ + inputs = [] + # The embedding lookup is currently only implemented for the CPU + split = tf.split(self.input_placeholder,self.config.num_steps,axis = 1) + with tf.device('/cpu:0'): + ### YOUR CODE HERE + for ii in range(self.config.num_steps): + embed = tf.nn.embedding_lookup(L,split[ii]) + + inputs.append(embed) + + ### END YOUR CODE + return inputs + """ + + def add_model(self, inputs): + """Creates the RNN LM model. + + In the space provided below, you need to implement the equations for the + RNN LM model. You can use built in rnn_cell functions from + tensorflow. + + Hint: Use a zeros tensor of shape (batch_size, hidden_size) as + initial state for the RNN. Add this to self as instance variable + + self.initial_state + + (Don't change variable name) + Hint: Add the last RNN output to self as instance variable + + self.final_state + + (Don't change variable name) + Hint: Make sure to apply dropout to the inputs and the outputs. + Hint: Use a variable scope (e.g. "RNN") to define RNN variables. + Hint: Perform an explicit for-loop over inputs. You can use + scope.reuse_variables() to ensure that the weights used at each + iteration (each time-step) are the same. (Make sure you don't call + this for iteration 0 though or nothing will be initialized!) + Hint: Here are the dimensions of the various variables you will need to + create: + + H: (hidden_size, hidden_size) + I: (embed_size, hidden_size) + b_1: (hidden_size,) + + Args: + inputs: List of length num_steps, each of whose elements should be + a tensor of shape (batch_size, embed_size). + Returns: + outputs: List of length num_steps, each of whose elements should be + a tensor of shape (batch_size, hidden_size) + """ + batch_size = self.config.batch_size + hidden_size = self.config.hidden_size + embed_size = self.config.embed_size + rnn_cell = tf.nn.rnn_cell.GRUCell(hidden_size) + self.initial_state = rnn_cell.zero_state(self.config.batch_size, dtype=tf.float32) + dropped_inputs = tf.nn.dropout(inputs, keep_prob=self.dropout_placeholder) + with tf.variable_scope('RNN'): + rnn_output, rnn_final_state = tf.nn.dynamic_rnn(rnn_cell, dropped_inputs, initial_state=self.initial_state, dtype=tf.float32) + rnn_output_dropped = tf.nn.dropout(rnn_output, self.dropout_placeholder) + self.final_state = rnn_final_state + return rnn_output_dropped + """ + list way + rnn_outputs = [] + with tf.variable_scope("RNN"): + for timestep, input_t in enumerate(inputs): + batch = tf.nn.dropout(inputs[timestep], self.dropout_placeholder) + rnn_output, rnn_final_state = tf.nn.dynamic_rnn(rnn_cell, batch, + initial_state=self.initial_state, + dtype=tf.float32) + + #rnn_output_dropped = tf.nn.dropout(rnn_output, keep_prob = self.dropout_placeholder) + + if timestep > 0: + tf.get_variable_scope().reuse_variables() + with tf.variable_scope("BasicRNNCell"): + rnn_outputs.append(rnn_output) + + self.final_state = rnn_final_state + + rnn_outputs_reshaped = tf.reshape(tf.concat(rnn_outputs,1),[self.config.batch_size, self.config.num_steps,hidden_size]) + rnn_outputs_dropped = tf.nn.dropout(rnn_outputs_reshaped, self.dropout_placeholder) + """ + return rnn_outputs_dropped + + def add_projection(self, rnn_outputs): + """Adds a projection layer. + + The projection layer transforms the hidden representation to a distribution + over the vocabulary. + + Hint: Here are the dimensions of the variables you will need to + create + + U: (hidden_size, len(vocab)) + b_2: (len(vocab),) + + Args: + rnn_outputs: List of length num_steps, each of whose elements should be + a tensor of shape (batch_size, embed_size). + This might be (batch_size, hidden_size) + Returns: + outputs: List of length num_steps, each a tensor of shape + (batch_size, len(vocab) + """ + with tf.variable_scope('RNN', reuse=tf.AUTO_REUSE): + U = tf.get_variable('U', shape=[self.config.hidden_size, len(self.vocab)]) + b_2 = tf.get_variable('b_2', shape=[len(self.vocab)]) + outputs = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, self.config.hidden_size]), U), [self.config.batch_size, self.config.num_steps, -1]) + b_2 + """ + list version + outputs = [] + for rnn_output in rnn_outputs: + rnn_output = tf.reshape(rnn_output,[self.config.batch_size,hidden_size]) + output = tf.matmul(rnn_output,U)+b_2 + outputs.append(output) + """ + return outputs + + def add_loss_op(self, output): + """Adds loss ops to the computational graph. + + Hint: Use tensorflow.python.ops.seq2seq.sequence_loss to implement sequence loss. + + Args: + output: A tensor of shape (None, self.vocab) + Returns: + loss: A 0-d tensor (scalar) + """ + loss = tf.contrib.seq2seq.sequence_loss(output, self.labels_placeholder, self.loss_weights) + return loss + + def add_training_op(self, loss): + """Sets up the training Ops. + + Creates an optimizer and applies the gradients to all trainable variables. + The Op returned by this function is what must be passed to the + `sess.run()` call to cause the model to train. See + + https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html#Optimizer + + for more information. + + Hint: Use tf.train.AdamOptimizer for this model. + Calling optimizer.minimize() will return a train_op object. + + Args: + loss: Loss tensor, from cross_entropy_loss. + Returns: + train_op: The Op for training. + """ + with tf.variable_scope('RNN', reuse=tf.AUTO_REUSE): + opt = tf.train.AdamOptimizer(self.config.lr) + train_op = opt.minimize(loss) + return train_op + + def __init__(self, config): + self.config = config + self.load_data(debug=False) + self.add_placeholders() + self.inputs = self.add_embedding() + self.rnn_outputs = self.add_model(self.inputs) + self.outputs = self.add_projection(self.rnn_outputs) + self.predictions = [tf.nn.softmax(tf.cast(self.outputs, 'float64'))] + output = tf.reshape(tf.concat(self.outputs, 1), [self.config.batch_size, self.config.num_steps, len(self.vocab)]) + self.calculate_loss = self.add_loss_op(output) + self.train_step = self.add_training_op(self.calculate_loss) + + def run_epoch(self, session, data, train_op=None, verbose=10): + config = self.config + dp = config.dropout + if not train_op: + train_op = tf.no_op() + dp = 1 + total_steps = sum(1 for x in ptb_iterator(data, config.batch_size, config.num_steps)) + total_loss = [] + state = self.initial_state.eval() + for step, (x, y) in enumerate(ptb_iterator(data, config.batch_size, config.num_steps)): + feed = {self.input_placeholder: x, self.labels_placeholder: y, self.initial_state: state, self.dropout_placeholder: dp} + loss, state, _ = session.run([self.calculate_loss, self.final_state, train_op], feed_dict=feed) + total_loss.append(loss) + if verbose and step % verbose == 0: + sys.stdout.write('\r{} / {} : pp = {}'.format(step, total_steps, np.exp(np.mean(total_loss)))) + sys.stdout.flush() + if verbose: + sys.stdout.write('\r') + return np.exp(np.mean(total_loss)) + + +def generate_text(session, model, config, starting_text='', stop_length=100, stop_tokens=None, temp=1.0): + """Generate text from the model. + + Hint: Create a feed-dictionary and use sess.run() to execute the model. Note + that you will need to use model.initial_state as a key to feed_dict + Hint: Fetch model.final_state and model.predictions[-1]. (You set + model.final_state in add_model() and model.predictions is set in + __init__) + Hint: Store the outputs of running the model in local variables state and + y_pred (used in the pre-implemented parts of this function.) + + Args: + session: tf.Session() object + model: Object of type RNNLM_Model + config: A Config() object + starting_text: Initial text passed to model. + Returns: + output: List of word idxs + """ + state = model.initial_state.eval() + tokens = [model.vocab.encode(word) for word in starting_text.split()] + for token in tokens: + feed = {model.input_placeholder: [[token]], model.initial_state: state, model.dropout_placeholder: 1} + state = session.run(model.final_state, feed_dict=feed) + last_word = token + for i in range(stop_length): + feed = {model.input_placeholder: [[last_word]], model.initial_state: state, model.dropout_placeholder: 1} + state, y_pred = session.run([model.final_state, model.predictions[-1]], feed_dict=feed) + next_word_idx = sample(y_pred[0, 0], temperature=temp) + tokens.append(next_word_idx) + last_word = next_word_idx + if stop_tokens and model.vocab.decode(tokens[-1]) in stop_tokens: + break + output = [model.vocab.decode(word_idx) for word_idx in tokens] + return output + + +def generate_sentence(session, model, config, *args, **kwargs): + """Convenice to generate a sentence from the model.""" + return generate_text(session, model, config, *args, stop_tokens=[''], **kwargs) + + +def test_RNNLM(): + config = Config() + gen_config = deepcopy(config) + gen_config.batch_size = gen_config.num_steps = 1 + tf.reset_default_graph() + with tf.variable_scope('RNNLM') as scope: + model = RNNLM_Model(config) + scope.reuse_variables() + gen_model = RNNLM_Model(gen_config) + init = tf.initialize_all_variables() + saver = tf.train.Saver() + with tf.Session() as session: + best_val_pp = float('inf') + best_val_epoch = 0 + session.run(init) + for epoch in range(Config.max_epochs): + print('Epoch {}'.format(epoch)) + start = time.time() + train_pp = model.run_epoch(session, model.encoded_train, train_op=model.train_step) + valid_pp = model.run_epoch(session, model.encoded_valid) + print('Training perplexity: {}'.format(train_pp)) + print('Validation perplexity: {}'.format(valid_pp)) + if valid_pp < best_val_pp: + best_val_pp = valid_pp + best_val_epoch = epoch + saver.save(session, './ptb_rnnlm.weights') + if epoch - best_val_epoch > config.early_stopping: + break + print('Total time: {}'.format(time.time() - start)) + saver.restore(session, 'ptb_rnnlm.weights') + test_pp = model.run_epoch(session, model.encoded_test) + print('=-=' * 5) + print('Test perplexity: {}'.format(test_pp)) + print('=-=' * 5) + compare_test_get = False + if compare_test_get: + test_gen = gen_model.run_epoch(session, model.encoded_test) + print('Gen Test perplexity: {}'.format(test_gen)) + print('=-=' * 5) + starting_snippets = ['in boston', 'they have', 'please', 'today', 'the president', 'in winter', 'i want', 'look at', 'come to', 'he said'] + for starting_text in starting_snippets: + print('\n') + print(' '.join(generate_sentence(session, gen_model, gen_config, starting_text=starting_text, temp=1))) + + +if __name__ == '__main__': + test_RNNLM() diff --git a/dataset/preprocessed/236.txt b/dataset/preprocessed/236.txt new file mode 100644 index 0000000..61e5479 --- /dev/null +++ b/dataset/preprocessed/236.txt @@ -0,0 +1,128 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +data_dir = '/home/jsemko/data/udacity' +pickle_file = 'notMNIST.pickle' +pickle_file = os.path.join(data_dir, pickle_file) +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels, return_wrong=False): + correct = np.argmax(predictions, 1) == np.argmax(labels, 1) + acc = 100.0 * np.sum(correct) / predictions.shape[0] + if return_wrong: + return acc, ~correct + else: + return acc + + +batch_size = 128 +beta = 0.02 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 101 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +beta = 0.001 +HL_size = 1024 +keep_prob_conn = 1 +keep_prob_neurons = 0.9 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.constant(beta) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, HL_size])) + biases_1 = tf.Variable(tf.zeros([HL_size])) + weights_2 = tf.Variable(tf.truncated_normal([HL_size, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + neurons = tf.nn.relu(tf.matmul(tf.nn.dropout(tf_train_dataset, keep_prob_neurons), tf.nn.dropout(weights_1, keep_prob_conn)) + biases_1) + logits = tf.matmul(tf.nn.dropout(neurons, keep_prob_neurons), tf.nn.dropout(weights_2, keep_prob_conn)) + biases_2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + tf.mul(tf_beta, tf.add(tf.nn.l2_loss(weights_1), tf.nn.l2_loss(weights_2)))) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 500, 0.95) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 15001 +plot_incorrect = False +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + if step % 100 == 0: + print(step) + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + acc, incorrect = accuracy(test_prediction.eval(), test_labels, return_wrong=True) + print('Test accuracy: %.1f%%' % acc) + if plot_incorrect: + import matplotlib.pyplot as plt + bad_pics = test_dataset[(incorrect), :].reshape((sum(incorrect), 28, 28)) + for i in range(bad_pics.shape[0]): + plt.imshow(bad_pics[(i), :, :], cmap='bone') + plt.show() diff --git a/dataset/preprocessed/237.txt b/dataset/preprocessed/237.txt new file mode 100644 index 0000000..9f6bd31 --- /dev/null +++ b/dataset/preprocessed/237.txt @@ -0,0 +1,140 @@ +import tensorflow as tf +import tensorflow.contrib.rnn as rnn +from math import sqrt +learning_rate = 0.01 +gaussian_noise = 0.2 +dropout_prob = 0.7 +regularize_beta = 1e-07 +word_vocab_size = 100004 +subword_vocab_size = 100004 +word_embedding_dim = 64 +subword_embedding_dim = 64 +input_subword_dim = 100 +input_dim = 100 +hidden_dim = 150 +hidden_2_dim = 100 +output_types = ['tag', 'word_freq'] +n_output = dict() +n_output['tag'] = 60 +n_output['word_freq'] = 15 +n_saved_models = 2 +x = dict() +x['words'] = tf.placeholder(tf.int32, [None, None], name='input_words') +x['subwords'] = tf.placeholder(tf.int32, [None, None], name='input_subwords') +x['subwords_len'] = tf.placeholder(tf.int32, [None], name='input_subwords_len') +label = dict() +label['tag'] = tf.placeholder(tf.int32, [None], name='label_tags') +label['word_freq'] = tf.placeholder(tf.int32, [None], name='label_word_freq') +word_embedding = tf.Variable(tf.zeros([word_vocab_size, word_embedding_dim]), trainable=False, name='word_emb') +subword_embedding = tf.Variable(tf.zeros([subword_vocab_size, subword_embedding_dim]), trainable=False, name='sw_emb') + + +def nn_run_subword_processing(subwords): + with tf.variable_scope('bpos_subword_lstm') as vs: + mapped_subwords = tf.nn.embedding_lookup(subword_embedding, subwords) + lstm_fw_cell = rnn.BasicLSTMCell(input_subword_dim, reuse=tf.get_variable_scope().reuse) + lstm_bw_cell = rnn.BasicLSTMCell(input_subword_dim, reuse=tf.get_variable_scope().reuse) + lstm_outputs, _ = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, mapped_subwords, dtype=tf.float32, scope=vs) + fw_outputs, bw_outputs = lstm_outputs + return tf.concat([fw_outputs[:, (-1), :], bw_outputs[:, (-1), :]], 1) + + +def nn_run_input_layer(input_dict): + mapped_words = tf.nn.embedding_lookup(word_embedding, input_dict['words']) + subword_vec = nn_run_subword_processing(input_dict['subwords']) + expanded_subword_vec = tf.expand_dims(subword_vec, 0) + word_subword = tf.concat([mapped_words, expanded_subword_vec], axis=2) + with tf.variable_scope('bpos_word_lstm') as vs: + lstm_fw_cell = rnn.BasicLSTMCell(input_dim, reuse=tf.get_variable_scope().reuse) + lstm_bw_cell = rnn.BasicLSTMCell(input_dim, reuse=tf.get_variable_scope().reuse) + lstm_outputs, _ = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, word_subword, dtype=tf.float32, scope=vs) + concat_data = tf.concat([lstm_outputs[0], lstm_outputs[1]], axis=2) + input_data = tf.reshape(concat_data, [-1, input_dim * 2]) + input_noise = tf.Variable(tf.truncated_normal([1, input_dim * 2], stddev=gaussian_noise), name='input_noise') + return tf.add(input_data, input_noise) + + +def nn_run_hidden_layer(input_vec): + hidden_weight = tf.Variable(tf.truncated_normal([input_dim * 2, hidden_dim], stddev=1.0 / sqrt(hidden_dim)), name='weights/hidden_layer') + hidden_bias = tf.Variable(tf.zeros([hidden_dim]), name='bias/hidden_layer') + hidden_2_weight = tf.Variable(tf.truncated_normal([hidden_dim, hidden_2_dim], stddev=1.0 / sqrt(hidden_2_dim)), name='weights/hidden_2_layer') + hidden_2_bias = tf.Variable(tf.zeros([hidden_2_dim]), name='bias/hidden_2_layer') + hidden_1_output = tf.nn.tanh(tf.add(tf.matmul(input_vec, hidden_weight), hidden_bias)) + hidden_2_output = tf.nn.relu(tf.add(tf.matmul(hidden_1_output, hidden_2_weight), hidden_2_bias)) + return hidden_2_output + + +def nn_classify(hidden_vec): + out_weights = dict() + out_biases = dict() + outputs = dict() + for output_type in output_types: + out_weights[output_type] = tf.Variable(tf.truncated_normal([hidden_2_dim, n_output[output_type]], stddev=1.0 / sqrt(n_output[output_type])), name='weights/out_' + output_type) + out_biases[output_type] = tf.Variable(tf.zeros([n_output[output_type]]), name='bias/out_' + output_type) + outputs[output_type] = tf.matmul(hidden_vec, out_weights[output_type]) + out_biases[output_type] + return outputs + + +def nn_calculate_loss(predicted_outputs): + all_loss = 0.0 + for output_type in output_types: + y = tf.one_hot(label[output_type], n_output[output_type], on_value=1.0, off_value=0.0, axis=-1) + dropped_output = tf.nn.dropout(predicted_outputs[output_type], dropout_prob) + ce_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=dropped_output)) + all_loss += ce_loss + all_weights = [tensor for tensor in tf.global_variables() if 'weights' in tensor.name] + l2_score = regularize_beta * sum([tf.nn.l2_loss(tensor) for tensor in all_weights]) + all_loss += l2_score + return all_loss + + +processed_input = nn_run_input_layer(x) +h = nn_run_hidden_layer(processed_input) +outputs = nn_classify(h) +loss = nn_calculate_loss(outputs) +optimize = tf.train.AdamOptimizer(name='bpos_tag_opt').minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver(max_to_keep=n_saved_models) + + +class TaggerModel: + + def __init__(self, embeddings=None, model_path=None): + self.session = tf.Session() + if model_path is not None: + saver.restore(self.session, model_path) + else: + self.session.run(init) + self.initial_embedding(embeddings[0], embeddings[1]) + + def train(self, input_list, labels): + feed_dict = TaggerModel.get_feed_dict(input_list, labels) + _, iter_loss = self.session.run([optimize, loss], feed_dict=feed_dict) + return iter_loss + + def predict(self, input_list): + feed_dict = TaggerModel.get_feed_dict(input_list) + prediction = self.session.run(outputs, feed_dict=feed_dict) + return prediction['tag'] + + def save_model(self, save_path, global_step): + saver.save(self.session, save_path, global_step=global_step) + print('Model at epoch', global_step, 'is saved.') + + def initial_embedding(self, word_emb_input, subword_emb_input): + word_emb_placeholder = tf.placeholder(tf.float32, [word_vocab_size, word_embedding_dim], name='word_pl') + subword_emb_placeholder = tf.placeholder(tf.float32, [subword_vocab_size, subword_embedding_dim], name='sw_pl') + word_assign = tf.assign(word_embedding, word_emb_placeholder) + subword_assign = tf.assign(subword_embedding, subword_emb_placeholder) + feed_dict = {word_emb_placeholder: word_emb_input, subword_emb_placeholder: subword_emb_input} + self.session.run([word_assign, subword_assign], feed_dict=feed_dict) + + @staticmethod + def get_feed_dict(input_list, labels=None): + feed_dict = dict() + feed_dict[x['words']] = input_list[0] + feed_dict[x['subwords']] = input_list[1] + feed_dict[x['subwords_len']] = input_list[2] + if labels is not None: + feed_dict[label['tag']], feed_dict[label['word_freq']] = labels + return feed_dict diff --git a/dataset/preprocessed/238.txt b/dataset/preprocessed/238.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/238.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/239.txt b/dataset/preprocessed/239.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/239.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/240.txt b/dataset/preprocessed/240.txt new file mode 100644 index 0000000..f20d8c7 --- /dev/null +++ b/dataset/preprocessed/240.txt @@ -0,0 +1,367 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import sys +if sys.version[0] == '2': + import cPickle as pkl +else: + import pickle as pkl +import numpy as np +import tensorflow as tf +from PNN.author import utils +dtype = utils.DTYPE + + +class Model: + + def __init__(self): + self.sess = None + self.X = None + self.y = None + self.layer_keeps = None + self.vars = None + self.keep_prob_train = None + self.keep_prob_test = None + + def run(self, fetches, X=None, y=None, mode='train'): + feed_dict = {} + if type(self.X) is list: + for i in range(len(X)): + feed_dict[self.X[i]] = X[i] + else: + feed_dict[self.X] = X + if y is not None: + feed_dict[self.y] = y + if self.layer_keeps is not None: + if mode == 'train': + feed_dict[self.layer_keeps] = self.keep_prob_train + elif mode == 'test': + feed_dict[self.layer_keeps] = self.keep_prob_test + return self.sess.run(fetches, feed_dict) + + def dump(self, model_path): + var_map = {} + for name, var in self.vars.iteritems(): + var_map[name] = self.run(var) + pkl.dump(var_map, open(model_path, 'wb')) + print('model dumped at', model_path) + + +class LR(Model): + + def __init__(self, input_dim=None, output_dim=1, init_path=None, opt_algo='gd', learning_rate=0.01, l2_weight=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + b = self.vars['b'] + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=logits)) + l2_weight * tf.nn.l2_loss(xw) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FM(Model): + + def __init__(self, input_dim=None, output_dim=1, factor_order=10, init_path=None, opt_algo='gd', learning_rate=0.01, l2_w=0, l2_v=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + v = self.vars['v'] + b = self.vars['b'] + X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + l2_w * tf.nn.l2_loss(xw) + l2_v * tf.nn.l2_loss(xv) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FNN(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + node_in = num_inputs * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + print(l.shape, wi.shape, bi.shape) + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class DeepFM(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + init_vars.append(('weight_%d' % i, [field_sizes[i], 1], 'xavier', dtype)) + init_vars.append(('bias', [1], 'zero', dtype)) + node_in = num_inputs * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = [self.vars['weight_%d' % i] for i in range(num_inputs)] + v = [self.vars['embed_%d' % i] for i in range(num_inputs)] + b = self.vars['bias'] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w[i]) for i in range(num_inputs)], 1) + xv = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], v[i]) for i in range(num_inputs)], 1) + l = xv + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + print(l.shape, wi.shape, bi.shape) + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + xv = tf.reshape(xv, [-1, num_inputs, embed_size]) + p = 0.5 * tf.reduce_sum(tf.square(tf.reduce_sum(xv, 1)) - tf.reduce_sum(tf.square(xv), 1), 1) + xw = tf.reduce_sum(xw, 1) + logits = tf.reshape(l + xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class CCPM(Model): + + def __init__(self, field_sizes=None, embed_size=10, filter_sizes=None, layer_acts=None, drop_out=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + init_vars.append(('b1', [1], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + f1 = self.vars['f1'] + l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + f2 = self.vars['f2'] + l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + b1 = self.vars['b1'] + l = tf.matmul(l, w1) + b1 + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN1(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + l = tf.concat([xw, ip], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN2(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None, layer_norm=True): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + init_vars.append(('kernel', [embed_size, num_pairs, embed_size], 'xavier', dtype)) + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + k = self.vars['kernel'] + p = tf.expand_dims(p, 1) + kp = tf.reduce_sum(tf.multiply(tf.transpose(tf.reduce_sum(tf.multiply(p, k), -1), [0, 2, 1]), q), -1) + l = tf.concat([xw, kp], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) diff --git a/dataset/preprocessed/241.txt b/dataset/preprocessed/241.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/241.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/242.txt b/dataset/preprocessed/242.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/242.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/243.txt b/dataset/preprocessed/243.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/243.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/245.txt b/dataset/preprocessed/245.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/245.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/246.txt b/dataset/preprocessed/246.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/246.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/247.txt b/dataset/preprocessed/247.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/247.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/248.txt b/dataset/preprocessed/248.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/248.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/249.txt b/dataset/preprocessed/249.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/249.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/25.txt b/dataset/preprocessed/25.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/25.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/250.txt b/dataset/preprocessed/250.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/250.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/251.txt b/dataset/preprocessed/251.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/251.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/252.txt b/dataset/preprocessed/252.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/252.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/253.txt b/dataset/preprocessed/253.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/253.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/254.txt b/dataset/preprocessed/254.txt new file mode 100644 index 0000000..97945ca --- /dev/null +++ b/dataset/preprocessed/254.txt @@ -0,0 +1,142 @@ +import tensorflow as tf +import numpy as np + + +class FullyConnectedLayer(object): + + def __init__(self, inp, dim, nonlinearity=False): + self.W = tf.Variable(tf.random_normal(dim)) + self.b = tf.Variable(tf.constant(1.0, shape=(1, dim[1]))) + if nonlinearity: + self.out = nonlinearity(tf.matmul(inp, self.W) + self.b) + else: + self.out = tf.matmul(inp, self.W) + self.b + + +class NeuralNetwork(object): + + def __init__(self, inp, indim, enddim, hidden_layers=[10], nonlinearity=False, name='default'): + self.layers = [] + self.x = inp + self.indim = indim + self.enddim = enddim + inp = self.x + prev_dim = indim + for out_dim in hidden_layers: + self.layers.append(FullyConnectedLayer(inp, (prev_dim, out_dim), nonlinearity=nonlinearity)) + inp = self.layers[-1].out + prev_dim = out_dim + self.layers.append(FullyConnectedLayer(inp, (prev_dim, enddim), nonlinearity=False)) + self.out = self.layers[-1].out + + def variables(self): + variables = [] + for layer in self.layers: + variables.extend([layer.W, layer.b]) + return variables + + +class CriticNeuralNetwork(object): + + def __init__(self, statedim, actiondim, enddim, hidden_layers=[10], nonlinearity=False, name='default'): + self.state = tf.placeholder(tf.float32, [None, statedim]) + self.action = tf.placeholder(tf.float32, [None, actiondim]) + inp = tf.concat(1, [self.state, self.action]) + self.network = NeuralNetwork(inp, statedim + actiondim, enddim, hidden_layers, nonlinearity, name) + self.out = self.network.out + + +class ActorNeuralNetwork(object): + + def __init__(self, indim, enddim, hidden_layers=[10], nonlinearity=False, name='default'): + self.x = tf.placeholder(tf.float32, [None, indim]) + self.network = NeuralNetwork(self.x, indim, enddim, hidden_layers, nonlinearity, name) + self.out = self.network.out + + def variables(self): + return self.network.variables() + + +class Critic(object): + + def __init__(self, nn, parameters, discount=0.95): + self.discount = tf.constant(discount) + self._setup_q_calculation(nn) + self._setup_next_q_calulcation(nn) + self._setup_train_step(parameters) + init = tf.global_variables_initializer() + self.session = tf.Session() + self.session.run(init) + + def _setup_q_calculation(self, nn): + self.state = nn.state + self.action = nn.action + self.q = nn.out + + def _setup_next_q_calulcation(self, nn): + self.r = tf.placeholder(tf.float32, [None, 1]) + self.terminal = tf.placeholder(tf.float32, [None, 1]) + self.update = self.q * (self.terminal * self.discount) + self.r + + def _setup_train_step(self, parameters): + self.target = tf.placeholder(tf.float32, [None, 1]) + self.loss = tf.reduce_sum(tf.square(self.target - self.q)) + self.train_step = tf.train.AdamOptimizer(learning_rate=parameters['learning_rate']).minimize(self.loss) + self.gradients = tf.gradients(self.q, self.action) + + def next_q(self, rewards, next_state, next_action, terminals): + return self.session.run(self.update, feed_dict={self.r: rewards, self.state: next_state, self.action: next_action, self.terminal: terminals}) + + def current_q(self, state, action): + return self.session.run(self.q, feed_dict={self.state: state, self.action: action}) + + def calcloss(self, state, action, rewards, next_state, terminals): + target = self.calcq(rewards, next_state, terminals) + return self.session.run(self.loss, feed_dict={self.target: target, self.x: state}) + + def calcgradient(self, state, action): + return self.session.run(self.gradients, feed_dict={self.state: state, self.action: action})[0] + + def trainstep(self, state, action, rewards, next_state, next_action, terminals): + target = self.next_q(rewards, next_state, next_action, terminals) + return self.session.run(self.train_step, feed_dict={self.target: target, self.state: state, self.action: action}) + + def coldstart(self, state, action): + target = [[-4] for _ in range(len(x))] + return self.session.run(self.train_step, feed_dict={self.target: target, self.state: state, self.action: action}) + + def __exit__(self): + self.session.close() + + +class Actor(object): + + def __init__(self, nn, scaler, parameters, discount=0.95): + self.discount = tf.constant(discount) + self._setup_q_calculation(nn, scaler) + self._setup_train_step(nn, parameters) + self.session = tf.Session() + init = tf.global_variables_initializer() + self.session.run(init) + + def _setup_train_step(self, nn, parameters): + self.critic_gradients = tf.placeholder(tf.float32, [None, nn.out.get_shape()[1]]) + self.trainable_variables = nn.variables() + self.gradients = tf.gradients(nn.out, self.trainable_variables, -self.critic_gradients) + self.train_step = tf.train.AdamOptimizer(learning_rate=parameters['learning_rate']).apply_gradients(zip(self.gradients, self.trainable_variables)) + + def _setup_q_calculation(self, nn, scaler): + self.x = nn.x + self.act = tf.nn.tanh(nn.out) * scaler + + def action(self, state): + return self.session.run(self.act, feed_dict={self.x: state})[0] + + def actions(self, state): + return self.session.run(self.act, feed_dict={self.x: state}) + + def train(self, state, gradients): + self.session.run(self.train_step, feed_dict={self.x: state, self.critic_gradients: map(lambda x, d=len(state): x / d, gradients)}) + + def __exit__(self): + self.session.close() diff --git a/dataset/preprocessed/255.txt b/dataset/preprocessed/255.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/255.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/256.txt b/dataset/preprocessed/256.txt new file mode 100644 index 0000000..9e521ae --- /dev/null +++ b/dataset/preprocessed/256.txt @@ -0,0 +1,65 @@ +import tensorflow as tf +import numpy as np + + +class MainModel(object): + + def __init__(self, sentMaxl, sentMaxp, sentMaxr, num_classes, wv, emb_size, l2_reg_lambda, learning_rate): + tf.reset_default_graph() + self.X_lids = tf.placeholder(tf.int32, [None, sentMaxl], name='X_lids') + self.X_pids = tf.placeholder(tf.int32, [None, sentMaxp], name='X_pids') + self.X_rids = tf.placeholder(tf.int32, [None, sentMaxr], name='X_rids') + self.X_llen = tf.placeholder(tf.int32, [None], name='X_llen') + self.X_plen = tf.placeholder(tf.int32, [None], name='X_plen') + self.X_rlen = tf.placeholder(tf.int32, [None], name='X_rlen') + self.input_y = tf.placeholder(tf.float32, [None, num_classes], name='input_y') + self.dropout_keep_prob = tf.placeholder(tf.float32, name='dropout_keep_prob') + W_wemb = tf.Variable(wv) + print('W_wemb, ', W_wemb.get_shape(), '--, self.X_lids ', self.X_lids.get_shape()) + emb_l = tf.nn.embedding_lookup(W_wemb, self.X_lids) + print('emb_l ', emb_l.get_shape()) + emb_p = tf.nn.embedding_lookup(W_wemb, self.X_pids) + emb_r = tf.nn.embedding_lookup(W_wemb, self.X_rids) + emb_l = tf.expand_dims(emb_l, -1) + print('emb_l expanded, ', emb_l.get_shape()) + emb_p = tf.expand_dims(emb_p, -1) + emb_r = tf.expand_dims(emb_r, -1) + pooled_l = tf.nn.max_pool(emb_l, ksize=[1, sentMaxl, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='pooll') + pooled_p = tf.nn.max_pool(emb_p, ksize=[1, sentMaxp, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='poolp') + pooled_r = tf.nn.max_pool(emb_r, ksize=[1, sentMaxr, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='poolr') + print('pooled_l ', pooled_l.get_shape()) + final_l = tf.reshape(pooled_l, [-1, emb_size]) + final_p = tf.reshape(pooled_p, [-1, emb_size]) + final_r = tf.reshape(pooled_r, [-1, emb_size]) + print('final_l is ', final_l.get_shape()) + XX = tf.concat(1, [final_l, final_p, final_r]) + print(XX.get_shape()) + h = tf.nn.dropout(XX, self.dropout_keep_prob) + h = tf.nn.relu(h) + W = tf.Variable(tf.truncated_normal([3 * emb_size, num_classes], stddev=0.1), name='W') + b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name='b') + scores = tf.nn.xw_plus_b(h, W, b, name='scores') + print('score', scores.get_shape()) + self.predictions = tf.argmax(scores, 1, name='predictions') + losses = tf.nn.softmax_cross_entropy_with_logits(scores, self.input_y) + self.loss = tf.reduce_mean(losses) + l2_reg_lambda * (tf.nn.l2_loss(W) + tf.nn.l2_loss(b)) + self.correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) + self.accuracy = tf.reduce_mean(tf.cast(self.correct_predictions, 'float'), name='accuracy') + self.optimizer = tf.train.AdamOptimizer(learning_rate) + self.grads_and_vars = self.optimizer.compute_gradients(self.loss) + self.global_step = tf.Variable(0, name='global_step', trainable=False) + self.train_op = self.optimizer.apply_gradients(self.grads_and_vars, global_step=self.global_step) + session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) + self.sess = tf.Session(config=session_conf) + self.sess.run(tf.global_variables_initializer()) + self.saver = tf.train.Saver(max_to_keep=50) + + def train_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y, drop_out): + feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: drop_out} + _, step, loss, accuracy, predictions = self.sess.run([self.train_op, self.global_step, self.loss, self.accuracy, self.predictions], feed_dict) + return loss, accuracy + + def test_step(self, batch_lids, batch_pids, batch_rids, batch_llen, batch_plen, batch_rlen, batch_y): + feed_dict = {self.X_lids: batch_lids, self.X_pids: batch_pids, self.X_rids: batch_rids, self.X_llen: batch_llen, self.X_plen: batch_plen, self.X_rlen: batch_rlen, self.input_y: batch_y, self.dropout_keep_prob: 1.0} + accuracy = self.sess.run([self.accuracy], feed_dict) + return accuracy diff --git a/dataset/preprocessed/257.txt b/dataset/preprocessed/257.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/257.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/258.txt b/dataset/preprocessed/258.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/258.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/259.txt b/dataset/preprocessed/259.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/259.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/26.txt b/dataset/preprocessed/26.txt new file mode 100644 index 0000000..2c77861 --- /dev/null +++ b/dataset/preprocessed/26.txt @@ -0,0 +1,73 @@ +import tensorflow as tf +import tensorflow.examples.tutorials.mnist.input_data +mnist = tensorflow.examples.tutorials.mnist.input_data.read_data_sets('MNIST_data/', one_hot=True) +x = tf.placeholder(tf.float32, [None, 784]) +W = tf.Variable(tf.zeros([784, 10])) +b = tf.Variable(tf.zeros([10])) +y = tf.nn.softmax(tf.matmul(x, W) + b) +y_ = tf.placeholder('float', [None, 10]) +print(W.name) +print(b.name) +saver = tf.train.Saver() +cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) +train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) +init = tf.global_variables_initializer() +sess = tf.Session() +sess.run(init) +for i in range(10000): + batch_xs, batch_ys = mnist.train.next_batch(100) + sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) + + +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) + + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder('float') +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) +train_step = tf.train.AdamOptimizer(0.0001).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.run(tf.initialize_all_variables()) +for i in range(20000): + batch = mnist.train.next_batch(50) + if i % 100 == 0: + train_accuracy = accuracy.eval(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) + print('step %d, training accuracy %g' % (i, train_accuracy)) + train_step.run(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) +print('test accuracy %g' % accuracy.eval(session=sess, feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) +saver.save(sess, './check/Mymodel.ckpt') +with open('myNote.txt', 'w+') as fi: + for i in b.eval(sess): + fi.write(i + ' ') diff --git a/dataset/preprocessed/260.txt b/dataset/preprocessed/260.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/260.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/261.txt b/dataset/preprocessed/261.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/261.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/262.txt b/dataset/preprocessed/262.txt new file mode 100644 index 0000000..43cc9cb --- /dev/null +++ b/dataset/preprocessed/262.txt @@ -0,0 +1,158 @@ +import tensorflow as tf +hello_constant = tf.constant('Hello World!') +with tf.Session() as sess: + output = sess.run(hello_constant) + print(output) +A = tf.constant(1234) +B = tf.constant([123, 456, 789]) +C = tf.constant([[123, 456, 789], [222, 333, 444]]) +with tf.Session() as sess2: + output1 = sess2.run(C) + print(output1) +x = tf.placeholder(tf.string) +y = tf.placeholder(tf.int32) +z = tf.placeholder(tf.float32) + + +def placeHolder(string): + output = None + x = tf.placeholder(tf.string) + with tf.Session() as sess: + output = sess.run(x, feed_dict={x: string}) + return print(output) + + +placeHolder('Good Morning World!') +x = tf.add(5, 2) +x = tf.subtract(10, 4) +y = tf.multiply(2, 5) +tf.subtract(tf.cast(tf.constant(2.0), tf.int32), tf.constant(1)) +x = tf.constant(10) +y = tf.constant(2) +z = tf.subtract(tf.divide(x, y), tf.cast(tf.constant(1), tf.float64)) +with tf.Session() as sess: + output = sess.run(z) + print(output) +matW = tf.constant([[-0.5, 0.2, 0.1], [0.7, -0.8, 0.2]]) +matX = tf.constant([[0.2], [0.5], [0.6]]) +b = tf.constant([[0.1], [0.2]]) +prod = tf.matmul(matW, matX) +sum1 = tf.add(prod, b) +with tf.Session() as sess: + output = sess.run(sum1) + print(output) +x = tf.Variable(5) +init = tf.global_variables_initializer() +with tf.Session() as sess: + sess.run(init) +n_features = 120 +n_labels = 5 +weights = tf.Variable(tf.truncated_normal((n_features, n_labels))) +n_labels = 5 +bias = tf.Variable(tf.zeros(n_labels)) + + +def get_weights(n_features, n_labels): + return tf.Variable(tf.truncated_normal((n_features, n_labels))) + + +def get_biases(n_labels): + return tf.Variable(tf.zeros(n_labels)) + + +def linear(input, w, b): + return tf.add(tf.matmul(input, w), b) + + +x = tf.nn.softmax([2.0, 1.0, 0.2]) + + +def run(): + output = None + logit_data = [2.0, 1.0, 0.1] + logits = tf.placeholder(tf.float32) + softmax = tf.nn.softmax(logit_data) + with tf.Session() as sess: + output = sess.run(softmax, feed_dict={logits: logit_data}) + return output + + +softmax_data = [0.7, 0.2, 0.1] +one_hot_data = [1.0, 0.0, 0.0] +softmax = tf.placeholder(tf.float32) +one_hot = tf.placeholder(tf.float32) +cross_entropy = -tf.reduce_sum(tf.multiply(one_hot, tf.log(softmax))) +with tf.Session() as sess: + print(sess.run(cross_entropy, feed_dict={softmax: softmax_data, one_hot: one_hot_data})) +from tensorflow.examples.tutorials.mnist import input_data +import tensorflow as tf +n_input = 784 +n_classes = 10 +mnist = input_data.read_data_sets('/Users/ChrisErnst/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/include/unsupported/Eigen/CXX11/src/datasets/ud730/mnist', one_hot=True) +train_features = mnist.train.images +test_features = mnist.test.images +train_labels = mnist.train.labels.astype(np.float32) +test_labels = mnist.test.labels.astype(np.float32) +weights = tf.Variable(tf.random_normal([n_input, n_classes])) +bias = tf.Variable(tf.random_normal([n_classes])) +features = tf.placeholder(tf.float32, [None, n_input]) +labels = tf.placeholder(tf.float32, [None, n_classes]) +example_features = [['F11', 'F12', 'F13', 'F14'], ['F21', 'F22', 'F23', 'F24'], ['F31', 'F32', 'F33', 'F34'], ['F41', 'F42', 'F43', 'F44']] +example_labels = [['L11', 'L12'], ['L21', 'L22'], ['L31', 'L32'], ['L41', 'L42']] +import math + + +def batches(batch_size, features, labels): + """ + Create batches of features and labels + :param batch_size: The batch size + :param features: List of features + :param labels: List of labels + :return: Batches of (Features, Labels) + """ + assert len(features) == len(labels) + output_batches = [] + sample_size = len(features) + for start_i in range(0, sample_size, batch_size): + end_i = start_i + batch_size + batch = [features[start_i:end_i], labels[start_i:end_i]] + output_batches.append(batch) + return output_batches + + +example_batches = batches(batch_size, example_features, example_labels) +hidden_layer = tf.add(tf.matmul(features, hidden_weights), hidden_biases) +hidden_layer = tf.nn.relu(hidden_layer) +output = tf.add(tf.matmul(hidden_layer, output_weights), output_biases) +import tensorflow as tf +output = None +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[1.0, 2.0, 3.0, 4.0], [-1.0, -2.0, -3.0, -4.0], [11.0, 12.0, 13.0, 14.0]]) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + print(sess.run(logits)) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +import tensorflow as tf +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[0.0, 2.0, 3.0, 4.0], [0.1, 0.2, 0.3, 0.4], [11.0, 12.0, 13.0, 14.0]]) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) +print(sess.run(logits, feed_dict={keep_prob: 0.5})) diff --git a/dataset/preprocessed/263.txt b/dataset/preprocessed/263.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/263.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/264.txt b/dataset/preprocessed/264.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/264.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/265.txt b/dataset/preprocessed/265.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/265.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/266.txt b/dataset/preprocessed/266.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/266.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/267.txt b/dataset/preprocessed/267.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/267.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/268.txt b/dataset/preprocessed/268.txt new file mode 100644 index 0000000..73e064c --- /dev/null +++ b/dataset/preprocessed/268.txt @@ -0,0 +1,77 @@ +import tensorflow as tf +import numpy as np + + +def main(): + with tf.name_scope('input'): + print('input') + NX = 60 + NY = 60 + RENDER = True + n_step = 500 + px = NY * (NX // 3) + py_ = NY * NX // (5 * 5) * 8 + with tf.name_scope('model'): + print('model') + s0 = tf.placeholder(tf.float32, shape=[NY, NX // 3], name='s0') + s1 = tf.placeholder(tf.float32, shape=[NY, NX // 3], name='s1') + s2 = tf.placeholder(tf.float32, shape=[NY, NX // 3], name='s2') + _red_ = tf.placeholder(tf.float32, shape=[None], name='_red_') + _green_ = tf.placeholder(tf.float32, shape=[None], name='_green_') + _blue_ = tf.placeholder(tf.float32, shape=[None], name='_blue_') + P_r = _red_ * s0 + _red_ * -1 * (s0 * -1 + 1) + P_g = _green_ * s1 + _green_ * -1 * (s1 * -1 + 1) + P_b = _blue_ * s2 + _blue_ * -1 * (s2 * -1 + 1) + Pool_red = tf.nn.avg_pool(tf.reshape(P_r, [1, NY, NX // 3, 1]), ksize=[1, NY, NX // 3, 1], strides=[1, 1, 1, 1], padding='SAME') + Pool_green = tf.nn.avg_pool(tf.reshape(P_g, [1, NY, NX // 3, 1]), ksize=[1, NY, NX // 3, 1], strides=[1, 1, 1, 1], padding='SAME') + Pool_blue = tf.nn.avg_pool(tf.reshape(P_b, [1, NY, NX // 3, 1]), ksize=[1, NY, NX // 3, 1], strides=[1, 1, 1, 1], padding='SAME') + Pool = tf.nn.avg_pool(tf.reshape(P_r + P_g + P_b, [1, NY, NX // 3, 1]), ksize=[1, NY, NX // 3, 1], strides=[1, 1, 1, 1], padding='SAME') + Loss_red = tf.nn.l2_loss(Pool - Pool_red, 'Loss_red') + Loss_green = tf.nn.l2_loss(Pool - Pool_green, 'Loss_green') + Loss_blue = tf.nn.l2_loss(Pool - Pool_blue, 'Loss_blue') + Loss_Pool = tf.nn.l2_loss(Pool, 'Loss_Pool') + Relu = tf.nn.relu(Loss_red) + with tf.name_scope('train'): + print('train') + with tf.name_scope('init'): + print('tnit') + init = tf.global_variables_initializer() + with tf.name_scope('session'): + print('session') + sess = tf.Session() + sess.run(init) + i = 0 + with open('BdD', 'r') as f: + for line in f.readlines(): + i += 1 + if i % 3 == 1: + red_point, row = line.strip().split(':') + stat00, foo = row.split(';') + stat0 = [[(0) for x in range(NX // 3)] for y in range(NY)] + for y in range(NY): + for x in range(NX // 3): + value = stat00[y * 41 + (x + 1) * 2] + stat0[y][x] = int(value) + if i % 3 == 2: + green_point, row = line.strip().split(':') + stat01, foo = row.split(';') + stat1 = [[(0) for x in range(NX // 3)] for y in range(NY)] + for y in range(NY): + for x in range(NX // 3): + value = stat01[y * 41 + (x + 1) * 2] + stat1[y][x] = int(value) + if i % 3 == 0: + blue_point, row = line.strip().split(':') + stat02, foo = row.split(';') + stat2 = [[(0) for x in range(NX // 3)] for y in range(NY)] + for y in range(NY): + for x in range(NX // 3): + value = stat02[y * 41 + (x + 1) * 2] + stat2[y][x] = int(value) + p = sess.run([Relu], feed_dict={s0: stat0, _red_: [red_point], s1: stat1, _green_: [green_point], s2: stat2, _blue_: [blue_point]}) + print(p) + print('end') + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/269.txt b/dataset/preprocessed/269.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/269.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/27.txt b/dataset/preprocessed/27.txt new file mode 100644 index 0000000..7022fe1 --- /dev/null +++ b/dataset/preprocessed/27.txt @@ -0,0 +1,140 @@ +import os +import tensorflow as tf +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' +import random +import math +import csv +import ast +import numpy as np +import sys +import inspectcheckpoint + + +def init_weights(shape): + return tf.Variable(tf.random_normal(shape, stddev=0.01)) + + +def model1(X, w_h, w_h2, w_o): + h = tf.nn.tanh(tf.matmul(X, w_h)) + h2 = tf.nn.tanh(tf.matmul(h, w_h2)) + return tf.matmul(h2, w_o) + + +X = tf.placeholder('float', [None, 4]) +Y = tf.placeholder('float', [None, 3]) +w_h = init_weights([4, 60]) +w_h2 = init_weights([60, 60]) +w_o = init_weights([60, 3]) +p_keep_input = tf.placeholder('float') +p_keep_hidden = tf.placeholder('float') +py_x = model1(X, w_h, w_h2, w_o) +predict_op1 = tf.argmax(py_x, 1) +secondplayersess1 = tf.Session() +init = tf.global_variables_initializer() +saver = tf.train.Saver() +secondplayersess2 = tf.Session() +savefile2 = 'models/turnmodel1.ckpt' +saver.restore(secondplayersess2, savefile2) +savefile = 'models/rivermodel1.ckpt' +saver.restore(secondplayersess1, savefile) +player1sess1 = tf.Session() +player1sess2 = tf.Session() +player1sess3 = tf.Session() +savefile3 = 'models/flopmodel2.ckpt' +savefile4 = 'models/turnmodel2.ckpt' +savefile5 = 'models/rivermodel2.ckpt' +saver.restore(player1sess1, savefile3) +saver.restore(player1sess2, savefile4) +saver.restore(player1sess3, savefile5) +nn3sess1 = tf.Session() +nn3sess2 = tf.Session() +nn3sess3 = tf.Session() +nn3savefile1 = 'models/flopmodel3.ckpt' +nn3savefile2 = 'models/turnmodel3.ckpt' +nn3savefile3 = 'models/rivermodel3.ckpt' +saver.restore(nn3sess1, nn3savefile1) +saver.restore(nn3sess2, nn3savefile2) +saver.restore(nn3sess3, nn3savefile3) +nn3sess1.run(init) +tf.reset_default_graph() +nn4sess1 = tf.Session() +nn4sess2 = tf.Session() +nn4sess3 = tf.Session() +init = tf.global_variables_initializer() + + +def model2(X, w_h, w_h2, w_h3, w_o): + h = tf.nn.tanh(tf.matmul(X, w_h)) + h2 = tf.nn.tanh(tf.matmul(h, w_h2)) + h3 = tf.nn.tanh(tf.matmul(h2, w_h3)) + return tf.matmul(h3, w_o) + + +w_h2 = init_weights([4, 60]) +w_h22 = init_weights([60, 60]) +w_h32 = init_weights([60, 60]) +w_o2 = init_weights([60, 3]) +""" +p_keep_input = tf.placeholder("float") +p_keep_hidden = tf.placeholder("float") +""" +X2 = tf.placeholder('float', [None, 4]) +Y2 = tf.placeholder('float', [None, 3]) +py_x2 = model2(X2, w_h2, w_h22, w_h32, w_o2) +predict_op2 = tf.argmax(py_x2, 1) +saver2 = tf.train.Saver() +nn4savefile1 = 'models/flopmodel4.ckpt' +nn4savefile2 = 'models/turnmodel4.ckpt' +nn4savefile3 = 'models/rivermodel4.ckpt' +saver2.restore(nn4sess1, nn4savefile1) +saver2.restore(nn4sess2, nn4savefile2) +saver2.restore(nn4sess3, nn4savefile3) +nn6sess1 = tf.Session() +nn6sess2 = tf.Session() +nn6sess3 = tf.Session() +nn6savefile1 = 'models/flopmodel6.ckpt' +nn6savefile2 = 'models/turnmodel6.ckpt' +nn6savefile3 = 'models/rivermodel6.ckpt' +saver2.restore(nn6sess1, nn6savefile1) +saver2.restore(nn6sess2, nn6savefile2) +saver2.restore(nn6sess3, nn6savefile3) +nn7sess1 = tf.Session() +nn7sess2 = tf.Session() +nn7sess3 = tf.Session() +nn7savefile1 = 'models/flopmodel7.ckpt' +nn7savefile2 = 'models/turnmodel7.ckpt' +nn7savefile3 = 'models/rivermodel7.ckpt' +saver2.restore(nn7sess1, nn7savefile1) +saver2.restore(nn7sess2, nn7savefile2) +saver2.restore(nn7sess3, nn7savefile3) +nn4sess2.run(init) +tf.reset_default_graph() + + +def model3(X, w_h, w_h2, w_h3, w_o): + h = tf.nn.tanh(tf.matmul(X, w_h)) + h2 = tf.nn.tanh(tf.matmul(h, w_h2)) + h3 = tf.nn.tanh(tf.matmul(h2, w_h3)) + return tf.matmul(h3, w_o) + + +X3 = tf.placeholder('float', [None, 5]) +Y3 = tf.placeholder('float', [None, 3]) +w_h3 = init_weights([5, 60]) +w_h23 = init_weights([60, 60]) +w_h33 = init_weights([60, 60]) +w_o3 = init_weights([60, 3]) +p_keep_input = tf.placeholder('float') +p_keep_hidden = tf.placeholder('float') +py_x = model3(X3, w_h3, w_h23, w_h33, w_o3) +predict_op3 = tf.argmax(py_x, 1) +nn5sess1 = tf.Session() +nn5sess2 = tf.Session() +nn5sess3 = tf.Session() +saver3 = tf.train.Saver() +nn5savefile1 = 'models/flopmodel5.ckpt' +nn5savefile2 = 'models/turnmodel5.ckpt' +nn5savefile3 = 'models/rivermodel5.ckpt' +saver3.restore(nn5sess1, nn5savefile1) +saver3.restore(nn5sess2, nn5savefile2) +saver3.restore(nn5sess3, nn5savefile3) diff --git a/dataset/preprocessed/270.txt b/dataset/preprocessed/270.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/270.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/271.txt b/dataset/preprocessed/271.txt new file mode 100644 index 0000000..fa39a88 --- /dev/null +++ b/dataset/preprocessed/271.txt @@ -0,0 +1,95 @@ +import numpy as np +import tensorflow as tf +import tflearn +DEFAULT_PADDING = 'valid' +DEFAULT_ACTIVATION = 'relu' +DEFAULT_STRIDES = [1, 1] +DEFAULT_WEIGHT_DECAY = 0.0 + + +class CNNEnsemble: + + def __init__(self, session, assets, window_size, config): + self.layers = [] + self.session = session + self.config = config + self.global_step = tf.Variable(0, trainable=False) + self.input_prices = tf.placeholder(tf.float32, shape=[None, 3, assets, window_size]) + self.input_prev_omega = tf.placeholder(tf.float32, shape=[None, assets]) + self.input_future_prices = tf.placeholder(tf.float32, shape=[None, assets]) + self.out_nn = self._build_output_network() + self.train_op = self._build_train_operation() + self._saver = tf.train.Saver(tf.trainable_variables()) + + def _build_output_network(self): + layers_cnf = self.config['layers'] + nn = tf.transpose(self.input_prices, [0, 2, 3, 1]) + for lc in layers_cnf[:-1]: + nn = self._make_layer(nn, lc) + self.layers.append(nn) + nn = self._make_output_layer(nn, layers_cnf[-1]) + self.layers.append(nn) + return nn + + @staticmethod + def _make_layer(prev, config): + return tflearn.layers.conv_2d(prev, config['out_channels'], config['kernel'], config.get('strides', DEFAULT_STRIDES), config.get('padding', DEFAULT_PADDING), config.get('activation', DEFAULT_ACTIVATION), regularizer=config.get('regularizer', None), weight_decay=config.get('weight_decay', DEFAULT_WEIGHT_DECAY)) + + def _make_output_layer(self, prev, config): + prev = self._add_previous_omega(prev) + prev = self._make_layer(prev, config) + prev = prev[:, :, (0), (0)] + prev = self._attach_cash_bias(prev) + return tflearn.layers.activation(prev, activation='softmax') + + def _add_previous_omega(self, nn): + shape = nn.shape + nn = tf.reshape(nn, [-1, shape[1], 1, shape[2] * shape[3]]) + w = tf.reshape(self.input_prev_omega, [-1, shape[1], 1, 1]) + nn = tf.concat([nn, w], axis=3) + return nn + + @staticmethod + def _attach_cash_bias(nn): + cash_bias = tf.get_variable('cash_bias', [1, 1], dtype=tf.float32, initializer=tf.zeros_initializer) + cash_bias = tf.tile(cash_bias, [tf.shape(nn)[0], 1]) + nn = tf.concat([cash_bias, nn], axis=1) + return nn + + def _build_train_operation(self): + future_prices = self._add_cash(self.input_future_prices) + mu = self._calc_commission(future_prices, self.out_nn) + portfolio_values = tf.reduce_sum(future_prices * self.out_nn, reduction_indices=[1]) * tf.concat([tf.ones(1), mu], axis=0) + loss = -tf.reduce_mean(tf.log(portfolio_values)) + train_cnf = self.config['training'] + lr = tf.train.exponential_decay(train_cnf['learning_rate'], self.global_step, train_cnf['decay_steps'], train_cnf['decay_rate'], staircase=True) + return tf.train.AdadeltaOptimizer(lr).minimize(loss, global_step=self.global_step) + + @staticmethod + def _add_cash(relative_prices): + return tf.concat([tf.ones([tf.shape(relative_prices)[0], 1]), relative_prices], axis=1) + + def _calc_commission(self, future_prices, omega): + future_omega = self._calc_future_omega(future_prices, omega) + w_prime = future_omega[:-1] + w = omega[1:] + return 1 - tf.reduce_sum(tf.abs(w_prime[:, 1:] - w[:, 1:]), axis=1) * self.config['trading']['commission'] + + @staticmethod + def _calc_future_omega(future_prices, omega): + return future_prices * omega / tf.reduce_sum(future_prices * omega, axis=1)[:, (None)] + + def predict(self, prices, previous_omega): + res = self.session.run(self.out_nn, feed_dict={self.input_prices: np.expand_dims(prices, axis=0), self.input_prev_omega: np.expand_dims(previous_omega, axis=0)}) + return res[0] + + def train(self, states, future_prices): + prices, omegas = states + result = self.session.run([self.train_op, self.out_nn], feed_dict={self.input_prices: prices, self.input_prev_omega: omegas, self.input_future_prices: future_prices}) + return result[-1] + + def save(self, path): + self._saver.save(self.session, path) + + def restore(self, path): + self._saver.restore(self.session, path) diff --git a/dataset/preprocessed/272.txt b/dataset/preprocessed/272.txt new file mode 100644 index 0000000..d49337f --- /dev/null +++ b/dataset/preprocessed/272.txt @@ -0,0 +1,126 @@ +import tensorflow as tf +""" +matrix1 = tf.constant([[3., 3.]]) +matrix2 = tf.constant([[2.], [2.]]) +product = tf.matmul(matrix1, matrix2) +sess = tf.Session() +with tf.Session() as sess: + result = sess.run(product) + print(result) +sess.close() +""" +""" +sess = tf.InteractiveSession() +x = tf.Variable([1.0, 2.0]) +a = tf.constant([3.0, 3.0]) +x.initializer.run() +sub = tf.sub(x, a) +print(sub.eval()) +# sub not found +""" +""" +state = tf.Variable(0, name="counter") +one = tf.constant(1) +new_value = tf.add(state, one) +update = tf.assign(state, new_value) +init_op = tf.initialize_all_variables() +with tf.Session() as sess: + sess.run(init_op) + print(sess.run(state)) + for _ in range(3): + sess.run(update) + print(sess.run(state)) +""" +""" +input1 = tf.constant(1) +input2 = tf.constant(2) +input3 = tf.constant(2) + +a = tf.add(input3, input2) +b = tf.matmul(input1, a) + +with tf.Session() as sess: + result = sess.run([a, b]) + print(result) +# ValueError: Shape must be rank 2 but is rank 0 for 'MatMul' (op: 'MatMul') with input shapes: [], []. +""" +""" +input1 = tf.placeholder(tf.float32) +input2 = tf.placeholder(tf.float32) +output = tf.mul(input1, input2) + +with tf.Session() as sess: + print(sess.run([output], feed_dict={input1:[7.], input2:[2.]})) +# AttributeError: module 'tensorflow' has no attribute 'mul' +# 输出: +# [array([ 14.], dtype=float32)] +""" +import tensorflow.examples.tutorials.mnist.input_data as input_data +mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) +x = tf.placeholder(tf.float32, [None, 784]) +W = tf.Variable(tf.zeros([784, 10])) +b = tf.Variable(tf.zeros([10])) +y = tf.nn.softmax(tf.matmul(x, W) + b) +y_ = tf.placeholder('float', [None, 10]) +cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) +train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) +init = tf.initialize_all_variables() +sess = tf.Session() +sess.run(init) +for i in range(1000): + batch_xs, batch_ys = mnist.train.next_batch(100) + sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) +correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) +print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) + + +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) + + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder('float') +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) +cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) +train_step = tf.train.AdamOptimizer(0.0001).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')) +with sess.as_default(): + sess.run(tf.initialize_all_variables()) + for i in range(20000): + batch = mnist.train.next_batch(50) + if i % 100 == 0: + train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) + print('step %d, training accuracy %g' % (i, train_accuracy)) + train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) + print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) diff --git a/dataset/preprocessed/273.txt b/dataset/preprocessed/273.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/273.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/274.txt b/dataset/preprocessed/274.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/274.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/275.txt b/dataset/preprocessed/275.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/275.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/276.txt b/dataset/preprocessed/276.txt new file mode 100644 index 0000000..e8e0d71 --- /dev/null +++ b/dataset/preprocessed/276.txt @@ -0,0 +1,84 @@ +import tensorflow as tf +import random +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +from tensorflow.examples.tutorials.mnist import input_data +batch_size = 100 +mnist_idx = 20 + + +def forth_test(): + mnist = input_data.read_data_sets('MNIST_data', one_hot=True) + digit = mnist.train.images[mnist_idx] + digitReshape = np.reshape(digit, [1, 28, 28, 1]) + x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + L1 = tf.nn.max_pool(digitReshape, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + init = tf.initialize_all_variables() + with tf.Session() as session: + session.run(init) + max_op = session.run(L1, feed_dict={x: digitReshape}) + plt.imshow(max_op[(0), :, :, (0)], cmap='Greys') + plt.show() + + +def third_test(): + mnist = input_data.read_data_sets('MNIST_data', one_hot=True) + digit = mnist.train.images[mnist_idx] + digitReshape = np.reshape(digit, [1, 28, 28, 1]) + x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01)) + L1 = tf.nn.conv2d(digitReshape, W1, strides=[1, 1, 1, 1], padding='SAME') + L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01)) + L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') + L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + W3 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01)) + L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME') + L3 = tf.nn.max_pool(L3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + init = tf.initialize_all_variables() + with tf.Session() as session: + session.run(init) + conv_op = session.run(L3, feed_dict={x: digitReshape}) + for i in range(1, conv_op.shape[3]): + aa = conv_op[(0), :, :, (i)] + plt.imshow(aa, cmap='Greys') + plt.show() + + +def second_test(): + mnist = input_data.read_data_sets('MNIST_data', one_hot=True) + digit = mnist.train.images[mnist_idx] + digitReshape = np.reshape(digit, [1, 28, 28, 1]) + x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01)) + conv = tf.nn.conv2d(digitReshape, W1, strides=[1, 1, 1, 1], padding='SAME') + init = tf.initialize_all_variables() + with tf.Session() as session: + session.run(init) + conv_op = session.run(conv, feed_dict={x: digitReshape}) + for i in range(1, conv_op.shape[3]): + aa = conv_op[(0), :, :, (i)] + plt.imshow(aa, cmap='Greys') + plt.show() + + +def first_test(): + mnist = input_data.read_data_sets('MNIST_data', one_hot=True) + digit = mnist.train.images[mnist_idx] + digitReshape = np.reshape(digit, [1, 28, 28, 1]) + x = tf.placeholder(tf.float32, [1, 28, 28, 1]) + kernel = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]).astype(np.float32) + kernelReshape = np.reshape(kernel, [3, 3, 1, 1]) + conv = tf.nn.conv2d(digitReshape, kernelReshape, strides=[1, 1, 1, 1], padding='SAME') + init = tf.initialize_all_variables() + with tf.Session() as session: + session.run(init) + conv_op = session.run(conv, feed_dict={x: digitReshape}) + im = np.reshape(conv_op, [28, 28]) + plt.imshow(im, cmap='Greys') + plt.show() + + +if __name__ == '__main__': + third_test() diff --git a/dataset/preprocessed/277.txt b/dataset/preprocessed/277.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/277.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/278.txt b/dataset/preprocessed/278.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/278.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/279.txt b/dataset/preprocessed/279.txt new file mode 100644 index 0000000..11b5aba --- /dev/null +++ b/dataset/preprocessed/279.txt @@ -0,0 +1,449 @@ +from __future__ import print_function +import tensorflow as tf +a = tf.placeholder('float') +b = tf.placeholder('float') +y = tf.multiply(a, b) + + +def multiplication_basics(): + with tf.Session() as sess: + print('%f should equal 2.0' % sess.run(y, feed_dict={a: 1, b: 2})) + print('%f should equal 9.0' % sess.run(y, feed_dict={a: 3, b: 3})) + + +def hello_world(): + hello = tf.constant('Hello,Tensorflow!') + sess = tf.Session() + print(sess.run(hello)) + + +def matrix_multiplication(): + matrix1 = tf.constant([[3.0, 3.0]]) + matrix2 = tf.constant([[2.0], [2.0]]) + product = tf.matmul(matrix1, matrix2) + with tf.Session() as sess: + result = sess.run(product) + print(result) + + +def nearest_neighbor(): + import numpy as np + from tensorflow.examples.tutorials.mnist import input_data + mnist = input_data.read_data_sets('tmp/data', one_hot=True) + Xtr, Ytr = mnist.train.next_batch(5000) + Xte, Yte = mnist.test.next_batch(200) + xtr = tf.placeholder('float', [None, 784]) + xte = tf.placeholder('float', [784]) + distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1) + pred = tf.arg_min(distance, 0) + accuracy = 0.0 + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + for i in range(len(Xte)): + nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[(i), :]}) + print('Test', i, 'Prediction:', np.argmax(Ytr[nn_index]), 'True Class:', np.argmax(Yte[i])) + if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]): + accuracy += 1.0 / len(Xte) + print('Done!') + print('Accuracy:', accuracy) + + +def linear_regression(): + import numpy + import matplotlib.pyplot as plt + rng = numpy.random + learning_rate = 0.01 + training_epochs = 1000 + display_step = 50 + train_X = numpy.asarray([3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1]) + train_Y = numpy.asarray([1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3]) + n_samples = train_X.shape[0] + X = tf.placeholder('float') + Y = tf.placeholder('float') + W = tf.Variable(rng.randn(), name='weight') + b = tf.Variable(rng.randn(), name='bias') + pred = tf.add(tf.multiply(X, W), b) + cost = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + for epoch in range(training_epochs): + for x, y in zip(train_X, train_Y): + sess.run(optimizer, feed_dict={X: x, Y: y}) + if (epoch + 1) % display_step == 0: + c = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) + print('Epoch:', '%04d' % (epoch + 1), 'cost=', '{:.9f}'.format(c), 'W=', sess.run(W), 'b=', sess.run(b)) + print('Optimization finished.') + training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) + print('Training cost=', training_cost, 'W=', sess.run(W), 'b=', sess.run(b), '\n') + plt.plot(train_X, train_Y, 'ro', label='Original data') + plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line') + plt.legend() + plt.show() + test_X = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1]) + test_Y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03]) + print('Testing Mean Square Loss Comparison') + testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * test_X.shape[0]), feed_dict={X: test_X, Y: test_Y}) + print('Testing cost=', testing_cost) + print('Absolute mean square loss difference:', abs(training_cost - testing_cost)) + plt.plot(test_X, test_Y, 'bo', label='Testing data') + plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line') + plt.legend() + plt.show() + + +def logistic_regression(): + from tensorflow.examples.tutorials.mnist import input_data + mnist = input_data.read_data_sets('/tmp/data/', one_hot=True) + learning_rate = 0.01 + training_epochs = 25 + batch_size = 100 + display_step = 1 + x = tf.placeholder(tf.float32, [None, 784]) + y = tf.placeholder(tf.float32, [None, 10]) + W = tf.Variable(tf.zeros([784, 10])) + b = tf.Variable(tf.zeros([10])) + pred = tf.nn.softmax(tf.matmul(x, W) + b) + cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1)) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + for epoch in range(training_epochs): + avg_cost = 0.0 + total_batch = int(mnist.train.num_examples / batch_size) + for i in range(total_batch): + batch_xs, batch_ys = mnist.train.next_batch(batch_size) + _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys}) + avg_cost += c / total_batch + if (epoch + 1) % display_step == 0: + print('Epoch:', '%04d' % (epoch + 1), 'cost=', '{:.9f}'.format(avg_cost)) + print('Optimization Finished.') + correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + print('Accuracy:', accuracy.eval({x: mnist.test.images, y: mnist.test.labels})) + + +def multilayer_perceptron(): + from tensorflow.examples.tutorials.mnist import input_data + mnist = input_data.read_data_sets('/tmp/data/', one_hot=True) + learning_rate = 0.001 + training_epochs = 15 + batch_size = 100 + display_step = 1 + n_hidden_1 = 256 + n_hidden_2 = 256 + n_input = 784 + n_classes = 10 + x = tf.placeholder('float', [None, n_input]) + y = tf.placeholder('float', [None, n_classes]) + + def perceptron(x, weights, biases): + layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + layer_1 = tf.nn.relu(layer_1) + layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) + layer_2 = tf.nn.relu(layer_2) + out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + return out_layer + weights = {'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))} + biases = {'b1': tf.Variable(tf.random_normal([n_hidden_1])), 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 'out': tf.Variable(tf.random_normal([n_classes]))} + pred = perceptron(x, weights, biases) + cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) + optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + for epoch in range(training_epochs): + avg_cost = 0.0 + total_batch = int(mnist.train.num_examples / batch_size) + for i in range(total_batch): + batch_x, batch_y = mnist.train.next_batch(batch_size) + _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y}) + avg_cost += c / total_batch + if epoch % display_step == 0: + print('Epoch:', '%04d' % (epoch + 1), 'cost=', '{:.9f}'.format(avg_cost)) + print('Optimization Finished.') + correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + print('Accuracy:', accuracy.eval({x: mnist.test.images, y: mnist.test.labels})) + + +def convolutional_network(): + from tensorflow.examples.tutorials.mnist import input_data + mnist = input_data.read_data_sets('tmp/data/', one_hot=True) + learning_rate = 0.001 + training_iters = 200000 + batch_size = 128 + display_step = 10 + n_input = 784 + n_classes = 10 + dropout = 0.75 + x = tf.placeholder(tf.float32, [None, n_input]) + y = tf.placeholder(tf.float32, [None, n_classes]) + keep_prob = tf.placeholder(tf.float32) + + def conv2d(x, W, b, strides=1): + x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') + x = tf.nn.bias_add(x, b) + return tf.nn.relu(x) + + def maxpool2d(x, k=2): + return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME') + + def conv_net(x, weights, biases, dropout): + x = tf.reshape(x, shape=[-1, 28, 28, 1]) + conv1 = conv2d(x, weights['wc1'], biases['bc1']) + conv1 = maxpool2d(conv1, k=2) + conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) + conv2 = maxpool2d(conv2, k=2) + fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) + fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) + fc1 = tf.nn.relu(fc1) + fc1 = tf.nn.dropout(fc1, dropout) + out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) + return out + weights = {'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 'wd1': tf.Variable(tf.random_normal([7 * 7 * 64, 1024])), 'out': tf.Variable(tf.random_normal([1024, n_classes]))} + biases = {'bc1': tf.Variable(tf.random_normal([32])), 'bc2': tf.Variable(tf.random_normal([64])), 'bd1': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_classes]))} + pred = conv_net(x, weights, biases, keep_prob) + cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) + optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) + correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + step = 1 + while step * batch_size < training_iters: + batch_x, batch_y = mnist.train.next_batch(batch_size) + sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout}) + if step % display_step == 0: + loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x, y: batch_y, keep_prob: 0.1}) + print('Iter ' + str(step * batch_size) + ', Minibatch Loss= ' + '{:.6f}'.format(loss) + ', Training Accuracy=' + '{:.5f}'.format(acc)) + step += 1 + print('Optimization Finished') + print('Testing Accuracy', sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.0})) + + +def second_convnet(): + import random + import numpy as np + import matplotlib.pyplot as plt + import datetime + from tensorflow.examples.tutorials.mnist import input_data + mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) + tf.reset_default_graph() + sess = tf.InteractiveSession() + x = tf.placeholder('float', shape=[None, 28, 28, 1]) + y_ = tf.placeholder('float', shape=[None, 10]) + W_conv1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1)) + b_conv1 = tf.Variable(tf.constant(0.1, shape=[32])) + print(x) + print(W_conv1) + h_conv1 = tf.nn.conv2d(input=x, filter=W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1 + h_conv1 = tf.nn.relu(h_conv1) + h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + def conv2d(x, W): + return tf.nn.conv2d(input=x, filter=W, strides=[1, 1, 1, 1], padding='SAME') + + def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + W_conv2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1)) + b_conv2 = tf.Variable(tf.constant(0.1, shape=[64])) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = max_pool_2x2(h_conv2) + W_fcl = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1)) + b_fcl = tf.Variable(tf.constant(0.1, shape=[1024])) + h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + h_fcl = tf.nn.relu(tf.matmul(h_pool2_flat, W_fcl) + b_fcl) + keep_prob = tf.placeholder('float') + h_fcl_drop = tf.nn.dropout(h_fcl, keep_prob) + W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1)) + b_fc2 = tf.Variable(tf.constant(0.1, shape=[10])) + y = tf.matmul(h_fcl_drop, W_fc2) + b_fc2 + crossEntropyLoss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) + trainStep = tf.train.AdamOptimizer().minimize(crossEntropyLoss) + correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + sess.run(tf.global_variables_initializer()) + tf.summary.scalar('Cross Entropy Loss', crossEntropyLoss) + tf.summary.scalar('Accuracy', accuracy) + merged = tf.summary.merge_all() + logdir = 'tensorboard/' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S') + '/' + writer = tf.summary.FileWriter(logdir, sess.graph) + b = mnist.train.next_batch(1) + print(b[0].shape) + image = tf.reshape(b[0], [-1, 28, 28, 1]) + print(image) + my_img = image.eval() + my_i = my_img.squeeze() + plt.imshow(my_i, cmap='gray_r') + plt.show() + batchSize = 50 + for i in range(1000): + batch = mnist.train.next_batch(batchSize) + trainingInputs = batch[0].reshape([batchSize, 28, 28, 1]) + trainingLabels = batch[1] + if i % 10 == 0: + summary = sess.run(merged, {x: trainingInputs, y_: trainingLabels, keep_prob: 1.0}) + writer.add_summary(summary, i) + if i % 100 == 0: + trainAccuracy = accuracy.eval(session=sess, feed_dict={x: trainingInputs, y_: trainingLabels, keep_prob: 1.0}) + print('step %d, training accuracy %g' % (i, trainAccuracy)) + trainStep.run(session=sess, feed_dict={x: trainingInputs, y_: trainingLabels, keep_prob: 0.5}) + testInputs = mnist.test.images.reshape([-1, 28, 28, 1]) + testLabels = mnist.test.labels + acc = accuracy.eval(feed_dict={x: testInputs, y_: testLabels, keep_prob: 1.0}) + print('test accuracy: {}'.format(acc)) + + +def recurrent_neural_network_for_spam_detection(): + import os + import re + import io + import requests + import numpy as np + import matplotlib.pyplot as plt + from zipfile import ZipFile + from tensorflow.python.framework import ops + ops.reset_default_graph() + sess = tf.Session() + epochs = 20 + batch_size = 250 + max_sequence_length = 25 + rnn_size = 10 + embedding_size = 50 + min_word_frequency = 10 + learning_rate = 0.0005 + dropout_keep_prob = tf.placeholder(tf.float32) + data_dir = 'temp' + + +def gaussian_mixture_models(): + import numpy as np + import matplotlib.pyplot as plt + from scipy import stats + import seaborn as sns + sns.set() + + +def modern_multilayer_perceptron(): + import numpy as np + import tensorflow as tf + import matplotlib.pyplot as plt + from tensorflow.examples.tutorials.mnist import input_data + mnist = input_data.read_data_sets('data', one_hot=True) + X_train = mnist.train.images + Y_train = mnist.train.labels + X_test = mnist.test.images + Y_test = mnist.test.labels + dimX = X_train.shape[1] + dimY = Y_train.shape[1] + nTrain = X_train.shape[0] + nTest = X_test.shape[0] + print('Shape of (X_train, X_test, Y_train, Y_test)') + print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape) + + def xavier_init(n_inputs, n_outputs, uniform=True): + if uniform: + init_range = tf.sqrt(6.0 / (n_inputs + n_outputs)) + return tf.random_uniform_initializer(-init_range, init_range) + else: + stddev = tf.sqrt(3.0, n_inputs + n_outputs) + return tf.truncated_normal_initializer(stddev=stddev) + nLayer0 = dimX + nLayer1 = 256 + nLayer2 = 256 + nLayer3 = dimY + sigma_init = 0.1 + W = {'W1': tf.Variable(tf.random_normal([nLayer0, nLayer1], stddev=sigma_init)), 'W2': tf.Variable(tf.random_normal([nLayer1, nLayer2], stddev=sigma_init)), 'W3': tf.Variable(tf.random_normal([nLayer2, nLayer3], stddev=sigma_init))} + b = {'b1': tf.Variable(tf.random_normal([nLayer1])), 'b2': tf.Variable(tf.random_normal([nLayer2])), 'b3': tf.Variable(tf.random_normal([nLayer3]))} + + def model_myNN(_X, _W, _b): + Layer1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _W['W1']), _b['b1'])) + Layer2 = tf.nn.sigmoid(tf.add(tf.matmul(Layer1, _W['W2']), _b['b2'])) + Layer3 = tf.add(tf.matmul(Layer2, _W['W3']), _b['b3']) + return Layer3 + X = tf.placeholder(tf.float32, [None, dimX], name='input') + Y = tf.placeholder(tf.float32, [None, dimY], name='output') + Y_pred = model_myNN(X, W, b) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Y_pred, labels=Y)) + learning_rate = 0.001 + optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) + training_epochs = 30 + display_epoch = 5 + batch_size = 100 + correct_prediction = tf.equal(tf.argmax(Y_pred, 1), tf.argmax(Y, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + + +def chi_squared_feature_selection(): + import numpy as np + import pandas as pd + from sklearn.preprocessing import LabelBinarizer + from sklearn.feature_selection import chi2, SelectKBest + from sklearn.feature_extraction.text import CountVectorizer + X = np.array(['call you tonight', 'Call me a cab', 'please call me... please', 'he will call me']) + y = [1, 1, 2, 0] + vect = CountVectorizer() + X_dtm = vect.fit_transform(X) + X_dtm = X_dtm.toarray() + print(pd.DataFrame(X_dtm, columns=vect.get_feature_names())) + y_binarized = LabelBinarizer().fit_transform(y) + print(y_binarized) + print() + observed = np.dot(y_binarized.T, X_dtm) + print(observed) + class_prob = y_binarized.mean(axis=0).reshape(1, -1) + feature_count = X_dtm.sum(axis=0).reshape(1, -1) + expected = np.dot(class_prob.T, feature_count) + print(expected) + chisq = (observed - expected) ** 2 / expected + chisq_score = chisq.sum(axis=0) + print(chisq_score) + chi2score = chi2(X_dtm, y) + print(chi2score) + kbest = SelectKBest(score_func=chi2, k=4) + X_dtm_kbest = kbest.fit_transform(X_dtm, y) + print(X_dtm_kbest) + + +def genetic(): + import numpy as np + import pandas as pd + import matplotlib.pyplot as plt + chromo_size = 5 + low = 0 + high = 100 + lol = np.random.randint(low, high + 1, chromo_size) + print(lol) + pop_size = 6 + pop = np.random.randint(low, high + 1, (pop_size, chromo_size)) + print(pop) + target = 200 + cost = np.abs(np.sum(pop, axis=1) - target) + graded = [(c, list(p)) for p, c in zip(pop, cost)] + for cost, chromo in graded: + print("chromo {}'s cost is {}".format(chromo, cost)) + + +def main(): + genetic() + chi_squared_feature_selection() + multiplication_basics() + hello_world() + matrix_multiplication() + nearest_neighbor() + logistic_regression() + multilayer_perceptron() + linear_regression() + convolutional_network() + second_convnet() + recurrent_neural_network_for_spam_detection() + gaussian_mixture_models() + modern_multilayer_perceptron() + + +main() diff --git a/dataset/preprocessed/28.txt b/dataset/preprocessed/28.txt new file mode 100644 index 0000000..4c1ac45 --- /dev/null +++ b/dataset/preprocessed/28.txt @@ -0,0 +1,58 @@ +import tensorflow as tf + + +class CNN_YoonKim: + + def __init__(self, model_name=None, session=None, learning_rate=None, optimizer=None, learning_decay_rate=None, filter_sizes=None, num_filters=None, max_sentence_length=None, num_classes=None, embeddings=None, new_embeddings=None, embedding_dim=None, vocabulary_size=None, static=None, max_l2_norm=None, regularization_lambda=None, dropout_keep_prob=None): + if model_name == None: + return + self.model_name = model_name + self.session = session + self.learning_rate = learning_rate + self.optimizer = optimizer + self.dropout_keep_prob_train = dropout_keep_prob + self.regularization_lambda = regularization_lambda + self.input_x = tf.placeholder(shape=(None, max_sentence_length), dtype=tf.int32, name='input_x') + self.input_y = tf.placeholder(shape=(None, num_classes), dtype=tf.float32, name='input_y') + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, name='dropout_keep_prob') + self.embeddings_placeholder = tf.placeholder(tf.float32, shape=(vocabulary_size, embedding_dim)) + self.embeddings = tf.Variable(self.embeddings_placeholder, trainable=not static) + self.new_embeddings = tf.Variable(new_embeddings, trainable=True) + self.all_embeddings = tf.concat([self.embeddings, self.new_embeddings], axis=0) + self.embedded_words = tf.nn.embedding_lookup(self.all_embeddings, self.input_x) + self.input_x_expanded = tf.expand_dims(self.embedded_words, -1) + self.pool_results = [] + for i, filter_size in enumerate(filter_sizes): + filter = tf.get_variable('filter' + str(i), shape=(filter_size, embedding_dim, 1, num_filters), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + if max_l2_norm != 0: + filter = tf.clip_by_norm(filter, max_l2_norm) + bias = tf.Variable(tf.constant(0.0, shape=(num_filters,))) + conv = tf.nn.conv2d(input=self.input_x_expanded, filter=filter, strides=[1, 1, 1, 1], padding='VALID') + relu = tf.nn.relu(tf.nn.bias_add(conv, bias)) + conv_dim = max_sentence_length - filter_size + 1 + pooled = tf.nn.max_pool(relu, ksize=[1, conv_dim, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + self.pool_results.append(pooled) + num_filters_total = num_filters * len(filter_sizes) + self.flat = tf.reshape(tf.concat(self.pool_results, 3), [-1, num_filters_total]) + self.dropout = tf.nn.dropout(self.flat, self.dropout_keep_prob) + W = tf.get_variable('W', shape=(num_filters_total, num_classes), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + b = tf.Variable(tf.constant(0.1, shape=(num_classes,))) + self.output = tf.nn.xw_plus_b(self.dropout, W, b, name='output') + self.predictions = tf.argmax(self.output, 1, name='predictions') + losses = tf.nn.softmax_cross_entropy_with_logits(labels=self.input_y, logits=self.output) + if regularization_lambda != 0: + l2_loss = tf.nn.l2_loss(W) + self.loss = tf.reduce_mean(tf.add(losses, tf.multiply(self.regularization_lambda, l2_loss)), name='loss') + else: + self.loss = tf.reduce_mean(losses, name='loss') + self.optimizer = optimizer(learning_rate=self.learning_rate) + self.train_op = self.optimizer.minimize(self.loss) + self.saver = tf.train.Saver() + self.session.run(tf.global_variables_initializer(), feed_dict={self.embeddings_placeholder: embeddings}) + + def train_step(self, input_x, input_y): + _, loss = self.session.run([self.train_op, self.loss], feed_dict={self.input_x: input_x, self.input_y: input_y, self.dropout_keep_prob: self.dropout_keep_prob_train}) + return loss + + def feed(self, input_x): + return self.session.run([self.output, self.predictions], feed_dict={self.input_x: input_x, self.dropout_keep_prob: 1}) diff --git a/dataset/preprocessed/280.txt b/dataset/preprocessed/280.txt new file mode 100644 index 0000000..429b473 --- /dev/null +++ b/dataset/preprocessed/280.txt @@ -0,0 +1,491 @@ +from unittest import TestCase +import pkg_resources +import logging, warnings +import tensorflow as tf +import numpy as np +from deepexplain.tensorflow import DeepExplain +from deepexplain.tensorflow.methods import original_grad +activations = {'Relu': tf.nn.relu, 'Sigmoid': tf.nn.sigmoid, 'Softplus': tf.nn.softplus, 'Tanh': tf.nn.tanh} + + +def simple_model(activation, session): + X = tf.placeholder('float', [None, 2]) + w1 = tf.Variable(initial_value=[[1.0, -1.0], [-1.0, 1.0]]) + b1 = tf.Variable(initial_value=[1.5, -1.0]) + w2 = tf.Variable(initial_value=[[1.1, 1.4], [-0.5, 1.0]]) + b2 = tf.Variable(initial_value=[0.0, 2.0]) + layer1 = activation(tf.matmul(X, w1) + b1) + out = tf.matmul(layer1, w2) + b2 + session.run(tf.global_variables_initializer()) + return X, out + + +def simpler_model(session): + """ + Implements ReLU( ReLU(x1 - 1) - ReLU(x2) ) + : + """ + X = tf.placeholder('float', [None, 2]) + w1 = tf.Variable(initial_value=[[1.0, 0.0], [0.0, 1.0]], trainable=False) + b1 = tf.Variable(initial_value=[-1.0, 0], trainable=False) + w2 = tf.Variable(initial_value=[[1.0], [-1.0]], trainable=False) + l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + out = tf.nn.relu(tf.matmul(l1, w2)) + session.run(tf.global_variables_initializer()) + return X, out + + +def simple_multi_inputs_model(session): + """ + Implements Relu (3*x1|2*x2) | is a concat op + : + """ + X1 = tf.placeholder('float', [None, 2]) + X2 = tf.placeholder('float', [None, 2]) + w1 = tf.Variable(initial_value=[[3.0, 0.0], [0.0, 3.0]], trainable=False) + w2 = tf.Variable(initial_value=[[2.0, 0.0], [0.0, 2.0]], trainable=False) + out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + session.run(tf.global_variables_initializer()) + return X1, X2, out + + +def simple_multi_inputs_model2(session): + """ + Implements Relu (3*x1|2*x2) | is a concat op + : + """ + X1 = tf.placeholder('float', [None, 2]) + X2 = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[[3.0, 0.0], [0.0, 3.0]], trainable=False) + w2 = tf.Variable(initial_value=[[2.0], [2.0]], trainable=False) + out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + session.run(tf.global_variables_initializer()) + return X1, X2, out + + +def train_xor(session): + X = tf.placeholder('float', [None, 2]) + Y = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[[0.10711301, -0.0987727], [-1.57625198, 1.34942603]]) + b1 = tf.Variable(initial_value=[-0.30955192, -0.14483099]) + w2 = tf.Variable(initial_value=[[0.69259691], [-0.16255915]]) + b2 = tf.Variable(initial_value=[1.53952825]) + l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + out = tf.matmul(l1, w2) + b2 + session.run(tf.global_variables_initializer()) + loss = tf.reduce_mean(tf.losses.mean_squared_error(Y, out)) + train_step = tf.train.GradientDescentOptimizer(0.05).minimize(loss) + np.random.seed(10) + x = np.random.randint(0, 2, size=(10, 2)) + y = np.expand_dims(np.logical_or(x[:, (0)], x[:, (1)]), -1) + l = None + for _ in range(100): + l, _ = session.run([loss, train_step], feed_dict={X: x, Y: y}) + return np.abs(l - 0.1) < 0.01 + + +class TestDeepExplainGeneralTF(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_tf_available(self): + try: + pkg_resources.require('tensorflow>=1.0') + except Exception: + self.fail('Tensorflow requirement not met') + + def test_simple_model(self): + X, out = simple_model(tf.nn.relu, self.session) + xi = np.array([[1, 0]]) + r = self.session.run(out, {X: xi}) + self.assertEqual(r.shape, xi.shape) + np.testing.assert_equal(r[0], [2.75, 5.5]) + + def test_simpler_model(self): + X, out = simpler_model(self.session) + xi = np.array([[3.0, 1.0]]) + r = self.session.run(out, {X: xi}) + self.assertEqual(r.shape, (xi.shape[0], 1)) + np.testing.assert_equal(r[0], [1.0]) + + def test_training(self): + session = tf.Session() + r = train_xor(session) + self.assertTrue(r) + + def test_context(self): + """ + DeepExplain overrides nonlinearity gradient + """ + from deepexplain.tensorflow import DeepExplain + X = tf.placeholder('float', [None, 1]) + for name in activations: + x1 = activations[name](X) + x1_g = tf.gradients(x1, X)[0] + self.assertEqual(x1_g.op.type, '%sGrad' % name) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + for name in activations: + x2 = activations[name](X) + self.assertEqual(x2.op.get_attr('_gradient_op_type').decode('utf-8'), 'DeepExplainGrad') + + def test_mismatch_input_lens(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1 = tf.placeholder('float', [None, 1]) + X2 = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[[0.10711301]]) + w2 = tf.Variable(initial_value=[[0.69259691]]) + out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + self.session.run(tf.global_variables_initializer()) + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', out, [X1, X2], [[1], [2], [3]]) + self.assertIn('List of input tensors and input data have different lengths', str(cm.exception)) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1 = tf.placeholder('float', [None, 1]) + X2 = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[[10.0]]) + w2 = tf.Variable(initial_value=[[10.0]]) + out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + self.session.run(tf.global_variables_initializer()) + attributions = de.explain('grad*input', out, [X1, X2], [[[2]], [[3]]]) + self.assertEqual(len(attributions), 2) + self.assertEqual(attributions[0][0], 20.0) + self.assertEqual(attributions[1][0], 30.0) + + def test_supported_activations(self): + X = tf.placeholder('float', [None, 3]) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + xi = [[-1, 0, 1]] + Y = tf.nn.relu(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [0, 0, 1], 7) + Y = tf.nn.elu(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [-0.632120558, 0, 1], 7) + Y = tf.nn.sigmoid(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [0.268941421, 0.5, 0.731058578], 7) + Y = tf.nn.tanh(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [-0.761594155, 0, 0.761594155], 7) + Y = tf.nn.softplus(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [0.313261687, 0.693147181, 1.31326168], 7) + + def test_original_grad(self): + X = tf.placeholder('float', [None, 3]) + for name in activations: + Y = activations[name](X) + grad = original_grad(Y.op, tf.ones_like(X)) + self.assertTrue('Tensor' in str(type(grad))) + + def test_warning_unsupported_activations(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X = tf.placeholder('float', [None, 3]) + Y = tf.nn.relu6(X) + xi = [[-1, 0, 1]] + de.explain('elrp', Y, X, xi) + assert any([('unsupported activation' in str(wi.message)) for wi in w]) + + def test_override_as_default(self): + """ + In DeepExplain context, nonlinearities behave as default, including training time + """ + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + r = train_xor(self.session) + self.assertTrue(r) + + def test_explain_not_in_context(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + pass + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', None, None, None) + self.assertEqual('Explain can be called only within a DeepExplain context.', str(cm.exception)) + + def test_invalid_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with self.assertRaises(RuntimeError) as cm: + de.explain('invalid', None, None, None) + self.assertIn('Method must be in', str(cm.exception)) + + def test_gradient_was_not_overridden(self): + X = tf.placeholder('float', [None, 3]) + Y = tf.nn.relu(X) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + de.explain('grad*input', Y, X, [[0, 0, 0]]) + assert any([('DeepExplain detected you are trying' in str(wi.message)) for wi in w]) + + +class TestDummyMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_dummy_zero(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.sigmoid, self.session) + xi = np.array([[10, -10]]) + attributions = de.explain('zero', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions[0], [0.0, 0.0], 10) + + def test_gradient_restored(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.sigmoid, self.session) + xi = np.array([[10, -10]]) + de.explain('zero', out, X, xi) + r = train_xor(self.session) + self.assertTrue(r) + + +class TestSaliencyMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_saliency_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('saliency', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, 1.0]], 10) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('saliency', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[2.0, 2.0]], 10) + + +class TestGradInputMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_saliency_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('grad*input', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 10) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('grad*input', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + +class TestIntegratedGradientsMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_int_grad(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('intgrad', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 1) + + def test_int_grad_higher_precision(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + print('X') + print(X) + attributions = de.explain('intgrad', out, X, xi, steps=500) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 2) + + def test_int_grad_baseline(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[3, 1]]) + attributions = de.explain('intgrad', out, X, xi, baseline=xi[0]) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0]], 5) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('intgrad', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + def test_multiple_inputs_different_sizes(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model2(self.session) + xi = [np.array([[-10, -5]]), np.array([[3]])] + attributions = de.explain('intgrad', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[12]], 10) + + +class TestEpsilonLRPMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_elrp_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('elrp', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 3) + + def test_elrp_epsilon(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('elrp', out, X, xi, epsilon=1e-09) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 7) + + def test_elrp_zero_epsilon(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + with self.assertRaises(AssertionError): + de.explain('elrp', out, X, xi, epsilon=0) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('elrp', out, [X1, X2], xi, epsilon=1e-09) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + +class TestDeepLIFTMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_deeplift(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('deeplift', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [2.0, -1.0]], 10) + + def test_deeplift_baseline(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[3, 1]]) + attributions = de.explain('deeplift', out, X, xi, baseline=xi[0]) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0]], 5) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('deeplift', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + def test_multiple_inputs_different_sizes(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model2(self.session) + xi = [np.array([[-10, -5]]), np.array([[3]])] + attributions = de.explain('deeplift', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[12]], 10) + + +class TestOcclusionMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_occlusion(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('occlusion', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, -1.0]], 10) + + def test_window_shape(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('occlusion', out, X, xi, window_shape=(2,)) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [0.5, 0.5]], 10) + + def test_nan_warning(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('occlusion', out, X, xi, step=2) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, np.nan], [1.0, np.nan]], 10) + assert any([('nans' in str(wi.message)) for wi in w]) + + def test_multiple_inputs_error(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + with self.assertRaises(RuntimeError) as cm: + de.explain('occlusion', out, [X1, X2], xi) + self.assertIn('not yet supported', str(cm.exception)) diff --git a/dataset/preprocessed/281.txt b/dataset/preprocessed/281.txt new file mode 100644 index 0000000..6dd7a2c --- /dev/null +++ b/dataset/preprocessed/281.txt @@ -0,0 +1,229 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = '../Assignment 1/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2): + h = tf.nn.relu(tf.matmul(tf_train_dataset, w_h1) + b1) + return tf.matmul(h, w_h2) + b2 + + +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + b1 = tf.Variable(tf.zeros([hidden_units])) + w_h2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + net = model(tf_train_dataset, w_h1, b1, w_h2, b2) + l2_loss = 0.0001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2)) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=net)) + l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(net) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w_h1) + b1), w_h2) + b2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w_h1) + b1), w_h2) + b2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2): + h = tf.nn.relu(tf.matmul(tf_train_dataset, w_h1) + b1) + return tf.matmul(h, w_h2) + b2 + + +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + b1 = tf.Variable(tf.zeros([hidden_units])) + w_h2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + net = model(tf_train_dataset, w_h1, b1, w_h2, b2) + l2_loss = 0.0001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2)) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=net)) + l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(net) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w_h1) + b1), w_h2) + b2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w_h1) + b1), w_h2) + b2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (200 - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2, p_keep_input, p_keep_hidden): + X = tf.nn.dropout(X, p_keep_input) + h = tf.nn.relu(tf.matmul(tf_train_dataset, w_h1) + b1) + return tf.matmul(h, w_h2) + b2 + + +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + b1 = tf.Variable(tf.zeros([hidden_units])) + w_h2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + p_keep_input = tf.placeholder('float') + p_keep_hidden = tf.placeholder('float') + net = model(tf_train_dataset, w_h1, b1, w_h2, b2, p_keep_input, p_keep_hidden) + l2_loss = 0.0001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2)) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=net)) + l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(net) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w_h1) + b1), w_h2) + b2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w_h1) + b1), w_h2) + b2) +num_steps = 5001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, p_keep_input: 0.5, p_keep_hidden: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 256 +hidden_units = 512 +graph = tf.Graph() + + +def model(X, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o, p_keep_input, p_keep_hidden): + X = tf.nn.dropout(X, p_keep_input) + h1 = tf.nn.relu(tf.matmul(X, w_h1) + b1) + h1 = tf.nn.dropout(h1, p_keep_hidden) + h2 = tf.nn.relu(tf.matmul(h1, w_h2) + b2) + h2 = tf.nn.dropout(h2, p_keep_hidden) + h3 = tf.nn.relu(tf.matmul(h2, w_h3) + b3) + h3 = tf.nn.dropout(h3, p_keep_hidden) + out = tf.nn.softmax(tf.matmul(h3, w_o) + b_o) + return out + + +def validate_model(X, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o): + h1 = tf.nn.relu(tf.matmul(X, w_h1) + b1) + h2 = tf.nn.relu(tf.matmul(h1, w_h2) + b2) + h3 = tf.nn.relu(tf.matmul(h2, w_h3) + b3) + out = tf.matmul(h3, w_o) + b_o + return tf.nn.softmax(out) + + +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + w_h1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + b1 = tf.Variable(tf.zeros([hidden_units])) + w_h2 = tf.Variable(tf.truncated_normal([hidden_units, hidden_units])) + b2 = tf.Variable(tf.zeros([hidden_units])) + w_h3 = tf.Variable(tf.truncated_normal([hidden_units, hidden_units])) + b3 = tf.Variable(tf.zeros([hidden_units])) + w_o = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + b_o = tf.Variable(tf.zeros([num_labels])) + p_keep_input = tf.placeholder('float') + p_keep_hidden = tf.placeholder('float') + net = model(tf_train_dataset, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o, p_keep_input, p_keep_hidden) + l2_loss = 0.001 * (tf.nn.l2_loss(w_h1) + tf.nn.l2_loss(w_h2) + tf.nn.l2_loss(w_h3) + tf.nn.l2_loss(w_o)) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=net)) + l2_loss + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 10000, 0.95) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(net) + valid_prediction = validate_model(tf_valid_dataset, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o) + test_prediction = validate_model(tf_test_dataset, w_h1, b1, w_h2, b2, w_h3, b3, w_o, b_o) +num_steps = 20001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, p_keep_input: 0.5, p_keep_hidden: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 1000 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/282.txt b/dataset/preprocessed/282.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/282.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/283.txt b/dataset/preprocessed/283.txt new file mode 100644 index 0000000..1a27237 --- /dev/null +++ b/dataset/preprocessed/283.txt @@ -0,0 +1,170 @@ +""" +Created on Tue May 16 12:30:50 2017 + +@author: Seagle + +""" +import os +os.chdir('E:/SeagleDLTrial') +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_bacthes + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.0} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + drop1 = tf.nn.dropout(lay1_train, keep_prob) + logits = tf.matmul(drop1, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_bacthes + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.0} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + logits = tf.matmul(lay2_train, weights3) + biases3 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/284.txt b/dataset/preprocessed/284.txt new file mode 100644 index 0000000..1e1723e --- /dev/null +++ b/dataset/preprocessed/284.txt @@ -0,0 +1,313 @@ +"""3_regularization.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/drive/1kIhA8d-ZLbUegSaKn3FK29R2T4NkS5mK + +Deep Learning +============= + +Assignment 3 +------------ + +Previously in `2_fullyconnected.ipynb`, you trained a logistic regression and a neural network model. + +The goal of this assignment is to explore regularization techniques. +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +"""First reload the data we generated in `1_notmnist.ipynb`.""" +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +"""Reformat into a shape that's more adapted to the models we're going to train: +- data as a flat matrix, +- labels as float 1-hot encodings. +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +"""--- +Problem 1 +--------- + +Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor `t` using `nn.l2_loss(t)`. The right amount of regularization should improve your validation / test accuracy. + +--- +""" +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + regularizer = tf.nn.l2_loss(weights) + loss = loss + beta * regularizer + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + B1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + B2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, W1) + B1 + relu_1 = tf.nn.relu(logits_1) + logits_2 = tf.matmul(relu_1, W2) + B2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_2)) + w1_regularizer = tf.nn.l2_loss(W1) + w2_regularizer = tf.nn.l2_loss(W2) + loss = loss + beta * (w1_regularizer + w2_regularizer) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + valid_y1 = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + B1) + valid_prediction = tf.nn.softmax(tf.matmul(valid_y1, W2) + B2) + test_y1 = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + B1) + test_prediction = tf.nn.softmax(tf.matmul(test_y1, W2) + B2) +num_step = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_step): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy; %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +"""--- +Problem 2 +--------- +Let's demonstrate an extreme case of overfitting. Restrict your training data to just a few batches. What happens? + +--- +""" +num_step = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_step): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy; %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +"""--- +Problem 3 +--------- +Introduce Dropout on the hidden layer of the neural network. Remember: Dropout should only be introduced during training, not evaluation, otherwise your evaluation results would be stochastic as well. TensorFlow provides `nn.dropout()` for that, but you have to make sure it's only inserted during training. + +What happens to our extreme overfitting case? + +--- +""" +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + B1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + B2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, W1) + B1 + relu_1 = tf.nn.relu(logits_1) + keep_prob = tf.placeholder('float') + dropout_logits_1 = tf.nn.dropout(relu_1, keep_prob) + logits_2 = tf.matmul(dropout_logits_1, W2) + B2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_2)) + w1_regularizer = tf.nn.l2_loss(W1) + w2_regularizer = tf.nn.l2_loss(W2) + loss = loss + beta * (w1_regularizer + w2_regularizer) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + valid_y1 = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + B1) + valid_prediction = tf.nn.softmax(tf.matmul(valid_y1, W2) + B2) + test_y1 = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + B1) + test_prediction = tf.nn.softmax(tf.matmul(test_y1, W2) + B2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step {}: {}'.format(step, l)) + print('Minibatch accuracy: {:.1f}'.format(accuracy(predictions, batch_labels))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) +"""--- +Problem 4 +--------- + +Try to get the best performance you can using a multi-layer model! The best reported test accuracy using a deep network is [97.1%](http://yaroslavvb.blogspot.com/2011/09/notmnist-dataset.html?showComment=1391023266211#c8758720086795711595). + +One avenue you can explore is to add multiple layers. + +Another one is to use learning rate decay: + + global_step = tf.Variable(0) # count the number of steps taken. + learning_rate = tf.train.exponential_decay(0.5, global_step, ...) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + + --- +""" +import math +batch_size = 128 +beta = 0.001 +hidden_size_1 = 1024 +hidden_size_2 = 512 +hidden_size_3 = 256 +hidden_size_4 = 128 +hidden_size_5 = 64 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size_1], stddev=math.sqrt(2.0 / (image_size * image_size)))) + B1 = tf.Variable(tf.zeros([hidden_size_1])) + W2 = tf.Variable(tf.truncated_normal([hidden_size_1, hidden_size_2], stddev=math.sqrt(2.0 / hidden_size_1))) + B2 = tf.Variable(tf.zeros([hidden_size_2])) + W3 = tf.Variable(tf.truncated_normal([hidden_size_2, hidden_size_3], stddev=math.sqrt(2.0 / hidden_size_2))) + B3 = tf.Variable(tf.zeros([hidden_size_3])) + W4 = tf.Variable(tf.truncated_normal([hidden_size_3, hidden_size_4], stddev=math.sqrt(2.0 / hidden_size_3))) + B4 = tf.Variable(tf.zeros([hidden_size_4])) + W5 = tf.Variable(tf.truncated_normal([hidden_size_4, hidden_size_5], stddev=math.sqrt(2.0 / hidden_size_4))) + B5 = tf.Variable(tf.zeros([hidden_size_5])) + W6 = tf.Variable(tf.truncated_normal([hidden_size_5, num_labels], stddev=math.sqrt(2.0 / hidden_size_5))) + B6 = tf.Variable(tf.zeros([num_labels])) + keep_prob = tf.placeholder('float') + logits_1 = tf.matmul(tf_train_dataset, W1) + B1 + relu_1 = tf.nn.relu(logits_1) + dropout_logits_1 = tf.nn.dropout(relu_1, keep_prob) + logits_2 = tf.matmul(dropout_logits_1, W2) + B2 + relu_2 = tf.nn.relu(logits_2) + dropout_logits_2 = tf.nn.dropout(relu_2, keep_prob) + logits_3 = tf.matmul(dropout_logits_2, W3) + B3 + relu_3 = tf.nn.relu(logits_3) + dropout_logits_3 = tf.nn.dropout(relu_3, keep_prob) + logits_4 = tf.matmul(dropout_logits_3, W4) + B4 + relu_4 = tf.nn.relu(logits_4) + dropout_logits_4 = tf.nn.dropout(relu_4, keep_prob) + logits_5 = tf.matmul(dropout_logits_4, W5) + B5 + relu_5 = tf.nn.relu(logits_5) + dropout_logits_5 = tf.nn.dropout(relu_5, keep_prob) + logits_6 = tf.matmul(dropout_logits_5, W6) + B6 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_6)) + w1_regularizer = tf.nn.l2_loss(W1) + w2_regularizer = tf.nn.l2_loss(W2) + w3_regularizer = tf.nn.l2_loss(W3) + w4_regularizer = tf.nn.l2_loss(W4) + w5_regularizer = tf.nn.l2_loss(W5) + w6_regularizer = tf.nn.l2_loss(W6) + loss = loss + beta * (w1_regularizer + w2_regularizer + w3_regularizer + w4_regularizer + w5_regularizer + w6_regularizer) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 100000, 0, 96) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits_6) + valid_y1 = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + B1) + valid_y2 = tf.nn.relu(tf.matmul(valid_y1, W2) + B2) + valid_y3 = tf.nn.relu(tf.matmul(valid_y2, W3) + B3) + valid_y4 = tf.nn.relu(tf.matmul(valid_y3, W4) + B4) + valid_y5 = tf.nn.relu(tf.matmul(valid_y4, W5) + B5) + valid_prediction = tf.nn.softmax(tf.matmul(valid_y5, W6) + B6) + test_y1 = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + B1) + test_y2 = tf.nn.relu(tf.matmul(test_y1, W2) + B2) + test_y3 = tf.nn.relu(tf.matmul(test_y2, W3) + B3) + test_y4 = tf.nn.relu(tf.matmul(test_y3, W4) + B4) + test_y5 = tf.nn.relu(tf.matmul(test_y4, W5) + B5) + test_prediction = tf.nn.softmax(tf.matmul(test_y5, W6) + B6) +num_steps = 15000 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step {}: {}'.format(step, l)) + print('Minibatch accuracy: {:.1f}'.format(accuracy(predictions, batch_labels))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) diff --git a/dataset/preprocessed/285.txt b/dataset/preprocessed/285.txt new file mode 100644 index 0000000..b73d8e4 --- /dev/null +++ b/dataset/preprocessed/285.txt @@ -0,0 +1,198 @@ +import numpy as np +import tensorflow as tf +import sys +import os +import math +sys.path.insert(0, os.path.abspath('..')) +from argparse import ArgumentParser +from Util.dataloader import DataLoader +from Util.configs import NNConfig +from ranking.NN import NN +import logging +logFormatter = logging.Formatter('%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s') +fileHandler = logging.FileHandler('{0}/{1}.log'.format('./', 'Fullyconnected_NN')) +fileHandler.setFormatter(logFormatter) +consoleHandler = logging.StreamHandler() +consoleHandler.setFormatter(logFormatter) +logger = logging.getLogger() +logger.setLevel(logging.INFO) +logger.addHandler(fileHandler) +logger.addHandler(consoleHandler) + + +class NN(NN): + + def __init__(self): + self.d_loader = DataLoader() + self.input_vector_size = self.d_loader.d_handler.get_vocab_size() + self.output_vector_size = 1 + self.train_dataset, self.train_labels, self.valid_dataset, self.valid_labels, self.test_dataset, self.test_labels = self.d_loader.get_ttv() + + def simple_NN(self, mode='/cpu:0'): + logger.info('creating the computational graph...') + graph = tf.Graph() + with graph.as_default(): + with tf.device(mode): + tf_train_dataset = tf.placeholder(tf.float32, shape=(NNConfig.batch_size, self.input_vector_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(NNConfig.batch_size, self.output_vector_size)) + tf_valid_dataset_init = tf.placeholder(tf.float32, shape=self.valid_dataset.shape) + tf_valid_dataset = tf.Variable(tf_valid_dataset_init) + tf_test_dataset_init = tf.placeholder(tf.float32, shape=self.test_dataset.shape) + tf_test_dataset = tf.Variable(tf_test_dataset_init) + if self.cfg['NNConfig']['regularization']: + beta_regu = tf.placeholder(tf.float32) + if self.cfg['NNConfig']['learning_rate_decay']: + global_step = tf.Variable(0) + + def init_weights(shape): + return tf.Variable(tf.truncated_normal(shape)) + + def init_biases(shape): + return tf.Variable(tf.zeros(shape)) + w_h = init_weights([self.input_vector_size, NNConfig.num_hidden_nodes]) + b_h = init_biases([NNConfig.num_hidden_nodes]) + w_o = init_weights([NNConfig.num_hidden_nodes, self.output_vector_size]) + b_o = init_biases([self.output_vector_size]) + + def model(dataset, w_h, b_h, w_o, b_o, train): + if NNConfig.dropout and train: + drop_i = tf.nn.dropout(dataset, NNConfig.dropout_keep_prob_input) + h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + drop_h = tf.nn.dropout(h_lay_train, NNConfig.dropout_keep_prob_hidden) + return tf.matmul(drop_h, w_o) + b_o + else: + h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + return tf.matmul(h_lay_train, w_o) + b_o + logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + loss = tf.reduce_sum(tf.pow(logits - tf_train_labels, 2)) / (2 * tf.cast(tf.shape(tf_train_labels)[0], tf.float32)) + if NNConfig.regularization: + loss += beta_regu * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + if NNConfig.learning_rate_decay: + learning_rate = tf.train.exponential_decay(NNConfig.learning_rate, global_step, NNConfig.decay_steps, NNConfig.decay_rate, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + else: + optimizer = tf.train.GradientDescentOptimizer(NNConfig.learning_rate).minimize(loss) + train_prediction = logits + valid_prediction = model(tf_valid_dataset, w_h, b_h, w_o, b_o, False) + test_prediction = model(tf_test_dataset, w_h, b_h, w_o, b_o, False) + """ + run accuracy scope + """ + with tf.name_scope('accuracy'): + pre = tf.placeholder('float', shape=[None, self.output_vector_size]) + lbl = tf.placeholder('float', shape=[None, self.output_vector_size]) + accuracy = tf.reduce_sum(tf.pow(pre - lbl, 2)) / (2 * tf.cast(tf.shape(lbl)[0], tf.float32)) + logger.info('running the session...') + with tf.Session(graph=graph, config=tf.ConfigProto(log_device_placement=True)) as session: + session.run(tf.global_variables_initializer(), feed_dict={tf_valid_dataset_init: self.valid_dataset, tf_test_dataset_init: self.test_dataset}) + logger.info('Initialized') + for step in range(NNConfig.num_steps): + offset = step * NNConfig.batch_size % (self.train_labels.shape[0] - NNConfig.batch_size) + batch_data = self.train_dataset[offset:offset + NNConfig.batch_size, :] + batch_labels = self.train_labels[offset:offset + NNConfig.batch_size] + batch_labels = batch_labels.reshape(len(batch_labels), 1) + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + if NNConfig.regularization: + feed_dict[beta_regu] = NNConfig.beta_regu + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % NNConfig.summary_steps == 0: + logger.info('Minibatch loss at step %d: %f' % (step, l)) + logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: predictions, lbl: batch_labels})) + logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: valid_prediction.eval(), lbl: self.valid_labels})) + logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: test_prediction.eval(), lbl: self.test_labels})) + self.print_words(test_prediction.eval(), self.test_labels) + + def simple_NN_w_CandidateSampling(self): + print('creating the computational graph...') + graph = tf.Graph() + with graph.as_default(): + with tf.device('/cpu:0'): + tf_train_dataset = tf.placeholder(tf.float32, shape=(NNConfig.batch_size, self.input_vector_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(NNConfig.batch_size, self.output_vector_size)) + tf_valid_dataset = tf.constant(self.valid_dataset) + tf_test_dataset = tf.constant(self.test_dataset) + if NNConfig.regularization: + beta_regu = tf.placeholder(tf.float32) + if NNConfig.learning_rate_decay: + global_step = tf.Variable(0) + + def init_biases(shape): + return tf.Variable(tf.zeros(shape)) + w_h = tf.Variable(tf.truncated_normal([self.input_vector_size, NNConfig.num_hidden_nodes], stddev=1.0 / math.sqrt(NNConfig.num_hidden_nodes))) + b_h = init_biases([NNConfig.num_hidden_nodes]) + w_o = tf.Variable(tf.truncated_normal([NNConfig.num_hidden_nodes, self.output_vector_size], stddev=1.0 / math.sqrt(self.output_vector_size))) + b_o = init_biases([self.output_vector_size]) + + def model(dataset, w_h, b_h, w_o, b_o, train): + if NNConfig.dropout and train: + drop_i = tf.nn.dropout(dataset, NNConfig.dropout_keep_prob_input) + h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + drop_h = tf.nn.dropout(h_lay_train, NNConfig.dropout_keep_prob_hidden) + return tf.matmul(drop_h, w_o) + b_o + else: + h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + return tf.matmul(h_lay_train, w_o) + b_o + logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + if NNConfig.candidate_sampling == 'nce_loss': + instances_loss = tf.nn.nce_loss(w_o, b_o, tf_train_dataset, tf_train_labels, NNConfig.num_sampled, self.output_vector_size, num_true=10) + elif NNConfig.candidate_sampling == 'softmax_loss': + instances_loss = tf.nn.sampled_softmax_loss(w_o, b_o, tf_train_dataset, tf_train_labels, NNConfig.num_sampled, self.output_vector_size, num_true=10) + else: + instances_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + print('no candidate sampling....') + loss = tf.reduce_mean(instances_loss) + if NNConfig.regularization: + loss += beta_regu * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + if NNConfig.learning_rate_decay: + learning_rate = tf.train.exponential_decay(NNConfig.learning_rate, global_step, NNConfig.decay_steps, NNConfig.decay_rate, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + else: + optimizer = tf.train.GradientDescentOptimizer(NNConfig.learning_rate).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset, w_h, b_h, w_o, b_o, False)) + test_prediction = tf.nn.softmax(model(tf_test_dataset, w_h, b_h, w_o, b_o, False)) + with tf.name_scope('accuracy'): + pre = tf.placeholder('float', shape=[None, self.output_vector_size]) + lbl = tf.placeholder('float', shape=[None, self.output_vector_size]) + accuracy = tf.reduce_mean(tf.cast(tf.nn.sigmoid_cross_entropy_with_logits(logits=pre, labels=lbl), 'float')) + logger.info('running the session...') + with tf.Session(graph=graph, config=tf.ConfigProto(log_device_placement=True)) as session: + tf.initialize_all_variables().run() + logger.info('Initialized') + for step in range(NNConfig.num_steps): + offset = step * NNConfig.batch_size % (self.train_labels.shape[0] - NNConfig.batch_size) + batch_data = self.train_dataset[offset:offset + NNConfig.batch_size, :] + batch_labels = self.train_labels[offset:offset + NNConfig.batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + if NNConfig.regularization: + feed_dict[beta_regu] = NNConfig.beta_regu + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % NNConfig.summary_steps == 0: + logger.info('Minibatch loss at step %d: %f' % (step, l)) + logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: predictions, lbl: batch_labels})) + logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: valid_prediction.eval(), lbl: self.valid_labels})) + logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: test_prediction.eval(), lbl: self.test_labels})) + + +if __name__ == '__main__': + parser = ArgumentParser(description='Required arguments') + parser.add_argument('-m', '--mode', help='computation mode', required=False) + parser.add_argument('-s', '--query_size', help='number of queries for training', required=False) + parser.add_argument('-l', '--loss', help='loss function [point-wise, pair-wise]', required=False) + args = parser.parse_args() + nn = NN() if args.query_size is None else NN(args.query_size) + try: + if args.mode == 'gpu': + nn.mode = '/gpu:0' + else: + nn.mode = '/cpu:0' + if args.loss == 'point-wise' or args.loss == 'pair-wise': + logger.info('learn with pair-wise loss') + nn.lf = args.loss + else: + nn.lf == 'point-wise' + nn.simple_NN() + logger.info('done..') + except Exception as e: + logger.exception(e) + raise diff --git a/dataset/preprocessed/286.txt b/dataset/preprocessed/286.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/286.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/287.txt b/dataset/preprocessed/287.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/287.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/288.txt b/dataset/preprocessed/288.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/288.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/289.txt b/dataset/preprocessed/289.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/289.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/29.txt b/dataset/preprocessed/29.txt new file mode 100644 index 0000000..519bc26 --- /dev/null +++ b/dataset/preprocessed/29.txt @@ -0,0 +1,128 @@ +import sys +import time +import numpy as np +import tensorflow as tf +from sklearn.model_selection import train_test_split +import siuts +import glob +num_epochs = 2 +batch_size = 128 + + +def main(): + start = time.time() + num_channels = 1 + num_labels = len(siuts.species_list) + image_size = siuts.resized_segment_size + num_files = len(glob.glob1(siuts.dataset_dir, '{}-training*'.format(siuts.species_list[0]))) + num_files = 3 + graph = tf.Graph() + with graph.as_default(): + tf.set_random_seed(1337) + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels), name='train_dataset_placeholder') + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels), name='train_labels_placeholder') + tf_test_dataset = tf.placeholder(tf.float32, shape=(siuts.test_batch_size, image_size, image_size, num_channels), name='test_dataset_placeholder') + tf_one_prediction = tf.placeholder(tf.float32, shape=(1, image_size, image_size, num_channels), name='tf_one_prediction') + + def conv2d(name, data, kernel_shape, bias_shape, stride=1): + with tf.variable_scope(name) as scope: + weights = tf.get_variable('weights', kernel_shape, initializer=tf.random_normal_initializer(0.0, 0.05)) + biases = tf.get_variable('biases', bias_shape, initializer=tf.constant_initializer(1.0)) + conv = tf.nn.conv2d(data, weights, [1, stride, stride, 1], padding='SAME', name='conv') + pre_activation = tf.nn.bias_add(conv, biases) + activation = tf.nn.elu(pre_activation, name='elu') + return activation + + def fully_connected(name, data, weights_shape, bias_shape, dropout): + with tf.variable_scope(name) as scope: + weights = tf.get_variable('weights', weights_shape, initializer=tf.random_normal_initializer(0.0, 0.05)) + biases = tf.get_variable('biases', bias_shape, initializer=tf.constant_initializer(1.0)) + activation = tf.nn.elu(tf.nn.bias_add(tf.matmul(data, weights), biases), name='elu') + return tf.nn.dropout(activation, dropout, name='dropout') + + def model(data, input_dropout, fc_dropout): + data = tf.nn.dropout(data, input_dropout) + print(data) + conv = conv2d('conv1', data, [5, 5, 1, 32], [32], 2) + pool = tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1') + print(pool) + conv = conv2d('conv2', pool, [5, 5, 32, 64], [64]) + pool = tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool2') + print(pool) + conv = conv2d('conv3', pool, [3, 3, 64, 128], [128]) + pool = tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool3') + print(pool) + shape = pool.get_shape().as_list() + reshaped_layer = tf.reshape(pool, [shape[0], shape[1] * shape[2] * shape[3]]) + fc = fully_connected('fc1', reshaped_layer, [shape[1] * shape[2] * shape[3], 256], [256], fc_dropout) + fc = fully_connected('fc2', fc, [256, 128], [128], fc_dropout) + return fully_connected('output', fc, [128, num_labels], [num_labels], 1) + logits = model(tf_train_dataset, 1, 0.8) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits + 1e-50, labels=tf_train_labels)) + optimizer = tf.train.MomentumOptimizer(0.0001, 0.95, use_locking=False, name='Momentum', use_nesterov=True).minimize(loss) + tf.get_variable_scope().reuse_variables() + train_prediction = tf.nn.softmax(model(tf_train_dataset, 1, 1), name='sm_train') + test_prediction = tf.nn.softmax(model(tf_test_dataset, 1, 1), name='sm_test') + one_prediction = tf.nn.softmax(model(tf_one_prediction, 1, 1), name='sm_one') + checkpoint_path = siuts.checkpoints_dir + 'model.ckpt' + with tf.Session(graph=graph) as session: + writer = tf.summary.FileWriter(siuts.checkpoints_dir, session.graph) + tf.global_variables_initializer().run() + saver = tf.train.Saver(tf.global_variables(), max_to_keep=200) + tf.train.write_graph(session.graph_def, siuts.checkpoints_dir, 'graph.pb', False) + train_dataset = np.empty + train_labels = np.empty + current_file = 0 + current_epoch = 1 + step = 0 + while True: + if step * batch_size % (siuts.samples_in_file * num_labels - batch_size) == 0: + if current_epoch > num_epochs: + break + del train_dataset + del train_labels + sys.stdout.write('Loading datasets nr ' + str(current_file)) + sys.stdout.flush() + counter = 0 + train_dataset = np.empty + train_labels = np.empty + for specimen in siuts.species_list: + new_data = siuts.load('{0}{1}-training_{2}.pickle'.format(siuts.standardized_dataset_dir, specimen, current_file)) + new_labels = np.empty(new_data.shape[0]) + new_labels.fill(siuts.species_list.index(specimen)) + if counter == 0: + train_dataset = new_data + train_labels = new_labels + else: + train_dataset = np.vstack((train_dataset, new_data)) + train_labels = np.concatenate((train_labels, new_labels)) + counter += 1 + sys.stdout.write('.') + sys.stdout.flush() + print() + current_file += 1 + if current_file >= num_files - 1: + current_file = 0 + current_epoch += 1 + train_dataset, _, train_labels, _ = train_test_split(train_dataset, siuts.reformat_labels(train_labels), test_size=0, random_state=1337) + shape = np.shape(train_dataset) + train_dataset = np.reshape(train_dataset, (shape[0], shape[1], shape[2], 1)) + offset = step * batch_size % (num_labels * siuts.samples_in_file - batch_size) + sys.stdout.write('.') + sys.stdout.flush() + batch_data = train_dataset[offset:offset + batch_size, :, :, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 25 == 0: + batch_acc = siuts.accuracy(predictions, batch_labels) + if step % 250 == 0: + saver.save(session, checkpoint_path, global_step=step) + print('%d - Minibatch loss: %f | Minibatch accuracy: %.1f%%' % (step, l, batch_acc)) + step += 1 + saver.save(session, checkpoint_path, global_step=step) + print('Training took ' + str(time.time() - start) + ' seconds') + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/290.txt b/dataset/preprocessed/290.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/290.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/291.txt b/dataset/preprocessed/291.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/291.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/292.txt b/dataset/preprocessed/292.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/292.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/293.txt b/dataset/preprocessed/293.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/293.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/294.txt b/dataset/preprocessed/294.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/294.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/295.txt b/dataset/preprocessed/295.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/295.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/296.txt b/dataset/preprocessed/296.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/296.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/297.txt b/dataset/preprocessed/297.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/297.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/298.txt b/dataset/preprocessed/298.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/298.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/299.txt b/dataset/preprocessed/299.txt new file mode 100644 index 0000000..dfcdbe5 --- /dev/null +++ b/dataset/preprocessed/299.txt @@ -0,0 +1,174 @@ +from __future__ import print_function +import numpy as np +import matplotlib.pyplot as plt +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +""" +Training set (200000, 28, 28) (200000,) +Validation set (10000, 28, 28) (10000,) +Test set (18724, 28, 28) (18724,) + +Reformat into a shape that's more adapted to the models we're going to train: + + data as a flat matrix, + labels as float 1-hot encodings. +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +""" + + +We're first going to train a multinomial logistic regression using simple gradient descent. + +TensorFlow works like this: + + First you describe the computation that you want to see performed: what the inputs, the variables, and the operations look like. These get created as nodes over a computation graph. This description is all contained within the block below: + + with graph.as_default(): + ... + + Then you can run the operations on this graph as many times as you want by calling session.run(), providing it outputs to fetch from the graph that get returned. This runtime operation is all contained in the block below: + + with tf.Session(graph=graph) as session: + ... + +Let's load all the data into TensorFlow and build the computation graph corresponding to our training: +""" +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy(predictions, train_labels[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +""" + + +Let's now switch to stochastic gradient descent training instead, which is much faster. + +The graph will be similar, except that instead of holding all the training data into a constant node, we create a Placeholder node which will be fed actual data at every call of session.run(). +""" +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +""" +Problem + +Turn the logistic regression example with SGD into a 1-hidden layer neural network with rectified linear units nn.relu() and 1024 hidden nodes. This model should improve your validation / test accuracy. +""" +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/3.txt b/dataset/preprocessed/3.txt new file mode 100644 index 0000000..33b4fd4 --- /dev/null +++ b/dataset/preprocessed/3.txt @@ -0,0 +1,160 @@ +import tensorflow as tf +import matplotlib.pyplot as plt +import numpy as np +from sklearn.datasets import load_boston +import sklearn.linear_model as sk +boston = load_boston() +features = np.array(boston.data) +target = np.array(boston.target) + + +def normalize(dataset): + mu = np.mean(dataset, axis=0) + sigma = np.std(dataset, axis=0) + return (dataset - mu) / sigma + + +feature_norm = normalize(features) +np.random.seed(42) +rnd = np.random.rand(len(feature_norm)) < 0.8 +train_x = np.transpose(feature_norm[rnd]) +train_y = np.transpose(target[rnd]) +dev_x = np.transpose(feature_norm[~rnd]) +dev_y = np.transpose(target[~rnd]) +train_y = train_y.reshape(1, len(train_y)) +dev_y = dev_y.reshape(1, len(dev_y)) + + +def create_layer(X, n, activation): + ndim = int(X.shape[0]) + stddev = 2.0 / np.sqrt(ndim) + initialization = tf.truncated_normal((n, ndim), stddev=stddev) + W = tf.Variable(initialization) + b = tf.Variable(tf.zeros([n, 1])) + Z = tf.matmul(W, X) + b + return activation(Z), W, b + + +tf.reset_default_graph() +n_dim = 13 +n1 = 20 +n2 = 20 +n3 = 20 +n4 = 20 +n_ouputs = 1 +tf.set_random_seed(42) +X = tf.placeholder(tf.float32, [n_dim, None]) +Y = tf.placeholder(tf.float32, [1, None]) +learning_rate = tf.placeholder(tf.float32, shape=()) +hidden1, w1, b1 = create_layer(X, n1, activation=tf.nn.relu) +hidden2, w2, b2 = create_layer(hidden1, n1, activation=tf.nn.relu) +hidden3, w3, b3 = create_layer(hidden2, n2, activation=tf.nn.relu) +hidden4, w4, b4 = create_layer(hidden3, n3, activation=tf.nn.relu) +y_, w5, b5 = create_layer(hidden4, n4, activation=tf.identity) +cost = tf.reduce_mean(tf.square(y_ - Y)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08).minimize(cost) +sess = tf.Session() +sess.run(tf.global_variables_initializer()) +cost_train_history = [] +cost_dev_history = [] +for epoch in range(10000 + 1): + sess.run(optimizer, feed_dict={X: train_x, Y: train_y, learning_rate: 0.001}) + cost_train_ = sess.run(cost, feed_dict={X: train_x, Y: train_y, learning_rate: 0.001}) + cost_dev_ = sess.run(cost, feed_dict={X: dev_x, Y: dev_y, learning_rate: 0.001}) + cost_train_history = np.append(cost_train_history, cost_train_) + cost_dev_history = np.append(cost_dev_history, cost_dev_) + if epoch % 10 == 0: + print('epoch: {}, cost J(train) = {:.6f}, cost J(test) = {:.6f}'.format(epoch, cost_train_, cost_dev_)) +plt.plot(range(len(cost_train_history)), cost_train_history, label='train') +plt.plot(range(len(cost_dev_history)), cost_dev_history, label='test') +plt.xlabel('epoch') +plt.ylabel('cost') +plt.legend(loc='best') +plt.show() +np_w1 = w1.eval(session=sess) +np_w1 = np_w1.reshape(20 * 13, 1) +np_w1.shape +plt.hist(np_w1, bins=100) +plt.show() +tf.reset_default_graph() +n_dim = 13 +n1 = 20 +n2 = 20 +n3 = 20 +n4 = 20 +n_ouputs = 1 +tf.set_random_seed(42) +X = tf.placeholder(tf.float32, [n_dim, None]) +Y = tf.placeholder(tf.float32, [1, None]) +learning_rate = tf.placeholder(tf.float32, shape=()) +hidden1, w1, b1 = create_layer(X, n1, activation=tf.nn.relu) +hidden2, w2, b2 = create_layer(hidden1, n1, activation=tf.nn.relu) +hidden3, w3, b3 = create_layer(hidden2, n2, activation=tf.nn.relu) +hidden4, w4, b4 = create_layer(hidden3, n3, activation=tf.nn.relu) +y_, w5, b5 = create_layer(hidden4, n4, activation=tf.identity) +lambd = tf.placeholder(tf.float32, shape=()) +reg = tf.nn.l2_loss(w1) + tf.nn.l2_loss(w2) + tf.nn.l2_loss(w3) + tf.nn.l2_loss(w4) + tf.nn.l2_loss(w5) +cost_mse = tf.reduce_mean(tf.square(y_ - Y)) +cost = tf.reduce_mean(cost_mse + lambd * reg) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08).minimize(cost) +sess = tf.Session() +sess.run(tf.global_variables_initializer()) +cost_train_history = [] +cost_dev_history = [] +learning_r = 0.001 +lambd_val = 0.1 +for epoch in range(10000 + 1): + sess.run(optimizer, feed_dict={X: train_x, Y: train_y, learning_rate: learning_r, lambd: lambd_val}) + cost_train_ = sess.run(cost, feed_dict={X: train_x, Y: train_y, learning_rate: learning_r, lambd: lambd_val}) + cost_dev_ = sess.run(cost, feed_dict={X: train_x, Y: train_y, learning_rate: learning_r, lambd: lambd_val}) + cost_train_history = np.append(cost_train_history, cost_train_) + cost_dev_history = np.append(cost_dev_history, cost_dev_) + if epoch % 10 == 0: + print('epoch: {}, cost J(train) = {:.6f}, cost J(test) = {:.6f}'.format(epoch, cost_train_, cost_dev_)) +plt.plot(range(len(cost_train_history)), cost_train_history, label='train') +plt.plot(range(len(cost_dev_history)), cost_dev_history, label='test') +plt.xlabel('epoch') +plt.ylabel('cost') +plt.legend(loc='best') +plt.show() +tf.reset_default_graph() +n_dim = 13 +n1 = 20 +n2 = 20 +n3 = 20 +n4 = 20 +n_ouputs = 1 +tf.set_random_seed(42) +X = tf.placeholder(tf.float32, [n_dim, None]) +Y = tf.placeholder(tf.float32, [1, None]) +learning_rate = tf.placeholder(tf.float32, shape=()) +keep_prob = tf.placeholder(tf.float32, shape=()) +hidden1, w1, b1 = create_layer(X, n1, activation=tf.nn.relu) +hidden1_drop = tf.nn.dropout(hidden1, keep_prob) +hidden2, w2, b2 = create_layer(hidden1_drop, n1, activation=tf.nn.relu) +hidden2_drop = tf.nn.dropout(hidden2, keep_prob) +hidden3, w3, b3 = create_layer(hidden2_drop, n2, activation=tf.nn.relu) +hidden3_drop = tf.nn.dropout(hidden3, keep_prob) +hidden4, w4, b4 = create_layer(hidden3_drop, n3, activation=tf.nn.relu) +hidden4_drop = tf.nn.dropout(hidden4, keep_prob) +y_, w5, b5 = create_layer(hidden4, n4, activation=tf.identity) +cost = tf.reduce_mean(tf.square(y_ - Y)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08).minimize(cost) +sess = tf.Session() +sess.run(tf.global_variables_initializer()) +cost_train_history = [] +cost_dev_history = [] +for epoch in range(10000 + 1): + sess.run(optimizer, feed_dict={X: train_x, Y: train_y, learning_rate: 0.001, keep_prob: 1.0}) + cost_train_ = sess.run(cost, feed_dict={X: train_x, Y: train_y, learning_rate: 0.001, keep_prob: 1.0}) + cost_dev_ = sess.run(cost, feed_dict={X: dev_x, Y: dev_y, learning_rate: 0.001, keep_prob: 1.0}) + cost_train_history = np.append(cost_train_history, cost_train_) + cost_dev_history = np.append(cost_dev_history, cost_dev_) + if epoch % 10 == 0: + print('epoch: {}, cost J(train) = {:.6f}, cost J(test) = {:.6f}'.format(epoch, cost_train_, cost_dev_)) +plt.plot(range(len(cost_train_history)), cost_train_history, label='train') +plt.plot(range(len(cost_dev_history)), cost_dev_history, label='test') +plt.xlabel('epoch') +plt.ylabel('cost') +plt.legend(loc='best') +plt.show() diff --git a/dataset/preprocessed/30.txt b/dataset/preprocessed/30.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/30.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/300.txt b/dataset/preprocessed/300.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/300.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/301.txt b/dataset/preprocessed/301.txt new file mode 100644 index 0000000..edb594a --- /dev/null +++ b/dataset/preprocessed/301.txt @@ -0,0 +1,233 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('L2 regularization(beta=%.5f) Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +print('Best beta=%f, accuracy=%.1f%%' % (beta_val[np.argmax(accuracy_val)], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Overfitting with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y1 = tf.nn.dropout(y1, 0.5) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + global_step = tf.Variable(0) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, fc1_size], stddev=np.sqrt(2.0 / (image_size * image_size)))) + b1 = tf.Variable(tf.zeros([fc1_size])) + W2 = tf.Variable(tf.truncated_normal([fc1_size, fc2_size], stddev=np.sqrt(2.0 / fc1_size))) + b2 = tf.Variable(tf.zeros([fc2_size])) + W3 = tf.Variable(tf.truncated_normal([fc2_size, fc3_size], stddev=np.sqrt(2.0 / fc2_size))) + b3 = tf.Variable(tf.zeros([fc3_size])) + W4 = tf.Variable(tf.truncated_normal([fc3_size, num_labels], stddev=np.sqrt(2.0 / fc3_size))) + b4 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + logits = tf.matmul(y3, W4) + b4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + valid_logits = tf.matmul(y3_valid, W4) + b4 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + test_logits = tf.matmul(y3_test, W4) + b4 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/302.txt b/dataset/preprocessed/302.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/302.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/303.txt b/dataset/preprocessed/303.txt new file mode 100644 index 0000000..9cf633b --- /dev/null +++ b/dataset/preprocessed/303.txt @@ -0,0 +1,145 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +beta = 0.004 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + beta * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_units = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + biases1 = tf.Variable(tf.zeros([hidden_units])) + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + weights = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = loss + 0.5 * beta * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(valid_prediction1, weights) + biases) + test_prediction1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(test_prediction1, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + batch_size = 128 + hidden_units = 1024 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + biases1 = tf.Variable(tf.zeros([hidden_units])) + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + hidden1 = tf.nn.dropout(hidden1, 0.6) + weights = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = loss + 0.5 * beta * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(valid_prediction1, weights) + biases) + test_prediction1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(test_prediction1, weights) + biases) + num_steps = 3001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/304.txt b/dataset/preprocessed/304.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/304.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/305.txt b/dataset/preprocessed/305.txt new file mode 100644 index 0000000..79f633f --- /dev/null +++ b/dataset/preprocessed/305.txt @@ -0,0 +1,134 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + print(one_hot_out) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/306.txt b/dataset/preprocessed/306.txt new file mode 100644 index 0000000..aa43b08 --- /dev/null +++ b/dataset/preprocessed/306.txt @@ -0,0 +1,57 @@ +import tensorflow as tf +from ntmnist import dataset +import numpy as np +pickle_file = 'notMNIST.pickle' +data = dataset(pickle_file) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +train_subset = 1000 +image_size = 28 +num_labels = 10 +hn_num = 1024 +batch_size = 128 +reg_beta = 0.025 +data.train_dataset = data.train_dataset[0:train_subset, :] +data.train_labels = data.train_labels[0:train_subset] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(None, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(None, num_labels)) + with tf.name_scope('hidden') as scope_1: + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hn_num]), name='weights') + biases_1 = tf.Variable(tf.zeros([hn_num]), name='biases') + print(tf.shape(weights_1)) + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + keep_prob = tf.placeholder('float') + hidden_layer_drop = tf.nn.dropout(hidden1, keep_prob) + with tf.name_scope('out') as scope_2: + weights = tf.Variable(tf.truncated_normal([hn_num, num_labels]), name='weights') + biases = tf.Variable(tf.zeros([num_labels]), name='biases') + print(tf.shape(weights)) + logits = tf.matmul(hidden_layer_drop, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + reg_beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) +num_steps = 5001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (data.train_labels.shape[0] - batch_size) + batch_data = data.train_dataset[offset:offset + batch_size, :] + batch_labels = data.train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(session.run(train_prediction, feed_dict={tf_train_dataset: data.valid_dataset, keep_prob: 1.0}), data.valid_labels)) + print(tf.nn.l2_loss(weights).eval()) + feed_dict = {tf_train_dataset: data.test_dataset, keep_prob: 1.0} + predictions = session.run(train_prediction, feed_dict=feed_dict) + print('Test accuracy: %.1f%%' % accuracy(predictions, data.test_labels)) diff --git a/dataset/preprocessed/307.txt b/dataset/preprocessed/307.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/307.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/308.txt b/dataset/preprocessed/308.txt new file mode 100644 index 0000000..12a2272 --- /dev/null +++ b/dataset/preprocessed/308.txt @@ -0,0 +1,116 @@ +""" import your model here """ +import joker as act_tf +import tensorflow as ans_tf +""" check the correctness of your model by comparing with tensorflow """ +import numpy as np +rrr = np.random.normal(size=(3 * 4 * 4 * 2,)).astype(np.float32) +sss = np.random.normal(size=(3 * 3 * 2 * 5,)).astype(np.float32) + + +def test_forward_conv(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.placeholder(tf.float32, shape=[3, 3, 2, 5]) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + res = sess.run(conv1, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2), w: np.arange(3 * 3 * 2 * 5).reshape(3, 3, 2, 5)}) + return res + + +def test_forward_conv2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.placeholder(tf.float32, shape=[3, 3, 2, 5]) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + res = sess.run(conv1, feed_dict={x: rrr.reshape(3, 4, 4, 2), w: sss.reshape(3, 3, 2, 5)}) + return res + + +def test_backward_conv(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(np.arange(3 * 3 * 2 * 5, dtype=np.float32).reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2)}) + res = w.eval() + return res + + +def test_backward_conv2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(sss.reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + sess.run(tf.global_variables_initializer()) + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + grad_W, = tf.gradients(loss, [w]) + res = grad_W.eval(feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_backward_conv3(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(sss.reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + sess.run(tf.global_variables_initializer()) + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + grad_W, = tf.gradients(loss, [x]) + res = grad_W.eval(feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_forward_maxpool(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + res = sess.run(pool1, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2)}) + return res + + +def test_forward_maxpool2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + res = sess.run(pool1, feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_backward_maxpool(tf): + with tf.Session() as sess: + x = tf.Variable(np.arange(3 * 4 * 4 * 2, dtype=np.float32).reshape(3, 4, 4, 2)) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + loss = tf.reduce_sum(pool1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op) + res = x.eval() + return res + + +def test_backward_maxpool2(tf): + with tf.Session() as sess: + x = tf.Variable(rrr.reshape(3, 4, 4, 2)) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + loss = tf.reduce_sum(pool1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op) + res = x.eval() + return res + + +if __name__ == '__main__': + testcases = [test_forward_conv, test_forward_conv2, test_backward_conv, test_backward_conv2, test_backward_conv3, test_forward_maxpool2, test_backward_maxpool2] + for item in testcases: + ans = item(ans_tf) + print() + print() + print(item) + res = item(act_tf) + np.testing.assert_allclose(res, ans, atol=0.01) + print('Pass') diff --git a/dataset/preprocessed/309.txt b/dataset/preprocessed/309.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/309.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/31.txt b/dataset/preprocessed/31.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/31.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/310.txt b/dataset/preprocessed/310.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/310.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/311.txt b/dataset/preprocessed/311.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/311.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/312.txt b/dataset/preprocessed/312.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/312.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/313.txt b/dataset/preprocessed/313.txt new file mode 100644 index 0000000..c548f1b --- /dev/null +++ b/dataset/preprocessed/313.txt @@ -0,0 +1,114 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import scipy.io as sio +import sys, os, os.path, inspect +filedir = inspect.getframeinfo(inspect.currentframe())[0] +filedir = os.path.dirname(os.path.abspath(filedir)) +mat_contents = sio.loadmat(filedir + '/sampleData_w4RegionLabels.mat') +train_dataset = np.array(mat_contents['train_data']).astype(np.float32) +train_labels = np.array(mat_contents['train_label']).astype(np.float32) +valid_dataset = np.array(mat_contents['test_data']).astype(np.float32) +valid_labels = np.array(mat_contents['test_label']).astype(np.float32) +print('Training set - ', train_dataset.shape, train_labels.shape) +print('Test set - ', valid_dataset.shape, ' ', valid_labels.shape) +sample_size = train_dataset.shape[1] +num_labels = train_labels.shape[1] + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +hidden_size = 512 +hidden_1_size = hidden_size +hidden_2_size = hidden_size +hidden_3_size = hidden_size +hidden_4_size = hidden_size +beta = 0.01 +SEED = None +dropoutPercent = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, sample_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_predict_label = tf.placeholder(tf.float32, shape=(1, sample_size)) + weights_1 = tf.Variable(tf.truncated_normal([sample_size, hidden_1_size], stddev=0.1), name='weights_1') + biases_1 = tf.Variable(tf.zeros([hidden_1_size]), name='biases_1') + weights_2 = tf.Variable(tf.truncated_normal([hidden_1_size, hidden_2_size]), name='weights_2') + biases_2 = tf.Variable(tf.zeros([hidden_2_size]), name='biases_2') + weights_3 = tf.Variable(tf.truncated_normal([hidden_2_size, hidden_3_size]), name='weights_3') + biases_3 = tf.Variable(tf.zeros([hidden_3_size]), name='biases_3') + weights_4 = tf.Variable(tf.truncated_normal([hidden_3_size, hidden_4_size]), name='weights_4') + biases_4 = tf.Variable(tf.zeros([hidden_4_size]), name='biases_4') + weights_o = tf.Variable(tf.truncated_normal([hidden_4_size, num_labels], stddev=0.1), name='weights_o') + biases_o = tf.Variable(tf.zeros([num_labels]), name='biases_o') + + def model(data, train=False): + hidden = tf.nn.relu(tf.matmul(data, weights_1) + biases_1) + if train: + hidden = tf.nn.dropout(hidden, dropoutPercent, seed=SEED) + hidden = tf.nn.relu(tf.matmul(hidden, weights_2) + biases_2) + if train: + hidden = tf.nn.dropout(hidden, dropoutPercent, seed=SEED) + hidden = tf.nn.relu(tf.matmul(hidden, weights_3) + biases_3) + if train: + hidden = tf.nn.dropout(hidden, dropoutPercent, seed=SEED) + return tf.matmul(hidden, weights_o) + biases_o + logits = model(tf_train_dataset, True) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + regularized_loss = loss + beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_4) + tf.nn.l2_loss(weights_o)) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.002, global_step, 250, 0.96) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(regularized_loss, global_step=global_step) + train_prediction = tf.nn.softmax(model(tf_train_dataset)) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + data_prediction = tf.nn.softmax(model(tf_predict_label)) + init_op = tf.initialize_all_variables() + saver = tf.train.Saver() +session = tf.Session(graph=graph) + + +def train(num_steps=15001, save=True, filename='model_regionDNN.ckpt'): + with session.as_default(): + session.run(init_op) + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f Learning_rate: %f' % (step, l, learning_rate.eval())) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + save_path = saver.save(session, filedir + '/' + filename, global_step=step) + if save: + save_path = saver.save(session, filedir + '/' + filename) + + +def init(): + with session.as_default(): + saver.restore(session, filedir + '/model_regionDNN.ckpt') + + +def predict(input_data): + with session.as_default(): + feed_dict = {tf_predict_label: input_data} + predictionArray = session.run([data_prediction], feed_dict=feed_dict) + prediction = np.argmax(predictionArray) + print('Region-Prediction: {}, np.argmax: {}'.format(predictionArray, prediction)) + return prediction + + +def close(): + session.close() + + +if __name__ == '__main__': + train(save=True) + close() diff --git a/dataset/preprocessed/314.txt b/dataset/preprocessed/314.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/314.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/315.txt b/dataset/preprocessed/315.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/315.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/316.txt b/dataset/preprocessed/316.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/316.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/317.txt b/dataset/preprocessed/317.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/317.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/319.txt b/dataset/preprocessed/319.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/319.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/32.txt b/dataset/preprocessed/32.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/32.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/320.txt b/dataset/preprocessed/320.txt new file mode 100644 index 0000000..9773d23 --- /dev/null +++ b/dataset/preprocessed/320.txt @@ -0,0 +1,370 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +""" +batch_size = 128 + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32,shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + + # Variables. + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + + # Training computation. + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels,logits=logits)) + beta_regul * tf.nn.l2_loss(weights) + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : 1e-3} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] + +# L2 regularization introduces a new meta parameter that should be tuned +for regul in regul_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) + + + +### L2 regularization with 1 layer Neural network ### +### increases efficiency significantly ### +batch_size = 128 +num_hidden_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32,shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + + # Variables. + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + + # Training computation. + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels,logits=logits)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) + +num_steps = 3001 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : 1e-3} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + + + +### Problem 2: extreme overfitting ### +batch_size = 128 +num_hidden_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + + # Variables. + weights1 = tf.Variable( + tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable( + tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + + # Training computation. + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) + +num_steps = 101 +num_bacthes = 3 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + #offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + offset = step % num_bacthes + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, beta_regul : 1e-3} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 2 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + + +### Problem 3 ### +### Introduce Dropout on the hidden layer of the neural network. Remember: Dropout should only be introduced during training, not evaluation, otherwise your evaluation results would be stochastic as well. TensorFlow provides nn.dropout() for that, but you have to make sure it's only inserted during training. +### What happens to our extreme overfitting case? +batch_size = 128 +num_hidden_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + # Variables. + weights1 = tf.Variable( + tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable( + tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + + # Training computation. + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + ##### introducing dropout #### + drop1 = tf.nn.dropout(lay1_train, 0.5) + logits = tf.matmul(drop1, weights2) + biases2 + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) + +num_steps = 101 +num_batches = 3 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + #offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + offset = step % num_batches + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 2 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) +""" +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_hidden_nodes3], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + weights4 = tf.Variable(tf.truncated_normal([num_hidden_nodes3, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes3))) + biases4 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + drop1 = tf.nn.dropout(lay1_train, 0.5) + lay2_train = tf.nn.relu(tf.matmul(drop1, weights2) + biases2) + drop2 = tf.nn.dropout(lay2_train, 0.5) + lay3_train = tf.nn.relu(tf.matmul(drop2, weights3) + biases3) + drop3 = tf.nn.dropout(lay3_train, 0.5) + logits = tf.matmul(drop3, weights4) + biases4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 4000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 9001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/321.txt b/dataset/preprocessed/321.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/321.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/322.txt b/dataset/preprocessed/322.txt new file mode 100644 index 0000000..b826592 --- /dev/null +++ b/dataset/preprocessed/322.txt @@ -0,0 +1,39 @@ +import tensorflow as tf + + +class NLPModel: + + def __init__(self, input_shape, output_classes, filters): + self.input = tf.placeholder(tf.float32, shape=[None, input_shape[0], input_shape[1], 1]) + self.labels = tf.placeholder(tf.float32, shape=[None, output_classes]) + self.dropout = tf.placeholder(tf.float32) + filter_count = 16 + filter_outputs = [] + for index, filter_length in enumerate(filters): + W = tf.Variable(tf.truncated_normal([filter_length, input_shape[1], 1, filter_count], stddev=0.1)) + b = tf.Variable(tf.constant(0.1, shape=[filter_count])) + conv_l1 = tf.nn.conv2d(self.input, W, strides=[1, 1, 1, 1], padding='VALID') + conv_pool = tf.nn.elu(tf.nn.bias_add(conv_l1, b)) + conv_pool = tf.nn.max_pool(conv_pool, ksize=[1, input_shape[0] - filter_length + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + filter_outputs.append(conv_pool) + total_filters = filter_count * len(filters) + final_pool = tf.concat(3, filter_outputs) + final_pool_flat = tf.reshape(final_pool, [-1, total_filters]) + drop = tf.nn.dropout(final_pool_flat, self.dropout) + W_fc = tf.Variable(tf.truncated_normal([total_filters, output_classes], stddev=0.1)) + b_fc = tf.Variable(tf.constant(0.1, shape=[output_classes])) + raw_scores = tf.nn.xw_plus_b(drop, W_fc, b_fc) + self.probabilities = tf.nn.softmax(raw_scores) + self.predictions = tf.argmax(self.probabilities, 1) + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.predictions, tf.argmax(self.labels, 1)), tf.float32)) + self.loss = tf.nn.softmax_cross_entropy_with_logits(raw_scores, self.labels) + self.optimize = tf.train.AdamOptimizer(0.0001).minimize(self.loss) + + def predict(self, session, inputs): + return session.run(self.predictions, feed_dict={self.input: inputs, self.dropout: 1.0})[0] + + def train(self, session, inputs, labels): + session.run(self.optimize, feed_dict={self.input: inputs, self.labels: labels, self.dropout: 0.75}) + + def get_accuracy(self, session, inputs, labels): + return session.run(self.accuracy, feed_dict={self.input: inputs, self.labels: labels, self.dropout: 1.0}) diff --git a/dataset/preprocessed/324.txt b/dataset/preprocessed/324.txt new file mode 100644 index 0000000..a0651da --- /dev/null +++ b/dataset/preprocessed/324.txt @@ -0,0 +1,136 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +def run_sgd_with_reg(): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta = tf.constant(0.005) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + reg_loss = loss + beta * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(reg_loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + num_steps = 3001 + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, reg_loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +def run_sgd_with_hidden_layer_with_reg(num_steps=3001, learning_rate=0.1, l2_reg_beta=0.0, dropout_keep_prob=0.5): + """Run SGD with a hidden layer and regularization. + + num_steps: + Number of steps to run SGD minibatch training. In each step a new + minibatch is fed through the network. + + learning_rate: + SGD learning rate, passed direction to GradientDescentOptimizer. + + l2_reg_beta: + The loss is regularized with L2 loss on all weights. This is the + regularization coefficient. Setting it to 0 means no L2 loss + regularization. Otherwise, small values work well. + + dropout_keep_prob: + Dropout "keep" probability for outputs of the hidden layer. Set to 1 to + avoid dropout altogether. + """ + batch_size = 128 + nfeatures = image_size * image_size + nhidden = 1024 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, nfeatures)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_dropout_keep_prob = tf.placeholder(tf.float32) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta = tf.constant(l2_reg_beta) + w1 = tf.Variable(tf.truncated_normal([nfeatures, nhidden])) + b1 = tf.Variable(tf.zeros([nhidden])) + w2 = tf.Variable(tf.truncated_normal([nhidden, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + hidden_out = tf.nn.dropout(tf.nn.relu(tf.matmul(tf_train_dataset, w1) + b1), keep_prob=tf_dropout_keep_prob) + logits = tf.matmul(hidden_out, w2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + reg_loss = loss + beta * tf.nn.l2_loss(w1) + beta * tf.nn.l2_loss(w2) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(learning_rate=learning_rate, global_step=global_step, decay_steps=1000, decay_rate=0.96) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(reg_loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, w1) + b1), w2) + b2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, w1) + b1), w2) + b2) + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_dropout_keep_prob: dropout_keep_prob} + _, l, predictions = session.run([optimizer, reg_loss, train_prediction], feed_dict=feed_dict) + if step % 100 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +if __name__ == '__main__': + run_sgd_with_hidden_layer_with_reg(num_steps=3711, learning_rate=0.01, l2_reg_beta=0.005, dropout_keep_prob=0.8) diff --git a/dataset/preprocessed/325.txt b/dataset/preprocessed/325.txt new file mode 100644 index 0000000..2940122 --- /dev/null +++ b/dataset/preprocessed/325.txt @@ -0,0 +1,134 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy(predictions, train_labels[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/326.txt b/dataset/preprocessed/326.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/326.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/327.txt b/dataset/preprocessed/327.txt new file mode 100644 index 0000000..627abfc --- /dev/null +++ b/dataset/preprocessed/327.txt @@ -0,0 +1,117 @@ +import tensorflow as tf +import numpy as np + + +class Model: + HIDDEN_UNIT_SIZE = 32 + HIDDEN_UNIT_SIZE2 = 6 + HIDDEN_UNIT_SIZE3 = 16 + INPUT_SIZE = 120 + + def __init__(self): + self.__setup_placeholder() + self.__setup_model() + self.__setup_ops() + + def __enter__(self): + self.session = tf.Session() + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.session.close() + return False + + def save(self, path): + saver = tf.train.Saver() + saver.save(self.session, path) + + def restore(self, path): + saver = tf.train.Saver() + saver.restore(self.session, path) + + def __setup_placeholder(self): + in_size = Model.INPUT_SIZE + self.trade_data = tf.placeholder('float', [None, in_size]) + self.actual_class = tf.placeholder('float', [None, 2]) + self.keep_prob = tf.placeholder('float') + self.label = tf.placeholder('string') + + def __setup_model(self): + in_size = Model.INPUT_SIZE + w1 = tf.Variable(tf.truncated_normal([in_size, Estimator.HIDDEN_UNIT_SIZE], stddev=0.1)) + b1 = tf.Variable(tf.constant(0.1, shape=[Estimator.HIDDEN_UNIT_SIZE])) + h1 = tf.nn.relu(tf.matmul(self.trade_data, w1) + b1) + w2 = tf.Variable(tf.truncated_normal([Estimator.HIDDEN_UNIT_SIZE, Estimator.HIDDEN_UNIT_SIZE2], stddev=0.1)) + b2 = tf.Variable(tf.constant(0.1, shape=[Estimator.HIDDEN_UNIT_SIZE2])) + h2 = tf.nn.relu(tf.matmul(h1, w2) + b2) + w3 = tf.Variable(tf.truncated_normal([Estimator.HIDDEN_UNIT_SIZE2, Estimator.HIDDEN_UNIT_SIZE3], stddev=0.1)) + b3 = tf.Variable(tf.constant(0.1, shape=[Estimator.HIDDEN_UNIT_SIZE3])) + h3 = tf.nn.relu(tf.matmul(h2, w3) + b3) + h3_drop = tf.nn.dropout(h3, self.keep_prob) + w3 = tf.Variable(tf.truncated_normal([Estimator.HIDDEN_UNIT_SIZE3, 2], stddev=0.1)) + b3 = tf.Variable(tf.constant(0.1, shape=[2])) + self.output = tf.nn.softmax(tf.matmul(h3_drop, w3) + b3) + + def __setup_ops(self): + cross_entropy = -tf.reduce_sum(self.actual_class * tf.log(self.output)) + self._summary = tf.summary.scalr(self.label, cross_entropy) + self.train_op = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + self.merge_summaries = tf.summary.merge([self._summary]) + correct_prediction = tf.equal(tf.argmax(self.output, 1), tf.argmax(self.actual_class, 1)) + self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + + +class Trainer(Model): + + def train(self, steps, data): + self.__prepare_train(self.session) + for i in range(steps): + self.__do_train(self.session, i, data) + if i % 100 == 0: + self.__add_summary(self.session, i, data) + self.__print_status(self.session, i, data) + + def __prepare_train(self, session): + self.summary_writer = tf.summary.FileWriter('logs', graph_def=session.graph_def) + session.run(tf.initialize_all_variables()) + + def __do_train(self, session, i, data): + session.run(self.train_op, feed_dict=self.train_feed_dict(data)) + + def __add_summary(self, session, i, data): + summary_str = session.run(self.merge_summaries, feed_dict=self.train_feed_dict(data)) + summary_str += session.run(self.merge_summaries, feed_dict=self.test_feed_dict(data)) + self.summary_writer.add_summary(summary_str, i) + + def __print_status(self, session, i, data): + train_accuracy = session.run(self.accuracy, feed_dict=self.train_feed_dict(data)) + test_accuracy = session.run(self.accuracy, feed_dict=self.test_feed_dict(data)) + print('step {0} ,train_accuracy={1} ,test_accuracy={2} '.format(i, train_accuracy, test_accuracy)) + + def train_feed_dict(self, data): + return {self.trade_data: data.train_data(), self.actual_class: data.train_up_down(), self.keep_prob: 0.8, self.label: 'train'} + + def test_feed_dict(self, data): + return {self.trade_data: data.test_data(), self.actual_class: data.test_up_down(), self.keep_prob: 1, self.label: 'test'} + + def __reshape(self, profit_or_loss): + return profit_or_loss.values.reshape(len(profit_or_loss.values), 1) + + +class Estimator(Model): + + def estimate(self, data): + return self.session.run(tf.argmax(self.output, 1), feed_dict=self.estimate_feed_dict(data)) + + def estimate_feed_dict(self, data): + return {self.trade_data: data, self.keep_prob: 1} + + +def main(): + with Trainer() as trainer: + trainer.train(10001, data) + trainer.save('./model.ckpt') + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/328.txt b/dataset/preprocessed/328.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/328.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/329.txt b/dataset/preprocessed/329.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/329.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/33.txt b/dataset/preprocessed/33.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/33.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/330.txt b/dataset/preprocessed/330.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/330.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/331.txt b/dataset/preprocessed/331.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/331.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/332.txt b/dataset/preprocessed/332.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/332.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/333.txt b/dataset/preprocessed/333.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/333.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/334.txt b/dataset/preprocessed/334.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/334.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/335.txt b/dataset/preprocessed/335.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/335.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/336.txt b/dataset/preprocessed/336.txt new file mode 100644 index 0000000..fe83d86 --- /dev/null +++ b/dataset/preprocessed/336.txt @@ -0,0 +1,71 @@ +import numpy as np +from math import sin, cos +from RLlib import NN +import tensorflow as tf +from collections import deque +import random +MAX_REPLAY_SIZE = 10000 +DISCOUNTFACTOR = 0.99 +CONTROL_DIM = 1 +STATE_DIM = 1 + + +class RLagent: + + def __init__(self): + self.discount_factor = DISCOUNTFACTOR + self.Q_out = tf.placeholder(tf.float32, [None, 1]) + self.s_next = tf.placeholder(tf.float32, [None, STATE_DIM]) + self.action = tf.placeholder(tf.float32, [None, CONTROL_DIM]) + self.s = tf.placeholder(tf.float32, [None, STATE_DIM]) + self.mu_out = tf.placeholder(tf.float32, [None, CONTROL_DIM]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.Q_NN = NN(tf.concat([self.s, self.action], 1), self.Q_out, [16, 16], CONTROL_DIM + STATE_DIM, 1) + self.mu_NN = NN(self.s, self.mu_out, [1, 1], STATE_DIM, CONTROL_DIM) + self.target_mu_NN = NN(self.s_next, self.mu_out, [1, 1], STATE_DIM, CONTROL_DIM) + self.target_Q_NN = NN(tf.concat([self.s_next, self.target_mu_NN.y_output], 1), self.Q_out, [16, 16], CONTROL_DIM + STATE_DIM, 1) + self.yt_pl = self.reward + DISCOUNTFACTOR * self.target_Q_NN.y_output + self.losses = tf.squared_difference(self.yt_pl, self.Q_NN.y_output) + self.L = tf.reduce_mean(self.losses) + self.optimizer = tf.train.RMSPropOptimizer(0.00025, 0.99, 0.0, 1e-06) + self.train_op = self.optimizer.minimize(self.L, global_step=tf.contrib.framework.get_global_step()) + copy_op = self.update_target_network(1, self.mu_NN, self.target_mu_NN) + self.R = Replay(MAX_REPLAY_SIZE) + init = tf.global_variables_initializer() + self.sess = tf.Session() + self.sess.run(init) + self.sess.run(copy_op) + + def update_target_network(self, tau, N, N_target): + var_copy = [] + for var, var_target in zip(N.params, N_target.params): + var_copy.append(var_target.assign(tau * var + (1 - tau) * var_target)) + return tf.group(*var_copy) + + def do_action(self, state): + return self.sess.run(self.mu_NN.y_output, {self.s: state}) + + def do_action_target(self, state): + return self.sess.run(self.target_mu_NN.y_output, {self.s: state}) + + def update_critic(self, state, action, next_state, reward): + dictionary = {self.reward: reward, self.s_next: next_state, self.s: state, self.action: action} + self.sess.run(self.train_op, dictionary) + + +class Replay: + + def __init__(self, max_size): + self.max_size = max_size + self.transitions = deque() + + def add(self, observation, action, reward, observation2): + if len(self.transitions) > self.max_size: + self.transitions.popleft() + self.transitions.append((observation, action, reward, observation2)) + + def sample(self, count): + return random.sample(self.transitions, count) + + def size(self): + return len(self.transitions) diff --git a/dataset/preprocessed/337.txt b/dataset/preprocessed/337.txt new file mode 100644 index 0000000..12a2272 --- /dev/null +++ b/dataset/preprocessed/337.txt @@ -0,0 +1,116 @@ +""" import your model here """ +import joker as act_tf +import tensorflow as ans_tf +""" check the correctness of your model by comparing with tensorflow """ +import numpy as np +rrr = np.random.normal(size=(3 * 4 * 4 * 2,)).astype(np.float32) +sss = np.random.normal(size=(3 * 3 * 2 * 5,)).astype(np.float32) + + +def test_forward_conv(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.placeholder(tf.float32, shape=[3, 3, 2, 5]) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + res = sess.run(conv1, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2), w: np.arange(3 * 3 * 2 * 5).reshape(3, 3, 2, 5)}) + return res + + +def test_forward_conv2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.placeholder(tf.float32, shape=[3, 3, 2, 5]) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + res = sess.run(conv1, feed_dict={x: rrr.reshape(3, 4, 4, 2), w: sss.reshape(3, 3, 2, 5)}) + return res + + +def test_backward_conv(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(np.arange(3 * 3 * 2 * 5, dtype=np.float32).reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2)}) + res = w.eval() + return res + + +def test_backward_conv2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(sss.reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + sess.run(tf.global_variables_initializer()) + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + grad_W, = tf.gradients(loss, [w]) + res = grad_W.eval(feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_backward_conv3(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(sss.reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + sess.run(tf.global_variables_initializer()) + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + grad_W, = tf.gradients(loss, [x]) + res = grad_W.eval(feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_forward_maxpool(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + res = sess.run(pool1, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2)}) + return res + + +def test_forward_maxpool2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + res = sess.run(pool1, feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_backward_maxpool(tf): + with tf.Session() as sess: + x = tf.Variable(np.arange(3 * 4 * 4 * 2, dtype=np.float32).reshape(3, 4, 4, 2)) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + loss = tf.reduce_sum(pool1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op) + res = x.eval() + return res + + +def test_backward_maxpool2(tf): + with tf.Session() as sess: + x = tf.Variable(rrr.reshape(3, 4, 4, 2)) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + loss = tf.reduce_sum(pool1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op) + res = x.eval() + return res + + +if __name__ == '__main__': + testcases = [test_forward_conv, test_forward_conv2, test_backward_conv, test_backward_conv2, test_backward_conv3, test_forward_maxpool2, test_backward_maxpool2] + for item in testcases: + ans = item(ans_tf) + print() + print() + print(item) + res = item(act_tf) + np.testing.assert_allclose(res, ans, atol=0.01) + print('Pass') diff --git a/dataset/preprocessed/338.txt b/dataset/preprocessed/338.txt new file mode 100644 index 0000000..a306078 --- /dev/null +++ b/dataset/preprocessed/338.txt @@ -0,0 +1,1692 @@ +import numpy as np +import tensorflow as tf +rnn_cell = {'rnn': tf.nn.rnn_cell.BasicRNNCell, 'lstm': tf.nn.rnn_cell.LSTMCell, 'gru': tf.nn.rnn_cell.GRUCell} +opt_method = {'sgd': tf.train.GradientDescentOptimizer, 'adam': tf.train.AdamOptimizer, 'adadelta': tf.train.AdadeltaOptimizer, 'adagrad': tf.train.AdagradOptimizer} + + +class Cnn(object): + """ + Basic CNN model. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'Cnn' + self.max_sentence_len = setting.sen_len + self.filter_sizes = setting.filter_sizes + self.filter_num = setting.filter_num + self.class_num = setting.class_num + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [setting.pos_num, setting.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [setting.pos_num, setting.pos_size]) + self.embed_size_pos = setting.pos_size + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_all_expanded = tf.expand_dims(self.emb_all, -1) + with tf.name_scope('conv_maxpooling'): + pooled_outputs = [] + for i, filter_size in enumerate(self.filter_sizes): + with tf.name_scope('conv-maxpool-{}'.format(filter_size)): + filter_shape = [filter_size, self.embed_size_x + 2 * self.embed_size_pos, 1, self.filter_num] + w = tf.get_variable('W_{}'.format(filter_size), filter_shape, initializer=tf.truncated_normal_initializer(stddev=0.1)) + b = tf.get_variable('b_{}'.format(filter_size), [self.filter_num], initializer=tf.constant_initializer(0.1)) + tf.summary.histogram('W_{}'.format(filter_size), w) + tf.summary.histogram('b_{}'.format(filter_size), b) + conv = tf.nn.conv2d(self.emb_all_expanded, w, strides=[1, 1, 1, 1], padding='VALID', name='conv') + h = tf.nn.relu(tf.nn.bias_add(conv, b), name='relu') + pooled = tf.nn.max_pool(h, ksize=[1, self.max_sentence_len - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='conv') + pooled_outputs.append(pooled) + num_filters_total = self.filter_num * len(self.filter_sizes) + h_pool = tf.concat(pooled_outputs, 3) + h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + self.outputs = tf.nn.dropout(h_pool_flat, self.dropout_keep_rate) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [self.filter_num * len(self.filter_sizes), self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + self.fc_out = tf.matmul(self.outputs, self.fc_w) + self.fc_b + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_out) + with tf.name_scope('accuracy'): + self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + self.class_label = tf.argmax(self.softmax_res, 1) + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.class_label, tf.argmax(self.input_labels, 1)), 'float'), name='accuracy') + tf.summary.scalar('accuracy', self.accuracy) + with tf.name_scope('model_predict'): + self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_out, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class PCnn(object): + """ + PCNN model. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'PCnn' + self.max_sentence_len = setting.sen_len + self.filter_sizes = setting.filter_sizes + self.filter_num = setting.filter_num + self.class_num = setting.class_num + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.input_mask = tf.placeholder(tf.float32, [None, 3, self.max_sentence_len], name='input_mask') + self.pcnn_mask = tf.expand_dims(tf.transpose(self.input_mask, [0, 2, 1]), axis=1) + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [setting.pos_num, setting.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [setting.pos_num, setting.pos_size]) + self.embed_size_pos = setting.pos_size + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.conv_input = tf.expand_dims(self.emb_all, 1) + with tf.name_scope('conv_maxpooling'): + pooled_outputs = [] + for i, filter_size in enumerate(self.filter_sizes): + with tf.name_scope('conv-maxpool-{}'.format(filter_size)): + with tf.name_scope('conv-{}'.format(filter_size)): + conv_out = tf.squeeze(tf.nn.relu(tf.layers.conv2d(self.conv_input, self.filter_num, [1, filter_size], padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer_conv2d()))) + conv_out = tf.expand_dims(tf.transpose(conv_out, [0, 2, 1]), axis=-1) + with tf.name_scope('maxpool-{}'.format(filter_size)): + pcnn_pool = tf.reduce_max(conv_out * self.pcnn_mask, axis=2) + pcnn_pool = tf.reshape(pcnn_pool, [-1, self.filter_num * 3]) + pooled_outputs.append(pcnn_pool) + feature_size = self.filter_num * 3 * len(self.filter_sizes) + h_pool = tf.concat(pooled_outputs, 3) + h_pool_flat = tf.reshape(h_pool, [-1, feature_size]) + self.outputs = tf.nn.dropout(h_pool_flat, self.dropout_keep_rate) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [feature_size, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + self.fc_out = tf.matmul(self.outputs, self.fc_w) + self.fc_b + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_out) + with tf.name_scope('accuracy'): + self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + self.class_label = tf.argmax(self.softmax_res, 1) + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.class_label, tf.argmax(self.input_labels, 1)), 'float'), name='accuracy') + tf.summary.scalar('accuracy', self.accuracy) + with tf.name_scope('model_predict'): + self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_out, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_mask: input_data.mask, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_mask: input_data.mask, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class Cnn_Deep(object): + """ + Multi-layer CNN model. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'Cnn_Deep' + with tf.name_scope('model_input'): + self.max_sentence_len = setting.sen_len + self.filter_sizes = setting.filter_sizes + self.filter_num = setting.filter_num + self.max_pool_sizes = setting.max_pool_sizes + self.fc_sizes = setting.fc_sizes + self.class_num = setting.class_num + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_mask = setting.dropout_mask + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [setting.pos_num, setting.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [setting.pos_num, setting.pos_size]) + self.embed_size_pos = setting.pos_size + with tf.name_scope('embedding_layer'): + self.emb_sen = tf.reshape(tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen), [-1, self.max_sentence_len, self.embed_size_x]) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_all_expanded = tf.expand_dims(self.emb_all, -1) + with tf.name_scope('conv_maxpooling'): + nl_input = self.emb_all_expanded + for i in range(len(self.filter_sizes)): + filter_size = self.filter_sizes[i] + filter_num = self.filter_num[i] + with tf.name_scope('conv_{}'.format(i)): + filter_shape = [filter_size, nl_input.shape[2], 1, filter_num] + w = tf.get_variable('W_conv_{}'.format(i), filter_shape, initializer=tf.truncated_normal_initializer(stddev=0.05)) + b = tf.get_variable('b_conv_{}'.format(i), [filter_num], initializer=tf.truncated_normal_initializer(stddev=0.05)) + tf.summary.histogram('W_conv_{}'.format(i), w) + tf.summary.histogram('b_conv_{}'.format(i), b) + conv = tf.nn.conv2d(nl_input, w, strides=[1, 1, 1, 1], padding='VALID', name='conv') + nl_input = tf.transpose(tf.nn.relu(tf.nn.bias_add(conv, b), name='relu'), [0, 1, 3, 2]) + if self.max_pool_sizes[i] != 0: + with tf.name_scope('max_pool_{}'.format(i)): + max_pool_size = self.max_pool_sizes[i] + nl_input = tf.nn.max_pool(nl_input, ksize=[1, max_pool_size[0], max_pool_size[1], 1], strides=[1, 1, 1, 1], padding='VALID', name='max_pool_{}'.format(i)) + if self.dropout_mask[i] != 0: + self.nl_input = tf.nn.dropout(nl_input, self.dropout_keep_rate) + with tf.name_scope('fc_layers'): + nl_input = tf.reshape(nl_input, [-1, int(nl_input.shape[1]) * int(nl_input.shape[2])]) + for i in range(len(self.fc_sizes)): + with tf.name_scope('fc_layer_{}'.format(i)): + fc_w = tf.get_variable('fc_W_{}'.format(i), [nl_input.shape[-1], self.fc_sizes[i]], initializer=tf.truncated_normal_initializer(stddev=0.05)) + fc_b = tf.get_variable('fc_b_{}'.format(i), [self.fc_sizes[i]], initializer=tf.truncated_normal_initializer(stddev=0.05)) + tf.summary.histogram('fc_W_{}'.format(i), fc_w) + tf.summary.histogram('fc_b_{}'.format(i), fc_b) + nl_input = tf.matmul(nl_input, fc_w) + fc_b + if self.dropout_mask[i - len(self.filter_sizes)] != 0: + self.nl_input = tf.nn.dropout(nl_input, self.dropout_keep_rate) + self.fc_out = nl_input + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_out) + with tf.name_scope('accuracy'): + self.class_label = tf.argmax(self.softmax_res, 1) + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.class_label, tf.argmax(self.input_labels, 1)), 'float'), name='accuracy') + tf.summary.scalar('accuracy', self.accuracy) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_out, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + input_x = input_data.x + feed_dict = {self.input_sen: input_x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + input_x = input_data.x + feed_dict = {self.input_sen: input_x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class Rnn(object): + """ + Basic Rnn model. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'Rnn' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.hidden_size = setting.hidden_size + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('rnn_layer'): + self.rnn_cell = rnn_cell[self.cell_type](self.hidden_size) + self.rnn_cell = tf.nn.rnn_cell.DropoutWrapper(self.rnn_cell, output_keep_prob=self.dropout_keep_rate) + self.outputs, self.states = tf.contrib.rnn.static_rnn(self.rnn_cell, self.emb_all_us, dtype=tf.float32) + if setting.hidden_select == 'last': + self.output_final = self.outputs[-1] + elif setting.hidden_select == 'avg': + self.output_final = tf.reduce_mean(tf.reshape(tf.concat(self.outputs, 1), [-1, self.max_sentence_len, self.hidden_size]), axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [self.hidden_size, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.output_final, self.fc_w) + self.fc_b + tf.summary.histogram('fc_w', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn(object): + """ + Bidirectional RNN model. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.hidden_size = setting.hidden_size + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('rnn_layer'): + self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.foward_cell = tf.nn.rnn_cell.DropoutWrapper(self.foward_cell, output_keep_prob=self.dropout_keep_rate) + self.backward_cell = tf.nn.rnn_cell.DropoutWrapper(self.backward_cell, output_keep_prob=self.dropout_keep_rate) + self.rnn_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(self.foward_cell, self.backward_cell, self.emb_all_us, dtype=tf.float32) + if setting.hidden_select == 'last': + self.rnn_output = self.rnn_outputs[-1] + elif setting.hidden_select == 'avg': + self.rnn_output = tf.reduce_mean(tf.reshape(tf.concat(self.rnn_outputs, 1), [-1, self.max_sentence_len, self.hidden_size * 2]), axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [self.hidden_size * 2, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rnn_output, self.fc_w) + self.fc_b + tf.summary.histogram('fc_w', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_output = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_output, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_output], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Deep(object): + """ + Bidirectional Deep RNN model. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Deep' + self.cell_type = setting.cells + self.max_sentence_len = setting.sen_len + self.hidden_sizes = setting.hidden_sizes + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + with tf.name_scope('rnn_layer'): + outputs_list = [] + for layer in range(len(self.cell_type)): + with tf.name_scope('birnn_layer_{}'.format(layer)): + with tf.variable_scope('birnn_layer_{}'.format(layer)): + foward_cell = rnn_cell[self.cell_type[layer]](self.hidden_sizes[layer]) + backward_cell = rnn_cell[self.cell_type[layer]](self.hidden_sizes[layer]) + layer_output, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_all, dtype=tf.float32) + outputs_list.append(layer_output) + self.rnn_output = tf.reduce_mean(outputs_list[-1], axis=1) + with tf.name_scope('fc_layer'): + fc_w = tf.get_variable('fc_W', [self.hidden_sizes[-1] * 2, self.class_num]) + fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rnn_output, fc_w) + fc_b + tf.summary.histogram('fc_W', fc_w) + tf.summary.histogram('fc_b', fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('pred'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Att(object): + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Att' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.hidden_size = setting.hidden_size + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('rnn_layer'): + with tf.name_scope('birnn'): + self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.foward_cell = tf.nn.rnn_cell.DropoutWrapper(self.foward_cell, output_keep_prob=self.dropout_keep_rate) + self.backward_cell = tf.nn.rnn_cell.DropoutWrapper(self.backward_cell, output_keep_prob=self.dropout_keep_rate) + rnn_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(self.foward_cell, self.backward_cell, self.emb_all_us, dtype=tf.float32) + outputs_forward = [i[:, :self.hidden_size] for i in rnn_outputs] + outputs_backward = [i[:, self.hidden_size:] for i in rnn_outputs] + output_forward = tf.reshape(tf.concat(axis=1, values=outputs_forward), [-1, self.max_sentence_len, self.hidden_size]) + output_backward = tf.reshape(tf.concat(axis=1, values=outputs_backward), [-1, self.max_sentence_len, self.hidden_size]) + self.rnn_outputs = tf.add(output_forward, output_backward) + with tf.name_scope('attention'): + self.attention_w = tf.get_variable('attention_omega', [self.hidden_size, 1]) + self.attention_A = tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.rnn_outputs), [-1, self.hidden_size]), self.attention_w), [-1, self.max_sentence_len])), [-1, 1, self.max_sentence_len]) + self.rnn_output = tf.reshape(tf.matmul(self.attention_A, self.rnn_outputs), [-1, self.hidden_size]) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [self.hidden_size, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rnn_output, self.fc_w) + self.fc_b + tf.summary.histogram('fc_w', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_output = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_output, 1) + with tf.name_scope('model_loss'): + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.l2_regular = tf.contrib.layers.apply_regularization(regularizer=tf.contrib.layers.l2_regularizer(0.0001), weights_list=tf.trainable_variables()) + self.model_loss = tf.reduce_mean(self.loss) + self.l2_regular + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_output], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_SelfAtt(object): + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_SelfAtt' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.hidden_size = setting.hidden_size + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('rnn_layer'): + with tf.name_scope('birnn'): + self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.foward_cell = tf.nn.rnn_cell.DropoutWrapper(self.foward_cell, output_keep_prob=self.dropout_keep_rate) + self.backward_cell = tf.nn.rnn_cell.DropoutWrapper(self.backward_cell, output_keep_prob=self.dropout_keep_rate) + self.rnn_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(self.foward_cell, self.backward_cell, self.emb_all_us, dtype=tf.float32) + self.output_h = tf.reshape(tf.concat(self.rnn_outputs, 1), [-1, self.max_sentence_len, self.hidden_size * 2]) + with tf.name_scope('attention'): + self.attention_Ws1 = tf.get_variable('attention_Ws1', [self.hidden_size * 2, setting.da]) + self.attention_Ws2 = tf.get_variable('attention_Ws2', [setting.da, setting.r]) + self.attention_A = tf.nn.softmax(tf.transpose(tf.reshape(tf.matmul(tf.tanh(tf.matmul(tf.reshape(self.output_h, [-1, self.hidden_size * 2]), self.attention_Ws1)), self.attention_Ws2), [-1, self.max_sentence_len, setting.r]), [0, 2, 1])) + self.M = tf.matmul(self.attention_A, self.output_h) + tf.summary.histogram('Ws1', self.attention_Ws1) + tf.summary.histogram('Ws2', self.attention_Ws2) + with tf.name_scope('full_connection'): + self.vec_M = tf.reshape(self.M, [-1, self.hidden_size * 2 * setting.r]) + self.fc_w = tf.get_variable('fc_W', [self.hidden_size * 2 * setting.r, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.sen_rep = tf.matmul(self.vec_M, self.fc_w) + self.fc_b + tf.summary.histogram('vec_M', self.vec_M) + tf.summary.histogram('fc_w', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_output = tf.nn.softmax(self.sen_rep) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_output, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.sen_rep, labels=self.input_labels) + self.P_att_matrix = tf.matmul(self.attention_A, tf.transpose(self.attention_A, [0, 2, 1])) - tf.eye(setting.r, setting.r) + self.P_att = tf.pow(tf.norm(self.P_att_matrix), 2) + self.model_loss = tf.reduce_mean(self.instance_loss) + 0.0001 * self.P_att + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_output], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Res(object): + """ + Bidirectional Residual RNN model. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Res' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.hidden_size = setting.hidden_size + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + with tf.name_scope('rnn_layer'): + outputs_list = [] + for layer in range(2): + with tf.name_scope('birnn_layer_{}'.format(layer)): + with tf.variable_scope('birnn_layer_{}'.format(layer)): + foward_cell = rnn_cell[self.cell_type](self.hidden_size) + backward_cell = rnn_cell[self.cell_type](self.hidden_size) + layer_output, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_all, dtype=tf.float32) + outputs_list.append(layer_output) + outputs = outputs_list[0] + outputs_list[1] + with tf.name_scope('max_pooling'): + pooled = tf.nn.max_pool(tf.expand_dims(outputs, -1), [1, self.max_sentence_len, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + mp_out = tf.squeeze(pooled) + with tf.name_scope('fc_layer'): + fc_w = tf.get_variable('fc_W', [self.hidden_size * 2, self.class_num]) + fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(mp_out, fc_w) + fc_b + tf.summary.histogram('fc_W', fc_w) + tf.summary.histogram('fc_b', fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('pred'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Ent(object): + """ + Bidirectional RNN model with entity description. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Ent' + self.cell_type = setting.cell + self.max_sen_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_e1_us = tf.unstack(self.emb_e1, num=self.max_ent_len, axis=1) + self.emb_e2_us = tf.unstack(self.emb_e2, num=self.max_ent_len, axis=1) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sen_len, axis=1) + with tf.name_scope('sentence_encoder'): + with tf.variable_scope('sentence_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + foward_cell = tf.nn.rnn_cell.DropoutWrapper(foward_cell, output_keep_prob=self.dropout_keep_rate) + backward_cell = tf.nn.rnn_cell.DropoutWrapper(backward_cell, output_keep_prob=self.dropout_keep_rate) + sen_output, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_all_us, dtype=tf.float32) + if setting.hidden_select == 'last': + self.sent_output = sen_output[-1] + elif setting.hidden_select == 'avg': + self.sent_output = tf.reduce_mean(tf.reshape(tf.concat(sen_output, 1), [-1, self.max_sen_len, self.hidden_size_sen * 2]), axis=1) + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e1_us, dtype=tf.float32) + ent2_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e2_us, dtype=tf.float32) + ent1_output = tf.reduce_mean(tf.reshape(tf.concat(ent1_outputs, 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis=1) + ent2_output = tf.reduce_mean(tf.reshape(tf.concat(ent2_outputs, 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis=1) + self.ent_out = tf.concat([ent1_output, ent2_output], axis=1) + with tf.name_scope('joint_layer'): + self.rep = tf.concat([self.sent_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [self.hidden_size_sen * 2 + self.hidden_size_ent * 4, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Att_Ent(object): + """ + Bidirectional RNN model with attention and entity spell. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Att_Ent' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_e1_us = tf.unstack(self.emb_e1, num=self.max_ent_len, axis=1) + self.emb_e2_us = tf.unstack(self.emb_e2, num=self.max_ent_len, axis=1) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('sentence_encoder'): + with tf.variable_scope('sentence_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + foward_cell = tf.nn.rnn_cell.DropoutWrapper(foward_cell, output_keep_prob=self.dropout_keep_rate) + backward_cell = tf.nn.rnn_cell.DropoutWrapper(backward_cell, output_keep_prob=self.dropout_keep_rate) + rnn_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_all_us, dtype=tf.float32) + self.sent_outputs = tf.reshape(tf.concat(rnn_outputs, axis=1), [-1, self.max_sentence_len, self.hidden_size_sen * 2]) + with tf.name_scope('attention'): + self.attention_w = tf.get_variable('attention_omega', [self.sent_outputs.shape[-1], 1]) + self.attention_A = tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]), self.attention_w), [-1, self.max_sentence_len])), [-1, 1, self.max_sentence_len]) + self.att_output = tf.reshape(tf.matmul(self.attention_A, self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]) + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e1_us, dtype=tf.float32) + e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + e1_outputs_forward = tf.reshape(tf.concat(axis=1, values=e1_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs_backward = tf.reshape(tf.concat(axis=1, values=e1_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + e1_output = tf.reduce_mean(e1_outputs, 1) + ent2_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e2_us, dtype=tf.float32) + e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + e2_output_forward = tf.reshape(tf.concat(axis=1, values=e2_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_output_backward = tf.reshape(tf.concat(axis=1, values=e2_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_outputs = tf.add(e2_output_forward, e2_output_backward) + e2_output = tf.reduce_mean(e2_outputs, 1) + self.ent_out = tf.concat([e1_output, e2_output], axis=1) + self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + with tf.name_scope('joint_layer'): + self.rep = tf.concat([self.att_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [int(self.att_output.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_SelfAtt_Ent(object): + """ + Bidirectional RNN model with attention and entity spell. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_SelfAtt_Ent' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_e1_us = tf.unstack(self.emb_e1, num=self.max_ent_len, axis=1) + self.emb_e2_us = tf.unstack(self.emb_e2, num=self.max_ent_len, axis=1) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('sentence_encoder'): + with tf.variable_scope('sentence_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + foward_cell = tf.nn.rnn_cell.DropoutWrapper(foward_cell, output_keep_prob=self.dropout_keep_rate) + backward_cell = tf.nn.rnn_cell.DropoutWrapper(backward_cell, output_keep_prob=self.dropout_keep_rate) + rnn_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_all_us, dtype=tf.float32) + self.sent_outputs = tf.reshape(tf.concat(rnn_outputs, axis=1), [-1, self.max_sentence_len, self.hidden_size_sen * 2]) + with tf.name_scope('attention'): + self.attention_Ws1 = tf.get_variable('attention_Ws1', [self.hidden_size_sen * 2, setting.da]) + self.attention_Ws2 = tf.get_variable('attention_Ws2', [setting.da, setting.r]) + self.attention_A = tf.nn.softmax(tf.transpose(tf.reshape(tf.matmul(tf.tanh(tf.matmul(tf.reshape(self.sent_outputs, [-1, self.hidden_size_sen * 2]), self.attention_Ws1)), self.attention_Ws2), [-1, self.max_sentence_len, setting.r]), [0, 2, 1])) + self.M = tf.matmul(self.attention_A, self.sent_outputs) + self.att_output = tf.reshape(self.M, [-1, self.hidden_size_sen * 2 * setting.r]) + tf.summary.histogram('Ws1', self.attention_Ws1) + tf.summary.histogram('Ws2', self.attention_Ws2) + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e1_us, dtype=tf.float32) + e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + e1_outputs_forward = tf.reshape(tf.concat(axis=1, values=e1_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs_backward = tf.reshape(tf.concat(axis=1, values=e1_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + e1_output = tf.reduce_mean(e1_outputs, 1) + ent2_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e2_us, dtype=tf.float32) + e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + e2_output_forward = tf.reshape(tf.concat(axis=1, values=e2_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_output_backward = tf.reshape(tf.concat(axis=1, values=e2_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_outputs = tf.add(e2_output_forward, e2_output_backward) + e2_output = tf.reduce_mean(e2_outputs, 1) + self.ent_out = tf.concat([e1_output, e2_output], axis=1) + self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + with tf.name_scope('joint_layer'): + self.rep = tf.concat([self.att_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [int(self.att_output.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_cAtt_Ent(object): + """ + Bidirectional RNN model with attention and entity spell. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_cAtt_Ent' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_e1_us = tf.unstack(self.emb_e1, num=self.max_ent_len, axis=1) + self.emb_e2_us = tf.unstack(self.emb_e2, num=self.max_ent_len, axis=1) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('sentence_encoder'): + with tf.variable_scope('sentence_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + foward_cell = tf.nn.rnn_cell.DropoutWrapper(foward_cell, output_keep_prob=self.dropout_keep_rate) + backward_cell = tf.nn.rnn_cell.DropoutWrapper(backward_cell, output_keep_prob=self.dropout_keep_rate) + rnn_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_all_us, dtype=tf.float32) + self.sent_outputs = tf.reshape(tf.concat(rnn_outputs, axis=1), [-1, self.max_sentence_len, self.hidden_size_sen * 2]) + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e1_us, dtype=tf.float32) + e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + e1_outputs_forward = tf.reshape(tf.concat(axis=1, values=e1_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs_backward = tf.reshape(tf.concat(axis=1, values=e1_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + e1_output = tf.reduce_mean(e1_outputs, 1) + ent2_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e2_us, dtype=tf.float32) + e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + e2_output_forward = tf.reshape(tf.concat(axis=1, values=e2_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_output_backward = tf.reshape(tf.concat(axis=1, values=e2_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_outputs = tf.add(e2_output_forward, e2_output_backward) + e2_output = tf.reduce_mean(e2_outputs, 1) + self.ent_out = tf.concat([e1_output, e2_output], axis=1) + self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + with tf.name_scope('attention_layer'): + self.attention_w = tf.get_variable('attention_omega', [self.sent_outputs.shape[-1], self.ent_att.shape[-2]]) + self.attention_A = tf.reshape(tf.nn.softmax(tf.squeeze(tf.matmul(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]), self.attention_w), [-1, self.max_sentence_len, self.hidden_size_ent]), self.ent_att))), [-1, 1, self.max_sentence_len]) + self.att_output = tf.reshape(tf.matmul(self.attention_A, self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]) + with tf.name_scope('joint_layer'): + self.rep = tf.concat([self.att_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [int(self.sent_outputs.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Cnn_Ent(object): + """ + A model use birnn and cnn. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Cnn_Ent' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.filter_sizes = setting.filter_sizes + self.filter_num = setting.filter_num + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_sen_us = tf.unstack(self.emb_sen, num=self.max_sentence_len, axis=1) + self.emb_e1_us = tf.unstack(self.emb_e1, num=self.max_ent_len, axis=1) + self.emb_e2_us = tf.unstack(self.emb_e2, num=self.max_ent_len, axis=1) + with tf.name_scope('sentence_encoder'): + with tf.name_scope('rnn_layer'): + with tf.variable_scope('sentence_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + foward_cell = tf.nn.rnn_cell.DropoutWrapper(foward_cell, output_keep_prob=self.dropout_keep_rate) + backward_cell = tf.nn.rnn_cell.DropoutWrapper(backward_cell, output_keep_prob=self.dropout_keep_rate) + sen_rnn_output, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_sen_us, dtype=tf.float32) + self.sen_rnn_output = tf.reshape(tf.concat(sen_rnn_output, axis=1), [-1, self.max_sentence_len, self.hidden_size_sen * 2]) + with tf.name_scope('add_pos'): + self.cnn_input = tf.expand_dims(tf.concat([self.sen_rnn_output, self.emb_pos1, self.emb_pos2], 2), -1) + with tf.name_scope('conv_maxpooling'): + pooled_outputs = [] + for i, filter_size in enumerate(self.filter_sizes): + with tf.name_scope('conv-maxpool-{}'.format(filter_size)): + filter_shape = [filter_size, self.hidden_size_sen * 2 + self.pos_size * 2, 1, self.filter_num] + w = tf.get_variable('W_{}'.format(filter_size), filter_shape, initializer=tf.truncated_normal_initializer(stddev=0.1)) + b = tf.get_variable('b_{}'.format(filter_size), [self.filter_num], initializer=tf.constant_initializer(0.1)) + tf.summary.histogram('W_{}'.format(filter_size), w) + tf.summary.histogram('b_{}'.format(filter_size), b) + conv = tf.nn.conv2d(self.cnn_input, w, strides=[1, 1, 1, 1], padding='VALID', name='conv') + h = tf.nn.relu(tf.nn.bias_add(conv, b), name='relu') + pooled = tf.nn.max_pool(h, ksize=[1, self.max_sentence_len - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='conv') + pooled_outputs.append(pooled) + num_filters_total = self.filter_num * len(self.filter_sizes) + h_pool = tf.concat(pooled_outputs, 3) + h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + self.sen_output = tf.nn.dropout(h_pool_flat, self.dropout_keep_rate) + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e1_us, dtype=tf.float32) + ent2_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e2_us, dtype=tf.float32) + ent1_output = tf.reduce_mean(tf.reshape(tf.concat(ent1_outputs, 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis=1) + ent2_output = tf.reduce_mean(tf.reshape(tf.concat(ent2_outputs, 1), [-1, self.max_ent_len, self.hidden_size_ent * 2]), axis=1) + self.ent_out = tf.concat([ent1_output, ent2_output], axis=1) + with tf.name_scope('joint_layer'): + self.outputs = tf.concat([self.sen_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [self.filter_num * len(self.filter_sizes) + self.hidden_size_ent * 4, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + self.fc_out = tf.matmul(self.outputs, self.fc_w) + self.fc_b + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_out) + with tf.name_scope('accuracy'): + self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + self.class_label = tf.argmax(self.softmax_res, 1) + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.class_label, tf.argmax(self.input_labels, 1)), 'float'), name='accuracy') + tf.summary.scalar('accuracy', self.accuracy) + with tf.name_scope('model_predict'): + self.maxres_by_rel = tf.reduce_max(self.softmax_res, 0) + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_out, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Res_Ent(object): + """ + Bidirectional Residual RNN model with entity spell. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Res_Ent' + self.cell_type = setting.cell + self.max_sen_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + with tf.name_scope('sentence_encoder'): + with tf.name_scope('rnn_layer'): + outputs_list = [] + for layer in range(2): + with tf.name_scope('birnn_layer_{}'.format(layer)): + with tf.variable_scope('birnn_layer_{}'.format(layer)): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + layer_output, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_all, dtype=tf.float32) + outputs_list.append(layer_output) + outputs = outputs_list[0] + outputs_list[1] + with tf.name_scope('max_pooling'): + pooled = tf.nn.max_pool(tf.expand_dims(outputs, -1), [1, self.max_sen_len, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + mp_out = tf.squeeze(pooled) + self.sen_output = mp_out + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_e1, dtype=tf.float32) + ent2_outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_e2, dtype=tf.float32) + ent1_output = tf.reduce_mean(ent1_outputs, axis=1) + ent2_output = tf.reduce_mean(ent2_outputs, axis=1) + self.ent_out = tf.concat([ent1_output, ent2_output], axis=1) + with tf.name_scope('joint_layer'): + self.rep = tf.concat([self.sen_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + fc_w = tf.get_variable('fc_W', [self.hidden_size_sen * 2 + self.hidden_size_ent * 4, self.class_num]) + fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rep, fc_w) + fc_b + tf.summary.histogram('fc_W', fc_w) + tf.summary.histogram('fc_b', fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('pred'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Res_cAtt_Ent(object): + """ + Bidirectional Residual RNN model with entity spell and attention. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Res_cAtt_Ent' + self.cell_type = setting.cell + self.max_sen_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + with tf.name_scope('model_input'): + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sen_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_e1, dtype=tf.float32) + ent2_outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_e2, dtype=tf.float32) + ent1_output = tf.reduce_mean(ent1_outputs, axis=1) + ent2_output = tf.reduce_mean(ent2_outputs, axis=1) + self.ent_out = tf.concat([ent1_output, ent2_output], axis=1) + self.ent_att = tf.expand_dims(tf.subtract(ent2_output, ent1_output), -1) + with tf.name_scope('sentence_encoder'): + with tf.name_scope('rnn_layer'): + outputs_list = [] + for layer in range(2): + with tf.name_scope('birnn_layer_{}'.format(layer)): + with tf.variable_scope('birnn_layer_{}'.format(layer)): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + layer_output, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([foward_cell], [backward_cell], self.emb_all, dtype=tf.float32) + outputs_list.append(layer_output) + self.sent_outputs = outputs_list[0] + outputs_list[1] + with tf.name_scope('attention_layer'): + self.attention_w = tf.get_variable('attention_omega', [self.sent_outputs.shape[-1], self.ent_att.shape[-2]]) + self.attention_A = tf.reshape(tf.nn.softmax(tf.squeeze(tf.matmul(tf.reshape(tf.matmul(tf.reshape(tf.tanh(self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]), self.attention_w), [-1, self.max_sen_len, int(self.ent_att.shape[-2])]), self.ent_att))), [-1, 1, self.max_sen_len]) + self.sen_output = tf.reshape(tf.matmul(self.attention_A, self.sent_outputs), [-1, int(self.sent_outputs.shape[-1])]) + with tf.name_scope('joint_layer'): + self.rep = tf.concat([self.sen_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + fc_w = tf.get_variable('fc_W', [self.hidden_size_sen * 2 + self.hidden_size_ent * 4, self.class_num]) + fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rep, fc_w) + fc_b + tf.summary.histogram('fc_W', fc_w) + tf.summary.histogram('fc_b', fc_b) + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('pred'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.instance_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.instance_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_loss = session.run([self.merge_summary, self.model_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + feed_dict = {self.input_sen: input_data.x, self.input_pos1: input_data.pos1, self.input_pos2: input_data.pos2, self.input_e1: input_data.e1, self.input_e2: input_data.e2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.model_loss, self.class_label, self.softmax_res], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_Mi(object): + """ + Bidirectional RNN model with multi-instance learning. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_Mi' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.hidden_size = setting.hidden_size + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_size + self.learning_rate = setting.learning_rate + self.bag_num = setting.bag_num + with tf.name_scope('input_layer'): + self.bag_shapes = tf.placeholder(tf.int32, [None], name='bag_shapes') + self.instance_num = self.bag_shapes[-1] + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('sentence_encoder'): + self.foward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.backward_cell = rnn_cell[self.cell_type](self.hidden_size) + self.foward_cell = tf.nn.rnn_cell.DropoutWrapper(self.foward_cell, output_keep_prob=self.dropout_keep_rate) + self.backward_cell = tf.nn.rnn_cell.DropoutWrapper(self.backward_cell, output_keep_prob=self.dropout_keep_rate) + self.outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(self.foward_cell, self.backward_cell, self.emb_all_us, dtype=tf.float32) + if setting.hidden_select == 'last': + self.sen_emb = self.outputs[-1] + elif setting.hidden_select == 'avg': + self.sen_emb = tf.reduce_mean(tf.reshape(tf.concat(self.outputs, 1), [-1, self.max_sentence_len, self.hidden_size * 2]), axis=1) + with tf.name_scope('sentence_attention'): + sen_repre = [] + sen_alpha = [] + sen_s = [] + sen_out = [] + self.prob = [] + self.predictions = [] + self.loss = [] + self.accuracy = [] + self.total_loss = 0.0 + self.sen_a = tf.get_variable('attention_A', [self.hidden_size * 2]) + self.sen_r = tf.get_variable('query_r', [self.hidden_size * 2, 1]) + relation_embedding = tf.get_variable('relation_embedding', [self.class_num, self.hidden_size * 2]) + sen_d = tf.get_variable('bias_d', [self.class_num]) + for i in range(self.bag_num): + sen_repre.append(tf.tanh(self.sen_emb[self.bag_shapes[i]:self.bag_shapes[i + 1]])) + bag_size = self.bag_shapes[i + 1] - self.bag_shapes[i] + sen_alpha.append(tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.multiply(sen_repre[i], self.sen_a), self.sen_r), [bag_size])), [1, bag_size])) + sen_s.append(tf.reshape(tf.matmul(sen_alpha[i], sen_repre[i]), [self.hidden_size * 2, 1])) + sen_out.append(tf.add(tf.reshape(tf.matmul(relation_embedding, sen_s[i]), [self.class_num]), sen_d)) + self.prob.append(tf.nn.softmax(sen_out[i])) + with tf.name_scope('output'): + self.predictions.append(tf.argmax(self.prob[i], 0, name='predictions')) + with tf.name_scope('loss'): + self.loss.append(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=sen_out[i], labels=self.input_labels[i]))) + if i == 0: + self.total_loss = self.loss[i] + else: + self.total_loss += self.loss[i] + with tf.name_scope('accuracy'): + self.accuracy.append(tf.reduce_mean(tf.cast(tf.equal(self.predictions[i], tf.argmax(self.input_labels[i], 0)), 'float'), name='accuracy')) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.total_loss) + tf.summary.histogram('sen_a', self.sen_a) + tf.summary.histogram('sen_r', self.sen_r) + tf.summary.scalar('loss', self.total_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + total_shape = [0] + total_num = 0 + total_x = [] + total_pos1 = [] + total_pos2 = [] + for bag_idx in range(len(input_data.x)): + total_num += len(input_data.x[bag_idx]) + total_shape.append(total_num) + for sent in input_data.x[bag_idx]: + total_x.append(sent) + for pos1 in input_data.pos1[bag_idx]: + total_pos1.append(pos1) + for pos2 in input_data.pos2[bag_idx]: + total_pos2.append(pos2) + feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_accuracy, model_loss = session.run([self.merge_summary, self.accuracy, self.total_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + total_shape = [0] + total_num = 0 + total_x = [] + total_pos1 = [] + total_pos2 = [] + for bag_idx in range(len(input_data.x)): + total_num += len(input_data.x[bag_idx]) + total_shape.append(total_num) + for sent in input_data.x[bag_idx]: + total_x.append(sent) + for pos1 in input_data.pos1[bag_idx]: + total_pos1.append(pos1) + for pos2 in input_data.pos2[bag_idx]: + total_pos2.append(pos2) + feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.total_loss, self.predictions, self.prob], feed_dict=feed_dict) + return model_loss, label_pred, label_prob + + +class BiRnn_SelfAtt_Ent_Mi(object): + """ + Bidirectional RNN model with attention and entity spell using multi-instance learning. + """ + + def __init__(self, x_embedding, setting): + self.model_name = 'BiRnn_SelfAtt_Ent_Mi' + self.cell_type = setting.cell + self.max_sentence_len = setting.sen_len + self.max_ent_len = setting.ent_len + self.hidden_size_sen = setting.hidden_size_sen + self.hidden_size_ent = setting.hidden_size_ent + self.class_num = setting.class_num + self.pos_num = setting.pos_num + self.pos_size = setting.pos_sized + self.learning_rate = setting.learning_rate + self.bag_num = setting.bag_num + with tf.name_scope('model_input'): + self.bag_shapes = tf.placeholder(tf.int32, [None], name='bag_shapes') + self.instance_num = self.bag_shapes[-1] + self.input_sen = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_sen') + self.input_labels = tf.placeholder(tf.int32, [None, self.class_num], name='labels') + self.input_pos1 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos1') + self.input_pos2 = tf.placeholder(tf.int32, [None, self.max_sentence_len], name='input_pos2') + self.input_e1 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e1') + self.input_e2 = tf.placeholder(tf.int32, [None, self.max_ent_len], name='input_e2') + self.dropout_keep_rate = tf.placeholder(tf.float32, name='dropout_keep_rate') + with tf.name_scope('embedding_layer'): + self.embed_matrix_x = tf.get_variable('embed_matrix_x', x_embedding.shape, initializer=tf.constant_initializer(x_embedding)) + ent_embedding = np.load('data/char_vec.npy') + self.embed_matrix_ent = tf.get_variable('embed_matrix_ent', ent_embedding.shape, initializer=tf.constant_initializer(ent_embedding)) + self.embed_size_x = int(self.embed_matrix_x.get_shape()[1]) + self.embed_matrix_pos1 = tf.get_variable('embed_matrix_pos1', [self.pos_num, self.pos_size]) + self.embed_matrix_pos2 = tf.get_variable('embed_matrix_pos2', [self.pos_num, self.pos_size]) + self.emb_sen = tf.nn.embedding_lookup(self.embed_matrix_x, self.input_sen) + self.emb_pos1 = tf.nn.embedding_lookup(self.embed_matrix_pos1, self.input_pos1) + self.emb_pos2 = tf.nn.embedding_lookup(self.embed_matrix_pos2, self.input_pos2) + self.emb_e1 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e1) + self.emb_e2 = tf.nn.embedding_lookup(self.embed_matrix_ent, self.input_e2) + self.emb_all = tf.concat([self.emb_sen, self.emb_pos1, self.emb_pos2], 2) + self.emb_e1_us = tf.unstack(self.emb_e1, num=self.max_ent_len, axis=1) + self.emb_e2_us = tf.unstack(self.emb_e2, num=self.max_ent_len, axis=1) + self.emb_all_us = tf.unstack(self.emb_all, num=self.max_sentence_len, axis=1) + with tf.name_scope('sentence_encoder'): + with tf.variable_scope('sentence_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_sen) + foward_cell = tf.nn.rnn_cell.DropoutWrapper(foward_cell, output_keep_prob=self.dropout_keep_rate) + backward_cell = tf.nn.rnn_cell.DropoutWrapper(backward_cell, output_keep_prob=self.dropout_keep_rate) + rnn_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_all_us, dtype=tf.float32) + self.sent_outputs = tf.reshape(tf.concat(rnn_outputs, axis=1), [-1, self.max_sentence_len, self.hidden_size_sen * 2]) + with tf.name_scope('attention'): + self.attention_Ws1 = tf.get_variable('attention_Ws1', [self.hidden_size_sen * 2, setting.da]) + self.attention_Ws2 = tf.get_variable('attention_Ws2', [setting.da, setting.r]) + self.attention_A = tf.nn.softmax(tf.transpose(tf.reshape(tf.matmul(tf.tanh(tf.matmul(tf.reshape(self.sent_outputs, [-1, self.hidden_size_sen * 2]), self.attention_Ws1)), self.attention_Ws2), [-1, self.max_sentence_len, setting.r]), [0, 2, 1])) + self.M = tf.matmul(self.attention_A, self.sent_outputs) + self.att_output = tf.reshape(self.M, [-1, self.hidden_size_sen * 2 * setting.r]) + tf.summary.histogram('Ws1', self.attention_Ws1) + tf.summary.histogram('Ws2', self.attention_Ws2) + with tf.name_scope('entity_encoder'): + with tf.variable_scope('entity_encoder'): + foward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + backward_cell = rnn_cell[self.cell_type](self.hidden_size_ent) + ent1_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e1_us, dtype=tf.float32) + e1_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent1_outputs] + e1_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent1_outputs] + e1_outputs_forward = tf.reshape(tf.concat(axis=1, values=e1_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs_backward = tf.reshape(tf.concat(axis=1, values=e1_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e1_outputs = tf.add(e1_outputs_forward, e1_outputs_backward) + e1_output = tf.reduce_mean(e1_outputs, 1) + ent2_outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(foward_cell, backward_cell, self.emb_e2_us, dtype=tf.float32) + e2_outputs_forward = [i[:, :self.hidden_size_ent] for i in ent2_outputs] + e2_outputs_backward = [i[:, self.hidden_size_ent:] for i in ent2_outputs] + e2_output_forward = tf.reshape(tf.concat(axis=1, values=e2_outputs_forward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_output_backward = tf.reshape(tf.concat(axis=1, values=e2_outputs_backward), [-1, self.max_ent_len, self.hidden_size_ent]) + e2_outputs = tf.add(e2_output_forward, e2_output_backward) + e2_output = tf.reduce_mean(e2_outputs, 1) + self.ent_out = tf.concat([e1_output, e2_output], axis=1) + self.ent_att = tf.expand_dims(tf.subtract(e1_output, e2_output), -1) + with tf.name_scope('joint_layer'): + self.rep = tf.concat([self.att_output, self.ent_out], axis=1) + with tf.name_scope('fc_layer'): + self.fc_w = tf.get_variable('fc_W', [int(self.att_output.shape[-1]) + self.hidden_size_ent * 2, self.class_num]) + self.fc_b = tf.get_variable('fc_b', [self.class_num]) + self.fc_output = tf.matmul(self.rep, self.fc_w) + self.fc_b + tf.summary.histogram('fc_W', self.fc_w) + tf.summary.histogram('fc_b', self.fc_b) + with tf.name_scope('sentence_attention'): + sen_repre = [] + sen_alpha = [] + sen_s = [] + sen_out = [] + self.prob = [] + self.predictions = [] + self.loss = [] + self.accuracy = [] + self.total_loss = 0.0 + self.sen_a = tf.get_variable('attention_A', [self.hidden_size * 2]) + self.sen_r = tf.get_variable('query_r', [self.hidden_size * 2, 1]) + relation_embedding = tf.get_variable('relation_embedding', [self.class_num, self.hidden_size * 2]) + sen_d = tf.get_variable('bias_d', [self.class_num]) + for i in range(self.bag_num): + sen_repre.append(tf.tanh(self.sen_emb[self.bag_shapes[i]:self.bag_shapes[i + 1]])) + bag_size = self.bag_shapes[i + 1] - self.bag_shapes[i] + sen_alpha.append(tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.multiply(sen_repre[i], self.sen_a), self.sen_r), [bag_size])), [1, bag_size])) + sen_s.append(tf.reshape(tf.matmul(sen_alpha[i], sen_repre[i]), [self.hidden_size * 2, 1])) + sen_out.append(tf.add(tf.reshape(tf.matmul(relation_embedding, sen_s[i]), [self.class_num]), sen_d)) + self.prob.append(tf.nn.softmax(sen_out[i])) + with tf.name_scope('output'): + self.predictions.append(tf.argmax(self.prob[i], 0, name='predictions')) + with tf.name_scope('loss'): + self.loss.append(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=sen_out[i], labels=self.input_labels[i]))) + if i == 0: + self.total_loss = self.loss[i] + else: + self.total_loss += self.loss[i] + with tf.name_scope('accuracy'): + self.accuracy.append(tf.reduce_mean(tf.cast(tf.equal(self.predictions[i], tf.argmax(self.input_labels[i], 0)), 'float'), name='accuracy')) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.total_loss) + tf.summary.histogram('sen_a', self.sen_a) + tf.summary.histogram('sen_r', self.sen_r) + tf.summary.scalar('loss', self.total_loss) + self.merge_summary = tf.summary.merge_all() + with tf.name_scope('softmax'): + self.softmax_res = tf.nn.softmax(self.fc_output) + with tf.name_scope('model_predict'): + self.class_label = tf.argmax(self.softmax_res, 1) + with tf.name_scope('model_loss'): + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc_output, labels=self.input_labels) + self.model_loss = tf.reduce_mean(self.loss) + tf.summary.scalar('model_loss', self.model_loss) + with tf.name_scope('optimizer'): + self.optimizer = opt_method[setting.optimizer](learning_rate=setting.learning_rate).minimize(self.model_loss) + self.merge_summary = tf.summary.merge_all() + + def fit(self, session, input_data, dropout_keep_rate): + total_shape = [0] + total_num = 0 + total_x = [] + total_pos1 = [] + total_pos2 = [] + for bag_idx in range(len(input_data.x)): + total_num += len(input_data.x[bag_idx]) + total_shape.append(total_num) + for sent in input_data.x[bag_idx]: + total_x.append(sent) + for pos1 in input_data.pos1[bag_idx]: + total_pos1.append(pos1) + for pos2 in input_data.pos2[bag_idx]: + total_pos2.append(pos2) + feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: dropout_keep_rate} + session.run(self.optimizer, feed_dict=feed_dict) + summary, model_accuracy, model_loss = session.run([self.merge_summary, self.accuracy, self.total_loss], feed_dict=feed_dict) + return summary, model_loss + + def evaluate(self, session, input_data): + total_shape = [0] + total_num = 0 + total_x = [] + total_pos1 = [] + total_pos2 = [] + for bag_idx in range(len(input_data.x)): + total_num += len(input_data.x[bag_idx]) + total_shape.append(total_num) + for sent in input_data.x[bag_idx]: + total_x.append(sent) + for pos1 in input_data.pos1[bag_idx]: + total_pos1.append(pos1) + for pos2 in input_data.pos2[bag_idx]: + total_pos2.append(pos2) + feed_dict = {self.bag_shapes: total_shape, self.input_sen: total_x, self.input_pos1: total_pos1, self.input_pos2: total_pos2, self.input_labels: input_data.y, self.dropout_keep_rate: 1} + model_loss, label_pred, label_prob = session.run([self.total_loss, self.predictions, self.prob], feed_dict=feed_dict) + return model_loss, label_pred, label_prob diff --git a/dataset/preprocessed/339.txt b/dataset/preprocessed/339.txt new file mode 100644 index 0000000..42804e7 --- /dev/null +++ b/dataset/preprocessed/339.txt @@ -0,0 +1,225 @@ +import json +from collections import OrderedDict, namedtuple +from config import * +from utils import * +from tensorflow.python.framework.graph_util import convert_variables_to_constants + + +class Model: + os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' + _MODEL_DEF_TEMPLATE = {'batch_size': 1, 'dropout': False, 'init': None} + + def __init__(self, name, load_dir, trainable, device): + self.name = name + self.trainable = trainable + self.load_dir = load_dir + self.write_dir = os.path.join(POLICY_PATH, 'tmp/') + self._vars = [] + self._ops = [] + self.scopes = [] + self._device = device + + def _build_graph(self): + pass + + def _constructor(self): + with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE): + try: + with tf.device(self._device): + self._build_graph() + except AttributeError: + self._build_graph() + + def init(self, session, load_dir=None): + load_dir = load_dir or self.load_dir + for i, layer_scope in enumerate(self.scopes): + vars_to_load = [v for v in tf.global_variables() if v.name.startswith(layer_scope)] + self._vars.extend(vars_to_load) + session.run(tf.variables_initializer(var_list=vars_to_load)) + if load_dir and vars_to_load: + saver = tf.train.Saver(vars_to_load) + saver.restore(session, os.path.join(load_dir, self.name)) + + def save_params(self, session, step=None): + assert self.write_dir + info('Saving {0} to {1}'.format(self.name, self.write_dir)) + saver = tf.train.Saver(list(set(self._vars))) + saver.save(session, os.path.join(self.write_dir, self.name), global_step=step) + + def save_graph(self, session: tf.Session, fname: str, var_names: list): + frozen_graph = convert_variables_to_constants(session, session.graph_def, var_names) + tf.train.write_graph(frozen_graph, self.write_dir, fname + '.pb', as_text=False) + tf.train.write_graph(frozen_graph, self.write_dir, fname + '.txt', as_text=True) + + +class Policy(Model): + + def __init__(self, session: tf.Session, name=None, load_dir=None, trainable=False, selection='greedy', device='GPU'): + super().__init__(name='POLICY_{}'.format(name), load_dir=load_dir, trainable=trainable, device=device) + self.session = session + self.state = tf.placeholder(dtype=tf.int32, shape=(None, 8, 4), name='state') + self.action_label = tf.placeholder(dtype=tf.int32, shape=(None, 128), name='action') + self.selection = selection + self._constructor() + + def _build_graph(self): + self.batch_size = tf.placeholder(shape=(), dtype=tf.float32, name='batch_size') + self.keep_prob = tf.placeholder(shape=(), dtype=tf.float32, name='keep_prob') if self.trainable else tf.constant(value=1, dtype=tf.float32, name='keep_prob') + self.lr = tf.placeholder(shape=(), dtype=tf.float32, name='learning_rate') + self.adv = tf.placeholder(shape=None, dtype=tf.float32, name='advantage') + X = tf.expand_dims(tf.cast(self.state, tf.float32), axis=3) + with tf.variable_scope('INCEPTION', reuse=False) as scope: + self.scopes.append(scope.name) + for i, (ksizes, nkernels) in enumerate(zip(KERNEL_SIZES, N_KERNELS)): + conv = [] + for ks, nk in zip(ksizes, nkernels): + w = tf.get_variable(shape=[ks[0], ks[1], X.shape[-1], nk], initializer=PARAM_INIT, trainable=self.trainable, name='incep_{0}_w_K{1}{2}'.format(i + 1, ks[0], ks[1])) + b = tf.get_variable(shape=[nk], initializer=PARAM_INIT, trainable=self.trainable, name='incep_{0}_b_K{1}{2}'.format(i + 1, ks[0], ks[1])) + c = tf.nn.conv2d(X, w, strides=[1, 1, 1, 1], padding='SAME') + z = INCEP_ACT(c + b) + conv.append(z) + X = tf.concat(conv, axis=3) + X = tf.nn.dropout(X, keep_prob=self.keep_prob) + conv_out = tf.contrib.layers.flatten(inputs=X) + X = conv_out + hwy_size = X.shape[-1] + with tf.variable_scope('HIGHWAY', reuse=False) as scope: + self.scopes.append(scope.name) + for i in range(HWY_LAYERS): + with tf.variable_scope('HWY_{}'.format(i)): + wh = tf.get_variable(shape=[hwy_size, hwy_size], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='hwy_w_{}'.format(i + 1)) + bh = tf.get_variable(shape=[hwy_size], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='hwy_b_{}'.format(i + 1)) + wt = tf.get_variable(shape=[hwy_size, hwy_size], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='T_w_{}'.format(i + 1)) + T = tf.sigmoid(tf.matmul(X, wt) + HWY_BIAS) + H = tf.nn.relu(tf.matmul(X, wh) + bh) + X = T * H + (1.0 - T) * X + X = tf.nn.dropout(X, keep_prob=self.keep_prob) + X = tf.concat([X, conv_out], axis=1) + with tf.variable_scope('OUTPUT', reuse=False) as scope: + self.scopes.append(scope.name) + w = tf.get_variable(shape=[X.shape[-1], 128], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='w_logit') + b = tf.get_variable(shape=[128], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='b_logit') + self.logits = tf.add(tf.matmul(X, w), b, name='policy_logits') + self.softmax = tf.nn.softmax(logits=self.logits, axis=1, name='policy_softmax') + self.action = tf.argmax(input=self.softmax, axis=1, name='action') + self.probs, self.actions = tf.nn.top_k(input=self.softmax, k=128, sorted=True) + with tf.variable_scope('METRICS', reuse=False) as scope: + self.scopes.append(scope.name) + self.top_1_acc = tf.metrics.accuracy(labels=tf.argmax(self.action_label, axis=1), predictions=self.action, name='accuracy') + self.top_2_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 2), tf.float32)) + self.top_3_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 3), tf.float32)) + self.top_5_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 5), tf.float32)) + self.top_10_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 10), tf.float32)) + if self.trainable: + with tf.variable_scope('LOSS', reuse=False) as scope: + self.scopes.append(scope.name) + self.step = tf.Variable(0, trainable=False) + self.reg_loss = LAMBDA * tf.add_n([tf.nn.l2_loss(v) for v in tf.global_variables() if v.name.__contains__('w_')]) + self.cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.action_label, logits=self.logits, name='cross_entropy') + self.loss1 = tf.add(self.reg_loss, self.cross_entropy, name='loss1') + self.optimizer1 = tf.train.AdamOptimizer(learning_rate=self.lr, name='optimizer_pretrain') + self.grad_update1 = self.optimizer1.minimize(loss=self.loss1, var_list=tf.trainable_variables(), global_step=self.step, name='grad_update') + self.gradlogprob_adv = self.adv * tf.log(self.softmax) + self.pg_loss = tf.reduce_mean(input_tensor=-self.gradlogprob_adv, axis=1, name='pg_loss') + self.optimizer2 = tf.train.RMSPropOptimizer(learning_rate=self.lr, decay=0.99, epsilon=1e-05) + self.policy_update = self.optimizer2.apply_gradients(grads_and_vars=[self.pg_loss, self.vars], global_step=self.step) + + @property + def vars(self): + return [v for v in tf.trainable_variables() if v.name.lower().__contains__(self.name.lower())] + + +class Value(Model): + + def __init__(self, session: tf.Session, name=None, load_dir=None, trainable=False, selection='greedy', device='GPU'): + super().__init__(name='VALUE_{}'.format(name), load_dir=load_dir, trainable=trainable, device=device) + self.session = session + self.state = tf.placeholder(dtype=tf.int32, shape=(None, 8, 4), name='state') + self.action_label = tf.placeholder(dtype=tf.int32, shape=(None, 128), name='action') + self.selection = selection + self._constructor() + + def _build_graph(self): + self.batch_size = tf.placeholder(shape=(), dtype=tf.float32, name='batch_size') + self.keep_prob = tf.placeholder(shape=(), dtype=tf.float32, name='keep_prob') if self.trainable else tf.constant(value=1, dtype=tf.float32, name='keep_prob') + self.lr = tf.placeholder(shape=(), dtype=tf.float32, name='learning_rate') + self.adv = tf.placeholder(shape=None, dtype=tf.float32, name='advantage') + X = tf.expand_dims(tf.cast(self.state, tf.float32), axis=3) + with tf.variable_scope('INCEPTION', reuse=False) as scope: + self.scopes.append(scope.name) + for i, (ksizes, nkernels) in enumerate(zip(KERNEL_SIZES, N_KERNELS)): + conv = [] + for ks, nk in zip(ksizes, nkernels): + w = tf.get_variable(shape=[ks[0], ks[1], X.shape[-1], nk], initializer=PARAM_INIT, trainable=self.trainable, name='incep_{0}_w_K{1}{2}'.format(i + 1, ks[0], ks[1])) + b = tf.get_variable(shape=[nk], initializer=PARAM_INIT, trainable=self.trainable, name='incep_{0}_b_K{1}{2}'.format(i + 1, ks[0], ks[1])) + c = tf.nn.conv2d(X, w, strides=[1, 1, 1, 1], padding='SAME') + z = INCEP_ACT(c + b) + conv.append(z) + X = tf.concat(conv, axis=3) + X = tf.nn.dropout(X, keep_prob=self.keep_prob) + conv_out = tf.contrib.layers.flatten(inputs=X) + X = conv_out + hwy_size = X.shape[-1] + with tf.variable_scope('HIGHWAY', reuse=False) as scope: + self.scopes.append(scope.name) + for i in range(HWY_LAYERS): + with tf.variable_scope('HWY_{}'.format(i)): + wh = tf.get_variable(shape=[hwy_size, hwy_size], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='hwy_w_{}'.format(i + 1)) + bh = tf.get_variable(shape=[hwy_size], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='hwy_b_{}'.format(i + 1)) + wt = tf.get_variable(shape=[hwy_size, hwy_size], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='T_w_{}'.format(i + 1)) + T = tf.sigmoid(tf.matmul(X, wt) + HWY_BIAS) + H = tf.nn.relu(tf.matmul(X, wh) + bh) + X = T * H + (1.0 - T) * X + X = tf.nn.dropout(X, keep_prob=self.keep_prob) + X = tf.concat([X, conv_out], axis=1) + with tf.variable_scope('OUTPUT', reuse=False) as scope: + self.scopes.append(scope.name) + w = tf.get_variable(shape=[X.shape[-1], 128], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='w_logit') + b = tf.get_variable(shape=[128], initializer=PARAM_INIT, trainable=self.trainable, dtype=tf.float32, name='b_logit') + self.logits = tf.add(tf.matmul(X, w), b, name='policy_logits') + self.softmax = tf.nn.softmax(logits=self.logits, axis=1, name='policy_softmax') + self.action = tf.argmax(input=self.softmax, axis=1, name='action') + self.probs, self.actions = tf.nn.top_k(input=self.softmax, k=128, sorted=True) + with tf.variable_scope('METRICS', reuse=False) as scope: + self.scopes.append(scope.name) + self.top_1_acc = tf.metrics.accuracy(labels=tf.argmax(self.action_label, axis=1), predictions=self.action, name='accuracy') + self.top_2_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 2), tf.float32)) + self.top_3_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 3), tf.float32)) + self.top_5_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 5), tf.float32)) + self.top_10_acc = tf.reduce_mean(tf.cast(tf.nn.in_top_k(self.softmax, tf.argmax(self.action_label, axis=1), 10), tf.float32)) + if self.trainable: + with tf.variable_scope('LOSS', reuse=False) as scope: + self.scopes.append(scope.name) + self.step = tf.Variable(0, trainable=False) + self.reg_loss = LAMBDA * tf.add_n([tf.nn.l2_loss(v) for v in tf.global_variables() if v.name.__contains__('w_')]) + self.cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.action_label, logits=self.logits, name='cross_entropy') + self.loss1 = tf.add(self.reg_loss, self.cross_entropy, name='loss1') + self.optimizer1 = tf.train.AdamOptimizer(learning_rate=self.lr, name='optimizer_pretrain') + self.grad_update1 = self.optimizer1.minimize(loss=self.loss1, var_list=tf.trainable_variables(), global_step=self.step, name='grad_update') + self.neg_grad_log_prob_adv = self.adv * -tf.log(self.softmax) + self.SFGE = tf.reduce_mean(input_tensor=self.neg_grad_log_prob_adv, axis=1, name='score_func_grad_estimator') + + +class A2CLoss: + + def __init__(self, policy_network, value_network): + self.policy = policy_network + self.value = value_network + self.__build_graph() + + def __build_graph(self): + with tf.variable_scope('AC2_LOSS', reuse=False) as scope: + self.policy.scopes.append(scope.name) + self.value.scopes.append(scope.name) + self.lrate = tf.placeholder(shape=(), dtype=tf.float32, name='lrate') + self.rewards = tf.placeholder(shape=None, dtype=tf.float32, name='rewards') + self.baseline = tf.placeholder(shape=None, dtype=tf.float32, name='value_estimate') + self.gradlogp = self.adv * tf.log(self.policy.softmax) + self.logprob = tf.reduce_mean(input_tensor=-self.gradlogprob_adv, axis=1, name='pg_loss') + self.policy_entropy = -tf.reduce_sum(self.policy.softmax * tf.log(self.policy.softmax), axis=1) + self.optimizer = tf.train.RMSPropOptimizer(learning_rate=self.lr, decay=0.99, epsilon=1e-05) + self.policy_update = self.optimizer.apply_gradients(grads_and_vars=[self.pg_loss, self.policy.vars], global_step=self.policy.step) + + +if __name__ == '__main__': + pass diff --git a/dataset/preprocessed/34.txt b/dataset/preprocessed/34.txt new file mode 100644 index 0000000..1c59c39 --- /dev/null +++ b/dataset/preprocessed/34.txt @@ -0,0 +1,132 @@ +import tensorflow as tf +import dataset +import random +import sys +from tensorflow.python import debug as tf_debug +NUM_CLASSES = len(dataset.CLASSES) +IMAGE_SIZE = 100 + + +def placeholder_inputs(batch_size): + images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_SIZE, IMAGE_SIZE, 3), name='images') + labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size,), name='labels') + return images_placeholder, labels_placeholder + + +def get_weights(shape): + initial = tf.truncated_normal(shape, stddev=0.1) + return tf.Variable(initial, name='Weights') + + +def get_biases(shape): + initial = tf.zeros(shape) + return tf.Variable(initial, name='Biases') + + +def conv_layer(input, filter_size, name, strides=[1, 1, 1, 1], pool_size=2, padding='SAME'): + with tf.name_scope(name): + weights = get_weights(filter_size) + biases = get_biases([filter_size[3]]) + conv = tf.nn.leaky_relu(tf.nn.conv2d(input, weights, strides, padding)) + pool = tf.nn.max_pool(conv, [1, pool_size, pool_size, 1], [1, pool_size, pool_size, 1], padding) + return pool + + +def dense_layer(inputs, input_size, output_size, name): + with tf.name_scope(name): + weights = get_weights([input_size, output_size]) + biases = get_biases([output_size]) + h = tf.matmul(inputs, weights) + biases + return tf.nn.leaky_relu(h) + + +def define_model(images): + conv1 = conv_layer(images, [20, 20, 3, 10], 'Conv1', pool_size=4) + conv2 = conv_layer(conv1, [5, 5, 10, 20], 'Conv2') + conv2 = tf.reshape(conv2, [-1, 13 * 13 * 20]) + dense1 = dense_layer(conv2, 13 * 13 * 20, 1000, 'Dense1') + dense2 = dense_layer(dense1, 1000, 100, 'Dense2') + logits = dense_layer(dense2, 100, NUM_CLASSES, 'Softmax_Linear') + return logits + + +def define_loss(logits, labels): + labels = tf.to_int64(labels) + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits, name='xentropy') + loss = tf.reduce_mean(cross_entropy, name='xentropy_mean') + return loss + + +def training(loss, learning_rate, momentum=0.75, beta1=0.9, beta2=0.999): + tf.summary.scalar('loss', loss) + optimizer = tf.train.AdamOptimizer(learning_rate, beta1=beta1, beta2=beta2) + train_op = optimizer.minimize(loss) + return train_op + + +def evaluation(logits, labels): + correct = tf.nn.in_top_k(logits, labels, 1) + return tf.reduce_sum(tf.cast(correct, tf.int32)) + + +def do_evaluation(session, eval_correct, testDataset, batch_size, images_placeholder, labels_placeholder): + training_data, training_labels = testDataset + batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + totalCorrect = 0 + for batch in batches: + image_data = batch[0] + label_data = batch[1] + feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + totalCorrect += session.run(eval_correct, feed_dict=feed_dict) + precision = float(totalCorrect) / len(training_data) + print('Num examples: %d Num correct: %d Precision @ 1: %0.04f' % (len(training_data), totalCorrect, precision)) + sys.stdout.flush() + return precision > 0.95 + + +def run_training(batch_size, learning_rate, epochs, run_number): + with tf.Graph().as_default(): + images_placeholder, labels_placeholder = placeholder_inputs(batch_size) + logits = define_model(images_placeholder) + lossFunction = define_loss(logits, labels_placeholder) + train_op = training(lossFunction, learning_rate) + eval_correct = evaluation(logits, labels_placeholder) + saver = tf.train.Saver() + init = tf.global_variables_initializer() + with tf.Session() as session: + logdir = 'log/' + str(run_number) + session.run(init) + for step in range(epochs): + training_data, training_labels = dataset.get_training_data() + batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + epochLoss = 0 + for batch in batches: + image_data = batch[0] + label_data = batch[1] + feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + activations, loss_value = session.run([train_op, lossFunction], feed_dict=feed_dict) + epochLoss += loss_value + if step % 2 == 0: + print('Step %d: loss = %.2f' % (step, loss_value)) + sys.stdout.flush() + early_stop = False + if (step + 1) % 5 == 0 or step + 1 == epochs: + validation_data = dataset.get_validation_data(batch_size) + print('Doing evaluation on validation Set') + sys.stdout.flush() + early_stop = do_evaluation(session, eval_correct, validation_data, batch_size, images_placeholder, labels_placeholder) + if step + 1 == epochs or early_stop: + print('Doing evaluation on training set') + sys.stdout.flush() + do_evaluation(session, eval_correct, (training_data, training_labels), batch_size, images_placeholder, labels_placeholder) + print('Doing evaluation on the test set') + sys.stdout.flush() + test_data = dataset.get_test_data(batch_size) + do_evaluation(session, eval_correct, test_data, batch_size, images_placeholder, labels_placeholder) + saver.save(session, 'model.ckpt') + if early_stop: + print('Achieved desired precision at step %d' % step) + return + + +run_training(89, 0.001, 40, 13) diff --git a/dataset/preprocessed/340.txt b/dataset/preprocessed/340.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/340.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/341.txt b/dataset/preprocessed/341.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/341.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/342.txt b/dataset/preprocessed/342.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/342.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/343.txt b/dataset/preprocessed/343.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/343.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/344.txt b/dataset/preprocessed/344.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/344.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/345.txt b/dataset/preprocessed/345.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/345.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/346.txt b/dataset/preprocessed/346.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/346.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/347.txt b/dataset/preprocessed/347.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/347.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/348.txt b/dataset/preprocessed/348.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/348.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/349.txt b/dataset/preprocessed/349.txt new file mode 100644 index 0000000..7c30fcf --- /dev/null +++ b/dataset/preprocessed/349.txt @@ -0,0 +1,216 @@ +import tensorflow as tf +from pretrained.vgg16 import vgg16 +import time +import numpy as np +from pretrained.imagenet_classes import class_names + + +class ImageCaptioner(object): + + def __init__(self, config, word_table): + self.config = config + self.word_table = word_table + self.training_flag = True + self.session = tf.Session() + self.imgs_placeholder = tf.placeholder(tf.float32, [None, 224, 224, 3]) + self.build_cnn() + self.build_rnn() + self.session.run(tf.global_variables_initializer()) + self.saver = tf.train.Saver(max_to_keep=100) + checkpoint = tf.train.get_checkpoint_state(config.ckpt_dir) + if checkpoint and checkpoint.model_checkpoint_path: + self.saver.restore(self.session, checkpoint.model_checkpoint_path) + print('Successfully loaded:', checkpoint.model_checkpoint_path) + self.train_writer = tf.summary.FileWriter(config.summary_file) + if config.cnn_model_file: + self.cnn.load_weights(config.cnn_model_file, self.session) + + def build_cnn(self): + print('Building CNN...') + if self.config.cnn_model == 'custom': + self.build_custom_cnn() + else: + self.build_vgg16() + + def build_custom_cnn(self): + print('Building custom model...') + W_conv1 = _weight_variable([5, 5, 1, 32]) + b_conv1 = _bias_variable([32]) + h_conv1 = tf.nn.relu(_conv2d(imgs_placeholder, W_conv1) + b_conv1) + h_pool1 = _max_pool_2x2(h_conv1) + W_conv2 = _weight_variable([5, 5, 32, 64]) + b_conv2 = _bias_variable([64]) + h_conv2 = tf.nn.relu(_conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = _max_pool_2x2(h_conv2) + W_conv3 = _weight_variable([5, 5, 64, 128]) + b_conv3 = _bias_variable([128]) + h_conv3 = tf.nn.relu(_conv2d(h_pool2, W_conv3) + b_conv3) + h_pool3 = _max_pool_2x2(h_conv3) + h_flat3 = tf.reshape(h_pool3, [-1]) + self.cnn_output = h_flat3 + + def build_vgg16(self): + print('Building VGG-16...') + self.cnn = vgg16(self.imgs_placeholder, sess=self.session, trainable=self.config.train_cnn) + self.cnn_output = self.cnn.fc2 + self.img_dim = 4096 + + def build_rnn(self): + print('Building RNN...') + batch_size = self.config.batch_size + hidden_size = self.config.hidden_size + vector_dim = self.config.vector_dim + learning_rate = self.config.learning_rate + num_words = self.word_table.num_words + max_num_words = self.config.max_word_len + vector_dim = self.config.vector_dim + self.rnn_input = tf.placeholder(tf.float32, [None, self.img_dim]) + self.sentences = tf.placeholder(tf.int32, [None, max_num_words]) + self.mask = tf.placeholder(tf.float32, [None, max_num_words]) + gen_captions = [] + W_conv2rnn = _weight_variable([self.img_dim, vector_dim]) + b_conv2rnn = _bias_variable([vector_dim]) + fc_conv2rnn = tf.nn.xw_plus_b(self.rnn_input, W_conv2rnn, b_conv2rnn) + lstm_cell = tf.contrib.rnn.BasicLSTMCell(hidden_size) + state = lstm_cell.zero_state(tf.shape(self.rnn_input)[0], dtype=tf.float32) + self.idx2vec = tf.convert_to_tensor(self.word_table.idx2vec_np, dtype=tf.float32) + W_word = tf.Variable(tf.random_uniform([hidden_size, num_words])) + b_word = tf.Variable(tf.zeros([num_words])) + total_loss = 0.0 + for idx in range(max_num_words): + if idx == 0: + curr_emb = fc_conv2rnn + elif self.training_flag: + curr_emb = tf.nn.embedding_lookup(self.idx2vec, self.sentences[:, (idx - 1)]) + else: + curr_emb = tf.nn.embedding_lookup(self.idx2vec, max_prob_word) + if self.config.num_lstm == 1: + output, state = lstm_cell(curr_emb, state) + else: + output, state = tf.nn.dynamic_rnn(cell=lstm_cell, inputs=curr_emb, dtype=tf.float32) + logits = tf.matmul(output, W_word) + b_word + max_prob_word = tf.argmax(logits, 1) + gen_captions.append(max_prob_word) + onehot_labels = tf.cast(self.sentences[:, (idx)], dtype=tf.int32) + logits = tf.cast(logits, dtype=tf.float32) + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=onehot_labels) * self.mask[:, (idx)] + loss = tf.reduce_sum(cross_entropy) + total_loss = total_loss + loss + self.gen_captions = tf.stack(gen_captions, axis=1) + self.total_loss = total_loss / tf.reduce_sum(self.mask) + tf.summary.scalar('total_loss', self.total_loss) + self.train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + self.merged = tf.summary.merge_all() + + def train(self, data): + print('Training network...') + start_time = time.time() + self.training_flag = True + word2idx = self.word_table.word2idx + idx2word = self.word_table.idx2word + train_images = data.training_data + train_caps = data.training_annotation + max_word_len = self.config.max_word_len + batch_size = self.config.batch_size + num_epochs = self.config.num_epochs + display_loss = self.config.display_loss + train_idx = np.arange(len(train_caps)) + for epoch in range(1, num_epochs + 1): + print('Epoch number: ', epoch) + shuffled_train_images = [] + shuffled_train_caps = [] + np.random.shuffle(train_idx) + for old_idx in train_idx: + shuffled_train_images.append(train_images[old_idx]) + shuffled_train_caps.append(train_caps[old_idx]) + for batch_idx in range(0, len(train_caps), batch_size): + if batch_idx + batch_size > len(train_caps): + continue + curr_images = shuffled_train_images[batch_idx:batch_idx + batch_size] + curr_caps = shuffled_train_caps[batch_idx:batch_idx + batch_size] + curr_sentences = np.zeros((batch_size, max_word_len)) + curr_mask = np.zeros((batch_size, max_word_len)) + for cap_idx, cap in enumerate(curr_caps): + for word_idx, word in enumerate(cap.lower().split(' ')): + if word_idx == max_word_len: + break + if word in word2idx and word in self.word_table.word2vec: + curr_sentences[cap_idx][word_idx] = word2idx[word] + else: + curr_sentences[cap_idx][word_idx] = word2idx[''] + curr_mask[cap_idx][word_idx] = 1 + if self.config.train_cnn: + print('Not implemented yet!') + else: + cnn_output = self.session.run(self.cnn_output, feed_dict={self.imgs_placeholder: curr_images}) + _, summary, total_loss = self.session.run([self.train_op, self.merged, self.total_loss], feed_dict={self.rnn_input: cnn_output, self.sentences: curr_sentences, self.mask: curr_mask}) + if epoch % display_loss == 0: + print('Current Training Loss = ' + str(total_loss)) + self.train_writer.add_summary(summary, epoch) + if epoch % self.config.ckpt_freq == 0: + print('Saving checkpoint...') + self.saver.save(self.session, self.config.ckpt_dir + 'Captioner', global_step=epoch) + print('Finished Training') + print('Elapsed time: ', self.elapsed(time.time() - start_time)) + + def elapsed(self, sec): + if sec < 60: + return str(sec) + ' sec' + elif sec < 60 * 60: + return str(sec / 60) + ' min' + else: + return str(sec / (60 * 60)) + ' hr' + + def test(self, data): + """ Test the model. """ + print('Testing model...') + self.training_flag = False + results_file = self.config.results_file + max_num_words = self.config.max_word_len + test_images = data.training_data + test_caps = data.training_annotation + max_word_len = self.config.max_word_len + captions = [] + empty_sentences = np.zeros((len(test_images), max_num_words)) + empty_mask = np.ones((len(test_images), max_num_words)) + if self.config.train_cnn: + print('Not implemented yet!') + else: + cnn_output = self.session.run(self.cnn_output, feed_dict={self.imgs_placeholder: test_images}) + print(cnn_output.shape) + print('Convolutional features computed.') + captions_idx = self.session.run(self.gen_captions, feed_dict={self.rnn_input: cnn_output, self.sentences: empty_sentences, self.mask: empty_mask}) + captions = [] + for x in range(len(captions_idx)): + captions.append([]) + for y in range(len(captions_idx[0])): + captions[x].append(self.word_table.idx2word[captions_idx[x][y]]) + output_text = '' + for cap_idx in range(len(captions)): + if '.' in captions[cap_idx]: + end_sentence = np.argmax(np.array(captions[cap_idx]) == '.') + else: + end_sentence = max_num_words - 1 + sentence = ' '.join(captions[cap_idx][:end_sentence + 1]) + output_text += sentence + '\n' + f = open(results_file, 'w') + f.write(output_text) + f.close() + + +def _conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +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) + + +def _max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') diff --git a/dataset/preprocessed/35.txt b/dataset/preprocessed/35.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/35.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/350.txt b/dataset/preprocessed/350.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/350.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/351.txt b/dataset/preprocessed/351.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/351.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/352.txt b/dataset/preprocessed/352.txt new file mode 100644 index 0000000..8905bb0 --- /dev/null +++ b/dataset/preprocessed/352.txt @@ -0,0 +1,55 @@ +from model import Model +import tensorflow as tf + + +class ModelStandard(Model): + + def __init__(self, activation_func): + super(ModelStandard, self).__init__('standard', activation_func) + with tf.device('/gpu:0'): + self.input = tf.placeholder(tf.float32, shape=[None, 32, 32, 3]) + self.labels = tf.placeholder(tf.float32, shape=[None, 10]) + self.dropout = tf.placeholder(tf.float32) + W_conv1 = self.__weights([5, 5, 3, 32]) + b_conv1 = self.__biases([32]) + h_conv1 = self.act_fn(tf.nn.conv2d(self.input, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1) + h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + W_conv2 = self.__weights([5, 5, 32, 32]) + b_conv2 = self.__biases([32]) + h_conv2 = self.act_fn(tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], padding='SAME') + b_conv2) + h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + W_conv3 = self.__weights([5, 5, 32, 64]) + b_conv3 = self.__biases([64]) + h_conv3 = self.act_fn(tf.nn.conv2d(h_pool2, W_conv3, strides=[1, 1, 1, 1], padding='SAME') + b_conv3) + h_pool3 = tf.nn.max_pool(h_conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + W_fc1 = self.__weights([4 * 4 * 64, 1024]) + b_fc1 = self.__biases([1024]) + h_pool3_flat = tf.reshape(h_pool3, [-1, 4 * 4 * 64]) + h_fc1 = self.act_fn(tf.nn.xw_plus_b(h_pool3_flat, W_fc1, b_fc1)) + W_fc2 = self.__weights([1024, 512]) + b_fc2 = self.__biases([512]) + h_fc2 = self.act_fn(tf.nn.xw_plus_b(h_fc1, W_fc2, b_fc2)) + h_fc2_dropout = tf.nn.dropout(h_fc2, self.dropout) + W_fc3 = self.__weights([512, 10]) + b_fc3 = self.__biases([10]) + self.raw_scores = tf.nn.xw_plus_b(h_fc2_dropout, W_fc3, b_fc3) + self.probabilities = tf.nn.softmax(self.raw_scores) + self.prediction = tf.argmax(self.probabilities, 1) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.raw_scores, self.labels)) + self.train = tf.train.AdamOptimizer(0.0001).minimize(loss) + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.prediction, tf.argmax(self.labels, 1)), tf.float32)) + + def train_model(self, session, images, labels): + session.run(self.train, feed_dict={self.input: images, self.labels: labels, self.dropout: 0.5}) + + def predict(self, session, images): + return session.run(self.prediction, feed_dict={self.input: images, self.dropout: 1.0}) + + def get_accuracy(self, session, images, labels): + return session.run(self.accuracy, feed_dict={self.input: images, self.labels: labels, self.dropout: 1.0}) + + def __weights(self, shape): + return tf.Variable(tf.truncated_normal(shape, stddev=0.1)) + + def __biases(self, shape): + return tf.Variable(tf.constant(0.1, shape=shape)) diff --git a/dataset/preprocessed/353.txt b/dataset/preprocessed/353.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/353.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/354.txt b/dataset/preprocessed/354.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/354.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/355.txt b/dataset/preprocessed/355.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/355.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/356.txt b/dataset/preprocessed/356.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/356.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/357.txt b/dataset/preprocessed/357.txt new file mode 100644 index 0000000..3e2b334 --- /dev/null +++ b/dataset/preprocessed/357.txt @@ -0,0 +1,63 @@ +""" Uses Tensorflow to identify digits in the MNIST hand-written digit data set """ +""" Uses multiple fully-connected layers """ +import tensorflow as tf +import math +from tensorflow.examples.tutorials.mnist import input_data +IMAGE_PIXEL_COUNT = 784 +layer_sizes = [6, 12, 24, 200, 10] +weights = [tf.Variable(tf.truncated_normal([6, 6, 1, layer_sizes[0]], stddev=0.1)), tf.Variable(tf.truncated_normal([5, 5, layer_sizes[0], layer_sizes[1]], stddev=0.1)), tf.Variable(tf.truncated_normal([4, 4, layer_sizes[1], layer_sizes[2]], stddev=0.1)), tf.Variable(tf.truncated_normal([7 * 7 * layer_sizes[2], layer_sizes[3]], stddev=0.1)), tf.Variable(tf.truncated_normal([layer_sizes[3], + layer_sizes[4]], stddev=0.1))] +biases = [tf.Variable(tf.ones([layer_sizes[0]]) / 10), tf.Variable(tf.ones([layer_sizes[1]]) / 10), tf.Variable(tf.ones([layer_sizes[2]]) / 10), tf.Variable(tf.ones([layer_sizes[3]]) / 10), tf.Variable(tf.ones([layer_sizes[4]]) / 10)] +learning_rate_placeholder = tf.placeholder(tf.float32) +keep_probability = tf.placeholder(tf.float32) +TRAINING_KEEP_PROBABILITY = 0.75 +TEST_KEEP_PROBABILITY = 1.0 +mnist_data = input_data.read_data_sets('mnist_data/', reshape=False, one_hot=True) +images = tf.placeholder(tf.float32, [None, 28, 28, 1]) +layer1 = tf.nn.relu(tf.nn.conv2d(images, weights[0], strides=[1, 1, 1, 1], padding='SAME') + biases[0]) +layer2 = tf.nn.relu(tf.nn.conv2d(layer1, weights[1], strides=[1, 2, 2, 1], padding='SAME') + biases[1]) +layer3 = tf.nn.relu(tf.nn.conv2d(layer2, weights[2], strides=[1, 2, 2, 1], padding='SAME') + biases[2]) +reshaped_layer = tf.reshape(layer3, shape=[-1, 7 * 7 * layer_sizes[2]]) +layer4 = tf.nn.relu(tf.matmul(reshaped_layer, weights[3]) + biases[3]) +layer4_dropout = tf.nn.dropout(layer4, keep_probability) +logits = tf.matmul(layer4_dropout, weights[4]) + biases[4] +layer5 = tf.nn.softmax(logits) +model_answers = tf.placeholder(tf.float32, [None, 10]) +cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, model_answers) +cross_entropy = tf.reduce_mean(cross_entropy) * 100 +is_correct = tf.equal(tf.argmax(layer5, 1), tf.argmax(model_answers, 1)) +accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) +optimizer = tf.train.AdamOptimizer(learning_rate_placeholder) +training_step = optimizer.minimize(cross_entropy) +init = tf.global_variables_initializer() +session = tf.Session() +session.run(init) +training_results = [] +test_results = [] + + +def run_training_step(results, lr): + curr_images, currmodel_answers = mnist_data.train.next_batch(100) + training_data = {images: curr_images, model_answers: currmodel_answers, learning_rate_placeholder: lr, keep_probability: TRAINING_KEEP_PROBABILITY} + session.run(training_step, feed_dict=training_data) + results.append(session.run([accuracy, cross_entropy], feed_dict=training_data)) + + +def run_test_step(results, lr): + print('.', end='', flush=True) + results.append(session.run([accuracy, cross_entropy], feed_dict={images: mnist_data.test.images, model_answers: mnist_data.test.labels, learning_rate_placeholder: lr, keep_probability: TEST_KEEP_PROBABILITY})) + + +for i in range(10000): + min_learning_rate = 0.0001 + max_learning_rate = 0.003 + decay_speed = 2000.0 + lr = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i / decay_speed) + run_training_step(training_results, lr) + if i % 100 == 0: + run_test_step(test_results, lr) +run_test_step(test_results, lr) +for i, step in enumerate(training_results): + print('Training Step #' + str(i) + ': ' + str(step)) +for i, step in enumerate(test_results): + print('Test Step #' + str(i) + ': ' + str(step)) diff --git a/dataset/preprocessed/358.txt b/dataset/preprocessed/358.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/358.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/36.txt b/dataset/preprocessed/36.txt new file mode 100644 index 0000000..047fcac --- /dev/null +++ b/dataset/preprocessed/36.txt @@ -0,0 +1,584 @@ +""" +Created on Sun Aug 7 15:36:12 2016 + +@author: priyankadwivedi +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +os.chdir('/Users/priyankadwivedi') +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +import matplotlib +import matplotlib.pyplot as plt +n = 1000 +image_array = train_dataset[n] +image_array.shape +plt.imshow(image_array, cmap='Greys', interpolation='None') +print(train_labels[n]) +print(train_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +""" +## Problem 1: Introduce and tune a L2 regularization +## Start with the relu model from fully connected + +batch_size = 128 +num_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + # Variables. + weights_1 = tf.Variable( + tf.truncated_normal([image_size * image_size, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable( + tf.truncated_normal([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + # Training computation. + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + logits = tf.matmul(hidden1, weights_2) + biases_2 + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + # L2 regularization for the fully connected parameters. + regularizers = (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(biases_1) + + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(biases_2)) + # Add the regularization term to the loss. + factor = 5e-4 + loss += factor * regularizers + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax( + tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax( + tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) + +num_steps = 5001 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + +#Minibatch accuracy: 91.4% +#Validation accuracy: 86.1% +#Test accuracy: 92.9% + +# Problem 2: Extreme case of overfitting. Restrict your training data to just a few batches +batch_size = 20 +num_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + # Variables. + weights_1 = tf.Variable( + tf.truncated_normal([image_size * image_size, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable( + tf.truncated_normal([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + # Training computation. + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + logits = tf.matmul(hidden1, weights_2) + biases_2 + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + # L2 regularization for the fully connected parameters. + regularizers = (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(biases_1) + + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(biases_2)) + # Add the regularization term to the loss. + factor = 5e-4 + loss += factor * regularizers + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax( + tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax( + tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) + +num_steps = 3001 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + +#Minibatch accuracy: 85% +#Validation accuracy: 75.4%% +#Test accuracy: 83.6% +""" +""" +#Problem 4: Introduce dropout to the hidden layer. First with noregularization and then +# with regularization + + +batch_size = 256 +num_nodes = 1024 + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + # Variables. + weights_1 = tf.Variable( + tf.truncated_normal([image_size * image_size, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable( + tf.truncated_normal([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + # Training computation. + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + + #add dropout on hidden layer + #we pick up the probabylity of switching off the activation + #and perform the switch off of the activations + + keep_prob = tf.placeholder(tf.float32) + keep_prob = 0.50 + hidden_layer_drop = tf.nn.dropout(hidden1, keep_prob) + + #final layer takes in the hidden layer drop + logits = tf.matmul(hidden_layer_drop, weights_2) + biases_2 + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + # L2 regularization for the fully connected parameters. + regularizers = (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(biases_1) + + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(biases_2)) + # Add the regularization term to the loss. + factor = 5e-4 + loss += factor * regularizers + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax( + tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax( + tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) + +num_steps = 5001 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + +#Dropout but no L2 regularization. Keep only 50% of records.Batch size: 128 +#Minibatch accuracy: 77.3% +#Validation accuracy: 78.6% +#Test accuracy: 85.9% + +#Minibatch accuracy: 85.2% +#Validation accuracy: 85.0% +#Test accuracy: 91.6% +#L2 regularization increases accuray over dropout.But dropout has very low accuracy. So increasing batch size + +#Minibatch accuracy: 82.0% +#Validation accuracy: 85.8% +#Test accuracy: 92.8% +""" +""" +#Problem 5: Multi layer model with L2 regularization + +batch_size = 128 +n_hidden_1 = 1024 # 1st layer number of features +n_hidden_2 = 1024 # 2nd layer number of features + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + # Create model + def multilayer_perceptron(x, weights, biases): + # Hidden layer with Sigmoid activation + layer_1 = tf.matmul(x, weights['h1'])+ biases['b1'] + layer_1 = tf.nn.sigmoid(layer_1) + + # Hidden layer with RELU activation + layer_2 = tf.matmul(layer_1, weights['h2'])+ biases['b2'] + layer_2 = tf.nn.sigmoid(layer_2) + n + # Output layer with linear activation + out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + return out_layer + + # Variables. + # Store layers weight & bias + weights = { + 'h1': tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_1])), + 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.truncated_normal([n_hidden_2, num_labels])) + } + biases = { + 'b1': tf.Variable(tf.truncated_normal([n_hidden_1])), + 'b2': tf.Variable(tf.truncated_normal([n_hidden_2])), + 'out': tf.Variable(tf.truncated_normal([num_labels])) + } + + #Construct model + logits = multilayer_perceptron(tf_train_dataset, weights, biases) + + #Compute the loss function + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + # L2 regularization for the fully connected parameters. + regularizers = (tf.nn.l2_loss(weights['h1']) + tf.nn.l2_loss(biases['b1']) + + tf.nn.l2_loss(weights['h2']) + tf.nn.l2_loss(biases['b2']) + + tf.nn.l2_loss(weights['out']) + tf.nn.l2_loss(biases['out'])) + + # Add the regularization term to the loss. + factor = 5e-4 + loss += factor * regularizers + + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(multilayer_perceptron(tf_valid_dataset, weights, biases)) + test_prediction = tf.nn.softmax(multilayer_perceptron(tf_test_dataset, weights, biases)) + +num_steps = 5001 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + + +# 1 hidden layer and L2 regularization +#Minibatch accuracy: 91.4% +#Validation accuracy: 86.1% +#Test accuracy: 92.9% + +# 2 hidden layers and L2 regularization and sigmoid function and 512 nodes +#Minibatch accuracy: 89.1% +#Validation accuracy: 85.0% +#Test accuracy: 91.9% + +# 2 hidden layers and L2 regularization and sigmoid function and 1024 nodes. Much slower with no benefit! +#Minibatch accuracy: 89.1% +#Validation accuracy: 84.3% +#Test accuracy: 91.3% +""" +""" +#Problem 5.2: Multi layer model with L2 regularization and learning rate + +batch_size = 128 +n_hidden_1 = 512 # 1st layer number of features +n_hidden_2 = 512 # 2nd layer number of features + +graph = tf.Graph() +with graph.as_default(): + + # Input data. For the training data, we use a placeholder that will be fed + # at run time with a training minibatch. + tf_train_dataset = tf.placeholder(tf.float32, + shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + # Create model + def multilayer_perceptron(x, weights, biases): + # Hidden layer with Sigmoid activation + layer_1 = tf.matmul(x, weights['h1'])+ biases['b1'] + layer_1 = tf.nn.relu(layer_1) + + # Hidden layer with Sigmoid activation + layer_2 = tf.matmul(layer_1, weights['h2'])+ biases['b2'] + layer_2 = tf.nn.sigmoid(layer_2) + + # Output layer with linear activation + out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + return out_layer + + # Variables. + # Store layers weight & bias + weights = { + 'h1': tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_1])), + 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.truncated_normal([n_hidden_2, num_labels])) + } + biases = { + 'b1': tf.Variable(tf.truncated_normal([n_hidden_1])), + 'b2': tf.Variable(tf.truncated_normal([n_hidden_2])), + 'out': tf.Variable(tf.truncated_normal([num_labels])) + } + + #Construct model + logits = multilayer_perceptron(tf_train_dataset, weights, biases) + + #Compute the loss function + loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + + + # L2 regularization for the fully connected parameters. + regularizers = (tf.nn.l2_loss(weights['h1']) + tf.nn.l2_loss(biases['b1']) + + tf.nn.l2_loss(weights['h2']) + tf.nn.l2_loss(biases['b2']) + + tf.nn.l2_loss(weights['out']) + tf.nn.l2_loss(biases['out'])) + + # Add the regularization term to the loss. + factor = 5e-4 + loss += factor * regularizers + + # Define learning rate + global_step = tf.Variable(0, trainable=False) + starter_learning_rate = 0.5 + learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, + 1000, 0.90, staircase=True) + # Optimizer. + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + + # Predictions for the training, validation, and test data. + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(multilayer_perceptron(tf_valid_dataset, weights, biases)) + test_prediction = tf.nn.softmax(multilayer_perceptron(tf_test_dataset, weights, biases)) + +num_steps = 5001 + +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print("Initialized") + for step in range(num_steps): + # Pick an offset within the training data, which has been randomized. + # Note: we could use better randomization across epochs. + offset = (step * batch_size) % (train_labels.shape[0] - batch_size) + # Generate a minibatch. + batch_data = train_dataset[offset:(offset + batch_size), :] + batch_labels = train_labels[offset:(offset + batch_size), :] + # Prepare a dictionary telling the session where to feed the minibatch. + # The key of the dictionary is the placeholder node of the graph to be fed, + # and the value is the numpy array to feed to it. + feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} + _, l, predictions = session.run( + [optimizer, loss, train_prediction], feed_dict=feed_dict) + if (step % 500 == 0): + print("Minibatch loss at step %d: %f" % (step, l)) + print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) + print("Validation accuracy: %.1f%%" % accuracy( + valid_prediction.eval(), valid_labels)) + print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) + + +# Multi layer and L2 regularization and learning rate decay rate of 0.96 +#Minibatch accuracy: 87.5% +#Validation accuracy: 85.2% +#Test accuracy: 92.0% + +# Multi layer and L2 regularization and learning rate decay rate of 0.50 +#Minibatch accuracy: 78.9% +#Validation accuracy: 80.5% +#Test accuracy: 87.5% + +# Multi layer and L2 regularization and learning rate decay rate of 0.90. +# 1 hidden layer has relu and the other sigmoid +#Minibatch accuracy: 78.9% +#Validation accuracy: 80.5% +#Test accuracy: 87.5% + +""" +batch_size = 128 +n_hidden_1 = 512 +n_hidden_2 = 512 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + + def multilayer_perceptron(x, weights, biases): + layer_1 = tf.matmul(x, weights['h1']) + biases['b1'] + layer_1 = tf.nn.relu(layer_1) + layer_2 = tf.matmul(layer_1, weights['h2']) + biases['b2'] + layer_2 = tf.nn.sigmoid(layer_2) + out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + return out_layer + weights = {'h1': tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_1])), 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])), 'out': tf.Variable(tf.truncated_normal([n_hidden_2, num_labels]))} + biases = {'b1': tf.Variable(tf.truncated_normal([n_hidden_1])), 'b2': tf.Variable(tf.truncated_normal([n_hidden_2])), 'out': tf.Variable(tf.truncated_normal([num_labels]))} + logits = multilayer_perceptron(tf_train_dataset, weights, biases) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + regularizers = tf.nn.l2_loss(weights['h1']) + tf.nn.l2_loss(biases['b1']) + tf.nn.l2_loss(weights['h2']) + tf.nn.l2_loss(biases['b2']) + tf.nn.l2_loss(weights['out']) + tf.nn.l2_loss(biases['out']) + factor = 0.0005 + loss += factor * regularizers + global_step = tf.Variable(0, trainable=False) + starter_learning_rate = 0.5 + learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, 3500, 0.86, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(multilayer_perceptron(tf_valid_dataset, weights, biases)) + test_prediction = tf.nn.softmax(multilayer_perceptron(tf_test_dataset, weights, biases)) +num_steps = 20001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 1000 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/360.txt b/dataset/preprocessed/360.txt new file mode 100644 index 0000000..512e82b --- /dev/null +++ b/dataset/preprocessed/360.txt @@ -0,0 +1,49 @@ +import tensorflow as tf +import numpy as np +from scipy.stats import norm + + +def fnn(x, output_dim): + w1 = tf.Variable(tf.random_normal([10, 20], stddev=0.35), name='weights1') + b1 = tf.Variable(tf.zeros([20]), name='biases1') + w2 = tf.Variable(tf.random_normal([20, output_dim], stddev=0.35), name='weights2') + b2 = tf.Variable(tf.zeros([20]), name='biases2') + y1 = tf.nn.relu(tf.matmul(x, w1) + b1) + y2 = tf.nn.sigmoid(tf.matmul(y1, w2) + b2) + return y2, [w1, w2] + + +x1 = tf.placeholder(tf.float32, shape=(1, 10)) +y1, w1 = fnn(x1, 1) +x2 = tf.placeholder(tf.float32, shape=(1, 10)) +y2, w2 = fnn(x2, 1) +with tf.Session() as sess: + tf.initialize_all_variables().run() + theta1, theta2 = sess.run([w1, w2], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])}) + print(theta1) + print(theta2) + + +def linear(x, out_dim, name, activation_fn=None): + with tf.variable_scope(name): + w = tf.get_variable(name='weights', shape=[x.get_shape()[1], out_dim], dtype=tf.float32, initializer=tf.random_normal_initializer()) + b = tf.get_variable(name='biases', shape=[out_dim], dtype=tf.float32, initializer=tf.constant_initializer(0.0)) + out = tf.matmul(x, w) + b + if activation_fn != None: + out = activation_fn(out) + return out, [w, b] + + +with tf.variable_scope('ffn') as scope: + x1 = tf.placeholder(tf.float32, shape=(1, 10)) + y11, theta11 = linear(x1, 10, name='h', activation_fn=tf.nn.relu) + y12, theta12 = linear(y1, 1, name='out', activation_fn=tf.nn.sigmoid) + scope.reuse_variables() + x2 = tf.placeholder(tf.float32, shape=(1, 10)) + y21, theta21 = linear(x2, 10, name='h', activation_fn=tf.nn.relu) + y22, theta22 = linear(y1, 1, name='out', activation_fn=tf.nn.sigmoid) +with tf.Session() as sess: + tf.initialize_all_variables().run() + theta1, theta2 = sess.run([theta12, theta22], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])}) + print(theta1[0]) + print(theta2[0]) diff --git a/dataset/preprocessed/361.txt b/dataset/preprocessed/361.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/361.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/362.txt b/dataset/preprocessed/362.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/362.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/363.txt b/dataset/preprocessed/363.txt new file mode 100644 index 0000000..c92b0a7 --- /dev/null +++ b/dataset/preprocessed/363.txt @@ -0,0 +1,74 @@ +import Common +from DataPreprocessing import resize +from DataPreprocessing import labelsToOutputs +from DataPreprocessing import createMapping +import Helper as help +import tensorflow as tf +import CNN +beta = 0.01 +epochs = 100 +batchSize = 512 +learningRate = 0.001 +testSamplesNumber = 50 +classes = range(10, 30) +samplesNumber = 200 +inputImgSize = 16, 16 +filtersShape = [(3, 3, 3, 32), (3, 3, 32, 64)] +biases = {'bc1': tf.get_variable('B0', shape=32, initializer=tf.contrib.layers.xavier_initializer()), 'bc2': tf.get_variable('B1', shape=64, initializer=tf.contrib.layers.xavier_initializer()), 'bd1': tf.get_variable('B2', shape=64, initializer=tf.contrib.layers.xavier_initializer()), 'out': tf.get_variable('B3', shape=len(classes), initializer=tf.contrib.layers.xavier_initializer())} +kwargs = {'inputImgSize': inputImgSize, 'outputsNumber': len(classes), 'convLayersNumber': 2, 'filtersShape': filtersShape, 'bias': biases} +images, labels = Common.readTrafficSigns('../Data/GTSRB_Final_Training_Images/GTSRB/Final_Training/Images', classes, samplesNumber) +images = resize(images, inputImgSize) +outputs = labelsToOutputs(classes, labels) +images, outputs = Common.shuffle(images, outputs) +testImages, testLabels = Common.readTestImages('../Data/GTSRB_Final_Test_Images/GTSRB/Final_Test/Images', classes, testSamplesNumber) +testImages = resize(testImages, inputImgSize) +testOutputs = labelsToOutputs(classes, testLabels) +if len(testImages) != len(testOutputs): + raise Exception +if not all([any(x) for x in testOutputs]): + raise ValueError('Not all outputs have value') +classesMapping = createMapping(classes) +cNN = CNN.CNN(**kwargs) +batchesX = [images[batch:batch + batchSize] for batch in range(0, len(images) - batchSize, batchSize)] +batchesY = [outputs[batch:batch + batchSize] for batch in range(0, len(outputs) - batchSize, batchSize)] +if len(batchesX) != len(batchesY): + raise Exception +pred = cNN.conv_net(cNN.xPlaceholder, cNN.keep_prob) +loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=cNN.yPlaceholder)) +classified_indexes = tf.argmax(pred, 1) +expected_indexes = tf.argmax(cNN.yPlaceholder, 1) +expectedAndClassified = tf.stack([expected_indexes, classified_indexes], axis=1) +correct_prediction = tf.equal(expected_indexes, classified_indexes) +regularizer = tf.nn.l2_loss(cNN.WEIGHTS['wc1']) + tf.nn.l2_loss(cNN.WEIGHTS['wc2']) + tf.nn.l2_loss(cNN.WEIGHTS['wd1']) + tf.nn.l2_loss(cNN.WEIGHTS['out']) +loss = tf.reduce_mean(loss + beta * regularizer) +optimizer = tf.train.AdamOptimizer(learning_rate=learningRate).minimize(loss) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +init = tf.global_variables_initializer() +with tf.Session() as session: + session.run(init) + train_loss_global = [] + train_accuracy_global = [] + test_loss = [] + test_accuracy = [] + for i in range(epochs): + train_loss = [] + train_accuracy = [] + for k in range(len(batchesX)): + trainBatchX = batchesX[k] + trainBatchY = batchesY[k] + op = cNN.run(session, optimizer, trainBatchX, trainBatchY, 0.5) + iteration_loss, acc = cNN.run(session, [loss, accuracy], trainBatchX, trainBatchY, 1.0) + train_accuracy.append(acc) + train_loss.append(iteration_loss) + avg_train_loss = sum(train_loss) / len(train_loss) + avg_train_acc = sum(train_accuracy) / len(train_accuracy) + print('Epoch ' + str(i) + ', Loss= ' + '{:.6f}'.format(avg_train_loss) + ', Training Accuracy= ' + '{:.5f}'.format(avg_train_acc)) + valid_loss, test_acc, expToClass = cNN.run(session, [loss, accuracy, expectedAndClassified], testImages, testOutputs, 1.0) + print('Testing Accuracy:', '{:.5f}'.format(test_acc)) + print('Testing Loss:', '{:.5f}'.format(valid_loss)) + train_accuracy_global.append(avg_train_acc * 100.0) + train_loss_global.append(avg_train_loss) + test_accuracy.append(test_acc * 100.0) + test_loss.append(valid_loss) +Common.displayClassificationPlot(help.createClassifiedAsList(expToClass), classes) +Common.displayResults(epochs, train_accuracy_global, train_loss_global, test_accuracy, test_loss) diff --git a/dataset/preprocessed/364.txt b/dataset/preprocessed/364.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/364.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/365.txt b/dataset/preprocessed/365.txt new file mode 100644 index 0000000..c380988 --- /dev/null +++ b/dataset/preprocessed/365.txt @@ -0,0 +1,317 @@ +""" +Created on Sun Aug 12 15:18:20 2018 + +@author: Arc +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +beta = 0.008 +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=logits)) + l2_loss = tf.nn.l2_loss(weights) + loss = loss + beta * l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy(predictions, train_labels[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +beta = 0.018 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_nodes = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + hidden_layer = tf.nn.relu(hidden_layer) + logits = tf.matmul(hidden_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=logits)) + l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + loss = loss + beta * l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +beta = 0.018 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_nodes = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + hidden_layer = tf.nn.relu(hidden_layer) + logits = tf.matmul(hidden_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=logits)) + l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + loss = loss + beta * l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset_limit = 300 + offset = step * batch_size % offset_limit + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +beta = 0.001 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_nodes = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + hidden_layer = tf.nn.relu(hidden_layer) + keep_prob = tf.placeholder(tf.float32) + hidden_layer_dropout = tf.nn.dropout(hidden_layer, keep_prob) + logits_training = tf.matmul(hidden_layer_dropout, weights) + biases + logits = tf.matmul(hidden_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=logits_training)) + l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + loss = loss + beta * l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 14001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2000 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +beta = 0.001 +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_nodes = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + hidden_biases = tf.Variable(tf.zeros([hidden_nodes])) + weights = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + hidden_layer = tf.add(tf.matmul(tf_train_dataset, hidden_weights), hidden_biases) + hidden_layer = tf.nn.relu(hidden_layer) + keep_prob = tf.placeholder(tf.float32) + hidden_layer_training = tf.nn.dropout(hidden_layer, keep_prob) + logits_training = tf.matmul(hidden_layer_training, weights) + biases + logits = tf.matmul(hidden_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=logits_training)) + l2_loss = tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(weights) + loss = loss + beta * l2_loss + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.3, global_step, 1000, 0.9, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_valid_dataset, hidden_weights), hidden_biases)) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_layer, weights) + biases) + test_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tf_test_dataset, hidden_weights), hidden_biases)) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden_layer, weights) + biases) +num_steps = 20001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 1000 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +beta = 0.001 +batch_size = 128 +layer_depth = 5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_nodes = 1024 + stddev = np.sqrt(2 / hidden_nodes) + weights0 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes], stddev=stddev)) + biases0 = tf.Variable(tf.zeros([hidden_nodes])) + hidden_nodes_pre = hidden_nodes + hidden_weights = [] + hidden_biases = [] + for i in range(layer_depth - 2): + if hidden_nodes_pre > 2: + hidden_nodes_cur = int(hidden_nodes_pre // 2) + else: + hidden_nodes_cur == hidden_nodes_pre + stddev = np.sqrt(2 / hidden_nodes_cur) + hidden_weights.append(tf.Variable(tf.truncated_normal([hidden_nodes_pre, hidden_nodes_cur], stddev=stddev))) + hidden_biases.append(tf.Variable(tf.zeros([hidden_nodes_cur]))) + hidden_nodes_pre = hidden_nodes_cur + weights = tf.Variable(tf.truncated_normal([hidden_nodes_cur, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + y0 = tf.add(tf.matmul(tf_train_dataset, weights0), biases0) + y0 = tf.nn.relu(y0) + y0_training = tf.nn.dropout(y0, 0.5) + valid_y0 = tf.matmul(tf_valid_dataset, weights0) + biases0 + valid_y0 = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(tf_test_dataset, weights0) + biases0 + test_y0 = tf.nn.relu(test_y0) + yi_training = y0_training + hidden_layer = y0 + valid_hidden_layer = valid_y0 + test_hidden_layer = test_y0 + for i in range(layer_depth - 2): + yi_training = tf.nn.relu(tf.matmul(yi_training, hidden_weights[i]) + hidden_biases[i]) + keep_prob = 0.5 * (1 + (i + 1) / (layer_depth - 1)) + hidden_layer_training = tf.nn.dropout(yi_training, keep_prob) + hidden_layer = tf.nn.relu(tf.matmul(hidden_layer, hidden_weights[i]) + hidden_biases[i]) + valid_hidden_layer = tf.nn.relu(tf.matmul(valid_hidden_layer, hidden_weights[i]) + hidden_biases[i]) + test_hidden_layer = tf.nn.relu(tf.matmul(test_hidden_layer, hidden_weights[i]) + hidden_biases[i]) + logits_training = tf.matmul(hidden_layer_training, weights) + biases + logits = tf.matmul(hidden_layer, weights) + biases + valid_logits = tf.matmul(valid_hidden_layer, weights) + biases + test_logits = tf.matmul(test_hidden_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_train_labels, logits=logits_training)) + l2_loss = tf.nn.l2_loss(weights0) + tf.nn.l2_loss(weights) + for i in range(layer_depth - 2): + l2_loss += tf.nn.l2_loss(hidden_weights[i]) + loss = loss + beta * l2_loss + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.1, global_step, 1000, 0.8, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(valid_logits) + test_prediction = tf.nn.softmax(test_logits) +num_steps = 18001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/366.txt b/dataset/preprocessed/366.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/366.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/367.txt b/dataset/preprocessed/367.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/367.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/368.txt b/dataset/preprocessed/368.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/368.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/369.txt b/dataset/preprocessed/369.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/369.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/37.txt b/dataset/preprocessed/37.txt new file mode 100644 index 0000000..d63f4f6 --- /dev/null +++ b/dataset/preprocessed/37.txt @@ -0,0 +1,200 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = '/home/noa.bratman/FERProject/udacity-DL/data/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + l = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = l + 0.001 * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + biases1 = tf.Variable(tf.zeros([1024])) + weights2 = tf.Variable(tf.truncated_normal([1024, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1), weights2) + biases2 + l = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = l + 0.001 * tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + l = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = l + 0.001 * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 1000 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + biases1 = tf.Variable(tf.zeros([1024])) + weights2 = tf.Variable(tf.truncated_normal([1024, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1), weights2) + biases2 + l = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = l + 0.001 * tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) +num_steps = 2000 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + biases1 = tf.Variable(tf.zeros([1024])) + weights2 = tf.Variable(tf.truncated_normal([1024, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf.nn.relu(tf.nn.dropout(tf.matmul(tf_train_dataset, weights1) + biases1, 0.5)), weights2) + biases2 + l = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = l + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) +num_steps = 2000 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/370.txt b/dataset/preprocessed/370.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/370.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/371.txt b/dataset/preprocessed/371.txt new file mode 100644 index 0000000..e818015 --- /dev/null +++ b/dataset/preprocessed/371.txt @@ -0,0 +1,137 @@ +import os +import random +import skimage.data +import skimage.transform +import matplotlib +import matplotlib.pyplot as plt +import numpy as np +import tensorflow as tf +from tensorflow.contrib.layers import flatten +import cv2 +import time +ROOT = 'A:' +training_imgs = os.path.join(ROOT, '/DataMining/training') +test_imgs = os.path.join(ROOT, '/DataMining/testing') + + +def data_load(dir_path): + dirs = [dir for dir in os.listdir(dir_path) if os.path.isdir(os.path.join(dir_path, dir))] + classes = [] + img = [] + for d in dirs: + label_dir = os.path.join(dir_path, d) + file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if f.endswith('.ppm')] + for f in file_names: + img.append(skimage.data.imread(f)) + classes.append(int(d)) + return img, classes + + +images, classes = data_load(training_imgs) +nclasses = len(set(classes)) +print("""The number of unique classes are: {0} +Count of images in all classes: {1}""".format(nclasses, len(images))) + + +def sample_from_class(images, classes): + tot_class = set(classes) + plt.figure(figsize=(20, 20)) + i = 1 + for label in tot_class: + img = images[classes.index(label)] + plt.subplot(10, 7, i) + plt.axis('off') + plt.title('class {0}, Num imgs {1}'.format(label, classes.count(label))) + i += 1 + _ = plt.imshow(img) + plt.show() + + +print(""" + +Images from each class before preprocessing""") +sample_from_class(images, classes) +print(""" +Pre-processing the data""") +print(""" +Images after resizing""") +resized_imgs = [skimage.transform.resize(image, (32, 32), mode='constant') for image in images] +sample_from_class(resized_imgs, classes) +ready_classes = np.array(classes) +ready_images = np.array(resized_imgs) + + +def net_architecture(img_ph): + mymean = 0 + mydev = 0.1 + weights_conv1 = tf.Variable(tf.truncated_normal(shape=(5, 5, 3, 6), mean=mymean, stddev=mydev), name='weights_conv1') + bias_conv1 = tf.Variable(tf.zeros(6), name='bias_conv1') + layer1_out = tf.nn.conv2d(img_ph, weights_conv1, strides=[1, 1, 1, 1], padding='VALID') + bias_conv1 + layer1_out = tf.nn.relu(layer1_out) + layer1_out = tf.nn.max_pool(layer1_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') + weights_conv2 = tf.Variable(tf.truncated_normal(shape=(5, 5, 6, 16), mean=mymean, stddev=mydev), name='weights_conv2') + bias_conv2 = tf.Variable(tf.zeros(16), name='bias_conv2') + conv2_out = tf.nn.conv2d(layer1_out, weights_conv2, strides=[1, 1, 1, 1], padding='VALID') + bias_conv2 + conv2_out = tf.nn.relu(conv2_out) + conv2_out = tf.nn.max_pool(conv2_out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') + fc0 = flatten(conv2_out) + weights_fullyconn = tf.Variable(tf.truncated_normal(shape=(400, 120), mean=mymean, stddev=mydev), name='weights_fullyconn') + bias_fullyconn = tf.Variable(tf.zeros(120), name='bias_fullyconn') + fullyconn_out = tf.matmul(fc0, weights_fullyconn) + bias_fullyconn + fullyconn_out = tf.nn.relu(fullyconn_out) + fullyconn2_weights = tf.Variable(tf.truncated_normal(shape=(120, 84), mean=mymean, stddev=mydev), name='fullyconn2_weights') + fullyconn2_bias = tf.Variable(tf.zeros(84), name='fullyconn2_bias') + fullyconn2_out = tf.matmul(fullyconn_out, fullyconn2_weights) + fullyconn2_bias + fullyconn2_out = tf.nn.relu(fullyconn2_out) + hidden_layer = tf.nn.dropout(fullyconn2_out, keep_prob) + fullyconn3_weights = tf.Variable(tf.truncated_normal(shape=(84, nclasses), mean=mymean, stddev=mydev), name='fullyconn3_weights') + fullyconn3_bias = tf.Variable(tf.zeros(nclasses), name='fullyconn3_bias') + logits = tf.matmul(fullyconn2_out, fullyconn3_weights) + fullyconn3_bias + return logits + + +graph = tf.Graph() +with graph.as_default(): + placeholder_img = tf.placeholder(tf.float32, [None, 32, 32, 3]) + placeholder_classes = tf.placeholder(tf.int32, [None]) + keep_prob = tf.placeholder(tf.float32) + logits = net_architecture(placeholder_img) + classify_labels = tf.argmax(logits, 1) + err = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=placeholder_classes)) + train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(err) + init = tf.global_variables_initializer() +session = tf.Session(graph=graph) +_ = session.run([init]) +start = time.time() +for i in range(201): + _, err_value = session.run([train, err], feed_dict={placeholder_img: ready_images, placeholder_classes: ready_classes}) + if i % 10 == 0: + print('error: ', err_value) +end = time.time() +tot = end - start +print(""" +Time taken for training the model is {0} +""".format(tot)) +try_random_20 = random.sample(range(len(resized_imgs)), 20) +img_to_be_classified = [resized_imgs[i] for i in try_random_20] +sample_classes = [classes[i] for i in try_random_20] +classify = session.run([classify_labels], feed_dict={placeholder_img: img_to_be_classified})[0] +classiedfig = plt.figure(figsize=(20, 20)) +for img in range(len(img_to_be_classified)): + actual_class = sample_classes[img] + after_classification = classify[img] + plt.subplot(10, 2, 1 + img) + plt.axis('off') + val = 'true' if actual_class == after_classification else 'false' + plt.text(80, 20, 'actual: {0}\nclassified: {1}\ntruth: {2}'.format(actual_class, after_classification, val), fontsize=12) + plt.imshow(img_to_be_classified[img]) +test_images, test_classes = data_load(test_imgs) +resized_test = [skimage.transform.resize(image, (32, 32), mode='constant') for image in test_images] +sample_from_class(resized_test, test_classes) +classify = session.run([classify_labels], feed_dict={placeholder_img: resized_test})[0] +correct_matches = sum([int(actual == classified) for actual, classified in zip(test_classes, classify)]) +print(""" +The correct matches were {0} out of a total of {1} test images.""".format(correct_matches, len(test_classes))) +correctness = correct_matches / len(test_classes) * 100 +print('The accuracy of the system is: {0} percent'.format(correctness)) +session.close() diff --git a/dataset/preprocessed/372.txt b/dataset/preprocessed/372.txt new file mode 100644 index 0000000..6d46bec --- /dev/null +++ b/dataset/preprocessed/372.txt @@ -0,0 +1,152 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss += 0.05 * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 12 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases_1 = tf.Variable(tf.zeros([hidden_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + def forward_prop(input): + h_layer = tf.nn.relu(tf.matmul(input, weights_1) + biases_1) + return tf.matmul(h_layer, weights_2) + biases_2 + logits = forward_prop(tf_train_dataset) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss += 0.005 * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(forward_prop(tf_valid_dataset)) + test_prediction = tf.nn.softmax(forward_prop(tf_test_dataset)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_keep_prob = tf.placeholder(tf.float32) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases_1 = tf.Variable(tf.zeros([hidden_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + + def forward_prop_dropout(input): + h_layer = tf.nn.relu(tf.matmul(input, weights_1) + biases_1) + drop = tf.nn.dropout(h_layer, tf_keep_prob) + return tf.matmul(drop, weights_2) + biases_2 + + def forward_prop(input): + h_layer = tf.nn.relu(tf.matmul(input, weights_1) + biases_1) + return tf.matmul(h_layer, weights_2) + biases_2 + logits = forward_prop_dropout(tf_train_dataset) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss += 0.005 * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(forward_prop(tf_valid_dataset)) + test_prediction = tf.nn.softmax(forward_prop(tf_test_dataset)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/373.txt b/dataset/preprocessed/373.txt new file mode 100644 index 0000000..bfa660e --- /dev/null +++ b/dataset/preprocessed/373.txt @@ -0,0 +1,172 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_datasets = save['train_dataset'] + train_labels = save['train_labels'] + valid_datasets = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_datasets = save['test_dataset'] + test_labels = save['test_labels'] +print(train_datasets) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_datasets, train_labels = reformat(train_datasets, train_labels) +valid_datasets, valid_labels = reformat(valid_datasets, valid_labels) +test_dataset, test_labels = reformat(test_datasets, test_labels) +print('Training set', train_datasets.shape, train_labels.shape) +print('Validation set', valid_datasets.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +train_subset = 10000 +beta = 0.01 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_datasets[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_datasets) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + baises = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + baises + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + l2 = tf.nn.l2_loss(weights) + loss = tf.reduce_mean(loss + beta * l2) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + baises) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + baises) + num_steps = 1000 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + _, l, prediction = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('loss at step{}: {}'.format(step, l)) + print('Training accuracy: {:.1f}'.format(accuracy(prediction, train_labels[:train_subset, :]))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) +num_nodes = 1024 +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_datasets) + tf_test_dataset = tf.constant(test_dataset) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + relu_layer = tf.nn.relu(logits_1) + logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_2, labels=tf_train_labels)) + regularizers = tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + loss = tf.reduce_mean(loss + beta * regularizers) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + logits_1 = tf.matmul(tf_valid_dataset, weights_1) + biases_1 + relu_layer = tf.nn.relu(logits_1) + logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + valid_prediction = tf.nn.softmax(logits_2) + logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + relu_layer = tf.nn.relu(logits_1) + logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + test_prediction = tf.nn.softmax(logits_2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for steps in range(num_steps): + offset = steps * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_datasets[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, prediction = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if steps % 500 == 0: + print('Minibatch loss at step {}: {}'.format(steps, l)) + print('Minibatch accuracy: {:.1f} '.format(accuracy(prediction, batch_labels))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) +train_datasets_2 = train_datasets[:500, :] +train_labels_2 = train_labels[:500, :] +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for steps in range(num_steps): + offset = steps * batch_size % (train_labels_2.shape[0] - batch_size) + batch_data = train_datasets_2[offset:offset + batch_size, :] + batch_labels = train_labels_2[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, prediction = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if steps % 500 == 0: + print('Minibatch loss at step {}: {}'.format(steps, l)) + print('Minibatch accuracy: {:.1f} '.format(accuracy(prediction, batch_labels))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) +num_nodes = 1024 +batch_size = 128 +beta = 0.01 +graph = tf.Graph() +with tf.Session(graph=graph) as session: + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_datasets) + tf_test_dataset = tf.constant(test_dataset) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + baises_1 = tf.Variable(tf.zeros([num_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + baises_2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + baises_1 + relu_layer = tf.nn.relu(logits_1) + keep_prob = tf.placeholder('float') + relu_layer_dropout = tf.nn.dropout(relu_layer, keep_prob) + logits_2 = tf.matmul(relu_layer_dropout, weights_2) + baises_2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_2, labels=tf_train_labels)) + regularizers = tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + loss = tf.reduce_mean(loss + beta * regularizers) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + logits_1 = tf.matmul(tf_valid_dataset, weights_1) + baises_1 + relu_layer = tf.nn.relu(logits_1) + logits_2 = tf.matmul(relu_layer, weights_2) + baises_2 + valid_prediction = tf.nn.softmax(logits_2) + logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + relu_layer = tf.nn.relu(logits_1) + logits_2 = tf.matmul(relu_layer, weights_2) + biases_2 + test_prediction = tf.nn.softmax(logits_2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialised') + for steps in range(num_steps): + offset = steps * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_datasets[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step {}: {}'.format(step, l)) + print('Minibatch accuracy: {:.1f}'.format(accuracy(predictions, batch_labels))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) diff --git a/dataset/preprocessed/374.txt b/dataset/preprocessed/374.txt new file mode 100644 index 0000000..d942be9 --- /dev/null +++ b/dataset/preprocessed/374.txt @@ -0,0 +1,142 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + + +_print_success_message() diff --git a/dataset/preprocessed/375.txt b/dataset/preprocessed/375.txt new file mode 100644 index 0000000..9ce7e5e --- /dev/null +++ b/dataset/preprocessed/375.txt @@ -0,0 +1,84 @@ +import tensorflow as tf +import numpy as np +dis = 0.9 + + +class CNN: + + def __init__(self, session, input_size, output_size, name='main'): + self.session = session + self.input_size = input_size + self.output_size = output_size + self.net_name = name + self._filter_size = 3 + self._keep_prob = tf.placeholder(tf.float32) + self._build_network() + + def _build_network(self, h_size=10, l_rate=0.1): + self._X = tf.placeholder(tf.float32, [None, 7, 7, 1], name='input_x') + W1 = tf.get_variable('W1', shape=[3, 3, 1, 32], initializer=tf.contrib.layers.xavier_initializer()) + print(W1) + layer1 = tf.nn.conv2d(self._X, W1, strides=[1, 1, 1, 1], padding='SAME') + layer1 = tf.nn.relu(layer1) + print(layer1) + layer1 = tf.nn.max_pool(layer1, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME') + print(layer1) + layer1 = tf.nn.dropout(layer1, keep_prob=self._keep_prob) + W2 = tf.get_variable('W2', shape=[3, 3, 32, 64], initializer=tf.contrib.layers.xavier_initializer()) + print(W2) + layer2 = tf.nn.conv2d(layer1, W2, strides=[1, 1, 1, 1], padding='SAME') + layer2 = tf.nn.relu(layer2) + print(layer2) + layer2 = tf.nn.max_pool(layer2, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME') + print(layer2) + layer2 = tf.nn.dropout(layer2, keep_prob=self._keep_prob) + W3 = tf.get_variable('W3', shape=[7 * 7 * 64, 512], initializer=tf.contrib.layers.xavier_initializer()) + print(W3) + layer3 = tf.reshape(layer2, [-1, 7 * 7 * 64]) + layer3 = tf.matmul(layer3, W3) + layer3 = tf.nn.relu(layer3) + print(layer3) + layer3 = tf.nn.dropout(layer3, keep_prob=self._keep_prob) + W4 = tf.get_variable('W4', shape=[512, 45], initializer=tf.contrib.layers.xavier_initializer()) + self._l_pred = tf.matmul(layer3, W4) + print(W4) + self._Y = tf.placeholder(tf.float32, [None, self.output_size], name='output_y') + self._loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self._l_pred, labels=self._Y)) + self._train = tf.train.AdamOptimizer(learning_rate=l_rate).minimize(self._loss) + + def predict(self, x): + return self.session.run(self._l_pred, feed_dict={self._X: x, self._keep_prob: 0.7}) + + def update(self, x, y): + return self.session.run([self._loss, self._train], feed_dict={self._X: x, self._Y: y, self._keep_prob: 0.7}) + + +class RNN: + + def __init__(self, session, n_input, n_step, n_output, name='main'): + self.session = session + self._n_input = n_input + self._n_output = n_output + self._n_step = n_step + self.net_name = name + self._n_hidden = 256 + self._build_network() + + def _build_network(self, h_size=10, l_rate=0.1): + self._X = tf.placeholder(tf.float32, [None, self._n_step, self._n_input], name='input_x') + self._Y = tf.placeholder(tf.float32, [None, self._n_output], name='output_y') + W = tf.get_variable('W', shape=[self._n_hidden, self._n_output], initializer=tf.contrib.layers.xavier_initializer()) + b = tf.get_variable('b', shape=[self._n_output], initializer=tf.contrib.layers.xavier_initializer()) + cell = tf.nn.rnn_cell.BasicLSTMCell(self._n_hidden) + outputs, states = tf.nn.dynamic_rnn(cell, self._X, dtype=tf.float32) + outputs = tf.transpose(outputs, [1, 0, 2]) + outputs = outputs[-1] + self._model = tf.matmul(outputs, W) + b + self._cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self._model, labels=self._Y)) + self._train = tf.train.AdamOptimizer(learning_rate=l_rate).minimize(self._cost) + + def predict(self, x): + return self.session.run(self._model, feed_dict={self._X: x}) + + def update(self, x, y): + return self.session.run([self._cost, self._train], feed_dict={self._X: x, self._Y: y}) diff --git a/dataset/preprocessed/376.txt b/dataset/preprocessed/376.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/376.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/377.txt b/dataset/preprocessed/377.txt new file mode 100644 index 0000000..446aa9a --- /dev/null +++ b/dataset/preprocessed/377.txt @@ -0,0 +1,275 @@ +import os +import numpy as np +import tensorflow as tf +import types +from tqdm import tqdm +from layers import * +CONFIG = types.SimpleNamespace() +CONFIG.saveDir = './saves/' +CONFIG.diffSaveDir = './diffSaves/' +CONFIG.logDir = './logs/' +CONFIG.maxImages = 200 +HYPERPARAMETER = types.SimpleNamespace() +HYPERPARAMETER.learningRate = 0.001 +HYPERPARAMETER.batchSize = CONFIG.maxImages +HYPERPARAMETER.epoch = 500 +HYPERPARAMETER.latentSpace = 15 +HYPERPARAMETER.diffLatentSpace = 5 +HYPERPARAMETER.contrast = 100 +HYPERPARAMETER.normByImage = True +HYPERPARAMETER.normalize = 'normal' + + +def SET_HYPERPARAMETER(key, value): + global HYPERPARAMETER + HYPERPARAMETER.__dict__[key] = value + + +def jtektModel(name, data, log=True, shuffle=True): + rawTrainSet, _ = data.getAll(data.goodOnly) + rawTestSet, _ = data.getAll(data.badOnly) + if shuffle is True: + np.random.shuffle(rawTrainSet) + np.random.shuffle(rawTestSet) + trainSet = rawTrainSet + testSet = rawTestSet + return Model(name, trainSet, testSet, log=log) + + +def mnistModel(name, log=True): + (rawTrainSet, trainLabels), (rawTestSet, testLabels) = tf.keras.datasets.mnist.load_data() + trainSet = np.expand_dims(rawTrainSet, 3) / 255 + testSet = np.expand_dims(rawTestSet, 3) / 255 + return Model(name, trainSet, testSet, log=log) + + +def npzModel(name, path, use, log=True, shuffle=True): + data = np.load(path) + trainSet = data['arr_0'] + testSet = data['arr_1'] + if shuffle is True: + np.random.shuffle(trainSet) + np.random.shuffle(testSet) + return Model(name, trainSet, testSet, use, log=log) + + +def emptyModel(name, use, inputsShape, log=True): + return Model(name, None, None, use, log=log, inputsShape=inputsShape) + + +class Model: + + def jtektEncoder(self): + self.use = 'jtekt' + InputLayer(self, self.placeholder.inputs, self.inputsShape, resize=60) + ConvutionalLayer(self, [2, 2, 1, 32], [1, 2, 2, 1], activation=tf.nn.leaky_relu) + ConvutionalLayer(self, [2, 2, 32, 64], [1, 2, 2, 1], activation=tf.nn.leaky_relu) + self.features = self.outputs() + Reshape(self, [self.batchSize, self.layers[-1].len()]) + Dense(self, HYPERPARAMETER.latentSpace, activation=tf.nn.leaky_relu) + self.encoder = self.outputs() + Dense(self, self.layers[-2].len(), activation=tf.nn.leaky_relu) + Reshape(self, [self.batchSize] + self.layers[-4].shape) + DeconvutionalLayer(self, [2, 2, 32, 64], [self.batchSize] + self.layers[1].shape, [1, 2, 2, 1], activation=tf.nn.leaky_relu) + DeconvutionalLayer(self, [2, 2, 1, 32], [self.batchSize] + self.inputsShape, [1, 2, 2, 1], activation=tf.nn.sigmoid) + + def diffEncoder_old(self): + self.use = 'diff' + InputLayer(self, self.placeholder.inputs, self.inputsShape, contrast=HYPERPARAMETER.contrast, normalize=True) + c1 = ConvutionalLayer(self, [2, 2, 1, 32], [1, 2, 2, 1], activation=tf.nn.leaky_relu) + c2 = ConvutionalLayer(self, [2, 2, 32, 64], [1, 2, 2, 1], activation=tf.nn.leaky_relu) + self.features = self.outputs() + reshape = Reshape(self, [self.batchSize, self.layers[-1].len()]) + std = Dense(self, HYPERPARAMETER.diffLatentSpace, activation=tf.nn.tanh, factor=0.5) + mean = Dense(self, HYPERPARAMETER.diffLatentSpace, activation=tf.nn.tanh) + VAE(self, mean, std) + self.encoder = self.outputs() + self.layers[-1].outputs2 = self.placeholder.code + Dense(self, reshape.len(), activation=tf.nn.relu) + Reshape(self, [self.batchSize] + c2.shape) + DeconvutionalLayer(self, [2, 2, 32, 64], [self.batchSize] + c1.shape, [1, 2, 2, 1], activation=tf.nn.leaky_relu) + DeconvutionalLayer(self, [2, 2, 1, 32], [self.batchSize] + self.inputsShape, [1, 2, 2, 1], activation=lambda x: tf.nn.relu(tf.nn.tanh(x))) + + def diffEncoder(self): + self.use = 'diff' + InputLayer(self, self.placeholder.inputs, self.inputsShape, contrast=HYPERPARAMETER.contrast, normalize=HYPERPARAMETER.normalize) + c1 = ConvutionalLayer(self, [2, 2, 1, 16], [1, 2, 2, 1], activation=tf.nn.leaky_relu) + c2 = ConvutionalLayer(self, [2, 2, 16, 32], [1, 2, 2, 1], activation=tf.nn.leaky_relu) + c3 = ConvutionalLayer(self, [2, 2, 32, 64], [1, 2, 2, 1], activation=tf.nn.leaky_relu) + self.features = self.outputs() + reshape = Reshape(self, [self.batchSize, self.layers[-1].len()]) + std = Dense(self, HYPERPARAMETER.diffLatentSpace, factor=0.5) + mean = Dense(self, HYPERPARAMETER.diffLatentSpace) + self.encoder = mean.outputs + mean.outputs2 = self.placeholder.code + VAE(self, mean, std) + Dense(self, reshape.len(), activation=tf.nn.relu) + Reshape(self, [self.batchSize] + c3.shape) + DeconvutionalLayer(self, [2, 2, 32, 64], [self.batchSize] + c2.shape, [1, 2, 2, 1], activation=tf.nn.leaky_relu) + DeconvutionalLayer(self, [2, 2, 16, 32], [self.batchSize] + c1.shape, [1, 2, 2, 1], activation=lambda x: tf.nn.relu(tf.nn.tanh(x))) + DeconvutionalLayer(self, [2, 2, 1, 16], [self.batchSize] + self.inputsShape, [1, 2, 2, 1], activation=lambda x: tf.nn.relu(tf.nn.tanh(x))) + + def __init__(self, name, trainSet, testSet, use='jtekt', log=True, inputsShape=None): + tf.reset_default_graph() + self.name = name + self.layers = [] + self.weights = [] + self.bias = [] + self.layersByType = {} + self.log = log + self.trainSet = trainSet + self.testSet = testSet + if inputsShape is not None: + self.inputsShape = inputsShape + else: + self.inputsShape = list(self.trainSet[0].shape) + print('inputs shape : ' + str(self.inputsShape)) + self.initInitialization() + self.initPlaceholder() + if use == 'diff': + self.diffEncoder() + else: + self.jtektEncoder() + self.diff = tf.abs(self.inputs() - self.outputs()) + self.cost = tf.reduce_sum(tf.square(self.inputs() - self.outputs())) + self.optim = tf.train.AdamOptimizer(self.placeholder.learningRate).minimize(self.cost) + if self.log: + self.initSummaries() + self.session = tf.Session() + if self.log: + self.writer = tf.summary.FileWriter(os.path.join(CONFIG.logDir, self.name), self.session.graph) + self.saver = tf.train.Saver() + self.reinit() + + def prefix(self): + if self.use == 'diff': + return 'DIFF/' + else: + return '' + + def reinit(self): + self.session.run(tf.global_variables_initializer()) + self.session.run(tf.local_variables_initializer()) + + def save(self): + if self.use == 'diff': + self.saver.save(self.session, os.path.join(CONFIG.diffSaveDir, self.name)) + else: + self.saver.save(self.session, os.path.join(CONFIG.saveDir, self.name)) + + def restore(self, path): + if self.use == 'diff': + self.saver.restore(self.session, os.path.join(CONFIG.diffSaveDir, path)) + else: + self.saver.restore(self.session, os.path.join(CONFIG.saveDir, path)) + + def initInitialization(self): + self.winit = tf.glorot_uniform_initializer() + self.convinit = tf.truncated_normal_initializer(stddev=0.03) + self.binit = tf.constant_initializer(0.0) + + def initSummaries(self): + self.inputsSummary = tf.summary.image('raw inputs', self.placeholder.inputs, max_outputs=CONFIG.maxImages) + self.inputsSummary = tf.summary.image('modified inputs', self.inputs(), max_outputs=CONFIG.maxImages) + self.outputsSummary = tf.summary.image('outputs', self.outputs(), max_outputs=CONFIG.maxImages) + self.diffSummary = tf.summary.image('diffs', self.diff, max_outputs=CONFIG.maxImages) + self.costSummary = tf.summary.scalar('cost', self.cost) + self.layerSummaries = [tf.summary.histogram('layer/raw_inputs', self.placeholder.inputs)] + for layer in self.layers: + self.layerSummaries.append(tf.summary.histogram('layer/' + layer.name, layer.outputs)) + weightGradient = tf.gradients(self.cost, self.weights) + self.weightSummaries = [] + for i in range(len(weightGradient)): + self.weightSummaries.append(tf.summary.histogram('weight/' + str(i), weightGradient[i])) + biasGradient = tf.gradients(self.cost, self.bias) + self.biasSummaries = [] + for i in range(len(biasGradient)): + self.biasSummaries.append(tf.summary.histogram('bias/' + str(i), biasGradient[i])) + self.summaries = tf.summary.merge_all() + + def initPlaceholder(self): + self.placeholder = types.SimpleNamespace() + self.placeholder.inputs = tf.placeholder(tf.float32, shape=[None] + self.inputsShape) + self.batchSize = tf.shape(self.placeholder.inputs)[0] + self.placeholder.learningRate = tf.placeholder(tf.float32, shape=[]) + self.placeholder.code = tf.placeholder(tf.float32, shape=[None, HYPERPARAMETER.diffLatentSpace]) + + def train(self, epoch=HYPERPARAMETER.epoch, dataset=None): + if dataset is None: + dataset = self.trainSet + batchs = [] + start = 0 + while start < dataset.shape[0]: + end = min(start + HYPERPARAMETER.batchSize, dataset.shape[0]) + batchs.append({'start': start, 'end': end}) + start = end + for i in range(epoch): + print('EPOCH %d :' % i) + for b in tqdm(batchs): + trainData = dataset[b['start']:b['end']] + self.session.run(self.optim, feed_dict={self.placeholder.learningRate: HYPERPARAMETER.learningRate, self.placeholder.inputs: trainData}) + data = dataset[0:CONFIG.maxImages] + if self.log: + summaries = self.session.run(self.summaries, feed_dict={self.placeholder.learningRate: HYPERPARAMETER.learningRate, self.placeholder.inputs: data}) + if self.log: + self.writer.add_summary(summaries, i) + + def getDiff(self, dataset): + diffs = [] + batchs = [] + start = 0 + while start < dataset.shape[0]: + end = min(start + HYPERPARAMETER.batchSize, dataset.shape[0]) + batchs.append({'start': start, 'end': end}) + start = end + for b in tqdm(batchs): + data = dataset[b['start']:b['end']] + diff = self.session.run(self.diff, feed_dict={self.placeholder.learningRate: HYPERPARAMETER.learningRate, self.placeholder.inputs: data}) + diffs.append(diff) + data = dataset[0:CONFIG.maxImages] + if self.log: + summaries = self.session.run(self.summaries, feed_dict={self.placeholder.learningRate: HYPERPARAMETER.learningRate, self.placeholder.inputs: data}) + if self.log: + self.writer.add_summary(summaries) + return np.concatenate(diffs) + + def encode(self, dataset): + result = self.session.run(self.encoder, feed_dict={self.placeholder.inputs: dataset}) + return result + + def getFeatures(self, dataset): + result = self.session.run(self.features, feed_dict={self.placeholder.inputs: dataset}) + return result + + def generate(self, codes, example): + result = self.session.run(self.outputs2(), feed_dict={self.placeholder.inputs: example, self.placeholder.code: codes}) + return result + + def reproduce(self, dataset): + result = self.session.run(self.outputs(), feed_dict={self.placeholder.inputs: dataset}) + return result + + def outputs(self): + return self.layers[-1].outputs + + def outputs2(self): + return self.layers[-1].outputs2 + + def inputs(self): + return self.layers[0].outputs + + def addLayer(self, layer): + self.layers.append(layer) + if not layer.layerType in self.layersByType: + self.layersByType[layer.layerType] = [] + self.layersByType[layer.layerType].append(layer) + + def getWeight(self, name, shape, dtype, initializer): + w = tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=initializer) + self.weights.append(w) + return w + + def getBias(self, name, shape, dtype, initializer): + w = tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=initializer) + self.bias.append(w) + return w diff --git a/dataset/preprocessed/378.txt b/dataset/preprocessed/378.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/378.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/38.txt b/dataset/preprocessed/38.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/38.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/380.txt b/dataset/preprocessed/380.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/380.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/381.txt b/dataset/preprocessed/381.txt new file mode 100644 index 0000000..3627682 --- /dev/null +++ b/dataset/preprocessed/381.txt @@ -0,0 +1,154 @@ +from __future__ import print_function +from __future__ import division +import imageio +import matplotlib.pyplot as plt +import numpy as np +import sys +import tensorflow as tf +from six.moves import cPickle as pickle +import random +image_size = 28 +pickle_file = '../Dataset/cleanShuffledMNIST.pickle' +with open(pickle_file, 'rb') as f: + a = pickle.load(f) +train_labels = a['train_labels'] +test_labels = a['test_labels'] +validate_labels = a['valid_labels'] +train_dataset = a['train_dataset'] +test_dataset = a['test_dataset'] +validate_dataset = a['valid_dataset'] +lst = ['I', 'C', 'J', 'F', 'B', 'H', 'A', 'E', 'D', 'G'] + + +def flattenAndOneHot(Dataset, labels): + Dataset = Dataset.reshape((-1, image_size * image_size)).astype(np.float32) + one_hot = np.zeros((len(labels), 10)) + one_hot[np.arange(len(labels)), labels] = 1 + return Dataset, one_hot + + +train_dataset, train_labels = flattenAndOneHot(train_dataset, train_labels) +test_dataset, test_labels = flattenAndOneHot(test_dataset, test_labels) +validate_dataset, validate_labels = flattenAndOneHot(validate_dataset, validate_labels) +num_category = 10 + + +def Accuracy(prediction1, labels1): + pred1 = np.argmax(prediction1, 1) + label1 = np.argmax(labels1, 1) + acc = 100 * np.sum(pred1 == label1) / prediction1.shape[0] + return acc + + +def GradientDecentDemo(): + train_length = 30000 + test_length = 3000 + graph = tf.Graph() + with graph.as_default(): + tf_train = tf.constant(train_dataset[:train_length, :]) + tf_test = tf.constant(test_dataset[:test_length, :]) + tf_validate = tf.constant(validate_dataset[:test_length, :]) + tf_train_labels = tf.constant(train_labels[:train_length]) + Weight = tf.Variable(tf.truncated_normal([image_size * image_size, num_category])) + bias = tf.Variable(tf.zeros([num_category])) + logits = tf.matmul(tf_train, Weight) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_validate, Weight) + bias) + test_prediction = tf.nn.softmax(tf.matmul(tf_test, Weight) + bias) + noSteps = 800 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('\n Initializing Gradient Decent..........') + for step in range(noSteps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('\nloss at step %d is %f' % (step, l)) + acc = Accuracy(predictions, train_labels[:train_length, :]) + print('Training accuracy is %lf ' % acc) + acc = Accuracy(valid_prediction.eval(session=session), validate_labels[0:test_length]) + print('Validation accuracy is %lf ' % acc) + print('\nTest accuracy: %lf ' % Accuracy(test_prediction.eval(session=session), test_labels[0:test_length])) + session.close() + + +def StocasticGradientDemo(): + batchsize = 200 + graph = tf.Graph() + with graph.as_default(): + tf_train_labels = tf.placeholder(tf.float32, shape=(batchsize, num_category)) + tf_train_data = tf.placeholder(tf.float32, shape=(batchsize, image_size * image_size)) + tf_validate = tf.constant(validate_dataset) + tf_test = tf.constant(test_dataset) + Weight = tf.Variable(tf.truncated_normal([image_size * image_size, num_category])) + bias = tf.Variable(tf.zeros(num_category)) + mat = tf.matmul(tf_train_data, Weight) + bias + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=mat, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(mat) + valid_prediction = tf.nn.softmax(tf.matmul(tf_validate, Weight) + bias) + test_prediction = tf.nn.softmax(tf.matmul(tf_test, Weight) + bias) + noSteps = 800 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('\n.......Initialized Stocastic Gradient Decent.........') + for step in range(noSteps): + offset = random.randint(1, train_dataset.shape[0] - batchsize) + batch_data = train_dataset[offset:offset + batchsize, :] + batch_labels = train_labels[offset:offset + batchsize, :] + feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + _, predictions, l = session.run([optimizer, train_prediction, loss], feed_dict=feed_dict) + if step % 100 == 0: + print('\nloss at step %d is %f' % (step, l)) + acc = Accuracy(predictions, batch_labels) + print('Training accuracy is %lf ' % acc) + acc = Accuracy(valid_prediction.eval(session=session), validate_labels) + print('Validation accuracy is %lf ' % acc) + print('\nTest accuracy: %lf ' % Accuracy(test_prediction.eval(session=session), test_labels)) + session.close() + + +def StocasticGradientReluDemo(): + batchsize = 200 + middle_layerSize = 1024 + graph = tf.Graph() + with graph.as_default(): + tf_train_labels = tf.placeholder(tf.float32, shape=(batchsize, num_category)) + tf_train_data = tf.placeholder(tf.float32, shape=(batchsize, image_size * image_size)) + tf_validate = tf.constant(validate_dataset) + tf_test = tf.constant(test_dataset) + Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + bias = tf.Variable(tf.zeros(middle_layerSize)) + WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + biasNext = tf.Variable(tf.zeros(num_category)) + layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + mat = tf.matmul(layer, WeightNext) + biasNext + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=mat, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(mat) + layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + noSteps = 800 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('\n.......Initialized SGD with Relu.........') + for step in range(noSteps): + offset = random.randint(1, train_dataset.shape[0] - batchsize) + batch_data = train_dataset[offset:offset + batchsize, :] + batch_labels = train_labels[offset:offset + batchsize, :] + feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + _, predictions, l = session.run([optimizer, train_prediction, loss], feed_dict=feed_dict) + if step % 100 == 0: + print('\nloss at step %d is %f' % (step, l)) + acc = Accuracy(predictions, batch_labels) + print('Training accuracy is %lf ' % acc) + acc = Accuracy(valid_prediction.eval(session=session), validate_labels) + print('Validation accuracy is %lf ' % acc) + print('\nTest accuracy: %lf ' % Accuracy(test_prediction.eval(session=session), test_labels)) + session.close() + + +StocasticGradientReluDemo() diff --git a/dataset/preprocessed/382.txt b/dataset/preprocessed/382.txt new file mode 100644 index 0000000..a04fb40 --- /dev/null +++ b/dataset/preprocessed/382.txt @@ -0,0 +1,617 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import random +import math +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.ops import rnn_cell +from tensorflow.python.ops.nn import bidirectional_dynamic_rnn +from tensorflow.python.ops.nn import dynamic_rnn +from tensorflow.python.ops.nn import sparse_softmax_cross_entropy_with_logits +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +import matplotlib +matplotlib.use('Agg') +import matplotlib.pyplot as plt +import time +import datetime +from tensorflow.python.ops import math_ops +from tensorflow.python.util import nest +from tensorflow.python.ops import init_ops +from evaluate import exact_match_score, f1_score +logging.basicConfig(level=logging.INFO) + + +def batch_linear(args, output_size, bias, bias_start=0.0, scope=None, name=None): + """Linear map: concat(W[i] * args[i]), where W[i] is a variable. + Args: + args: a 3D Tensor with shape [batch x m x n]. + output_size: int, second dimension of W[i] with shape [output_size x m]. + bias: boolean, whether to add a bias term or not. + bias_start: starting value to initialize the bias; 0 by default. + scope: (optional) Variable scope to create parameters in. + name: (optional) variable name. + Returns: + A 3D Tensor with shape [batch x output_size x n] equal to + concat(W[i] * args[i]), where W[i]s are newly created matrices. + Raises: + ValueError: if some of the arguments has unspecified or wrong shape. + """ + if args is None or nest.is_sequence(args) and not args: + raise ValueError('`args` must be specified') + if args.get_shape().ndims != 3: + raise ValueError('`args` must be a 3D Tensor') + shape = args.get_shape() + m = shape[1].value + n = shape[2].value + dtype = args.dtype + scope = vs.get_variable_scope() + with vs.variable_scope(scope) as outer_scope: + w_name = 'weights_' + if name is not None: + w_name += name + weights = vs.get_variable(w_name, [output_size, m], dtype=dtype) + res = tf.map_fn(lambda x: math_ops.matmul(weights, x), args) + if not bias: + return res + with vs.variable_scope(outer_scope) as inner_scope: + b_name = 'biases_' + if name is not None: + b_name += name + inner_scope.set_partitioner(None) + biases = vs.get_variable(b_name, [output_size, n], dtype=dtype, initializer=init_ops.constant_initializer(bias_start, dtype=dtype)) + return tf.map_fn(lambda x: math_ops.add(x, biases), res) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +def plot_losses(losses): + plt.plot(losses) + ts = time.time() + st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') + plt.title('Loss') + plt.savefig('loss/Losses_' + st) + plt.clf() + + +def plot_metrics(metrics_dict): + train_f1, = plt.plot(metrics_dict['train']['f1'], label='train') + val_f1, = plt.plot(metrics_dict['val']['f1'], label='val') + plt.title('F1 Score') + plt.legend(handles=[train_f1, val_f1]) + ts = time.time() + st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H-%M-%S') + plt.savefig('metric_plots/f1_' + st) + plt.clf() + train_em, = plt.plot(metrics_dict['train']['em'], label='train') + val_em, = plt.plot(metrics_dict['val']['em'], label='val') + plt.legend(handles=[train_em, val_em]) + plt.title('Exact Match') + ts = time.time() + st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H-%M-%S') + plt.savefig('metric_plots/em_' + st) + + +class GRUAttnCell(rnn_cell.GRUCell): + + def __init__(self, num_units, encoder_output, scope=None): + self.hs = encoder_output + super(GRUAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = rnn_cell._linear(gru_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.hs * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.hs * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + return out, out + + +def matrix_multiply_with_batch(matrix=None, batch=None, matrixByBatch=True): + ret = None + if matrixByBatch: + ret = tf.scan(lambda a, x: tf.matmul(matrix, x), batch) + else: + n = batch.get_shape().as_list()[1] + m = batch.get_shape().as_list()[2] + c = matrix.get_shape().as_list()[1] + batch = tf.reshape(batch, [-1, m]) + ret = tf.matmul(batch, matrix) + ret = tf.reshape(ret, [-1, n, c]) + return ret + + +class Encoder(object): + + def __init__(self, state_size, embedding_size, output_size): + self.state_size = state_size + self.embedding_size = embedding_size + self.output_size = output_size + self.h_q = None + self.h_p = None + self.H_q = None + self.H_p = None + + def encode_v2(self, question_embeddings, document_embeddings, question_mask, context_mask, encoderb_state_input, dropout_keep_prob, max_question_len): + """ encode_v2() + """ + lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, input_keep_prob=dropout_keep_prob) + with tf.variable_scope('question_embedding'): + question_length = tf.reduce_sum(tf.cast(question_mask, tf.int32), reduction_indices=1) + Q_prime, _ = dynamic_rnn(lstm_cell, question_embeddings, sequence_length=question_length, dtype=tf.float32) + print('Q_prime: ', Q_prime) + Q = tf.tanh(batch_linear(Q_prime, max_question_len, True)) + Q = tf.transpose(Q, [0, 2, 1]) + print('Q: ', Q) + with tf.variable_scope('context_embedding'): + context_length = tf.reduce_sum(tf.cast(context_mask, tf.int32), reduction_indices=1) + D, _ = dynamic_rnn(lstm_cell, document_embeddings, sequence_length=context_length, dtype=tf.float32) + D = tf.transpose(D, [0, 2, 1]) + print('D: ', D) + with tf.variable_scope('coattention'): + L = tf.batch_matmul(tf.transpose(D, [0, 2, 1]), Q) + print('L: ', L) + A_Q = tf.map_fn(lambda x: tf.nn.softmax(x), L, dtype=tf.float32) + A_D = tf.map_fn(lambda x: tf.nn.softmax(x), tf.transpose(L, [0, 2, 1]), dtype=tf.float32) + print('A_Q: ', A_Q) + print('A_D: ', A_D) + C_Q = batch_matmul(D, A_Q) + print('C_Q: ', C_Q) + concat = tf.concat(1, [Q, C_Q]) + print('concat: ', concat) + C_D = batch_matmul(tf.concat(1, [Q, C_Q]), A_D) + print('C_D: ', C_D) + co_att = tf.concat(1, [D, C_D]) + co_att = tf.transpose(co_att, [0, 2, 1]) + print('co_att: ', co_att) + with tf.variable_scope('encoder'): + cell_fw = tf.nn.rnn_cell.LSTMCell(self.state_size) + cell_bw = tf.nn.rnn_cell.LSTMCell(self.state_size) + cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, input_keep_prob=dropout_keep_prob) + cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, input_keep_prob=dropout_keep_prob) + (fw_out, bw_out), _ = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, co_att, sequence_length=context_length, dtype=tf.float32) + print('fw out: ', fw_out) + print('bw out: ', bw_out) + U = tf.concat(2, [fw_out, bw_out]) + print('U: ', U) + return U + + def encode(self, question_embeddings, context_embeddings, question_mask, context_mask, encoder_state_input, dropout_keep_prob, batch_size): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with vs.variable_scope('encoder', True): + with vs.variable_scope('question', True): + lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, output_keep_prob=dropout_keep_prob) + question_length = tf.reduce_sum(tf.cast(question_mask, tf.int32), reduction_indices=1) + print('Question length: ', question_length) + self.H_q, _ = dynamic_rnn(lstm_cell, question_embeddings, sequence_length=question_length, time_major=False, dtype=tf.float64, swap_memory=True) + self.h_q = self.H_q[:, (1), :] + print('H_q: ', self.H_q) + print('h_q: ', self.h_q) + with vs.variable_scope('context', True): + context_length = tf.reduce_sum(tf.cast(context_mask, tf.int32), reduction_indices=1) + print('Context length: ', context_length) + context_lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + context_lstm_cell = tf.nn.rnn_cell.DropoutWrapper(context_lstm_cell, output_keep_prob=dropout_keep_prob) + self.H_p, _ = dynamic_rnn(context_lstm_cell, context_embeddings, sequence_length=context_length, time_major=False, dtype=tf.float64, swap_memory=True) + self.h_p = self.H_p[:, (1), :] + print('H_p: ', self.H_p) + print('h_p: ', self.h_p) + return self.h_q, self.h_p + + +class Decoder(object): + + def __init__(self, output_size, state_size): + self.state_size = state_size + self.output_size = output_size + + def decode_v2(self, U, W, W_prime): + with vs.variable_scope('answer_start'): + a_s = tf.squeeze(matrix_multiply_with_batch(matrix=W, batch=U, matrixByBatch=False)) + print('a_s: ', a_s) + with vs.variable_scope('answer_end'): + a_e = tf.squeeze(matrix_multiply_with_batch(matrix=W_prime, batch=U, matrixByBatch=False)) + print('a_e: ', a_e) + return a_s, a_e + + def decode(self, h_q, h_p): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + with vs.variable_scope('answer_start'): + a_s = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + with vs.variable_scope('answer_end'): + a_e = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + return a_s, a_e + + +class QASystem(object): + + def __init__(self, encoder, decoder, **kwargs): + """ + Initializes your System + + :param encoder: an encoder that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + self.max_question_len = 60 + self.max_context_len = 301 + self.max_answer_len = 46 + self.n_classes = 2 + self.saver = None + self.encoder = encoder + self.decoder = decoder + self.question_embeddings = None + self.context_embeddings = None + self.a_s_probs = None + self.a_e_probs = None + self.loss = None + self.train_op = None + self.grad_norm = None + self.gradients = None + self.clipped_gradients = None + self.state_size = kwargs['state_size'] + self.embed_path = kwargs['embed_path'] + self.embedding_size = kwargs['embedding_size'] + self.output_size = kwargs['output_size'] + self.optimizer = kwargs['optimizer'] + self.initial_learning_rate = kwargs['learning_rate'] + self.global_step = tf.Variable(0, trainable=False) + self.epochs = kwargs['epochs'] + self.batch_size = kwargs['batch_size'] + self.max_gradient_norm = kwargs['max_gradient_norm'] + self.dropout_keep_prob = kwargs['dropout_keep_prob'] + self.train_dir = kwargs['train_dir'] + self.question_input_placeholder = tf.placeholder(tf.int32, (None, self.max_question_len)) + self.context_input_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + self.question_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_question_len)) + self.context_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_context_len)) + self.start_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + self.end_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + self.dropout_placeholder = tf.placeholder(tf.float32, ()) + self.batch_size_placeholder = tf.placeholder(tf.int32, ()) + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.setup_training_op() + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + W = tf.get_variable('W', (2 * self.state_size, 1)) + W_prime = tf.get_variable('W_prime', (2 * self.state_size, 1)) + U = self.encoder.encode_v2(self.question_embeddings, self.context_embeddings, self.question_mask_placeholder, self.context_mask_placeholder, None, self.dropout_placeholder, self.max_question_len) + self.a_s_probs, self.a_e_probs = self.decoder.decode_v2(U, W, W_prime) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + l1 = tf.nn.softmax_cross_entropy_with_logits(logits=self.a_s_probs, labels=self.start_answer_placeholder) + l2 = tf.nn.softmax_cross_entropy_with_logits(logits=self.a_e_probs, labels=self.end_answer_placeholder) + self.loss = tf.reduce_mean(l1 + l2) + + def setup_training_op(self): + """ + Sets up the training ops. + + Creates an optimizer and applies the gradients to all trainable variables. + Clips the global norm of the gradients. + """ + lr = tf.train.exponential_decay(self.initial_learning_rate, self.global_step, 1000, 0.96) + opt = get_optimizer(self.optimizer)(learning_rate=lr) + self.gradients, params = zip(*opt.compute_gradients(self.loss)) + for param in params: + print('Param: ', param) + self.clipped_gradients, _ = tf.clip_by_global_norm(self.gradients, self.max_gradient_norm) + grads_and_params = zip(self.clipped_gradients, params) + self.grad_norm = tf.global_norm(self.clipped_gradients) + self.train_op = opt.apply_gradients(grads_and_params, global_step=self.global_step) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + pretrained_embeddings = np.load(self.embed_path)['glove'] + self.question_embeddings = tf.constant(pretrained_embeddings, name='question_embeddings', dtype=tf.float32) + self.question_embeddings = tf.nn.embedding_lookup(self.question_embeddings, self.question_input_placeholder) + self.question_embeddings = tf.reshape(self.question_embeddings, [-1, self.max_question_len, self.embedding_size]) + print('Question embeddings: ', self.question_embeddings) + self.context_embeddings = tf.constant(pretrained_embeddings, name='context_embeddings', dtype=tf.float32) + self.context_embeddings = tf.nn.embedding_lookup(self.context_embeddings, self.context_input_placeholder) + self.context_embeddings = tf.reshape(self.context_embeddings, [-1, self.max_context_len, self.embedding_size]) + print('Context embeddings: ', self.context_embeddings) + + def optimize(self, session, train_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*train_batch) + feed = {} + feed[self.question_input_placeholder] = question_batch + feed[self.context_input_placeholder] = context_batch + feed[self.question_mask_placeholder] = question_mask_batch + feed[self.context_mask_placeholder] = context_mask_batch + feed[self.start_answer_placeholder] = start_answer_batch + feed[self.end_answer_placeholder] = end_answer_batch + feed[self.dropout_placeholder] = self.dropout_keep_prob + self.batch_size = len(question_batch) + feed[self.batch_size_placeholder] = self.batch_size + _, loss, grad_norm = session.run([self.train_op, self.loss, self.grad_norm], feed_dict=feed) + return loss, grad_norm + + def test(self, session, valid_x, valid_y): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed['valid_x'] = valid_x + if valid_y is not None: + input_feed['valid_y'] = valid_y + output_feed = [] + outputs = session.run(output_feed, input_feed) + return outputs + + def decode(self, session, test_x): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*test_x) + input_feed = {} + input_feed[self.question_input_placeholder] = question_batch + input_feed[self.context_input_placeholder] = context_batch + input_feed[self.question_mask_placeholder] = question_mask_batch + input_feed[self.context_mask_placeholder] = context_mask_batch + input_feed[self.start_answer_placeholder] = start_answer_batch + input_feed[self.end_answer_placeholder] = end_answer_batch + input_feed[self.dropout_placeholder] = 1.0 + self.batch_size = len(question_batch) + input_feed[self.batch_size_placeholder] = self.batch_size + _, loss, grad_norm = session.run([self.train_op, self.loss, self.grad_norm], feed_dict=input_feed) + a_s = self.a_s_probs.eval(feed_dict=input_feed, session=session) + a_e = self.a_e_probs.eval(feed_dict=input_feed, session=session) + return a_s, a_e + + def answer(self, session, test_x): + yp, yp2 = self.decode(session, test_x) + a_s = np.argmax(yp, axis=1) + a_e = np.argmax(yp2, axis=1) + return a_s, a_e + + def validate(self, session, dataset, model_path, model_name): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + new_saver = tf.train.import_meta_graph(model_path + model_name) + new_saver.restore(session, tf.train.latest_checkpoint(model_path)) + f1s = [] + ems = [] + step = 500 + for start_idx in range(0, len(dataset), step): + end_idx = min(start_idx + step, len(dataset)) + f1s_one_batch, ems_one_batch = self.evaluate_answer(session, dataset[start_idx:end_idx], sample=None, log=True) + f1s += f1s_one_batch + ems += ems_one_batch + f1_total = sum(f1s) / float(len(f1s)) + em_total = sum(ems) / float(len(ems)) + print('Total f1: ', f1_total) + print('Total em: ', em_total) + + def evaluate_answer(self, session, dataset, sample=100, log=False, shuffle=True): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sample = len(dataset) + print('Sample: ', sample) + if shuffle: + random.shuffle(dataset) + batch = dataset[0:sample] + question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*batch) + a_s, a_e = self.answer(session, batch) + true_a_s = np.argmax(start_answer_batch, axis=1) + true_a_e = np.argmax(end_answer_batch, axis=1) + print('predicted a_s: ', a_s) + print('predicted a_e: ', a_e) + print('true start answer: ', true_a_s) + print('true end answer: ', true_a_e) + answers = [context_batch[i][a_s[i]:a_e[i] + 1] for i in range(len(a_s))] + true_answers = [context_batch[i][true_a_s[i]:true_a_e[i] + 1] for i in range(len(true_a_s))] + f1s = [] + ems = [] + for i in range(len(true_answers)): + answer = answers[i] + true_answer = true_answers[i] + f1_one_example = f1_score(answer, true_answer) + f1s.append(f1_one_example) + em_one_example = exact_match_score(answer, true_answer) + ems.append(em_one_example) + f1 = np.sum(f1s) / float(sample) + em = np.sum(ems) / float(sample) + if log: + logging.info('F1: {}, EM: {}, for {} samples'.format(f1, em, sample)) + return f1s, ems + + def train(self, session, dataset, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious appoarch can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + dataset = self.preprocess_dataset(dataset) + init = tf.global_variables_initializer() + session.run(init) + training_losses = [] + training_metrics = {'train': {'f1': [], 'em': []}, 'val': {'f1': [], 'em': []}} + for epoch in range(100): + logging.info('Epoch %d out of %d', epoch + 1, self.epochs) + self.run_epoch(session, dataset['train'], dataset['val'], epoch, training_losses, training_metrics) + self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch)) + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + plot_losses(training_losses) + plot_metrics(training_metrics) + plot_losses(training_losses) + self.evaluate_answer(session, dataset['val'], sample=None, log=True) + + def preprocess_dataset(self, dataset): + for data_subset_name in ['train', 'val']: + for i in range(len(dataset[data_subset_name])): + question = dataset[data_subset_name][i][0] + context = dataset[data_subset_name][i][1] + answer = dataset[data_subset_name][i][2] + padded_question, question_mask = self.pad_sequence(question, self.max_question_len) + padded_context, context_mask = self.pad_sequence(context, self.max_context_len) + start_answer = [0] * self.max_context_len + if answer[0] < self.output_size: + start_answer[answer[0]] = 1 + end_answer = [0] * self.max_context_len + if answer[1] < self.output_size: + end_answer[answer[1]] = 1 + dataset[data_subset_name][i] = padded_question, padded_context, question_mask, context_mask, start_answer, end_answer + return dataset + + def pad_sequence(self, sequence, max_length): + new_sequence = [] + mask = [] + if len(sequence) >= max_length: + new_sequence = sequence[0:max_length] + mask = [True] * max_length + elif len(sequence) < max_length: + delta = max_length - len(sequence) + new_sequence = sequence + [0] * delta + mask = [True] * len(sequence) + [False] * delta + return new_sequence, mask + + def run_epoch(self, session, train_examples, val_examples, epoch_no, training_losses, training_metrics): + for i, batch in enumerate(self.minibatches(train_examples, self.batch_size, shuffle=True)): + print('Global step: ', self.global_step.eval()) + loss, grad_norm = self.optimize(session, batch) + print('Loss: ', loss, ' , grad norm: ', grad_norm) + training_losses.append(loss) + if i % 100 == 0: + train_f1s, train_ems = self.evaluate_answer(session, train_examples, sample=100, log=True, shuffle=True) + val_f1s, val_ems = self.evaluate_answer(session, val_examples, sample=100, log=True, shuffle=True) + train_f1 = sum(train_f1s) / float(len(train_f1s)) + val_f1 = sum(val_f1s) / float(len(val_f1s)) + train_em = sum(train_ems) / float(len(train_ems)) + val_em = sum(val_ems) / float(len(val_ems)) + training_metrics['train']['f1'].append(train_f1) + training_metrics['val']['f1'].append(val_f1) + training_metrics['train']['em'].append(train_em) + training_metrics['val']['em'].append(val_em) + plot_losses(training_losses) + plot_metrics(training_metrics) + if i % 1000 == 0: + self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch_no) + '_iter_' + str(i)) + + def get_tiny_batches(self, data, sample=20): + ret = [] + step = self.batch_size + for start_idx in range(0, sample, step): + ret.append(data[start_idx:start_idx + step]) + return ret + + def minibatches(self, data, batch_size, shuffle=True): + if shuffle: + random.shuffle(data) + num_batches = int(math.ceil(len(data) / batch_size)) + q, r = divmod(len(data), num_batches) + indices = [(q * i + min(i, r)) for i in xrange(num_batches + 1)] + return [data[indices[i]:indices[i + 1]] for i in xrange(num_batches)] diff --git a/dataset/preprocessed/383.txt b/dataset/preprocessed/383.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/383.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/384.txt b/dataset/preprocessed/384.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/384.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/385.txt b/dataset/preprocessed/385.txt new file mode 100644 index 0000000..173e410 --- /dev/null +++ b/dataset/preprocessed/385.txt @@ -0,0 +1,70 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +batch_size = 128 +h1_length = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(dtype=tf.float32, shape=(batch_size, image_size ** 2)) + tf_train_labels = tf.placeholder(dtype=tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size ** 2, h1_length])) + biases1 = tf.Variable(tf.zeros([h1_length])) + logits1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + weights2 = tf.Variable(tf.truncated_normal([h1_length, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits2 = tf.matmul(logits1, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits2, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits2) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) + + def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + num_steps = 3001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + for step in range(num_steps): + offset = batch_size * step % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Testing accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/386.txt b/dataset/preprocessed/386.txt new file mode 100644 index 0000000..d30118a --- /dev/null +++ b/dataset/preprocessed/386.txt @@ -0,0 +1,196 @@ +import os.path +import tensorflow as tf +import helper +import warnings +from distutils.version import LooseVersion +import project_tests as tests +from matplotlib import pyplot as plt +import time +import shutil +assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer. You are using {}'.format(tf.__version__) +print('TensorFlow Version: {}'.format(tf.__version__)) +if not tf.test.gpu_device_name(): + warnings.warn('No GPU found. Please use a GPU to train your neural network.') +else: + print('Default GPU Device: {}'.format(tf.test.gpu_device_name())) +num_classes = 3 +NUMBER_OF_CLASSES = 3 +image_shape = 352, 640 +IMAGE_SHAPE = 352, 640 +EPOCHS = 40 +BATCH_SIZE = 4 +DROPOUT = 0.75 +data_dir = './data' +test_data_dir = '../data/360_raw/test' +runs_dir = '../data/runs' +training_dir = './data/data_road/training' +vgg_path = './data/vgg' +correct_label = tf.placeholder(tf.float32, [None, IMAGE_SHAPE[0], IMAGE_SHAPE[1], NUMBER_OF_CLASSES]) +learning_rate = tf.placeholder(tf.float32) +keep_prob = tf.placeholder(tf.float32) + + +def load_vgg(sess, vgg_path): + """ + Load Pretrained VGG Model into TensorFlow. + :param sess: TensorFlow Session + :param vgg_path: Path to vgg folder, containing "variables/" and "saved_model.pb" + :return: Tuple of Tensors from VGG model (image_input, keep_prob, layer3_out, layer4_out, layer7_out) + """ + model = tf.saved_model.loader.load(sess, ['vgg16'], vgg_path) + graph = tf.get_default_graph() + image_input = graph.get_tensor_by_name('image_input:0') + keep_prob = graph.get_tensor_by_name('keep_prob:0') + layer3 = graph.get_tensor_by_name('layer3_out:0') + layer4 = graph.get_tensor_by_name('layer4_out:0') + parameters = [] + kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, stddev=0.1), name='conv5_1/weights') + conv = tf.nn.conv2d(layer4, kernel, [1, 1, 1, 1], padding='SAME') + biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), trainable=True, name='conv5_1/biases') + out = tf.nn.bias_add(conv, biases) + conv5_1 = tf.nn.relu(out, name='conv5_1') + parameters += [kernel, biases] + kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, stddev=0.1), name='conv5_2/weights') + conv = tf.nn.conv2d(conv5_1, kernel, [1, 1, 1, 1], padding='SAME') + biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), trainable=True, name='conv5_2/biases') + out = tf.nn.bias_add(conv, biases) + conv5_2 = tf.nn.relu(out, 'conv5_2') + parameters += [kernel, biases] + kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, stddev=0.1), name='conv5_3/weights') + conv = tf.nn.conv2d(conv5_2, kernel, [1, 1, 1, 1], padding='SAME') + biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), trainable=True, name='conv5_3/biases') + out = tf.nn.bias_add(conv, biases) + conv5_3 = tf.nn.relu(out, name='conv5_3') + parameters += [kernel, biases] + pool5 = tf.nn.max_pool(conv5_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool5') + kernel = tf.Variable(tf.truncated_normal([11, 20, 512, 32], dtype=tf.float32, stddev=0.1), name='conv6/weights') + conv = tf.nn.conv2d(pool5, kernel, [1, 1, 1, 1], padding='VALID') + biases = tf.Variable(tf.constant(0.0, shape=[32], dtype=tf.float32), trainable=True, name='conv6/biases') + out = tf.nn.bias_add(conv, biases) + conv6 = tf.nn.relu(out, name='conv6') + parameters += [kernel, biases] + kernel = tf.Variable(tf.truncated_normal([1, 1, 32, 4096], dtype=tf.float32, stddev=0.1), name='conv6/weights') + conv = tf.nn.conv2d(conv6, kernel, [1, 1, 1, 1], padding='VALID') + biases = tf.Variable(tf.constant(0.0, shape=[4096], dtype=tf.float32), trainable=True, name='conv6/biases') + out = tf.nn.bias_add(conv, biases) + conv7 = tf.nn.relu(out, name='conv7') + parameters += [kernel, biases] + layer7 = conv7 + return image_input, keep_prob, layer3, layer4, layer7 + + +def layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes): + """ + Create the layers for a fully convolutional network. Build skip-layers using the vgg layers. + :param vgg_layer3_out: TF Tensor for VGG Layer 3 output + :param vgg_layer4_out: TF Tensor for VGG Layer 4 output + :param vgg_layer7_out: TF Tensor for VGG Layer 7 output + :param num_classes: Number of classes to classify + :return: The Tensor for the last layer of output + """ + layer3, layer4, layer7 = vgg_layer3_out, vgg_layer4_out, vgg_layer7_out + fcn8 = tf.layers.conv2d(layer7, filters=num_classes, kernel_size=1, name='fcn8') + fcn9 = tf.layers.conv2d_transpose(fcn8, filters=layer4.get_shape().as_list()[-1], kernel_size=4, strides=(2, 2), padding='SAME', name='fcn9') + fcn9_skip_connected = tf.add(fcn9, layer4, name='fcn9_plus_vgg_layer4') + fcn10 = tf.layers.conv2d_transpose(fcn9_skip_connected, filters=layer3.get_shape().as_list()[-1], kernel_size=4, strides=(2, 2), padding='SAME', name='fcn10_conv2d') + fcn10_skip_connected = tf.add(fcn10, layer3, name='fcn10_plus_vgg_layer3') + fcn11 = tf.layers.conv2d_transpose(fcn10_skip_connected, filters=num_classes, kernel_size=16, strides=(8, 8), padding='SAME', name='fcn11') + print(fcn11.shape.dims) + print(layer7.shape.dims) + print(fcn8.shape.dims) + print(fcn9.shape.dims) + print(fcn9_skip_connected.shape.dims) + print(fcn10.shape.dims) + print(fcn11.shape.dims) + return fcn11 + + +tests.test_layers(layers) + + +def optimize(nn_last_layer, correct_label, learning_rate, num_classes): + """ + Build the TensorFLow loss and optimizer operations. + :param nn_last_layer: TF Tensor of the last layer in the neural network + :param correct_label: TF Placeholder for the correct label image + :param learning_rate: TF Placeholder for the learning rate + :param num_classes: Number of classes to classify + :return: Tuple of (logits, train_op, cross_entropy_loss) + """ + logits = tf.reshape(nn_last_layer, (-1, num_classes), name='fcn_logits') + correct_label_reshaped = tf.reshape(correct_label, (-1, num_classes)) + cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf.stop_gradient(correct_label_reshaped[:])) + loss_op = tf.reduce_mean(cross_entropy, name='fcn_loss') + train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss_op, name='fcn_train_op') + return logits, correct_label_reshaped, train_op, loss_op + + +tests.test_optimize(optimize) + + +def train_nn(sess, epochs, batch_size, get_batches_fn, train_op, cross_entropy_loss, input_image, correct_label, keep_prob, learning_rate, correct_labels, logits): + """ + Train neural network and print out the loss during training. + :param sess: TF Session + :param epochs: Number of epochs + :param batch_size: Batch size + :param get_batches_fn: Function to get batches of training data. Call using get_batches_fn(batch_size) + :param train_op: TF Operation to train the neural network + :param cross_entropy_loss: TF Tensor for the amount of loss + :param input_image: TF Placeholder for input images + :param correct_label: TF Placeholder for label images + :param keep_prob: TF Placeholder for dropout keep probability + :param learning_rate: TF Placeholder for learning rate + """ + keep_prob_value = 0.5 + learning_rate_value = 0.001 + for epoch in range(epochs): + counter = 0 + total_loss = 0 + for X_batch, gt_batch in get_batches_fn(batch_size): + loss, _, cl, lo = sess.run([cross_entropy_loss, train_op, correct_labels, logits], feed_dict={input_image: X_batch, correct_label: gt_batch, keep_prob: keep_prob_value, learning_rate: learning_rate_value}) + total_loss += loss + counter += 1 + print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())) + print('EPOCH {} ...'.format(epoch + 1)) + print('Loss = {:.3f}'.format(total_loss)) + print() + + +def run(): + print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())) + helper.maybe_download_pretrained_vgg(data_dir) + print('Pretrained model downloaded') + with tf.Session() as sess: + vgg_path = os.path.join(data_dir, 'vgg') + print(vgg_path) + get_batches_fn = helper.gen_batch_function('../data', image_shape) + print('Prepared function to get batches') + with tf.Session() as session: + image_input, keep_prob, layer3, layer4, layer7 = load_vgg(session, vgg_path) + print('Get layers form vgg - Done') + model_output = layers(layer3, layer4, layer7, num_classes) + print('Added Decoder - Full network architecture On-Line') + logits, correct_labels, train_op, cross_entropy_loss = optimize(model_output, correct_label, learning_rate, num_classes) + print('Model_optimised') + print('Initialize all variables - this might slow your computer, just do not worry and wait about 10 min') + print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())) + session.run(tf.global_variables_initializer()) + session.run(tf.local_variables_initializer()) + print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())) + print('Model build successful, starting training') + train_nn(session, EPOCHS, BATCH_SIZE, get_batches_fn, train_op, cross_entropy_loss, image_input, correct_label, keep_prob, learning_rate, correct_labels, logits) + print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())) + print('Training Done! :)') + print('Saving model') + saver = tf.train.Saver() + output_dir = '../Trained_Model/' + str(time.strftime('%Y_%m_%d-%H_%M_%S', time.gmtime())) + if os.path.exists(output_dir): + shutil.rmtree(output_dir) + os.makedirs(output_dir) + saver.save(session, output_dir + '/model.ckpt') + print('All done!') + + +if __name__ == '__main__': + run() diff --git a/dataset/preprocessed/387.txt b/dataset/preprocessed/387.txt new file mode 100644 index 0000000..6d80669 --- /dev/null +++ b/dataset/preprocessed/387.txt @@ -0,0 +1,245 @@ +import models +import tensorflow as tf +import numpy as np +import csv +import random +all_data = [] +data_time = [] +train_data = [] +train_label = [] +test_data = [] +test_label = [] +with open('train_set211.csv', 'r') as f: + rd = csv.reader(f) + for line in rd: + all_data.append(line) +with open('test_set.csv', 'r') as f: + rd = csv.reader(f) + for line in rd: + test_data.append([float(i) for i in line[2:-1]]) + test_label.append([float(line[-1]) - 1]) +random.shuffle(all_data) +for data in all_data: + data_time.append([data[0]]) + train_data.append([float(i) for i in data[2:-1]]) + train_label.append([float(data[-1]) - 1]) +train_data = np.array(train_data) +train_label = np.array(train_label) +test_data = np.array(test_data) +test_label = np.array(test_label) +print('Data setting is done!') +""" +hypo = [] +for i in range(len(train_data)): + hypo.append([train_data[i][0] * 0.0779 + train_data[i][1] * 0.1324 + train_data[i][2] * 0.1300 + train_data[i][3] * 0.1194 + train_data[i][4] * 0.0908 - 1.0850]) +hypo = np.array(hypo) +print(hypo.shape) +print(train_label.shape) +#print(list(hypo)) +#print(list(train_label)) +print(models.get_accuracy(np.rint(hypo), train_label, mode=1)) +""" +lr = 0.0015 +nn = models.NvNN(label_size=10, one_hot=True, out_one_hot=True) +nn_train = (np.arange(train_data.max()) == train_data[..., None] - 1).astype(float) +nn_test = (np.arange(test_data.max()) == test_data[..., None] - 1).astype(float) +X_data = tf.placeholder(tf.float32, shape=[None, 4, 20]) +X = tf.reshape(X_data, shape=[-1, 80]) +Y_data = tf.placeholder(tf.int32, shape=[None]) +Y = tf.one_hot(Y_data, depth=10) +keep_prob = tf.placeholder(tf.float32) +h_nn = nn.forward(X, keep_prob=keep_prob) +h_out = nn.forward(X, keep_prob=keep_prob) +cost_nn = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=h_nn) +train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost_nn) +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + y_train = train_label.reshape([-1]) + y_test = test_label.reshape([-1]) + for i in range(2000): + if i % 10 == 0: + hypo_test, test_y = sess.run([h_nn, Y], feed_dict={X_data: nn_test, Y_data: y_test, keep_prob: 1.0}) + act1 = models.get_accuracy(hypo_test, test_y, mode=0, one_hot=True) + act2 = models.get_accuracy(hypo_test, test_y, mode=1, one_hot=True) + print(f'step {i} test acc1 = {act1}, act2 = {act2}') + sess.run(train, feed_dict={X_data: nn_train, Y_data: y_train, keep_prob: 0.8}) +""" + # For check loss + cost_arr = sess.run(cost_nn, feed_dict={X_data: nn_train, Y_data: y_train, keep_prob: 1.0}) + with open('error.txt', 'w') as f: + for err in cost_arr: + f.write(str(err)) + f.write(' +') + + +# Linear Regression is started +X = tf.placeholder(tf.float32, shape=[None, 4]) +Y = tf.placeholder(tf.float32, shape=[None, 1]) +lr = 0.001 +lin = models.LinReg() +h_lin = lin.forward(X) +cost_lin = tf.reduce_mean(tf.square(h_lin - Y)) +train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost_lin) + +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + + for i in range(3000): + if i % 10 == 0: + hypo, y_real = sess.run([h_lin, Y], feed_dict={X: train_data, Y: train_label}) + hypo_test, y_test = sess.run([h_lin, Y], feed_dict={X: test_data, Y:test_label}) + acc1 = models.get_accuracy(np.rint(hypo), y_real, mode=0) + acc2 = models.get_accuracy(np.rint(hypo), y_real, mode=2) + + act1 = models.get_accuracy(np.rint(hypo_test), y_test, mode=0) + act2 = models.get_accuracy(np.rint(hypo_test), y_test, mode=2) + print(f"step {i} accuracy1 = {acc1}, acc2 = {acc2}") + print(f"step {i} test acc1 = {act1}, act2 = {act2}") + + sess.run(train, feed_dict={X: train_data, Y: train_label}) + + +# Logistic regression +lr = 0.001 +logistic = models.LogReg(label_size=10) +X = tf.placeholder(tf.float32, shape=[None, 4]) +Y_data = tf.placeholder(tf.int32, shape=[None]) +Y = tf.one_hot(Y_data, depth=10) +h_log = logistic.forward(X) +cost_log = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=h_log) +train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost_log) + +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + y_train = train_label.reshape([-1]) + y_test = test_label.reshape([-1]) + for i in range(3000): + if i % 10 == 0: + hypo, y_real = sess.run([h_log, Y], feed_dict={X: train_data, Y_data: y_train}) + hypo_test, test_y = sess.run([h_log, Y], feed_dict={X: test_data, Y_data: y_test}) + acc1 = models.get_accuracy(hypo, y_real, mode=0, one_hot=True) + acc2 = models.get_accuracy(hypo, y_real, mode=1, one_hot=True) + + act1 = models.get_accuracy(hypo_test, test_y, mode=0, one_hot=True) + act2 = models.get_accuracy(hypo_test, test_y, mode=1, one_hot=True) + print(f"step {i} accuracy1 = {acc1}, acc2 = {acc2}") + print(f"step {i} test acc1 = {act1}, act2 = {act2}") + + sess.run(train, feed_dict={X: train_data, Y_data: y_train}) + + +# Neural Network ( input is regression ) +nn = models.NvNN(label_size=10) +lr = 0.0015 +X = tf.placeholder(tf.float32, shape=[None, 4]) +Y = tf.placeholder(tf.float32, shape=[None, 1]) +keep_prob = tf.placeholder(tf.float32) +h_nn = nn.forward(X, keep_prob=keep_prob) +cost_nn = tf.reduce_mean(tf.square(h_nn - Y)) +train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost_nn) + +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + for i in range(5000): + if i % 10 == 0: + hypo, y_real = sess.run([h_nn, Y], feed_dict={X: train_data, Y: train_label, keep_prob: 1.0}) + hypo_test, y_test = sess.run([h_nn, Y], feed_dict={X: test_data, Y: test_label, keep_prob: 1.0}) + acc1 = models.get_accuracy(np.rint(hypo), y_real, mode=0) + acc2 = models.get_accuracy(np.rint(hypo), y_real, mode=2) + + act1 = models.get_accuracy(np.rint(hypo_test), y_test, mode=0) + act2 = models.get_accuracy(np.rint(hypo_test), y_test, mode=2) + print(f"step {i} accuracy1 = {acc1}, acc2 = {acc2}") + print(f"step {i} test acc1 = {act1}, act2 = {act2}") + + sess.run(train, feed_dict={X: train_data, Y: train_label, keep_prob: 0.8}) + + +# Neural Network ( output is categorical ) +lr = 0.0015 +nn = models.NvNN(label_size=10, out_one_hot=True) +X = tf.placeholder(tf.float32, shape=[None, 4]) +Y_data = tf.placeholder(tf.int32, shape=[None]) +Y = tf.one_hot(Y_data, depth=10) +keep_prob = tf.placeholder(tf.float32) +h_nn = nn.forward(X, keep_prob=keep_prob) +cost_nn = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=h_nn) +train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost_nn) + +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + y_train = train_label.reshape([-1]) + y_test = test_label.reshape([-1]) + for i in range(5000): + if i % 10 == 0: + hypo, y_real = sess.run([h_nn, Y], feed_dict={X: train_data, Y_data: y_train, keep_prob: 1.0}) + hypo_test, test_y = sess.run([h_nn, Y], feed_dict={X: test_data, Y_data: y_test, keep_prob: 1.0}) + acc1 = models.get_accuracy(hypo, y_real, mode=0, one_hot=True) + acc2 = models.get_accuracy(hypo, y_real, mode=1, one_hot=True) + + act1 = models.get_accuracy(hypo_test, test_y, mode=0, one_hot=True) + act2 = models.get_accuracy(hypo_test, test_y, mode=1, one_hot=True) + print(f"step {i} accuracy1 = {acc1}, acc2 = {acc2}") + print(f"step {i} test acc1 = {act1}, act2 = {act2}") + + sess.run(train, feed_dict={X: train_data, Y_data: y_train, keep_prob: 0.8}) + + +# Neural Network ( input is categorical, output is regression ) +lr = 0.0015 +nn_train = (np.arange(train_data.max()) == train_data[..., None] - 1).astype(float) +nn_test = (np.arange(test_data.max()) == test_data[..., None] - 1).astype(float) +nn = models.NvNN(label_size=10, one_hot=True, out_one_hot=False) +X_data = tf.placeholder(tf.float32, shape=[None, 4, 20]) +X = tf.reshape(X_data, shape=[-1, 80]) +Y = tf.placeholder(tf.float32, shape=[None, 1]) +keep_prob = tf.placeholder(tf.float32) +h_nn = nn.forward(X, keep_prob=keep_prob) +cost_nn = tf.reduce_mean(tf.square(h_nn - Y)) +train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost_nn) + + +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + for i in range(5000): + if i % 10 == 0: + hypo, y_real = sess.run([h_nn, Y], feed_dict={X_data: nn_train, Y: train_label, keep_prob: 1.0}) + hypo_test, y_test = sess.run([h_nn, Y], feed_dict={X_data: nn_test, Y: test_label, keep_prob: 1.0}) + acc1 = models.get_accuracy(np.rint(hypo), y_real, mode=0) + acc2 = models.get_accuracy(np.rint(hypo), y_real, mode=2) + + act1 = models.get_accuracy(np.rint(hypo_test), y_test, mode=0) + act2 = models.get_accuracy(np.rint(hypo_test), y_test, mode=2) + print(f"step {i} accuracy1 = {acc1}, acc2 = {acc2}") + print(f"step {i} test acc1 = {act1}, act2 = {act2}") + + sess.run(train, feed_dict={X_data: nn_train, Y: train_label, keep_prob: 0.8}) +""" +nv_rnn = models.NvRNN() +rnn_train = (np.arange(train_data.max()) == train_data[..., None] - 1).astype(float) +X1 = tf.placeholder(tf.float32, shape=[None, 21]) +X2 = tf.placeholder(tf.float32, shape=[None, 21]) +X3 = tf.placeholder(tf.float32, shape=[None, 21]) +X4 = tf.placeholder(tf.float32, shape=[None, 21]) +X5 = tf.placeholder(tf.float32, shape=[None, 21]) +Y_data = tf.placeholder(tf.int32, shape=[None]) +Y = tf.one_hot(Y_data, depth=10) +keep_prob = tf.placeholder(tf.float32) +h_rnn = nv_rnn.forward(X1, X2, X3, X4, X5, keep_prob=keep_prob) +cost_rnn = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=h_rnn) +train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost_rnn) +in_x1 = rnn_train[:, (0)] +in_x2 = rnn_train[:, (1)] +in_x3 = rnn_train[:, (2)] +in_x4 = rnn_train[:, (3)] +in_x5 = rnn_train[:, (4)] +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + y_train = train_label.reshape([-1]) + for i in range(10000): + if i % 10: + hypo, y_real = sess.run([h_rnn, Y], feed_dict={X1: in_x1, X2: in_x2, X3: in_x3, X4: in_x4, X5: in_x5, Y_data: y_train, keep_prob: 1.0}) + acc = models.get_accuracy(hypo, y_real, mode=1, one_hot=True) + print(f'step {i} accuracy = {acc}') + sess.run(train, feed_dict={X1: in_x1, X2: in_x2, X3: in_x3, X4: in_x4, X5: in_x5, Y_data: y_train, keep_prob: 0.8}) diff --git a/dataset/preprocessed/388.txt b/dataset/preprocessed/388.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/388.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/389.txt b/dataset/preprocessed/389.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/389.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/39.txt b/dataset/preprocessed/39.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/39.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/390.txt b/dataset/preprocessed/390.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/390.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/391.txt b/dataset/preprocessed/391.txt new file mode 100644 index 0000000..c56d19b --- /dev/null +++ b/dataset/preprocessed/391.txt @@ -0,0 +1,59 @@ +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +import numpy as np +batch = 100 +X = tf.placeholder(tf.float32, [None, 784]) +X_reshaped = tf.reshape(X, [-1, 28, 28, 1]) +Y = tf.placeholder(tf.float32, [None, 10]) +mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) + + +def trainown(X, kernel, kernel2, kernel3, kernel4, kernel5, droupout_prob): + ksize = [1, 2, 2, 1] + strides = [1, 1, 1, 1] + strides2 = [1, 2, 2, 1] + filter1 = tf.Variable(tf.random_normal(kernel, stddev=0.05)) + layer1_conv = tf.nn.relu(tf.nn.conv2d(X, filter1, strides=strides, padding='SAME')) + layer1_maxpool = tf.nn.relu(tf.nn.max_pool(layer1_conv, ksize=ksize, strides=[1, 2, 2, 1], padding='SAME')) + filter2 = tf.Variable(tf.random_normal(kernel2, stddev=0.05)) + layer2_conv = tf.nn.relu(tf.nn.conv2d(layer1_maxpool, filter2, strides=strides, padding='SAME')) + layer2_maxpool = tf.nn.relu(tf.nn.max_pool(layer2_conv, ksize=ksize, strides=strides2, padding='SAME')) + layer2 = tf.nn.dropout(layer2_maxpool, droupout_prob) + filter3 = tf.Variable(tf.random_normal(kernel3, stddev=0.05)) + layer3_conv = tf.nn.relu(tf.nn.conv2d(layer2, filter3, strides=strides, padding='SAME')) + layer3_maxpool = tf.nn.relu(tf.nn.max_pool(layer3_conv, ksize=ksize, strides=strides2, padding='SAME')) + layer3 = tf.nn.dropout(layer3_maxpool, droupout_prob) + filter4 = tf.Variable(tf.random_normal(kernel4, stddev=0.05)) + shapestraight = [-1, 4 * 4 * 128] + layer4_shaping = tf.reshape(layer3, shapestraight) + layer4drop = tf.nn.dropout(layer4_shaping, droupout_prob) + layer4 = tf.nn.relu(tf.matmul(layer4drop, filter4)) + filter5 = tf.Variable(tf.random_normal(kernel5, stddev=0.05)) + layer5drop = tf.nn.dropout(layer4, droupout_prob) + output = tf.nn.relu(tf.matmul(layer5drop, filter5)) + return output + + +kernel = [3, 3, 1, 32] +kernel2 = [3, 3, 32, 64] +kernel3 = [3, 3, 64, 128] +kernel4 = [4 * 4 * 128, 500] +kernel5 = [500, 10] +droupout_prob = tf.placeholder(tf.float32) +train_layer = trainown(X_reshaped, kernel, kernel2, kernel3, kernel4, kernel5, droupout_prob) +Y_ = tf.nn.softmax(train_layer) +correct = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +cost = tf.nn.softmax_cross_entropy_with_logits(logits=train_layer, labels=Y) +optimize = tf.train.GradientDescentOptimizer(0.005).minimize(cost) +init = tf.initialize_all_variables() +with tf.Session() as session: + session.run(init) + for ep in range(10): + for i in range(int(len(mnist.train.labels) / batch)): + trainimg, trainlbl = mnist.train.next_batch(batch_size=batch) + data = {X: trainimg, Y: trainlbl, droupout_prob: 0.5} + session.run(optimize, feed_dict=data) + data = {X: mnist.test.images, Y: mnist.test.labels, droupout_prob: 1} + print('after training : ') + print(session.run(accuracy, feed_dict=data)) diff --git a/dataset/preprocessed/392.txt b/dataset/preprocessed/392.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/392.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/393.txt b/dataset/preprocessed/393.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/393.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/394.txt b/dataset/preprocessed/394.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/394.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/395.txt b/dataset/preprocessed/395.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/395.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/396.txt b/dataset/preprocessed/396.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/396.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/397.txt b/dataset/preprocessed/397.txt new file mode 100644 index 0000000..e8c5d97 --- /dev/null +++ b/dataset/preprocessed/397.txt @@ -0,0 +1,83 @@ +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from SpeechDataUtils import SpeechDataSet +timit_database = TimitDatabase('G:\\TIMIT') +max_input_sequence_length = timit_database.get_max_mfcc_features_length() +max_output_sequence_length = timit_database.get_max_phonemes_length() +dictionary_size = timit_database.phonemes_dictionary_size +train_data = timit_database.train_dataset +eval_data = timit_database.test_dataset +input_placeholder = tf.placeholder(tf.float32, shape=[None, max_input_sequence_length, 40]) +input_lengths_placeholder = tf.placeholder(tf.int32, shape=[None]) +output_placeholder = tf.sparse_placeholder(tf.int32) +output_lengths_placeholder = tf.placeholder(tf.int32, shape=[None]) +cell = tf.nn.rnn_cell.LSTMCell(128) +cell = tf.nn.rnn_cell.MultiRNNCell([cell] * 2) +rnn, _ = tf.nn.dynamic_rnn(cell, input_placeholder, input_lengths_placeholder, dtype=tf.float32) +rnn = tf.reshape(rnn, shape=[-1, max_input_sequence_length * 128]) +weights = tf.Variable(tf.random_normal(shape=[max_input_sequence_length * 128, 256], stddev=0.1)) +biases = tf.Variable(tf.zeros(shape=[256])) +layer = tf.matmul(rnn, weights) + biases +layer = tf.nn.relu(layer) +weights = tf.Variable(tf.random_normal(shape=[256, max_output_sequence_length * dictionary_size], stddev=0.1)) +biases = tf.Variable(tf.zeros(shape=[max_output_sequence_length * dictionary_size])) +layer = tf.matmul(layer, weights) + biases +layer = tf.nn.relu(layer) +logits = tf.reshape(layer, shape=[-1, max_output_sequence_length, dictionary_size]) +logits_timemajor = tf.transpose(logits, [1, 0, 2]) +loss = tf.nn.ctc_loss(logits_timemajor, output_placeholder, output_lengths_placeholder) +loss = tf.reduce_mean(loss) +optimizer = tf.train.AdamOptimizer() +train = optimizer.minimize(loss) +session_config = tf.ConfigProto() +session_config.gpu_options.allow_growth = True +with tf.Session(config=session_config) as session: + init = tf.global_variables_initializer() + session.run(init) + for i in tqdm(range(1000)): + batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(11, False) + tmp = [] + for j in range(len(batch_outputs)): + tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + batch_outputs = np.array(tmp) + batch_outputs = SpeechDataSet.tokens_for_sparse(batch_outputs) + feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths, output_placeholder: batch_outputs, output_lengths_placeholder: batch_output_lengths} + session.run(train, feed_dict=feed_dict) + if i % 100 == 0: + print('######') + print(session.run(loss, feed_dict=feed_dict)) + print('######') + batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = train_data.next_batch(1, False) + feed_dict = {input_placeholder: batch_inputs, input_lengths_placeholder: batch_input_lengths} + test = session.run(logits, feed_dict=feed_dict) + test = session.run(tf.transpose(test, [1, 0, 2])) + decoded, prob = tf.nn.ctc_beam_search_decoder(test, batch_output_lengths, 100, 5) + decoded = session.run(decoded) + prob = session.run(prob) + results = [] + for decoded_path in decoded: + phonemes_ids = decoded_path.values + result_words = '' + for idx in phonemes_ids: + word = timit_database.id_to_phoneme_dictionary[idx] + if word != '': + result_words += word + ' ' + results += [result_words] + target = batch_outputs[0] + target_words = '' + for idx in target: + word = timit_database.id_to_phoneme_dictionary[idx] + if word != '': + target_words += word + ' ' + print('--------------') + print('--------------') + print('--------------') + print('Target : \n', target_words, '\n') + print('Results') + for result_sentence in results: + print(result_sentence) + print('--------------') + print('--------------') + print('--------------') diff --git a/dataset/preprocessed/398.txt b/dataset/preprocessed/398.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/398.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/399.txt b/dataset/preprocessed/399.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/399.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/4.txt b/dataset/preprocessed/4.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/4.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/40.txt b/dataset/preprocessed/40.txt new file mode 100644 index 0000000..28eb8c3 --- /dev/null +++ b/dataset/preprocessed/40.txt @@ -0,0 +1,193 @@ +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import load_policy +import math +import logz +import time + + +class Config(object): + n_features = 17 + n_classes = 6 + dropout = 0.5 + hidden_size_1 = 128 + hidden_size_2 = 256 + hidden_size_3 = 64 + batch_size = 256 + lr = 0.0005 + itera = 20 + train_itera = 20 + envname = 'Walker2d-v1' + max_steps = 1000 + + +class NN(object): + + def add_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_features), name='input') + self.labels_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_classes), name='label') + self.dropout_placeholder = tf.placeholder(tf.float32, name='drop') + self.is_training = tf.placeholder(tf.bool) + + def create_feed_dict(self, inputs_batch, labels_batch=None, dropout=1, is_training=False): + if labels_batch is None: + feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + else: + feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + return feed_dict + + def add_prediction_op(self): + self.global_step = tf.Variable(0) + with tf.name_scope('layer1'): + hidden1 = tf.contrib.layers.fully_connected(self.input_placeholder, num_outputs=Config.hidden_size_1, activation_fn=tf.nn.relu) + with tf.name_scope('layer2'): + hidden2 = tf.contrib.layers.fully_connected(hidden1, num_outputs=Config.hidden_size_2, activation_fn=tf.nn.relu) + with tf.name_scope('layer3'): + hidden3 = tf.contrib.layers.fully_connected(hidden2, num_outputs=Config.hidden_size_3, activation_fn=tf.nn.relu) + with tf.name_scope('output'): + pred = tf.contrib.layers.fully_connected(hidden3, num_outputs=Config.n_classes, activation_fn=None) + return pred + + def add_loss_op(self, pred): + loss = tf.losses.mean_squared_error(predictions=pred, labels=self.labels_placeholder) + tf.summary.scalar('loss', loss) + return loss + + def add_training_op(self, loss): + extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + with tf.control_dependencies(extra_update_ops): + learning_rate = tf.train.exponential_decay(Config.lr, self.global_step, 1000, 0.8, staircase=True) + train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=self.global_step) + return train_op + + def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + rs, _, loss = sess.run([merged, self.train_op, self.loss], feed_dict=feed) + train_writer.add_summary(rs, i) + return loss + + def __init__(self, config): + self.config = config + self.build() + + def fit(self, sess, train_x, train_y): + loss = self.train_on_batch(sess, train_x, train_y) + + def build(self): + with tf.name_scope('inputs'): + self.add_placeholders() + with tf.name_scope('predict'): + self.pred = self.add_prediction_op() + with tf.name_scope('loss'): + self.loss = self.add_loss_op(self.pred) + with tf.name_scope('train'): + self.train_op = self.add_training_op(self.loss) + + def get_pred(self, sess, inputs_batch): + feed = self.create_feed_dict(inputs_batch, dropout=1, is_training=False) + p = sess.run(self.pred, feed_dict=feed) + return p + + +def load(path): + all = np.load(path) + X = all['arr_0'] + y = all['arr_1'] + y1 = y.reshape(y.shape[0], y.shape[2]) + return X, y1 + + +def run_env(env, nn, session): + obs = env.reset() + done = False + totalr = 0.0 + steps = 0 + observations = [] + while not done: + action = nn.get_pred(session, obs[(None), :]) + observations.append(obs) + obs, r, done, _ = env.step(action) + totalr += r + steps += 1 + if steps >= Config.max_steps: + break + return totalr, observations + + +def shuffle(X_train, y_train): + training_data = np.concatenate((X_train, y_train), axis=1) + np.random.shuffle(training_data) + X = training_data[:, :-Config.n_classes] + y = training_data[:, -Config.n_classes:] + return X, y + + +def main(): + PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + policy_path = os.path.join(PROJECT_ROOT, 'experts/' + Config.envname + '.pkl') + train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_DA_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + X_train, y_train = load(train_path) + print('train size :', X_train.shape, y_train.shape) + print('start training') + with tf.Graph().as_default(): + config = Config() + nn = NN(config) + init = tf.global_variables_initializer() + saver = tf.train.Saver(max_to_keep=10, keep_checkpoint_every_n_hours=0.5) + print('loading and building expert policy') + policy_fn = load_policy.load_policy(policy_path) + print('loaded and built') + with tf.Session() as session: + merged = tf.summary.merge_all() + train_writer = tf.summary.FileWriter(train_log_path, session.graph) + session.run(init) + coord = tf.train.Coordinator() + threads = tf.train.start_queue_runners(session, coord) + for j in tqdm.tqdm(range(Config.itera)): + X_train, y_train = shuffle(X_train, y_train) + i = 0 + try: + for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + offset = i * Config.batch_size % X_train.shape[0] + batch_x = X_train[offset:offset + Config.batch_size, :] + batch_y = y_train[offset:offset + Config.batch_size] + loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + i += 1 + print('step:', i, 'loss:', loss) + except tf.errors.OutOfRangeError: + print('done') + finally: + coord.request_stop() + coord.join(threads) + observations = [] + actions = [] + env = gym.make(Config.envname) + for _ in range(10): + _, o = run_env(env, nn, session) + observations.extend(o) + action = policy_fn(o) + actions.extend(action) + new_x = np.array(observations) + new_y = np.array(actions) + X_train = np.concatenate((X_train, new_x)) + y_train = np.concatenate((y_train, new_y)) + print('train size :', X_train.shape, y_train.shape) + rollouts = 20 + returns = [] + for _ in range(rollouts): + totalr, _ = run_env(env, nn, session) + returns.append(totalr) + print() + logz.log_tabular('Iteration', j) + logz.log_tabular('AverageReturn', np.mean(returns)) + logz.log_tabular('StdReturn', np.std(returns)) + logz.dump_tabular() + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/401.txt b/dataset/preprocessed/401.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/401.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/402.txt b/dataset/preprocessed/402.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/402.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/403.txt b/dataset/preprocessed/403.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/403.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/404.txt b/dataset/preprocessed/404.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/404.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/405.txt b/dataset/preprocessed/405.txt new file mode 100644 index 0000000..051316c --- /dev/null +++ b/dataset/preprocessed/405.txt @@ -0,0 +1,214 @@ +from __future__ import print_function +from __future__ import division +import imageio +import matplotlib.pyplot as plt +import numpy as np +import sys +import tensorflow as tf +from six.moves import cPickle as pickle +import random +image_size = 28 +pickle_file = '../Dataset/cleanShuffledMNIST.pickle' +with open(pickle_file, 'rb') as f: + a = pickle.load(f) +train_labels = a['train_labels'] +test_labels = a['test_labels'] +validate_labels = a['valid_labels'] +train_dataset = a['train_dataset'] +test_dataset = a['test_dataset'] +validate_dataset = a['valid_dataset'] +lst = ['I', 'C', 'J', 'F', 'B', 'H', 'A', 'E', 'D', 'G'] + + +def flattenAndOneHot(Dataset, labels): + Dataset = Dataset.reshape((-1, image_size * image_size)).astype(np.float32) + one_hot = np.zeros((len(labels), 10)) + one_hot[np.arange(len(labels)), labels] = 1 + return Dataset, one_hot + + +train_dataset, train_labels = flattenAndOneHot(train_dataset, train_labels) +test_dataset, test_labels = flattenAndOneHot(test_dataset, test_labels) +validate_dataset, validate_labels = flattenAndOneHot(validate_dataset, validate_labels) +num_category = 10 + + +def Accuracy(prediction1, labels1): + pred1 = np.argmax(prediction1, 1) + label1 = np.argmax(labels1, 1) + acc = 100 * np.sum(pred1 == label1) / prediction1.shape[0] + return acc + + +def SGDRegularise(): + batchsize = 200 + graph = tf.Graph() + with graph.as_default(): + tf_train_labels = tf.placeholder(tf.float32, shape=(batchsize, num_category)) + tf_train_data = tf.placeholder(tf.float32, shape=(batchsize, image_size * image_size)) + beta_regul = tf.placeholder(tf.float32) + tf_validate = tf.constant(validate_dataset) + tf_test = tf.constant(test_dataset) + Weight = tf.Variable(tf.truncated_normal([image_size * image_size, num_category])) + bias = tf.Variable(tf.zeros(num_category)) + mat = tf.matmul(tf_train_data, Weight) + bias + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=mat, labels=tf_train_labels) + beta_regul * tf.nn.l2_loss(Weight)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(mat) + valid_prediction = tf.nn.softmax(tf.matmul(tf_validate, Weight) + bias) + test_prediction = tf.nn.softmax(tf.matmul(tf_test, Weight) + bias) + noSteps = 900 + lossArray = np.zeros(1) + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('\n.......Initialized Stocastic Gradient Decent.........') + for step in range(noSteps): + offset = random.randint(1, train_dataset.shape[0] - batchsize) + batch_data = train_dataset[offset:offset + batchsize, :] + batch_labels = train_labels[offset:offset + batchsize, :] + feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, predictions, l = session.run([optimizer, train_prediction, loss], feed_dict=feed_dict) + if step % 100 == 0: + print('\nloss at step %d is %f' % (step, l)) + lossArray = np.concatenate((lossArray, [l])) + acc = Accuracy(predictions, batch_labels) + print('Training accuracy is %lf ' % acc) + accv = Accuracy(valid_prediction.eval(session=session), validate_labels) + print('Validation accuracy is %lf ' % accv) + x = np.arange(300, 1000, 100) + plt.plot(x, lossArray[3:], linewidth=2) + plt.show() + print('\nTest accuracy: %lf ' % Accuracy(test_prediction.eval(session=session), test_labels)) + session.close() + + +def SGDReluRegulariseDemo(): + batchsize = 200 + middle_layerSize = 1024 + graph = tf.Graph() + with graph.as_default(): + tf_train_labels = tf.placeholder(tf.float32, shape=(batchsize, num_category)) + tf_train_data = tf.placeholder(tf.float32, shape=(batchsize, image_size * image_size)) + beta_regul = tf.placeholder(tf.float32) + tf_validate = tf.constant(validate_dataset) + tf_test = tf.constant(test_dataset) + Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + bias = tf.Variable(tf.zeros(middle_layerSize)) + WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + biasNext = tf.Variable(tf.zeros(num_category)) + layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + mat = tf.matmul(layer, WeightNext) + biasNext + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=mat, labels=tf_train_labels) + tf.nn.l2_loss(beta_regul * Weight) + tf.nn.l2_loss(beta_regul * WeightNext)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(mat) + layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + noSteps = 800 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('\n.......Initialized SGD with Relu.........') + for step in range(noSteps): + offset = random.randint(1, train_dataset.shape[0] - batchsize) + batch_data = train_dataset[offset:offset + batchsize, :] + batch_labels = train_labels[offset:offset + batchsize, :] + feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, predictions, l = session.run([optimizer, train_prediction, loss], feed_dict=feed_dict) + if step % 100 == 0: + print('\nloss at step %d is %f' % (step, l)) + acc = Accuracy(predictions, batch_labels) + print('Training accuracy is %lf ' % acc) + acc = Accuracy(valid_prediction.eval(session=session), validate_labels) + print('Validation accuracy is %lf ' % acc) + print('\nTest accuracy: %lf ' % Accuracy(test_prediction.eval(session=session), test_labels)) + session.close() + + +def Dropout(): + batchsize = 200 + middle_layerSize = 1024 + graph = tf.Graph() + with graph.as_default(): + tf_train_labels = tf.placeholder(tf.float32, shape=(batchsize, num_category)) + tf_train_data = tf.placeholder(tf.float32, shape=(batchsize, image_size * image_size)) + tf_validate = tf.constant(validate_dataset) + tf_test = tf.constant(test_dataset) + Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + bias = tf.Variable(tf.zeros(middle_layerSize)) + WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + biasNext = tf.Variable(tf.zeros(num_category)) + layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + drop = tf.nn.dropout(layer, 0.5) + mat = tf.matmul(drop, WeightNext) + biasNext + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=mat, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(mat) + layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + noSteps = 5000 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('\n.......Initialized SGD with Relu.........') + for step in range(noSteps): + offset = step * batchsize % (train_dataset.shape[0] - batchsize) + batch_data = train_dataset[offset:offset + batchsize, :] + batch_labels = train_labels[offset:offset + batchsize, :] + feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + _, predictions, l = session.run([optimizer, train_prediction, loss], feed_dict=feed_dict) + if step % 100 == 0: + print('\nloss at step %d is %f' % (step, l)) + acc = Accuracy(predictions, batch_labels) + print('Training accuracy is %lf ' % acc) + acc = Accuracy(valid_prediction.eval(session=session), validate_labels) + print('Validation accuracy is %lf ' % acc) + print('\nTest accuracy: %lf ' % Accuracy(test_prediction.eval(session=session), test_labels)) + session.close() + + +Dropout() + + +def DeepDropout(): + batchsize = 200 + middle_layerSize = 1024 + graph = tf.Graph() + with graph.as_default(): + tf_train_labels = tf.placeholder(tf.float32, shape=(batchsize, num_category)) + tf_train_data = tf.placeholder(tf.float32, shape=(batchsize, image_size * image_size)) + tf_validate = tf.constant(validate_dataset) + tf_test = tf.constant(test_dataset) + Weight = tf.Variable(tf.truncated_normal([image_size * image_size, middle_layerSize])) + bias = tf.Variable(tf.zeros(middle_layerSize)) + WeightNext = tf.Variable(tf.truncated_normal([middle_layerSize, num_category])) + biasNext = tf.Variable(tf.zeros(num_category)) + layer = tf.nn.relu(tf.matmul(tf_train_data, Weight) + bias) + drop = tf.nn.dropout(layer, 0.5) + mat = tf.matmul(drop, WeightNext) + biasNext + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=mat, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(mat) + layer_valid = tf.nn.relu(tf.matmul(tf_validate, Weight) + bias) + valid_prediction = tf.nn.softmax(tf.matmul(layer_valid, WeightNext) + biasNext) + layer_test = tf.nn.relu(tf.matmul(tf_test, Weight) + bias) + test_prediction = tf.nn.softmax(tf.matmul(layer_test, WeightNext) + biasNext) + noSteps = 5000 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('\n.......Initialized SGD with Relu.........') + for step in range(noSteps): + offset = step * batchsize % (train_dataset.shape[0] - batchsize) + batch_data = train_dataset[offset:offset + batchsize, :] + batch_labels = train_labels[offset:offset + batchsize, :] + feed_dict = {tf_train_data: batch_data, tf_train_labels: batch_labels} + _, predictions, l = session.run([optimizer, train_prediction, loss], feed_dict=feed_dict) + if step % 100 == 0: + print('\nloss at step %d is %f' % (step, l)) + acc = Accuracy(predictions, batch_labels) + print('Training accuracy is %lf ' % acc) + acc = Accuracy(valid_prediction.eval(session=session), validate_labels) + print('Validation accuracy is %lf ' % acc) + print('\nTest accuracy: %lf ' % Accuracy(test_prediction.eval(session=session), test_labels)) + session.close() diff --git a/dataset/preprocessed/406.txt b/dataset/preprocessed/406.txt new file mode 100644 index 0000000..e3a0ca0 --- /dev/null +++ b/dataset/preprocessed/406.txt @@ -0,0 +1,248 @@ +import matplotlib.pyplot as plt +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regular = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss += beta_regular * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Problem 1 : using Logistic Regression') + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.01} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regular = tf.placeholder(tf.float32) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + hidden_1 = tf.nn.relu(logits_1) + logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_2)) + loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Problem 1 : using Nueral Network') + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regular = tf.placeholder(tf.float32) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + hidden_1 = tf.nn.relu(logits_1) + logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_2)) + loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +num_bacthes = 3 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Problem 2 : using Nueral Network') + print('Initialized') + for step in range(num_steps): + offset = step % num_bacthes + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_layer_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regular = tf.placeholder(tf.float32) + keep_prob = tf.placeholder(tf.float32) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes])) + biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes, num_labels])) + biases_2 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + hidden_1 = tf.nn.relu(logits_1) + drop_1 = tf.nn.dropout(hidden_1, keep_prob) + logits_2 = tf.matmul(drop_1, weights_2) + biases_2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_2)) + loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_2) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1), weights_2) + biases_2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1), weights_2) + biases_2) +num_steps = 3001 +num_bacthes = 3 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Problem 3 : using Nueral Network') + print('Initialized') + for step in range(num_steps): + offset = step % num_bacthes + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.001, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_layer_nodes_1 = 1024 +hidden_layer_nodes_2 = 512 +hidden_layer_nodes_3 = 256 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regular = tf.placeholder(tf.float32) + keep_prob = tf.placeholder(tf.float32) + global_step = tf.Variable(0) + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_nodes_1], stddev=np.sqrt(2.0 / image_size ** 2))) + biases_1 = tf.Variable(tf.zeros([hidden_layer_nodes_1])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_nodes_1, hidden_layer_nodes_2], stddev=np.sqrt(2.0 / hidden_layer_nodes_1))) + biases_2 = tf.Variable(tf.zeros([hidden_layer_nodes_2])) + weights_3 = tf.Variable(tf.truncated_normal([hidden_layer_nodes_2, hidden_layer_nodes_3], stddev=np.sqrt(2.0 / hidden_layer_nodes_2))) + biases_3 = tf.Variable(tf.zeros([hidden_layer_nodes_3])) + weights_4 = tf.Variable(tf.truncated_normal([hidden_layer_nodes_3, num_labels], stddev=np.sqrt(2.0 / hidden_layer_nodes_3))) + biases_4 = tf.Variable(tf.zeros([num_labels])) + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + hidden_1 = tf.nn.relu(logits_1) + logits_2 = tf.matmul(hidden_1, weights_2) + biases_2 + hidden_2 = tf.nn.relu(logits_2) + logits_3 = tf.matmul(hidden_2, weights_3) + biases_3 + hidden_3 = tf.nn.relu(logits_3) + drop_3 = tf.nn.dropout(hidden_3, keep_prob) + logits_4 = tf.matmul(drop_3, weights_4) + biases_4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits_4)) + loss += beta_regular * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_4)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 10000, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits_4) + valid_hidden_1 = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_1) + biases_1) + valid_hidden_2 = tf.nn.relu(tf.matmul(valid_hidden_1, weights_2) + biases_2) + valid_hidden_3 = tf.nn.relu(tf.matmul(valid_hidden_2, weights_3) + biases_3) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden_3, weights_4) + biases_4) + test_hidden_1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights_1) + biases_1) + test_hidden_2 = tf.nn.relu(tf.matmul(test_hidden_1, weights_2) + biases_2) + test_hidden_3 = tf.nn.relu(tf.matmul(test_hidden_2, weights_3) + biases_3) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden_3, weights_4) + biases_4) +num_steps = 20001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Problem 4 : using Nueral Network') + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regular: 0.002, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/407.txt b/dataset/preprocessed/407.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/407.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/408.txt b/dataset/preprocessed/408.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/408.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/409.txt b/dataset/preprocessed/409.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/409.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/410.txt b/dataset/preprocessed/410.txt new file mode 100644 index 0000000..f94bc49 --- /dev/null +++ b/dataset/preprocessed/410.txt @@ -0,0 +1,142 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape(-1, image_size * image_size) + zero_labels = np.zeros((len(labels), len(set(labels)))) + zero_labels[np.arange(len(labels)), labels] = 1 + return dataset, zero_labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +print(train_labels[0]) +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + _, l, prediction = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d %f' % (step, l)) + print('Training_accuarcy : %.1f%%' % accuracy(prediction, train_labels[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden1 = 500 +num_hidden2 = 500 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_one = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden1])) + biases_one = tf.Variable(tf.zeros([num_hidden1])) + weights_two = tf.Variable(tf.truncated_normal([num_hidden1, num_labels])) + biases_two = tf.Variable(tf.zeros([num_labels])) + logits_one = tf.matmul(tf_train_dataset, weights_one) + biases_one + layer1 = tf.nn.relu(logits_one) + logits = tf.matmul(layer1, weights_two) + biases_two + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_one) + biases_one), weights_two) + biases_two) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_one) + biases_one), weights_two) + biases_two) +num_steps = 1001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_one = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden1])) + biases_one = tf.Variable(tf.zeros([num_hidden1])) + weights_three = tf.Variable(tf.truncated_normal([num_hidden1, num_hidden2])) + biases_three = tf.Variable(tf.zeros([num_hidden2])) + weights_two = tf.Variable(tf.truncated_normal([num_hidden2, num_labels])) + biases_two = tf.Variable(tf.zeros([num_labels])) + logits_one = tf.matmul(tf_train_dataset, weights_one) + biases_one + layer2 = tf.nn.softmax(logits_one) + logits_two = tf.matmul(layer2, weights_three) + biases_three + layer1 = tf.nn.relu(logits_two) + logits = tf.matmul(layer1, weights_two) + biases_two + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf.nn.softmax(tf.matmul(tf_test_dataset, weights_one) + biases_one), weights_three) + biases_three), weights_two) + biases_two) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf.nn.softmax(tf.matmul(tf_valid_dataset, weights_one) + biases_one), weights_three) + biases_three), weights_two) + biases_two) +num_steps = 30001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/411.txt b/dataset/preprocessed/411.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/411.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/412.txt b/dataset/preprocessed/412.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/412.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/413.txt b/dataset/preprocessed/413.txt new file mode 100644 index 0000000..2b03454 --- /dev/null +++ b/dataset/preprocessed/413.txt @@ -0,0 +1,65 @@ +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.01) + return tf.Variable(initial) + + +def bias_variable(shape): + initial = tf.constant(0.01, shape=shape) + return tf.Variable(initial) + + +def conv2d(x, W, stride): + return tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +def createNetwork(action_count): + W_conv1 = weight_variable([8, 8, 4, 32]) + b_conv1 = bias_variable([32]) + W_conv2 = weight_variable([4, 4, 32, 64]) + b_conv2 = bias_variable([64]) + W_conv3 = weight_variable([3, 3, 64, 64]) + b_conv3 = bias_variable([64]) + W_fc1 = weight_variable([1600, 512]) + b_fc1 = bias_variable([512]) + W_fc2 = weight_variable([512, action_count]) + b_fc2 = bias_variable([action_count]) + s = tf.placeholder('float', [None, 80, 80, 4]) + h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + return s, readout, h_fc1 + + +class DQNAgent(object): + + def __init__(self, action_count): + self.action_count = action_count + self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + self.action_placeholder = tf.placeholder('float', [None, action_count]) + self.reward_placeholder = tf.placeholder('float', [None]) + self.readout_action = tf.reduce_sum(tf.mul(self.readout, self.action_placeholder), reduction_indices=1) + self.cost = tf.reduce_mean(tf.square(self.reward_placeholder - self.readout_action)) + self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.cost) + + def act(self, session, state): + scores = self.score_actions(session, [state])[0] + return np.argmax(scores) + + def score_actions(self, session, state_batch): + return session.run(self.readout, feed_dict={self.state_placeholder: state_batch}) + + def train(self, session, reward_batch, action_batch, state_batch): + _, loss = session.run([self.train_step, self.cost], feed_dict={self.reward_placeholder: reward_batch, self.action_placeholder: action_batch, self.state_placeholder: state_batch}) + return loss diff --git a/dataset/preprocessed/414.txt b/dataset/preprocessed/414.txt new file mode 100644 index 0000000..2179fc6 --- /dev/null +++ b/dataset/preprocessed/414.txt @@ -0,0 +1,75 @@ +from read_data import * +from time import time +import tensorflow as tf +batch_size = 128 +hidden_nodes = 2048 +hidden_nodes_l2 = 512 +starter_learn_rate = 0.5 +reg_beta = 0.002 +num_steps = 5001 + + +def train_three_layer(X, use_dropout): + y1 = tf.nn.relu(tf.matmul(X, W1) + b1) + y2 = None + if use_dropout: + y1d = tf.nn.dropout(y1, input_keep_prob) + y2 = tf.nn.relu(tf.matmul(y1d, W2) + b2) + else: + y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + y3 = None + if use_dropout: + y2d = tf.nn.dropout(y2, train_keep_prob) + y3 = tf.nn.relu(tf.matmul(y2d, W3) + b3) + else: + y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + return y3 + + +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + input_keep_prob = tf.placeholder('float') + train_keep_prob = tf.placeholder('float') + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes], stddev=0.03)) + b1 = tf.Variable(tf.zeros([hidden_nodes])) + W2 = tf.Variable(tf.truncated_normal([hidden_nodes, hidden_nodes_l2], stddev=0.01)) + b2 = tf.Variable(tf.zeros([hidden_nodes_l2])) + W3 = tf.Variable(tf.truncated_normal([hidden_nodes_l2, num_labels], stddev=0.01)) + b3 = tf.Variable(tf.zeros([num_labels])) + y_train = train_three_layer(tf_train_dataset, True) + y_valid = train_three_layer(tf_valid_dataset, True) + y_test = train_three_layer(tf_test_dataset, True) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_train, tf_train_labels)) + regularizers = tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + loss += reg_beta * regularizers + loss_summary = tf.scalar_summary('loss', loss) + global_step = tf.Variable(0) + learn_rate = tf.train.exponential_decay(starter_learn_rate, global_step, 500, 0.96) + optimizer = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss, global_step=global_step) + merged = tf.merge_all_summaries() +train_prediction = tf.nn.softmax(y_train) +valid_prediction = tf.nn.softmax(y_valid) +test_prediction = tf.nn.softmax(y_test) +with tf.Session(graph=graph) as session: + writer = tf.train.SummaryWriter('/tmp/notmnist_logs', session.graph_def) + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, input_keep_prob: 0.9, train_keep_prob: 0.5} + _, l, predictions, merged_summary, lr = session.run([optimizer, loss, train_prediction, merged, learn_rate], feed_dict=feed_dict) + writer.add_summary(merged_summary, step) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(train_prediction.eval(feed_dict={tf_train_dataset: batch_data, tf_train_labels: batch_labels, input_keep_prob: 1.0, train_keep_prob: 1.0}), batch_labels)) + print('Learn rate: ', lr) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(feed_dict={input_keep_prob: 1.0, train_keep_prob: 1.0}), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(feed_dict={input_keep_prob: 1.0, train_keep_prob: 1.0}), test_labels)) + print('Elapsed time: %.1f seconds' % (time() - t0)) diff --git a/dataset/preprocessed/416.txt b/dataset/preprocessed/416.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/416.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/417.txt b/dataset/preprocessed/417.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/417.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/418.txt b/dataset/preprocessed/418.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/418.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/419.txt b/dataset/preprocessed/419.txt new file mode 100644 index 0000000..f18d651 --- /dev/null +++ b/dataset/preprocessed/419.txt @@ -0,0 +1,264 @@ +import numpy as np +import tensorflow as tf +from helpers.layer_helpers import weight_variable, conv2d, flatten, linear, normalized_columns_initializer, categorical_sample +from helpers.custom_rnn_cells import GridPredictionLSTMCell, PredictionLSTMStateTuple +""" +The following class it be used when the tasks are (1) A3C and (2) grid pixel intensity change predictions, +and there is a feedback of predictions to the LSTM cell. +""" + + +class GridPredictionLSTMPolicy(object): + + def __init__(self, ob_space, ac_space, replay_size=2000, grid_size=20): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.bs = tf.placeholder(dtype=tf.int32) + self.replay_memory = [] + self.replay_size = replay_size + self.grid_size = grid_size + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + x = tf.concat(concat_dim=1, values=[x, self.action, self.reward]) + x = tf.expand_dims(x, [0]) + size = 256 + lstm = GridPredictionLSTMCell(size, state_is_tuple=True, ac_space=ac_space, grid_size=20) + self.state_size = lstm.state_size + step_size = tf.shape(self.x)[:1] + c_init = np.zeros((1, lstm.state_size.c), np.float32) + h_init = np.zeros((1, lstm.state_size.h), np.float32) + pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + self.state_init = [c_init, h_init, pred_init] + c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + pred_in = tf.placeholder(tf.float32, [1, lstm.state_size.pred]) + self.state_in = [c_in, h_in, pred_in] + state_in = PredictionLSTMStateTuple(c_in, h_in, pred_in) + lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm, x, initial_state=state_in, sequence_length=step_size, time_major=False) + lstm_c, lstm_h, lstm_pred = lstm_state + x = tf.reshape(lstm_outputs, [-1, size]) + self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + self.sample = categorical_sample(self.logits, ac_space)[(0), :] + self.predictions = tf.reshape(lstm_pred, shape=[-1, grid_size, grid_size, ac_space]) + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + self.target_weights = [] + + def get_initial_features(self): + return self.state_init + + def act(self, ob, prev_a, prev_r, c, h, pred): + sess = tf.get_default_session() + return sess.run([self.sample, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred}) + + def value(self, ob, prev_a, prev_r, c, h, pred): + sess = tf.get_default_session() + return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred})[0] + + def update_replay_memory(self, tuple): + self.replay_memory.append(tuple) + if len(self.replay_memory) > self.replay_size: + self.replay_memory.pop(0) + + def update_target_weights(self): + sess = tf.get_default_session() + self.target_weights = sess.run(self.var_list) + + +""" +The following class it be used when the tasks are (1) A3C and (2) grid pixel intensity change predictions, +and there is NOT a feedback of predictions to the LSTM cell ie this is a replication from the Jaderberg paper. +""" + + +class GridLSTMPolicy(object): + + def __init__(self, ob_space, ac_space, mode='Grid', replay_size=2000, grid_size=20): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.replay_memory = [] + self.replay_size = replay_size + self.grid_size = grid_size + self.bs = tf.placeholder(dtype=tf.int32) + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + x = tf.concat(concat_dim=1, values=[x, self.action, self.reward]) + x = tf.expand_dims(x, [0]) + size = 256 + lstm = tf.nn.rnn_cell.BasicLSTMCell(size, state_is_tuple=True) + self.state_size = lstm.state_size + step_size = tf.shape(self.x)[:1] + c_init = np.zeros((1, lstm.state_size.c), np.float32) + h_init = np.zeros((1, lstm.state_size.h), np.float32) + self.state_init = [c_init, h_init] + c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + self.state_in = [c_in, h_in] + state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) + lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm, x, initial_state=state_in, sequence_length=step_size, time_major=False) + lstm_c, lstm_h = lstm_state + x = tf.reshape(lstm_outputs, [-1, size]) + self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + self.state_out = [lstm_c[:1, :], lstm_h[:1, :]] + self.sample = categorical_sample(self.logits, ac_space)[(0), :] + if mode == 'Grid': + y = linear(x, 32 * (grid_size - 10) * (grid_size - 10), 'auxbranch', normalized_columns_initializer(0.1)) + y = tf.reshape(y, shape=[-1, grid_size - 10, grid_size - 10, 32]) + deconv_weights = weight_variable(shape=[4, 4, ac_space, 32], name='deconvweights') + self.predictions = tf.nn.conv2d_transpose(y, deconv_weights, output_shape=[self.bs, grid_size, grid_size, ac_space], strides=[1, 2, 2, 1], padding='SAME') + if mode == 'Features': + pass + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + self.target_weights = [] + + def get_initial_features(self): + return self.state_init + + def act(self, ob, prev_a, prev_r, c, h): + sess = tf.get_default_session() + return sess.run([self.sample, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h}) + + def value(self, ob, prev_a, prev_r, c, h): + sess = tf.get_default_session() + return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h})[0] + + def update_replay_memory(self, tuple): + self.replay_memory.append(tuple) + if len(self.replay_memory) > self.replay_size: + self.replay_memory.pop(0) + + def update_target_weights(self): + sess = tf.get_default_session() + self.target_weights = sess.run(self.var_list) + + +""" +Baseline A3C agent policy +""" + + +class A3CLSTMPolicy(object): + + def __init__(self, ob_space, ac_space): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + x = tf.concat(concat_dim=1, values=[x, self.action, self.reward]) + x = tf.expand_dims(x, [0]) + size = 256 + lstm = tf.nn.rnn_cell.BasicLSTMCell(size, state_is_tuple=True) + self.state_size = lstm.state_size + step_size = tf.shape(self.x)[:1] + c_init = np.zeros((1, lstm.state_size.c), np.float32) + h_init = np.zeros((1, lstm.state_size.h), np.float32) + self.state_init = [c_init, h_init] + c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + self.state_in = [c_in, h_in] + state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) + lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm, x, initial_state=state_in, sequence_length=step_size, time_major=False) + lstm_c, lstm_h = lstm_state + x = tf.reshape(lstm_outputs, [-1, size]) + self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + self.state_out = [lstm_c[:1, :], lstm_h[:1, :]] + self.sample = categorical_sample(self.logits, ac_space)[(0), :] + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + + def get_initial_features(self): + return self.state_init + + def act(self, ob, prev_a, prev_r, c, h): + sess = tf.get_default_session() + return sess.run([self.sample, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h}) + + def value(self, ob, prev_a, prev_r, c, h): + sess = tf.get_default_session() + return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h})[0] + + +""" +The following class it be used when the tasks are (1) A3C and (2) convolutional feature predictions, +and there is NOT a feedback of predictions to the LSTM cell ie this is a replication from the Jaderberg paper. +""" + + +class FeatureLSTMPolicy(object): + + def __init__(self, ob_space, ac_space, mode='Grid', replay_size=2000, grid_size=20): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.replay_memory = [] + self.replay_size = replay_size + self.grid_size = grid_size + self.bs = tf.placeholder(dtype=tf.int32) + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4], pad='VALID')) + x = self.conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2], pad='VALID')) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + x = tf.concat(concat_dim=1, values=[x, self.action, self.reward]) + x = tf.expand_dims(x, [0]) + size = 256 + lstm = tf.nn.rnn_cell.BasicLSTMCell(size, state_is_tuple=True) + self.state_size = lstm.state_size + step_size = tf.shape(self.x)[:1] + c_init = np.zeros((1, lstm.state_size.c), np.float32) + h_init = np.zeros((1, lstm.state_size.h), np.float32) + self.state_init = [c_init, h_init] + c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + self.state_in = [c_in, h_in] + state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) + lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm, x, initial_state=state_in, sequence_length=step_size, time_major=False) + lstm_c, lstm_h = lstm_state + x = tf.reshape(lstm_outputs, [-1, size]) + self.logits = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + self.vf = tf.reshape(linear(x, 1, 'value', normalized_columns_initializer(1.0)), [-1]) + self.state_out = [lstm_c[:1, :], lstm_h[:1, :]] + self.sample = categorical_sample(self.logits, ac_space)[(0), :] + if mode == 'Grid': + y = linear(x, 32 * (grid_size - 7) * (grid_size - 7), 'auxbranch', normalized_columns_initializer(0.1)) + y = tf.reshape(y, shape=[-1, grid_size - 7, grid_size - 7, 32]) + deconv_weights = weight_variable(shape=[4, 4, ac_space, 32], name='deconvweights') + self.predictions = tf.nn.conv2d_transpose(y, deconv_weights, output_shape=[self.bs, grid_size, grid_size, ac_space], strides=[1, 2, 2, 1], padding='SAME') + if mode == 'Features': + y = linear(x, 16 * 9 * 9, 'auxbranch', normalized_columns_initializer(0.1)) + y = tf.reshape(y, shape=[-1, 9, 9, 16]) + deconv_weights = weight_variable(shape=[1, 1, 32 * ac_space, 16], name='deconvweights') + predictions = tf.nn.conv2d_transpose(y, deconv_weights, output_shape=[self.bs, 9, 9, 32 * ac_space], strides=[1, 1, 1, 1], padding='SAME') + self.predictions = tf.reshape(predictions, shape=[-1, 9, 9, 32, ac_space]) + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + self.target_weights = [] + + def get_initial_features(self): + return self.state_init + + def act(self, ob, prev_a, prev_r, c, h): + sess = tf.get_default_session() + return sess.run([self.sample, self.vf, self.conv_features] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h}) + + def value(self, ob, prev_a, prev_r, c, h): + sess = tf.get_default_session() + return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h})[0] + + def update_replay_memory(self, tuple): + self.replay_memory.append(tuple) + if len(self.replay_memory) > self.replay_size: + self.replay_memory.pop(0) + + def update_target_weights(self): + sess = tf.get_default_session() + self.target_weights = sess.run(self.var_list) diff --git a/dataset/preprocessed/42.txt b/dataset/preprocessed/42.txt new file mode 100644 index 0000000..9c2475c --- /dev/null +++ b/dataset/preprocessed/42.txt @@ -0,0 +1,290 @@ +""" +Created on Sun Jul 23 14:33:49 2017 +explore regulation tech + +@author: Yuting Kou +""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +import matplotlib.pyplot as plt +""" +1. reload data +""" +data_root = 'D:\\\\data\\\\Yuting\\\\Deep Learning' +pickle_file = os.path.join(data_root, 'notMNIST.pickle') +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 +""" +2. reform data +""" + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +""" +Problem 1: +Introduce and tune L2 regularization for both logistic and neural network models. + +#logistic models# +""" +batch_size = 128 +graph_l2 = tf.Graph() +with graph_l2.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +""" +training model +""" +num_steps = 3001 +with tf.Session(graph=graph_l2) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +def tuning_beta(regul_value, graph, name): + for regul in regul_value: + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) + plt.semilogx(regul_value, accuracy_val) + plt.grid(True) + plt.title('Test accuracy by regularization (%s)' % name) + plt.show() + + +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +tuning_beta(regul_val, graph_l2, 'logistic') +""" +neural network reg +""" +num_hidden_nodes = 1024 +batch_size = 128 +graph_nn_l2 = tf.Graph() +with graph_nn_l2.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = [tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])), tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels]))] + biases = [tf.Variable(tf.zeros([num_hidden_nodes])), tf.Variable(tf.zeros([num_labels]))] + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, weights[0]) + biases[0]) + logits = tf.matmul(hidden_layer, weights[1]) + biases[1] + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + beta_regul * (tf.nn.l2_loss(weights[0]) + tf.nn.l2_loss(weights[1]))) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights[0]) + biases[0]) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights[1]) + biases[1]) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights[0]) + biases[0]) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights[1]) + biases[1]) +""" +run +""" +num_steps = 3001 +with tf.Session(graph=graph_nn_l2) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +tuning_beta(regul_val, graph_nn_l2, '1-layer net') +""" +Problem 2 +Let's demonstrate an extreme case of overfitting. Restrict your training data to just a few batches. What happens? + +logistic +""" +batch_size = 128 +num_hidden_nodes = 1024 +num_steps = 101 +num_batches = 3 +with tf.Session(graph=graph_nn_l2) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_batches * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +""" +Problem 3 +Introduce Dropout on the hidden layer of the neural network. Remember: Dropout should only be introduced during training, not evaluation, otherwise your evaluation results would be stochastic as well. TensorFlow provides nn.dropout() for that, but you have to make sure it's only inserted during training. +What happens to our extreme overfitting case? + +NN +""" +batch_size = 128 +num_hidden_nodes = 1024 +graph_nn = tf.Graph() +with graph_nn.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = [tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])), tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels]))] + biases = [tf.Variable(tf.zeros([num_hidden_nodes])), tf.Variable(tf.zeros([num_labels]))] + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, weights[0]) + biases[0]) + drop1 = tf.nn.dropout(hidden_layer, 0.5) + logits = tf.matmul(drop1, weights[1]) + biases[1] + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights[0]) + biases[0]) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights[1]) + biases[1]) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights[0]) + biases[0]) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights[1]) + biases[1]) +num_steps = 3001 +batch_size = 128 +with tf.Session(graph=graph_nn) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +""" +Problem 4: Find Best Performance +""" +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +batch_size = 128 +graph_nn_l2_2 = tf.Graph() +with graph_nn_l2_2.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights = [tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))), tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))), tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes2)))] + biases = [tf.Variable(tf.zeros([num_hidden_nodes1])), tf.Variable(tf.zeros([num_hidden_nodes2])), tf.Variable(tf.zeros([num_labels]))] + hidden_layer1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights[0]) + biases[0]) + hidden_layer2 = tf.nn.relu(tf.matmul(hidden_layer1, weights[1]) + biases[1]) + logits = tf.matmul(hidden_layer2, weights[2]) + biases[2] + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights[0]) + biases[0]) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights[1]) + biases[1]) + valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights[2]) + biases[2]) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights[0]) + biases[0]) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights[1]) + biases[1]) + test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights[2]) + biases[2]) +""" +run +""" +num_steps = 9001 +import random + + +def disp_sample_dataset(dataset, labels): + items = random.sample(range(len(labels)), 8) + for i, item in enumerate(items): + plt.subplot(2, 4, i + 1) + plt.axis('off') + plt.title(pretty_labels[labels[item]]) + plt.imshow(dataset[item]) + + +pretty_labels = {(0): 'A', (1): 'B', (2): 'C', (3): 'D', (4): 'E', (5): 'F', (6): 'G', (7): 'H', (8): 'I', (9): 'J'} +with tf.Session(graph=graph_nn_l2_2) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + disp_sample_dataset(test_dataset.reshape(-1, image_size, image_size), np.argmax(test_prediction.eval(), 1)) + plt.show() diff --git a/dataset/preprocessed/420.txt b/dataset/preprocessed/420.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/420.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/422.txt b/dataset/preprocessed/422.txt new file mode 100644 index 0000000..088c7f2 --- /dev/null +++ b/dataset/preprocessed/422.txt @@ -0,0 +1,164 @@ +import sys +import time +import tensorflow as tf +import nn_datasource as ds +from checkpoint_manager import CheckpointManager +IMAGE_WIDTH = 72 +IMAGE_HEIGHT = 170 +IMAGE_CHANNEL = 1 +EPOCH_LENGTH = 300 +BATCH_SIZE = 100 +LEARNING_RATE = 0.001 +RANDOM_SEED = 2 +WEIGHT_COUNTER = 0 +BIAS_COUNTER = 0 +CONVOLUTION_COUNTER = 0 +POOLING_COUNTER = 0 +sess = None + + +def new_weights(shape): + global WEIGHT_COUNTER + weight = tf.Variable(tf.random_normal(shape=shape, seed=RANDOM_SEED), name='w_' + str(WEIGHT_COUNTER)) + WEIGHT_COUNTER += 1 + return weight + + +def new_biases(length): + global BIAS_COUNTER + bias = tf.Variable(tf.zeros(shape=[length]), name='b_' + str(BIAS_COUNTER)) + BIAS_COUNTER += 1 + return bias + + +def new_conv_layer(input, num_input_channels, filter_size, num_filters, pooling=2): + global CONVOLUTION_COUNTER + global POOLING_COUNTER + shape = [filter_size, filter_size, num_input_channels, num_filters] + weights = new_weights(shape=shape) + biases = new_biases(length=num_filters) + layer = tf.nn.conv2d(input=input, filter=weights, strides=[1, 1, 1, 1], padding='SAME', name='conv_' + str(CONVOLUTION_COUNTER)) + CONVOLUTION_COUNTER += 1 + layer = tf.add(layer, biases) + layer = tf.nn.relu(layer) + if pooling is not None and pooling > 1: + layer = tf.nn.max_pool(value=layer, ksize=[1, pooling, pooling, 1], strides=[1, pooling, pooling, 1], padding='SAME', name='pool_' + str(POOLING_COUNTER)) + POOLING_COUNTER += 1 + return layer, weights + + +def flatten_layer(layer): + layer_shape = layer.get_shape() + num_features = layer_shape[1:4].num_elements() + layer_flat = tf.reshape(layer, [-1, num_features]) + return layer_flat, num_features + + +def new_fc_layer(input, num_inputs, num_outputs): + weights = new_weights(shape=[num_inputs, num_outputs]) + biases = new_biases(length=num_outputs) + layer = tf.add(tf.matmul(input, weights), biases) + return layer + + +tf.reset_default_graph() +TEST = True +NETWORK_NUMBER = 4 +print(NETWORK_NUMBER) +input_placeholder = tf.placeholder(tf.float32, shape=[None, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNEL], name='input_placeholder') +output_placeholder = tf.placeholder(tf.float32, shape=[None, 2], name='output_placeholder') +layer_conv_1, weights_conv_1 = new_conv_layer(input=input_placeholder, num_input_channels=IMAGE_CHANNEL, filter_size=5, num_filters=64, pooling=2) +layer_conv_2, weights_conv_2 = new_conv_layer(input=layer_conv_1, num_input_channels=64, filter_size=3, num_filters=128, pooling=2) +layer_conv_3, weights_conv_3 = new_conv_layer(input=layer_conv_2, num_input_channels=128, filter_size=3, num_filters=128, pooling=None) +layer_conv_4, weights_conv_4 = new_conv_layer(input=layer_conv_3, num_input_channels=128, filter_size=3, num_filters=128, pooling=None) +layer_conv_5, weights_conv_5 = new_conv_layer(input=layer_conv_4, num_input_channels=128, filter_size=3, num_filters=256, pooling=3) +layer_flat, num_features = flatten_layer(layer_conv_5) +layer_fc_1 = new_fc_layer(input=layer_flat, num_inputs=num_features, num_outputs=4096) +layer_fc_1 = tf.nn.softmax(layer_fc_1) +if TEST is not True: + layer_fc_1 = tf.nn.dropout(layer_fc_1, 0.5) +layer_fc_2 = new_fc_layer(input=layer_fc_1, num_inputs=4096, num_outputs=4096) +layer_fc_2 = tf.nn.softmax(layer_fc_2) +if TEST is not True: + layer_fc_2 = tf.nn.dropout(layer_fc_2, 0.5) +layer_output = new_fc_layer(input=layer_fc_2, num_inputs=4096, num_outputs=2) +cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=output_placeholder, logits=layer_output) +cost = tf.reduce_mean(cross_entropy) +optimizer = tf.train.AdamOptimizer(LEARNING_RATE).minimize(cost) +predictions = tf.argmax(tf.nn.softmax(layer_output), dimension=1) +prediction_equalities = tf.equal(predictions, tf.argmax(output_placeholder, dimension=1)) +accuracy = tf.reduce_mean(tf.cast(prediction_equalities, tf.float32)) + + +def train_nn(number, input_placeholder, output_placeholder, accuracy, cost, optimizer): + global TEST + checkpoint_manager = CheckpointManager(number) + init_g = tf.global_variables_initializer() + init_l = tf.local_variables_initializer() + with tf.Session() as sess: + sess.run(init_g) + sess.run(init_l) + checkpoint_manager.on_training_start(ds.DATASET_FOLDER, EPOCH_LENGTH, BATCH_SIZE, LEARNING_RATE, 'AdamOptimizer', True) + for batch_index, batch_images, batch_labels in ds.training_batch_generator(BATCH_SIZE, grayscale=True): + print('Starting batch {:3}'.format(batch_index + 1)) + for current_epoch in range(EPOCH_LENGTH): + feed = {input_placeholder: batch_images, output_placeholder: batch_labels} + epoch_accuracy, epoch_cost, _ = sess.run([accuracy, cost, optimizer], feed_dict=feed) + print('Batch {:3}, Epoch {:3} -> Accuracy: {:3.1%}, Cost: {}'.format(batch_index + 1, current_epoch + 1, epoch_accuracy, epoch_cost)) + checkpoint_manager.on_epoch_completed() + TEST = True + batch_accuracy_training, batch_cost_training = sess.run([accuracy, cost], feed_dict=feed) + TEST = False + print('Batch {} has been finished. Accuracy: {:3.1%}, Cost: {}'.format(batch_index + 1, batch_accuracy_training, batch_cost_training)) + checkpoint_manager.on_batch_completed(batch_cost_training, batch_accuracy_training) + checkpoint_manager.save_model(sess) + print('\nTraining finished at {}!'.format(time.asctime())) + checkpoint_manager.on_training_completed(None) + + +def test_frame(frame): + prediction = tf.argmax(tf.nn.softmax(layer_output), 1) + print(prediction.eval(feed_dict={input_placeholder: [frame]}, session=sess)) + + +def test_nn(number, input_placeholder, output_placeholder, accuracy, cost): + checkpoint_manager = CheckpointManager(number) + init_g = tf.global_variables_initializer() + init_l = tf.local_variables_initializer() + with tf.Session() as sess: + sess.run(init_g) + sess.run(init_l) + checkpoint_manager.restore_model(sess) + total_accuracy = 0 + total_cost = 0 + batches = None + for batch_index, test_images, test_labels in ds.test_batch_generator(100, grayscale=True): + feed = {input_placeholder: test_images, output_placeholder: test_labels} + test_accuracy, test_cost = sess.run([accuracy, cost], feed_dict=feed) + print('Batch {:3}, Accuracy: {:3.1%}, Cost: {}'.format(batch_index, test_accuracy, test_cost)) + total_accuracy += test_accuracy + total_cost += test_cost + batches = batch_index + overall_accuracy = total_accuracy / (batches + 1) + overall_cost = total_cost / (batches + 1) + print('Total test accuracy: {:5.1%}'.format(overall_accuracy)) + return overall_accuracy, overall_cost + + +def main(): + pass + + +def init(): + checkpoint_manager = CheckpointManager(NETWORK_NUMBER) + init_g = tf.global_variables_initializer() + init_l = tf.local_variables_initializer() + with tf.Session() as ses: + ses.run(init_g) + ses.run(init_l) + checkpoint_manager.restore_model(ses) + sess = ses + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/423.txt b/dataset/preprocessed/423.txt new file mode 100644 index 0000000..f6692dd --- /dev/null +++ b/dataset/preprocessed/423.txt @@ -0,0 +1,64 @@ +import tensorflow as tf +from ntmnist import dataset +import numpy as np +pickle_file = 'notMNIST.pickle' +data = dataset(pickle_file) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +train_subset = 200000 +image_size = 28 +num_labels = 10 +hn1_num = 1024 +hn2_num = 512 +batch_size = 128 +reg_beta = 0.005 +data.set_length(train_subset) +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(None, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(None, num_labels)) + keep_prob = tf.placeholder('float') + with tf.name_scope('hidden1') as scope_1: + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hn1_num]), name='weights') + biases_1 = tf.Variable(tf.zeros([hn1_num]), name='biases') + print(tf.shape(weights_1)) + hidden1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_1) + biases_1) + keep_prob = tf.placeholder('float') + hidden_layer_drop1 = tf.nn.dropout(hidden1, keep_prob) + with tf.name_scope('hidden2') as scope_2: + weights_2 = tf.Variable(tf.truncated_normal([hn1_num, hn2_num]), name='weights') + biases_2 = tf.Variable(tf.zeros([hn2_num]), name='biases') + print(tf.shape(weights_2)) + hidden2 = tf.nn.relu(tf.matmul(hidden_layer_drop1, weights_2) + biases_2) + hidden_layer_drop2 = tf.nn.dropout(hidden2, keep_prob) + with tf.name_scope('out') as scope_3: + weights = tf.Variable(tf.truncated_normal([hn2_num, num_labels]), name='weights') + biases = tf.Variable(tf.zeros([num_labels]), name='biases') + print(tf.shape(weights)) + logits = tf.matmul(hidden_layer_drop2, weights) + biases + loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss1 + reg_beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_1)) + optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss) + train_prediction = tf.nn.softmax(logits) +num_steps = 30001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + batch_data, batch_labels = data.next_batch(batch_size) + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, loss_o, l, predictions = session.run([optimizer, loss1, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print(batch_labels.shape[0]) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(session.run(train_prediction, feed_dict={tf_train_dataset: data.valid_dataset, keep_prob: 1.0}), data.valid_labels)) + print(loss_o) + print(tf.nn.l2_loss(weights_1).eval(), tf.nn.l2_loss(weights_2).eval()) + feed_dict = {tf_train_dataset: data.test_dataset, keep_prob: 1.0} + predictions = session.run(train_prediction, feed_dict=feed_dict) + print('Test accuracy: %.1f%%' % accuracy(predictions, data.test_labels)) diff --git a/dataset/preprocessed/424.txt b/dataset/preprocessed/424.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/424.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/425.txt b/dataset/preprocessed/425.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/425.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/426.txt b/dataset/preprocessed/426.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/426.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/427.txt b/dataset/preprocessed/427.txt new file mode 100644 index 0000000..2adba96 --- /dev/null +++ b/dataset/preprocessed/427.txt @@ -0,0 +1,116 @@ +import numpy as np +import matplotlib.pyplot as plt +import tensorflow as tf +import os +from tensorflow.python.ops.parallel_for.gradients import jacobian +np.random.seed(10) +file_path = os.path.dirname(os.getcwd()) + '/Data/SINDY_Data.npy' +states = np.load(file_path)[:-1, :] +data_tsteps = np.shape(states)[0] +state_len = np.shape(states)[1] +cft_max = np.amax(states, axis=0) +cft_min = np.amin(states, axis=0) +states = (-1.0 + 2.0 * (states[:, :] - cft_min[(None), :]) / (cft_max[(None), :] - cft_min[(None), :])) * (1.0 + np.random.randn() * 0.02) +seq_num = 20 +num_batch = 1000 +total_size = np.shape(states)[0] - int(seq_num) +input_seq = np.zeros(shape=(num_batch, seq_num, state_len)) +output_seq = np.zeros(shape=(num_batch, seq_num - 1, state_len)) +for bnum in range(num_batch): + t = np.random.randint(low=0, high=total_size) + input_seq[(bnum), :, :] = states[(None), t:t + seq_num, :] + output_seq[(bnum), :, :] = states[(None), t + 2:t + seq_num + 1, :] +lstm_session = tf.Session() +lstm_neurons = 32 +weights = {'ig': tf.Variable(tf.truncated_normal(shape=[state_len, lstm_neurons], seed=1, mean=0.0, stddev=0.1)), 'fg': tf.Variable(tf.truncated_normal(shape=[state_len, lstm_neurons], seed=2, mean=0.0, stddev=0.1)), 'og': tf.Variable(tf.truncated_normal(shape=[state_len, lstm_neurons], seed=3, mean=0.0, stddev=0.1)), 'iz': tf.Variable(tf.truncated_normal(shape=[state_len, lstm_neurons], seed= + 4, mean=0.0, stddev=0.1)), 'op': tf.Variable(tf.truncated_normal(shape=[lstm_neurons, state_len], seed=5, mean=0.0, stddev=0.1))} +biases = {'ig': tf.Variable(tf.truncated_normal(shape=[lstm_neurons], seed=1, mean=0.0, stddev=0.1)), 'fg': tf.Variable(tf.truncated_normal(shape=[lstm_neurons], seed=2, mean=0.0, stddev=0.1)), 'og': tf.Variable(tf.truncated_normal(shape=[lstm_neurons], seed=3, mean=0.0, stddev=0.1)), 'iz': tf.Variable(tf.truncated_normal(shape=[lstm_neurons], seed=4, mean=0.0, stddev=0.1)), 'op': tf.Variable( + tf.truncated_normal(shape=[state_len], seed=5, mean=0.0, stddev=0.1))} +placeholder_batch_size = num_batch // seq_num +ph_input = tf.placeholder(tf.float32, [placeholder_batch_size, seq_num, state_len]) +ph_output = tf.placeholder(tf.float32, [placeholder_batch_size, seq_num - 1, state_len]) +ig = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['ig']), biases['ig'])) +fg = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['fg']), biases['fg'])) +og = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['og']), biases['og'])) +iz = tf.nn.tanh(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['iz']), biases['iz'])) +internal_state_temp = tf.multiply(ig, iz) +internal_state_curr = tf.slice(internal_state_temp, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state_prev = tf.slice(internal_state_temp, [0, 0, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +fg_c = tf.slice(fg, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +og_c = tf.slice(og, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state = tf.add(tf.multiply(fg_c, internal_state_prev), internal_state_curr) +ht = tf.multiply(og_c, tf.nn.tanh(internal_state)) +output_seq_pred = tf.add(tf.einsum('ijk,kl->ijl', ht, weights['op']), biases['op']) +loss_temp = tf.reduce_mean(tf.squared_difference(output_seq_pred, ph_output)) +J = jacobian(loss_temp, ph_input) +J = tf.reshape(J, shape=[placeholder_batch_size, seq_num * state_len]) +loss = loss_temp +optimizer = tf.train.AdamOptimizer(learning_rate=0.005) +grads_and_vars = optimizer.compute_gradients(loss) +backprop = optimizer.apply_gradients(grads_and_vars) +init_op = tf.global_variables_initializer() +lstm_session.run(init_op) +saver = tf.train.Saver() +current_pwd = os.getcwd() + '/' +num_epochs = int(10 * num_batch) +best_loss = np.Inf +with lstm_session.as_default(): + bnum = 0 + for i in range(num_epochs): + start_id = bnum * placeholder_batch_size + end_id = (bnum + 1) * placeholder_batch_size + input_batch = input_seq[start_id:end_id, :, :] + output_batch = output_seq[start_id:end_id, :, :] + bnum = bnum + 1 + val_num = np.random.randint(low=0, high=seq_num - 1) + val_start_id = val_num * placeholder_batch_size + val_end_id = (val_num + 1) * placeholder_batch_size + val_input_batch = input_seq[val_start_id:val_end_id, :, :] + val_output_batch = output_seq[val_start_id:val_end_id, :, :] + if bnum == seq_num: + bnum = 0 + backprop.run(feed_dict={ph_input: input_batch, ph_output: output_batch}) + mse_batch = np.mean(loss.eval(feed_dict={ph_input: input_batch, ph_output: output_batch})) + mse_batch_val = np.mean(loss.eval(feed_dict={ph_input: val_input_batch, ph_output: val_output_batch})) + print('MSE Batch: ', mse_batch, 'MSE Batch Validation: ', mse_batch_val) + if mse_batch_val < best_loss: + save_path = saver.save(lstm_session, current_pwd) + print('Model saved in path: %s' % save_path) + best_loss = mse_batch_val +eval_session = tf.Session() +placeholder_batch_size = 1 +ph_input = tf.placeholder(tf.float32, [placeholder_batch_size, seq_num, state_len]) +ig = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['ig']), biases['ig'])) +fg = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['fg']), biases['fg'])) +og = tf.nn.sigmoid(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['og']), biases['og'])) +iz = tf.nn.tanh(tf.add(tf.einsum('ijk,kl->ijl', ph_input, weights['iz']), biases['iz'])) +internal_state_temp = tf.multiply(ig, iz) +internal_state_curr = tf.slice(internal_state_temp, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state_prev = tf.slice(internal_state_temp, [0, 0, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +fg_c = tf.slice(fg, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +og_c = tf.slice(og, [0, 1, 0], [placeholder_batch_size, seq_num - 1, lstm_neurons]) +internal_state = tf.add(tf.multiply(fg_c, internal_state_prev), internal_state_curr) +ht = tf.multiply(og_c, tf.nn.tanh(internal_state)) +output_seq_pred = tf.add(tf.einsum('ijk,kl->ijl', ht, weights['op']), biases['op']) +init_op = tf.global_variables_initializer() +eval_session.run(init_op) +saver = tf.train.Saver() +save_path = os.getcwd() + '/' +saver.restore(eval_session, save_path) +print('Model sucessfully restored') +state_tracker = np.zeros(shape=(1, data_tsteps, state_len), dtype='double') +state_tracker[(0), 0:seq_num, :] = states[0:seq_num, :] +with eval_session.as_default(): + for t in range(seq_num, data_tsteps): + lstm_input = state_tracker[:, t - seq_num:t, :] + output_state = np.asarray(output_seq_pred.eval(feed_dict={ph_input: lstm_input})) + state_tracker[(0), (t), :] = output_state[:, (-1), :] +fig, ax = plt.subplots(nrows=2, ncols=2) +mnum = 0 +for j in range(2): + for i in range(2): + ax[i, j].plot(states[:, (mnum)], label='True', color='blue') + ax[i, j].plot(state_tracker[(0), :, (mnum)], label='Predicted', color='red') + mnum = mnum + 1 +plt.legend() +plt.show() diff --git a/dataset/preprocessed/428.txt b/dataset/preprocessed/428.txt new file mode 100644 index 0000000..86356b6 --- /dev/null +++ b/dataset/preprocessed/428.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/43.txt b/dataset/preprocessed/43.txt new file mode 100644 index 0000000..bfb803a --- /dev/null +++ b/dataset/preprocessed/43.txt @@ -0,0 +1,135 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +from time import time +from read_data import * +train_subset = 100000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +""" +with tf.Session(graph=graph) as session: + # This is a one-time operation which ensures the parameters get initialized as + # we described in the graph: random weights for the matrix, zeros for the + # biases. + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + # Run the computations. We tell .run() that we want to run the optimizer, + # and get the loss value and the training predictions returned as numpy + # arrays. + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if (step % 100 == 0): + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy( + predictions, train_labels[:train_subset, :])) + # Calling .eval() on valid_prediction is basically like calling run(), but + # just to get that one numpy array. Note that it recomputes all its graph + # dependencies. + print('Validation accuracy: %.1f%%' % accuracy( + valid_prediction.eval(), valid_labels)) + print(' +') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +""" +print('') +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +print('') + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + output1 = tf.matmul(data, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/430.txt b/dataset/preprocessed/430.txt new file mode 100644 index 0000000..ab60388 --- /dev/null +++ b/dataset/preprocessed/430.txt @@ -0,0 +1,72 @@ +from __future__ import print_function, division +import tensorflow as tf +import numpy as np +from tf_utils import * + + +class Network(object): + + def __init__(self, config): + self.state_dim = config.state_dim + self.state_shape = config.state_shape + self.state_channels = self.state_shape[-1] + self.action_num = config.action_num + self.weight_decay = config.weight_decay + self.lr = config.lr + self.state = tf.placeholder(tf.float32, shape=[None, self.state_dim]) + self.q_target = tf.placeholder(tf.float32, shape=[None]) + self.actions = tf.placeholder(tf.float32, shape=[None, self.action_num]) + self.q_difference = tf.placeholder(tf.float32, shape=[None, self.action_num]) + self.state2 = tf.placeholder(tf.float32, shape=[None, self.state_dim]) + self.q_predict, self.action_predict = self.build_network() + self.q_predict_with_action = tf.reduce_sum(self.q_predict * self.actions, axis=1) + losses = tf.square(self.q_predict_with_action - self.q_target) + self.l2_loss = tf.reduce_mean(losses) + self.w_norm = tf.add_n(tf.get_collection('weight_norms')) + self.a_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.action_predict, self.actions)) + self.objective = self.l2_loss + self.a_loss + self.weight_decay * self.w_norm + self.train_step = tf.train.AdamOptimizer(self.lr).minimize(self.objective) + + def build_network(self): + h_num = 400 + w = weight_variable([self.state_dim, h_num]) + b = bias_variable([h_num]) + tf.add_to_collection('weight_norms', tf.nn.l2_loss(w)) + h_s = tf.nn.relu(tf.nn.bias_add(tf.matmul(self.state, w), b)) + h_s2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(self.state2, w), b)) + h2_num = 100 + w = weight_variable([h_num, h2_num]) + b = bias_variable([h2_num]) + tf.add_to_collection('weight_norms', tf.nn.l2_loss(w)) + h_s = tf.nn.relu(tf.nn.bias_add(tf.matmul(h_s, w), b)) + h_s2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(h_s2, w), b)) + w = weight_variable([h2_num, self.action_num]) + b = bias_variable([self.action_num]) + tf.add_to_collection('weight_norms', tf.nn.l2_loss(w)) + q = tf.nn.bias_add(tf.matmul(h_s, w), b) + h_a = tf.concat(1, [h_s, h_s2]) + w = weight_variable([h2_num * 2, self.action_num]) + b = bias_variable([self.action_num]) + a = tf.nn.bias_add(tf.matmul(h_a, w), b) + return q, a + + def get_q_output(self, sess, s): + return self.q_predict.eval(session=sess, feed_dict={self.state: s}) + + def update_with_targets(self, sess, s, a, targets): + self.train_step.run(session=sess, feed_dict={self.state: s, self.q_target: targets, self.actions: a}) + + def update_to_predict_action(self, sess, s, s2, a): + self.train_step_a.run(session=sess, feed_dict={self.state: s, self.state2: s2, self.actions: a}) + + def update_step(self, sess, s, s2, a, targets): + self.train_step.run(session=sess, feed_dict={self.state: s, self.state2: s2, self.actions: a, self.q_target: targets}) + + def get_loss(self, sess, s, a, targets): + return self.l2_loss.eval(session=sess, feed_dict={self.state: s, self.q_target: targets, self.actions: a}) + + def get_action_loss(self, sess, s, s2, a): + return self.a_loss.eval(session=sess, feed_dict={self.state: s, self.state2: s2, self.actions: a}) + + def get_weight_norm(self, sess): + return self.w_norm.eval(session=sess) diff --git a/dataset/preprocessed/431.txt b/dataset/preprocessed/431.txt new file mode 100644 index 0000000..f34ce5d --- /dev/null +++ b/dataset/preprocessed/431.txt @@ -0,0 +1,276 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import time +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +train_subset = 10000 +batch_size = 128 +beta = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + print('train_prediction shape: ', train_prediction) + print('valid_prediction shape: ', valid_prediction) + print('test_prediction shape: ', test_prediction) +batch_size = 128 +num_steps = 3001 +with tf.Session(graph=graph) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with no RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 128 +num_nodes = 1024 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(relu1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 12 +num_nodes = 1024 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(relu1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + l2_regularizer = tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_relu1) + loss += 0.0005 * l2_regularizer + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 12 +num_nodes = 1024 +beta = 0.001 +prob_limit = 0.75 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + keep_prob = tf.placeholder('float') + hidden_layer_drop = tf.nn.dropout(relu1, keep_prob) + logits = tf.matmul(hidden_layer_drop, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: prob_limit} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 128 +h1_num_nodes = 1024 +h2_num_nodes = 768 +h3_num_nodes = 512 +h1_stddev = np.sqrt(2.0 / 784) +h2_stddev = np.sqrt(2.0 / h1_num_nodes) +h3_stddev = np.sqrt(2.0 / h2_num_nodes) +logits_stddev = np.sqrt(2.0 / h3_num_nodes) +beta = 0.001 +print('graph best: l2 regularization, no dropout') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + h1_weights = tf.Variable(tf.truncated_normal([image_size * image_size, h1_num_nodes], stddev=h1_stddev)) + h1_biases = tf.Variable(tf.zeros([h1_num_nodes])) + h1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, h1_weights) + h1_biases) + print('TF hidden layer 1->', h1_layer) + h2_weights = tf.Variable(tf.truncated_normal([h1_num_nodes, h2_num_nodes], stddev=h2_stddev)) + h2_biases = tf.Variable(tf.zeros([h2_num_nodes])) + h2_layer = tf.nn.relu(tf.matmul(h1_layer, h2_weights) + h2_biases) + print('TF hidden layer 2->', h2_layer) + h3_weights = tf.Variable(tf.truncated_normal([h2_num_nodes, h3_num_nodes], stddev=h3_stddev)) + h3_biases = tf.Variable(tf.zeros([h3_num_nodes])) + h3_layer = tf.nn.relu(tf.matmul(h2_layer, h3_weights) + h3_biases) + print('TF hidden layer 3->', h3_layer) + weights = tf.Variable(tf.truncated_normal([h3_num_nodes, num_labels], stddev=logits_stddev)) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(h3_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(h1_weights) + beta * tf.nn.l2_loss(h2_weights) + beta * tf.nn.l2_loss(h3_weights) + beta * tf.nn.l2_loss(weights)) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 100000, 0.95, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + h1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, h1_weights) + h1_biases) + h2_valid = tf.nn.relu(tf.matmul(h1_valid, h2_weights) + h2_biases) + h3_valid = tf.nn.relu(tf.matmul(h2_valid, h3_weights) + h3_biases) + valid_logits = tf.matmul(h3_valid, weights) + biases + valid_prediction = tf.nn.softmax(valid_logits) + print('valid_prediction(raw)->', valid_prediction) + h1_test = tf.nn.relu(tf.matmul(tf_test_dataset, h1_weights) + h1_biases) + h2_test = tf.nn.relu(tf.matmul(h1_test, h2_weights) + h2_biases) + h3_test = tf.nn.relu(tf.matmul(h2_test, h3_weights) + h3_biases) + test_logits = tf.matmul(h3_test, weights) + biases + test_prediction = tf.nn.softmax(test_logits) +print('run with best: l2 regularization, no dropout') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('valid_prediction(eval)->', valid_prediction.eval()) + print('Initialized with best: l2 regularization, no dropout') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) diff --git a/dataset/preprocessed/432.txt b/dataset/preprocessed/432.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/432.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/434.txt b/dataset/preprocessed/434.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/434.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/435.txt b/dataset/preprocessed/435.txt new file mode 100644 index 0000000..052a808 --- /dev/null +++ b/dataset/preprocessed/435.txt @@ -0,0 +1,167 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +train_subset = 10000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 1 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if step % 100 == 0: + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy(predictions, train_labels[:train_subset, :])) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 1 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +n_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_01 = tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_nodes])) + weights_12 = tf.Variable(tf.truncated_normal([n_hidden_nodes, num_labels])) + biases_01 = tf.Variable(tf.zeros([n_hidden_nodes])) + biases_12 = tf.Variable(tf.zeros([num_labels])) + z_01 = tf.matmul(tf_train_dataset, weights_01) + biases_01 + h1 = tf.nn.relu(z_01) + z_12 = tf.matmul(h1, weights_12) + biases_12 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=z_12, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(z_12) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_01) + biases_01), weights_12) + biases_12) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_01) + biases_01), weights_12) + biases_12) +num_steps = 1 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step', step, ':', l) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + print('FINAL') +batch_size = 18000 +n_hidden_nodes = 1024 +num_steps = 32 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_01 = tf.Variable(tf.truncated_normal([image_size * image_size, n_hidden_nodes])) + weights_12 = tf.Variable(tf.truncated_normal([n_hidden_nodes, num_labels])) + biases_01 = tf.Variable(tf.zeros([n_hidden_nodes])) + biases_12 = tf.Variable(tf.zeros([num_labels])) + z_01 = tf.matmul(tf_train_dataset, weights_01) + biases_01 + h1 = tf.nn.relu(z_01) + z_12 = tf.matmul(h1, weights_12) + biases_12 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=z_12, labels=tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(z_12) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_01) + biases_01), weights_12) + biases_12) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_01) + biases_01), weights_12) + biases_12) +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = 0 + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step', step, ':', l) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/436.txt b/dataset/preprocessed/436.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/436.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/437.txt b/dataset/preprocessed/437.txt new file mode 100644 index 0000000..22cf81c --- /dev/null +++ b/dataset/preprocessed/437.txt @@ -0,0 +1,502 @@ +import numpy as np +import tensorflow as tf +from tensorflow.contrib import layers as L +from sklearn.ensemble import RandomForestRegressor +import matplotlib.pyplot + + +def euclidean_loss(a, b): + return tf.reduce_mean(tf.reduce_sum(tf.pow(a - b, 2))) / tf.cast(tf.shape(a)[0], 'float') + + +def MSE(a, b): + return np.mean((a - b) ** 2) + + +def scaled_sigmoid(x): + return 2 * tf.sigmoid(x) - 1 + + +class CATE_estimator: + + def evaluate(self, X, TAU): + t_hat = self.predict(X) + t_dist = MSE(t_hat, TAU) + return t_dist + + +class LinearTlearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, catagorial=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + self.nn_input_X1 = tf.placeholder('float', [None, XS.shape[1]]) + self.nn_input_X0 = tf.placeholder('float', [None, XS.shape[1]]) + if catagorial: + self.nn_input_Y1 = tf.placeholder('int64', [None]) + self.nn_input_Y0 = tf.placeholder('int64', [None]) + else: + self.nn_input_Y1 = tf.placeholder('float', [None, YS.shape[1]]) + self.nn_input_Y0 = tf.placeholder('float', [None, YS.shape[1]]) + Y_hat_0 = L.fully_connected(self.nn_input_X0, YS.shape[1], activation_fn=tf.identity, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + Y_hat_1 = L.fully_connected(self.nn_input_X1, YS.shape[1], activation_fn=tf.identity, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + self.loss0 = euclidean_loss(Y_hat_0, self.nn_input_Y0) + self.loss1 = euclidean_loss(Y_hat_1, self.nn_input_Y1) + self.tau_estimate = Y_hat_1 - Y_hat_0 + self.trainer_0 = tf.train.AdamOptimizer().minimize(self.loss0) + self.trainer_1 = tf.train.AdamOptimizer().minimize(self.loss1) + self.name = 't_lm' + self.catagorial = catagorial + + def train(self, X0, Y0, X1, Y1): + if len(Y1.shape) == 1: + Y1 = np.expand_dims(Y1, axis=1) + if len(Y0.shape) == 1: + Y0 = np.expand_dims(Y0, axis=1) + fd = {self.nn_input_X0: X0, self.nn_input_X1: X1, self.nn_input_Y0: Y0, self.nn_input_Y1: Y1} + sess = tf.get_default_session() + _, _, l0, l1 = sess.run([self.trainer_0, self.trainer_1, self.loss0, self.loss1], feed_dict=fd) + return l0 + l1 + + def predict(self, X): + fd = {self.nn_input_X0: X, self.nn_input_X1: X, self.nn_input_Y0: np.expand_dims(np.zeros(len(X)), axis=1), self.nn_input_Y1: np.expand_dims(np.zeros(len(X)), axis=1)} + sess = tf.get_default_session() + t_hat = sess.run([self.tau_estimate], feed_dict=fd)[0] + return np.squeeze(t_hat.flatten()) + + +class Tlearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, activation=tf.nn.tanh, link_function=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + self.nn_input_X1 = tf.placeholder('float', [None, XS.shape[1]]) + self.nn_input_X0 = tf.placeholder('float', [None, XS.shape[1]]) + self.nn_input_Y1 = tf.placeholder('float', [None, YS.shape[1]]) + self.nn_input_Y0 = tf.placeholder('float', [None, YS.shape[1]]) + n_hid = 64 + h0_0 = L.fully_connected(self.nn_input_X0, n_hid, activation_fn=activation, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + h1_0 = L.fully_connected(h0_0, n_hid, activation_fn=activation, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + Y_hat_0 = L.fully_connected(h1_0, YS.shape[1], activation_fn=activation, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + h0_1 = L.fully_connected(self.nn_input_X1, n_hid, activation_fn=activation, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + h1_1 = L.fully_connected(h0_1, n_hid, activation_fn=activation, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + Y_hat_1 = L.fully_connected(h1_1, YS.shape[1], activation_fn=tf.identity, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False)) + if link_function: + Y_hat_1 = tf.sigmoid(Y_hat_1) + Y_hat_0 = tf.sigmoid(Y_hat_0) + self.loss0 = euclidean_loss(Y_hat_0, self.nn_input_Y0) + self.loss1 = euclidean_loss(Y_hat_1, self.nn_input_Y1) + self.tau_estimate = Y_hat_1 - Y_hat_0 + self.trainer_0 = tf.train.AdamOptimizer().minimize(self.loss0) + self.trainer_1 = tf.train.AdamOptimizer().minimize(self.loss1) + if activation == tf.nn.relu: + self.name = 't_relu' + elif activation == tf.nn.tanh: + self.name = 't_tanh' + elif activation == tf.nn.sigmoid: + self.name = 't_sigmoid' + + def train(self, X0, Y0, X1, Y1): + if len(Y1.shape) == 1: + Y1 = np.expand_dims(Y1, axis=1) + if len(Y0.shape) == 1: + Y0 = np.expand_dims(Y0, axis=1) + fd = {self.nn_input_X0: X0, self.nn_input_X1: X1, self.nn_input_Y0: Y0, self.nn_input_Y1: Y1} + sess = tf.get_default_session() + _, _, l0, l1 = sess.run([self.trainer_0, self.trainer_1, self.loss0, self.loss1], feed_dict=fd) + return (l0 + l1) / 2 + + def predict(self, X, clip_range=(-1, 1)): + fd = {self.nn_input_X0: X, self.nn_input_X1: X, self.nn_input_Y0: np.expand_dims(np.zeros(len(X)), axis=1), self.nn_input_Y1: np.expand_dims(np.zeros(len(X)), axis=1)} + sess = tf.get_default_session() + t_hat = sess.run([self.tau_estimate], feed_dict=fd)[0] + return t_hat.flatten() + + +class Slearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, activation=tf.nn.tanh, link_function=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + self.nn_input_X = tf.placeholder('float', [None, XS.shape[1]]) + self.nn_input_W = tf.placeholder('float', [None]) + self.nn_input_XW = tf.concat([self.nn_input_X, tf.expand_dims(self.nn_input_W, axis=-1)], axis=1) + self.nn_input_Y = tf.placeholder('float', [None, YS.shape[1]]) + n_hid = 64 + h0_0 = L.fully_connected(self.nn_input_XW, n_hid, activation_fn=activation, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=True)) + h1_0 = L.fully_connected(h0_0, n_hid, activation_fn=activation, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=True)) + self.Y_hat = L.fully_connected(h1_0, YS.shape[1], activation_fn=tf.identity) + if link_function: + self.Y_hat = tf.sigmoid(self.Y_hat) + self.loss = euclidean_loss(self.Y_hat, self.nn_input_Y) + self.trainer = tf.train.AdamOptimizer().minimize(self.loss) + if activation == tf.nn.relu: + self.name = 's_relu' + elif activation == tf.nn.tanh: + self.name = 's_tanh' + + def train(self, X_mb, Y_mb, W_mb): + if len(Y_mb.shape) == 1: + Y_mb = np.expand_dims(Y_mb, axis=1) + fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb} + sess = tf.get_default_session() + _, l = sess.run([self.trainer, self.loss], feed_dict=fd) + return l + + def predict(self, X, clip_range=(-1, 1)): + sess = tf.get_default_session() + fd = {self.nn_input_X: X, self.nn_input_Y: np.expand_dims(np.zeros(len(X)), axis=1), self.nn_input_W: np.expand_dims(np.zeros(len(X)), axis=1)} + W0 = np.zeros(X.shape[0]) + W1 = np.ones(X.shape[0]) + fd[self.nn_input_W] = W0 + mu_hat_x_0 = sess.run([self.Y_hat], feed_dict=fd)[0] + fd[self.nn_input_W] = W1 + mu_hat_x_1 = sess.run([self.Y_hat], feed_dict=fd)[0] + tau_hat = mu_hat_x_1 - mu_hat_x_0 + return tau_hat.flatten() + + +class YLearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, activation=tf.nn.tanh, link_function=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + self.nn_input_X = tf.placeholder('float', [None, XS.shape[1]]) + self.nn_input_sigma_0_ground_truth = tf.placeholder('float', [None, YS.shape[1]]) + self.nn_input_sigma_1_grond_truth = tf.placeholder('float', [None, YS.shape[1]]) + self.nn_input_sigma_tau_ground_truth = tf.placeholder('float', [None, YS.shape[1]]) + n_hid = 64 + if activation == tf.nn.relu: + self.name = 'y_relu' + elif activation == tf.nn.tanh: + self.name = 'y_tanh' + h0_sigma_0 = L.fully_connected(self.nn_input_X, n_hid, activation_fn=activation) + h1_sigma_0 = L.fully_connected(h0_sigma_0, n_hid, activation_fn=activation) + h0_sigma_1 = L.fully_connected(self.nn_input_X, n_hid, activation_fn=activation) + h1_sigma_1 = L.fully_connected(h0_sigma_1, n_hid, activation_fn=activation) + h0_sigma_tau = L.fully_connected(self.nn_input_X, n_hid, activation_fn=activation) + h1_sigma_tau = L.fully_connected(h0_sigma_tau, n_hid, activation_fn=activation) + self.sigma_0_hat = L.fully_connected(h1_sigma_0, YS.shape[1], activation_fn=tf.identity) + self.sigma_1_hat = L.fully_connected(h1_sigma_1, YS.shape[1], activation_fn=tf.identity) + self.sigma_tau_hat = L.fully_connected(h1_sigma_tau, YS.shape[1], activation_fn=tf.identity) + if link_function: + self.sigma_0_hat = tf.sigmoid(self.sigma_0_hat) + self.sigma_1_hat = tf.sigmoid(self.sigma_1_hat) + self.sigma_tau_hat = tf.nn.tanh(self.sigma_tau_hat) + loss_one = euclidean_loss(self.sigma_0_hat, self.nn_input_sigma_0_ground_truth) + loss_two = euclidean_loss(self.sigma_1_hat, self.nn_input_sigma_1_grond_truth) + loss_three = euclidean_loss(self.sigma_tau_hat, self.nn_input_sigma_tau_ground_truth) + self.loss = loss_one + loss_two + loss_three + self.trainer = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, X_mb, Y_mb, W_mb): + if len(Y_mb.shape) == 1: + Y_mb = np.expand_dims(Y_mb, axis=1) + sigma_0, sigma_1, sigma_tau = self.generate_labels(X_mb, Y_mb, W_mb) + fd = {self.nn_input_X: X_mb, self.nn_input_sigma_0_ground_truth: sigma_0, self.nn_input_sigma_1_grond_truth: sigma_1, self.nn_input_sigma_tau_ground_truth: sigma_tau} + sess = tf.get_default_session() + _, l = sess.run([self.trainer, self.loss], feed_dict=fd) + return l + + def predict(self, X, clip_range=(-1, 1)): + tau_hat = self.get_sigma(self.sigma_tau_hat, X) + return tau_hat + + def get_sigma(self, sigma, X): + sess = tf.get_default_session() + fd = {self.nn_input_X: X} + out = sess.run([sigma], feed_dict=fd)[0] + out = out.flatten() + return out + + def generate_labels(self, X, Y, W): + Y = Y.flatten() + W = W.flatten() + sigma_0 = np.zeros_like(Y) + sigma_1 = np.zeros_like(Y) + sigma_tau = np.zeros_like(Y) + W_0 = np.where(W == 0) + W_1 = np.where(W == 1) + sigma_0[W_0] = Y[W_0] + sigma_0[W_1] = Y[W_1] - self.get_sigma(self.sigma_tau_hat, X[W_1]) + sigma_1[W_1] = Y[W_1] + sigma_1[W_0] = Y[W_0] + self.get_sigma(self.sigma_tau_hat, X[W_0]) + sigma_tau[W_1] = Y[W_1] - self.get_sigma(self.sigma_0_hat, X[W_1]) + sigma_tau[W_0] = self.get_sigma(self.sigma_1_hat, X[W_0]) - Y[W_0] + sigma_0 = np.expand_dims(sigma_0, -1) + sigma_1 = np.expand_dims(sigma_1, -1) + sigma_tau = np.expand_dims(sigma_tau, -1) + return sigma_0, sigma_1, sigma_tau + + +class Xlearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, activation=tf.nn.tanh, link_function=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + self.nn_input_X1 = tf.placeholder('float', [None, XS.shape[1]]) + self.nn_input_Y1 = tf.placeholder('float', [None, YS.shape[1]]) + self.nn_input_X0 = tf.placeholder('float', [None, XS.shape[1]]) + self.nn_input_Y0 = tf.placeholder('float', [None, YS.shape[1]]) + self.nn_input_D_hat_0 = tf.placeholder('float', [None, YS.shape[1]]) + self.nn_input_D_hat_1 = tf.placeholder('float', [None, YS.shape[1]]) + n_hid = 64 + if activation == tf.nn.relu: + self.name = 'x_relu' + elif activation == tf.nn.tanh: + self.name = 'x_tanh' + + def get_weights_and_biases(n_layers, in_shape, out_shape): + w_dict = dict() + b_dict = dict() + n_units = n_hid + for i in range(n_layers): + if i == n_layers - 1: + n_units = out_shape + w_dict['h_' + str(i)] = tf.Variable(tf.random_normal([in_shape, n_units])) + b_dict['b_' + str(i)] = tf.Variable(tf.random_normal([n_units])) + in_shape = n_hid + return w_dict, b_dict + + def MLP(nn_input, weights, biases): + n_layers = len(weights) + l = nn_input + for i in range(n_layers): + if i == n_layers - 1: + Y_hat = tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)] + else: + l = activation(tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)]) + return Y_hat + weights_0, biases_0 = get_weights_and_biases(3, XS.shape[1], YS.shape[1]) + weights_1, biases_1 = get_weights_and_biases(3, XS.shape[1], YS.shape[1]) + mu_hat_0 = MLP(self.nn_input_X0, weights_0, biases_0) + mu_hat_1 = MLP(self.nn_input_X1, weights_1, biases_1) + if link_function: + mu_hat_0 = tf.sigmoid(mu_hat_0) + mu_hat_1 = tf.sigmoid(mu_hat_1) + self.D_hat_1 = self.nn_input_Y1 - MLP(self.nn_input_X1, weights_0, biases_0) + self.D_hat_0 = MLP(self.nn_input_X0, weights_1, biases_1) - self.nn_input_Y0 + tau_hat_0 = L.fully_connected(self.nn_input_X0, n_hid, activation_fn=activation) + tau_hat_0 = L.fully_connected(tau_hat_0, n_hid, activation_fn=activation) + tau_hat_0 = L.fully_connected(tau_hat_0, YS.shape[1], activation_fn=tf.identity) + tau_hat_1 = L.fully_connected(self.nn_input_X1, n_hid, activation_fn=activation) + tau_hat_1 = L.fully_connected(tau_hat_1, n_hid, activation_fn=activation) + tau_hat_1 = L.fully_connected(tau_hat_1, YS.shape[1], activation_fn=tf.identity) + if link_function: + tau_hat_0 = tf.nn.tanh(tau_hat_0) + tau_hat_1 = tf.nn.tanh(tau_hat_1) + self.tau_hat_0 = tau_hat_0 + self.tau_hat_1 = tau_hat_1 + self.loss_tau_0 = euclidean_loss(tau_hat_0, self.nn_input_D_hat_0) + self.loss_tau_1 = euclidean_loss(tau_hat_1, self.nn_input_D_hat_1) + self.loss_mu_0 = euclidean_loss(mu_hat_0, self.nn_input_Y0) + self.loss_mu_1 = euclidean_loss(mu_hat_1, self.nn_input_Y1) + self.trainer_0 = tf.train.AdamOptimizer().minimize(self.loss_mu_0) + self.trainer_1 = tf.train.AdamOptimizer().minimize(self.loss_mu_1) + self.trainer_tau_0 = tf.train.AdamOptimizer().minimize(self.loss_tau_0) + self.trainer_tau_1 = tf.train.AdamOptimizer().minimize(self.loss_tau_1) + + def train(self, X0, Y0, X1, Y1): + if len(Y1.shape) == 1: + Y1 = np.expand_dims(Y1, axis=1) + if len(Y0.shape) == 1: + Y0 = np.expand_dims(Y0, axis=1) + fd = {self.nn_input_X0: X0, self.nn_input_X1: X1, self.nn_input_Y0: Y0, self.nn_input_Y1: Y1} + sess = tf.get_default_session() + sess.run([self.trainer_0, self.trainer_1], feed_dict=fd) + D_0, D_1 = sess.run([self.D_hat_0, self.D_hat_1], feed_dict=fd) + fd[self.nn_input_D_hat_1] = D_1 + fd[self.nn_input_D_hat_0] = D_0 + _, _, l0t, l1t = sess.run([self.trainer_tau_0, self.trainer_tau_1, self.loss_tau_0, self.loss_tau_1], feed_dict=fd) + return (l0t + l1t) / 2 + + def predict(self, X, clip_range=(-1, 1)): + fd = {self.nn_input_X0: X, self.nn_input_X1: X, self.nn_input_Y0: np.expand_dims(np.zeros(len(X)), axis=1), self.nn_input_Y1: np.expand_dims(np.zeros(len(X)), axis=1)} + sess = tf.get_default_session() + D_0, D_1 = sess.run([self.D_hat_0, self.D_hat_1], feed_dict=fd) + fd[self.nn_input_D_hat_1] = D_1 + fd[self.nn_input_D_hat_0] = D_0 + t_0, t_1 = sess.run([self.tau_hat_0, self.tau_hat_1], feed_dict=fd) + t_hat = 0.5 * t_0 + 0.5 * t_1 + return t_hat.flatten() + + +class Ulearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, activation=tf.nn.tanh, link_function=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + WS = WS.reshape([-1, 1]) + self.nn_input_X = tf.placeholder('float64', [None, XS.shape[1]]) + self.nn_input_W = tf.placeholder('float64', [None, 1]) + self.nn_input_Y = tf.placeholder('float64', [None, YS.shape[1]]) + self.slice = tf.placeholder('int32') + self.batch_size = tf.placeholder('int32') + n_hid = 64 + if activation == tf.nn.relu: + self.name = 'u_relu' + elif activation == tf.nn.tanh: + self.name = 'u_tanh' + + def get_weights_and_biases(n_layers, in_shape, out_shape): + w_dict = dict() + b_dict = dict() + n_units = n_hid + for i in range(n_layers): + if i == n_layers - 1: + n_units = out_shape + w_dict['h_' + str(i)] = tf.Variable(tf.random_normal([in_shape, n_units], dtype=tf.float64)) + b_dict['b_' + str(i)] = tf.Variable(tf.random_normal([n_units], dtype=tf.float64)) + in_shape = n_hid + return w_dict, b_dict + + def MLP(nn_input, weights, biases): + n_layers = len(weights) + l = nn_input + for i in range(n_layers): + if i == n_layers - 1: + Y_hat = tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)] + else: + l = activation(tf.matmul(l, weights['h_' + str(i)]) + biases['b_' + str(i)]) + return Y_hat + M_obs_weights, M_obs_biases = get_weights_and_biases(3, XS.shape[1], YS.shape[1]) + self.mu_hat_obs = MLP(self.nn_input_X, M_obs_weights, M_obs_biases) + mu_hat_obs_stopped = tf.stop_gradient(self.mu_hat_obs) + self.rf = RandomForestRegressor(n_estimators=200, oob_score=True, verbose=1) + self.rf.fit(XS, np.ravel(WS)) + self.ehat_oob = self.rf.oob_prediction_ + self.e_hat_x = tf.reshape(tf.gather(self.ehat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + self.R = (self.nn_input_Y - self.mu_hat_obs) / tf.clip_by_value(self.nn_input_W - self.e_hat_x, 1e-06, 1 - 1e-06) + h0_1 = L.fully_connected(self.nn_input_X, n_hid, activation_fn=activation) + h1_1 = L.fully_connected(h0_1, n_hid, activation_fn=activation) + self.tau_hat = L.fully_connected(h1_1, int(self.R.shape[1]), activation_fn=tf.identity) + if link_function: + self.tau_hat = tf.nn.tanh(self.tau_hat) + self.loss_mu_obs = tf.reduce_mean(tf.reduce_sum(tf.pow(self.mu_hat_obs - self.nn_input_Y, 2))) + self.mu_obs_trainer = tf.train.AdamOptimizer().minimize(self.loss_mu_obs) + self.tau_loss = tf.reduce_mean(tf.reduce_sum(tf.pow(self.tau_hat - self.R, 2))) + self.tau_trainer = tf.train.AdamOptimizer().minimize(self.tau_loss) + + def train(self, X_mb, Y_mb, W_mb, slice_num, batch_size): + if len(Y_mb.shape) == 1: + Y_mb = np.expand_dims(Y_mb, axis=1) + if len(W_mb.shape) == 1: + W_mb = np.expand_dims(W_mb, axis=1) + fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb, self.slice: slice_num, self.batch_size: batch_size} + sess = tf.get_default_session() + _, _, t_l = sess.run([self.mu_obs_trainer, self.tau_trainer, self.tau_loss], feed_dict=fd) + return t_l + + def predict(self, X): + fd = {self.nn_input_X: X} + sess = tf.get_default_session() + t_hat = sess.run(self.tau_hat, feed_dict=fd) + return t_hat.flatten() + + +class Flearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, activation=tf.nn.tanh, link_function=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + WS = WS.reshape([-1, 1]) + self.nn_input_X = tf.placeholder('float64', [None, XS.shape[1]], name='X') + self.nn_input_W = tf.placeholder('float64', [None, 1], name='W') + self.nn_input_Y = tf.placeholder('float64', [None, YS.shape[1]], name='Y') + self.slice = tf.placeholder('int32', name='slice') + self.batch_size = tf.placeholder('int32', name='batch') + self.rf = RandomForestRegressor(n_estimators=200, oob_score=True, verbose=1) + self.rf.fit(XS, np.ravel(WS)) + self.ehat_oob = self.rf.oob_prediction_ + self.e_hat_x = tf.reshape(tf.gather(self.ehat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + self.e_hat_x = tf.clip_by_value(self.e_hat_x, 1e-06, 1 - 1e-06) + Y_star = self.nn_input_Y * ((self.nn_input_W - self.e_hat_x) / (self.e_hat_x * (1 - self.e_hat_x))) + n_hid = 64 + h0_1 = L.fully_connected(self.nn_input_X, n_hid, activation_fn=activation) + h1_1 = L.fully_connected(h0_1, n_hid, activation_fn=activation) + self.tau_hat = L.fully_connected(h1_1, int(Y_star.shape[1]), activation_fn=tf.identity) + if link_function: + self.tau_hat = tf.nn.tanh(self.tau_hat) + self.tau_loss = tf.reduce_mean(tf.reduce_sum(tf.pow(self.tau_hat - Y_star, 2))) + self.tau_trainer = tf.train.AdamOptimizer().minimize(self.tau_loss) + if activation == tf.nn.relu: + self.name = 'f_relu' + elif activation == tf.nn.tanh: + self.name = 'f_tanh' + + def train(self, X_mb, Y_mb, W_mb, slice_num, batch_size): + if len(Y_mb.shape) == 1: + Y_mb = np.expand_dims(Y_mb, axis=1) + if len(W_mb.shape) == 1: + W_mb = np.expand_dims(W_mb, axis=1) + fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb, self.slice: slice_num, self.batch_size: batch_size} + sess = tf.get_default_session() + _, t_l = sess.run([self.tau_trainer, self.tau_loss], feed_dict=fd) + return t_l + + def predict(self, X): + fd = {self.nn_input_X: X} + sess = tf.get_default_session() + t_hat = sess.run(self.tau_hat, feed_dict=fd) + return t_hat.flatten() + + +class RLearner(CATE_estimator): + tf_trainable = True + + def __init__(self, XS, YS, WS, activation=tf.nn.tanh, link_function=False): + if len(YS.shape) == 1: + YS = np.expand_dims(YS, axis=1) + WS = WS.reshape([-1, 1]) + self.nn_input_X = tf.placeholder('float64', [None, XS.shape[1]]) + self.nn_input_W = tf.placeholder('float64', [None, 1]) + self.nn_input_Y = tf.placeholder('float64', [None, YS.shape[1]]) + self.slice = tf.placeholder(tf.int32) + self.batch_size = tf.placeholder(tf.int32) + self.rf = RandomForestRegressor(n_estimators=500, oob_score=True, verbose=1) + self.rf.fit(XS, np.ravel(WS)) + self.ehat_oob = self.rf.oob_prediction_ + self.e_hat_x = tf.reshape(tf.gather(self.ehat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + self.rf_m = RandomForestRegressor(n_estimators=500, oob_score=True, verbose=1) + self.rf_m.fit(XS, np.ravel(YS)) + self.m_hat_oob = self.rf_m.oob_prediction_ + self.m_hat_x = tf.reshape(tf.gather(self.m_hat_oob, tf.range(self.slice, self.slice + self.batch_size)), [-1, 1]) + n_hid = 64 + h0_1 = L.fully_connected(self.nn_input_X, n_hid, activation_fn=activation) + h1_1 = L.fully_connected(h0_1, n_hid, activation_fn=activation) + self.tau_hat = L.fully_connected(h1_1, int(self.nn_input_Y.shape[1]), activation_fn=tf.identity) + if link_function: + self.tau_hat = tf.nn.tanh(self.tau_hat) + self.tau_loss = tf.reduce_mean((self.nn_input_Y - self.m_hat_x - (self.nn_input_W - self.e_hat_x) * self.tau_hat) ** 2) + self.tau_trainer = tf.train.AdamOptimizer().minimize(self.tau_loss) + if activation == tf.nn.relu: + self.name = 'r_relu' + elif activation == tf.nn.tanh: + self.name = 'r_tanh' + + def train(self, X_mb, Y_mb, W_mb, slice_num, batch_size): + if len(Y_mb.shape) == 1: + Y_mb = np.expand_dims(Y_mb, axis=1) + if len(W_mb.shape) == 1: + W_mb = np.expand_dims(W_mb, axis=1) + fd = {self.nn_input_X: X_mb, self.nn_input_Y: Y_mb, self.nn_input_W: W_mb, self.slice: slice_num, self.batch_size: batch_size} + sess = tf.get_default_session() + _, t_l = sess.run([self.tau_trainer, self.tau_loss], feed_dict=fd) + return t_l + + def predict(self, X): + fd = {self.nn_input_X: X} + sess = tf.get_default_session() + t_hat = sess.run(self.tau_hat, feed_dict=fd) + return t_hat.flatten() + + +if __name__ == '__main__': + print(3 + 3) diff --git a/dataset/preprocessed/438.txt b/dataset/preprocessed/438.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/438.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/439.txt b/dataset/preprocessed/439.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/439.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/44.txt b/dataset/preprocessed/44.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/44.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/441.txt b/dataset/preprocessed/441.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/441.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/442.txt b/dataset/preprocessed/442.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/442.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/443.txt b/dataset/preprocessed/443.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/443.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/444.txt b/dataset/preprocessed/444.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/444.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/445.txt b/dataset/preprocessed/445.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/445.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/446.txt b/dataset/preprocessed/446.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/446.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/447.txt b/dataset/preprocessed/447.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/447.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/448.txt b/dataset/preprocessed/448.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/448.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/449.txt b/dataset/preprocessed/449.txt new file mode 100644 index 0000000..89b3adf --- /dev/null +++ b/dataset/preprocessed/449.txt @@ -0,0 +1,60 @@ +""" +@author sourabhxiii + +""" +import numpy as np +import tensorflow as tf +from tensorflow import layers +from collections import namedtuple + + +class Critic: + + def __init__(self, state_dim, axn_dim, output_dim, np_index, seed): + tf.set_random_seed(seed) + self.lr = 0.03 + with tf.name_scope('input'): + self.features = tf.placeholder(dtype=tf.float32, shape=(None, state_dim), name='features') + self.action = tf.placeholder(dtype=tf.float32, shape=(None, axn_dim), name='action') + self.target_q = tf.placeholder(dtype=tf.float32, shape=(None, 1), name='target_q') + with tf.name_scope('output'): + self.y = tf.placeholder(dtype=tf.float32, shape=(None, 1), name='qvalue') + with tf.name_scope('critic_net'): + nn = layers.Dense(8, kernel_initializer=tf.random_normal_initializer(), bias_initializer=tf.random_normal_initializer(), activation=tf.nn.relu, name='CM_D1')(self.features) + nn = tf.concat([layers.flatten(nn), self.action], 1) + nn = layers.Dense(8, kernel_initializer=tf.random_normal_initializer(), bias_initializer=tf.random_normal_initializer(), activation=tf.nn.relu, name='CM_D2')(nn) + nn = layers.Dense(4, kernel_initializer=tf.random_normal_initializer(), bias_initializer=tf.random_normal_initializer(), activation=tf.nn.relu, name='CM_D3')(nn) + self.predict_op = layers.Dense(output_dim, kernel_initializer=tf.random_normal_initializer(), bias_initializer=tf.random_normal_initializer(), activation=tf.nn.tanh, name='CM_D4')(nn) + with tf.name_scope('critic_gradient'): + with tf.name_scope('critic_loss'): + self.loss = tf.reduce_mean(tf.losses.mean_squared_error(labels=self.target_q, predictions=self.predict_op)) + with tf.name_scope('critic_train'): + self.train_op = tf.train.AdamOptimizer(self.lr).minimize(self.loss) + """ + https://pemami4911.github.io/blog/2016/08/21/ddpg-rl.html + https://github.com/pemami4911/deep-rl/tree/master/ddpg + """ + self.action_grads = tf.gradients(self.predict_op, self.action) + with tf.name_scope('init'): + self.init = tf.global_variables_initializer() + self.network_params = tf.trainable_variables()[np_index:] + return + + def set_session(self, sess): + self.session = sess + self.session.run(self.init) + + def model_session(self): + return self.session + + def update_weight(self, obs, axn, target): + self.session.run(self.train_op, feed_dict={self.features: obs, self.action: axn, self.target_q: target}) + + def get_q(self, obs, axns): + return self.session.run(self.predict_op, feed_dict={self.features: obs, self.action: np.squeeze(axns)}) + + def get_action_gradients(self, obs, axns): + return self.session.run(self.action_grads, feed_dict={self.features: obs, self.action: np.squeeze(axns)}) + + def get_weights(self): + return self.network_params diff --git a/dataset/preprocessed/45.txt b/dataset/preprocessed/45.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/45.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/450.txt b/dataset/preprocessed/450.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/450.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/451.txt b/dataset/preprocessed/451.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/451.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/452.txt b/dataset/preprocessed/452.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/452.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/453.txt b/dataset/preprocessed/453.txt new file mode 100644 index 0000000..ddad5a4 --- /dev/null +++ b/dataset/preprocessed/453.txt @@ -0,0 +1,175 @@ +import os +import json +import numpy as np +import tensorflow as tf +from skimage import io +from matplotlib.patches import Rectangle +from PIL import Image +import progressbar +import time +import warnings +warnings.filterwarnings('ignore') +pic_dir_path = '../data/img2' +json_dir_path = '../data/ann2' +MODEL_PATH = '../data/model_artif/' +PIXEL_COUNT = 480 * 640 +LABEL_COUNT = 4 +EPOCH = 30 +if not os.path.exists(MODEL_PATH): + os.makedirs(MODEL_PATH) + + +def read_img(img_path, height=480, width=640, dim=3): + return io.imread(img_path)[:, :, :3] / 255 + + +def read_collection(img_paths, height=480, width=640, dim=3): + n_imgs = img_paths.shape[0] + output = np.zeros((n_imgs, height, width, dim), dtype='float32') + for i, path in enumerate(img_paths): + output[(i), :, :, :] = read_img(path) + return output.reshape(n_imgs, height * width, dim) + + +class Dataset: + + def __init__(self, X, Y): + self.X = X + self.Y = Y + self._epochs_completed = 0 + self._index_in_epoch = 0 + self._num_examples = X.shape[0] + + def next_batch(self, batch_size=20): + start = self._index_in_epoch + self._index_in_epoch += batch_size + if self._index_in_epoch > self._num_examples: + self._epochs_completed += 1 + perm = np.arange(self._num_examples) + np.random.shuffle(perm) + self.X = self.X[perm] + self.Y = self.Y[perm] + start = 0 + self._index_in_epoch = batch_size + assert batch_size <= self._num_examples + end = self._index_in_epoch + return read_collection(self.X[start:end]), self.Y[start:end] + + def epoch_completed(self): + return self._epochs_completed + + +def mse(expected, predicted): + se = tf.square(expected - predicted) + return tf.reduce_mean(se) + + +def weight_variable(name, shape): + initial = tf.truncated_normal(shape, stddev=0.1) + return tf.get_variable(name, initializer=initial) + + +def bias_variable(name, shape): + initial = tf.constant(0.1, shape=shape) + return tf.get_variable(name, initializer=initial) + + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +class Model(object): + xxx = 0 + + +def build_model(): + x_placeholder = tf.placeholder(tf.float32, shape=[None, PIXEL_COUNT, 3]) + y_placeholder = tf.placeholder(tf.float32, shape=[None, LABEL_COUNT]) + x_image = tf.reshape(x_placeholder, [-1, 480, 640, 3]) + W_conv1 = weight_variable('w1', [3, 3, 3, 32]) + b_conv1 = bias_variable('b1', [32]) + h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + W_conv2 = weight_variable('w2', [3, 3, 32, 64]) + b_conv2 = bias_variable('b2', [64]) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = max_pool_2x2(h_conv2) + W_conv3 = weight_variable('w3', [3, 3, 64, 128]) + b_conv3 = bias_variable('b3', [128]) + h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) + h_pool3 = max_pool_2x2(h_conv3) + h_pool3_flat = tf.reshape(h_pool3, [-1, 60 * 80 * 128]) + W_fc1 = weight_variable('w4', [60 * 80 * 128, 500]) + b_fc1 = bias_variable('b4', [500]) + h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1) + W_fc2 = weight_variable('w5', [500, 500]) + b_fc2 = bias_variable('b5', [500]) + h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2) + W_out = weight_variable('w6', [500, LABEL_COUNT]) + b_out = bias_variable('b6', [LABEL_COUNT]) + output = tf.matmul(h_fc2, W_out) + b_out + model = Model() + model.x_placeholder = x_placeholder + model.y_placeholder = y_placeholder + model.output = output + return model + + +img_names = np.array(os.listdir(pic_dir_path)) +json_names = os.listdir(json_dir_path) +img = io.imread('../data/img2/' + img_names[10]) +for i, name in enumerate(json_names): + json_names[i] = os.path.join(json_dir_path, name) +info = [] +for name in json_names: + f = open(name, 'r') + f_json = json.load(f) + info += f_json[0]['imgs'] +info = np.array(info) +X_paths = [] +Y = np.zeros((1001, 4), dtype=int) +info1001 = info[:1001] +bar = progressbar.ProgressBar(max_value=info1001.shape[0]) +for i, inf in enumerate(info1001): + path = os.path.join(pic_dir_path, inf['car_number_pic']) + X_paths.append(path) + Y[(i), :] = inf['coordinates'] + bar.update(i) +X_paths = np.array(X_paths) +X2_train = X_paths[:901] +X2_test = X_paths[901:] +Y_train = Y[:901, :] +Y_test = Y[901:, :] +Y2_train = Y_train / (64.0, 32.0, 64.0, 32.0) - 1.0 +Y2_test = Y_test / (64.0, 32.0, 64.0, 32.0) - 1 +dataset = Dataset(X2_train, Y2_train) +g = tf.Graph() +with g.as_default(): + session = tf.InteractiveSession() + model = build_model() + loss = mse(model.y_placeholder, model.output) + saver = tf.train.Saver() + start_time = time.time() + best_score = 1 + train_step = tf.train.RMSPropOptimizer(learning_rate=0.1).minimize(loss) + session = tf.InteractiveSession() + session.run(tf.global_variables_initializer()) + last_epoch = -1 + while dataset.epoch_completed() < EPOCH: + batch_x, batch_y = dataset.next_batch(4) + train_step.run(feed_dict={model.x_placeholder: batch_x, model.y_placeholder: batch_y}) + if dataset.epoch_completed() > last_epoch: + last_epoch = dataset.epoch_completed() + score_test = loss.eval(feed_dict={model.x_placeholder: read_collection(X2_test), model.y_placeholder: Y2_test}) + if score_test < best_score: + best_score = score_test + saver.save(session, os.path.join(MODEL_PATH, 'model')) + if dataset.epoch_completed() % 1 == 0: + epm = 60 * dataset.epoch_completed() / (time.time() - start_time) + print('Epoch: %d, Score: %f, Epoch per minute: %f' % (dataset.epoch_completed(), score_test, epm)) + print('Finished in %f seconds.' % (time.time() - start_time)) + session.close() diff --git a/dataset/preprocessed/454.txt b/dataset/preprocessed/454.txt new file mode 100644 index 0000000..cb11c89 --- /dev/null +++ b/dataset/preprocessed/454.txt @@ -0,0 +1,115 @@ +import tensorflow as tf +import numpy as np + + +class Model: + HIDDEN_UNIT_SIZE = 32 + HIDDEN_UNIT_SIZE2 = 16 + COLUMN_SIZE = 9 + + def __init__(self): + self.__setup_placeholder() + self.__setup_model() + self.__setup_ops() + + def __enter__(self): + self.session = tf.Session() + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.session.close() + return False + + def save(self, path): + saver = tf.train.Saver() + saver.save(self.session, path) + + def restore(self, path): + saver = tf.train.Saver() + saver.restore(self.session, path) + + def __setup_placeholder(self): + column_size = Model.COLUMN_SIZE + self.trade_data = tf.placeholder('float', [None, column_size]) + self.actual_class = tf.placeholder('float', [None, 2]) + self.keep_prob = tf.placeholder('float') + self.label = tf.placeholder('string') + + def __setup_model(self): + column_size = Model.COLUMN_SIZE + w1 = tf.Variable(tf.truncated_normal([column_size, Estimator.HIDDEN_UNIT_SIZE], stddev=0.1)) + b1 = tf.Variable(tf.constant(0.1, shape=[Estimator.HIDDEN_UNIT_SIZE])) + h1 = tf.nn.relu(tf.matmul(self.trade_data, w1) + b1) + w2 = tf.Variable(tf.truncated_normal([Estimator.HIDDEN_UNIT_SIZE, Estimator.HIDDEN_UNIT_SIZE2], stddev=0.1)) + b2 = tf.Variable(tf.constant(0.1, shape=[Estimator.HIDDEN_UNIT_SIZE2])) + h2 = tf.nn.relu(tf.matmul(h1, w2) + b2) + h2_drop = tf.nn.dropout(h2, self.keep_prob) + w2 = tf.Variable(tf.truncated_normal([Estimator.HIDDEN_UNIT_SIZE2, 2], stddev=0.1)) + b2 = tf.Variable(tf.constant(0.1, shape=[2])) + self.output = tf.nn.softmax(tf.matmul(h2_drop, w2) + b2) + + def __setup_ops(self): + cross_entropy = -tf.reduce_sum(self.actual_class * tf.log(self.output)) + self.summary = tf.scalar_summary(self.label, cross_entropy) + self.train_op = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + self.merge_summaries = tf.merge_summary([self.summary]) + correct_prediction = tf.equal(tf.argmax(self.output, 1), tf.argmax(self.actual_class, 1)) + self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) + + +class Trainer(Model): + + def train(self, steps, data): + self.__prepare_train(self.session) + for i in range(steps): + self.__do_train(self.session, i, data) + if i % 100 == 0: + self.__add_summary(self.session, i, data) + self.__print_status(self.session, i, data) + + def __prepare_train(self, session): + self.summary_writer = tf.train.SummaryWriter('logs', graph_def=session.graph_def) + session.run(tf.initialize_all_variables()) + + def __do_train(self, session, i, data): + session.run(self.train_op, feed_dict=self.train_feed_dict(data)) + + def __add_summary(self, session, i, data): + summary_str = session.run(self.merge_summaries, feed_dict=self.train_feed_dict(data)) + summary_str += session.run(self.merge_summaries, feed_dict=self.test_feed_dict(data)) + self.summary_writer.add_summary(summary_str, i) + + def __print_status(self, session, i, data): + train_accuracy = session.run(self.accuracy, feed_dict=self.train_feed_dict(data)) + test_accuracy = session.run(self.accuracy, feed_dict=self.test_feed_dict(data)) + print('step {0} ,train_accuracy={1} ,test_accuracy={2} '.format(i, train_accuracy, test_accuracy)) + + def train_feed_dict(self, data): + return {self.trade_data: data.train_data(), self.actual_class: data.train_up_down(), self.keep_prob: 0.8, self.label: 'train'} + + def test_feed_dict(self, data): + return {self.trade_data: data.test_data(), self.actual_class: data.test_up_down(), self.keep_prob: 1, self.label: 'test'} + + def __reshape(self, profit_or_loss): + return profit_or_loss.values.reshape(len(profit_or_loss.values), 1) + + +class Estimator(Model): + + def estimate(self, data): + return self.session.run(tf.argmax(self.output, 1), feed_dict=self.estimate_feed_dict(data)) + + def estimate_feed_dict(self, data): + return {self.trade_data: data, self.keep_prob: 1} + + +def main(): + loader = TradeResultsLoader() + data = TradeResults(loader.retrieve_trade_data()) + with Trainer() as trainer: + trainer.train(10001, data) + trainer.save('./model.ckpt') + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/455.txt b/dataset/preprocessed/455.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/455.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/456.txt b/dataset/preprocessed/456.txt new file mode 100644 index 0000000..7794fbe --- /dev/null +++ b/dataset/preprocessed/456.txt @@ -0,0 +1,181 @@ +""" +Created on Sat Apr 21 19:59:10 2018 + +transfer learning conv const fully init + +@author: latlab +""" +import numpy as np +import tensorflow as tf +import random as rn +from sklearn.cross_validation import KFold + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +num_class = 2 +labels = np.asarray(range(num_class)) +img_size_x = 64 +img_size_y = 64 +img_size_z = 28 +num_channels = 1 +num_labels = 2 +batch_size1 = 4 +num_samples1 = 92 +kernel_size_1 = 5 +depth_1 = 5 +num_hid_1 = 64 +beta = 0.01 +import time +start = time.time() +num_steps1 = 1501 +keep_prob1 = 1 +file = open('D:\\Domonkos\\transfer_learning\\' + str(num_steps1) + '_' + str(keep_prob1) + '_' + str(batch_size1) + '_transferConstInitprior_adam.txt', 'w') +file.write('execution_time' + ' ' + 'minibatch_loss' + ' ' + 'minibatch_accuracy' + ' ' + ' ' + 'num_step' + ' ' + '\n') +num_kmeans = 10 +num_steps2 = 151 +keep_prob2 = 1 +batch_size2 = 4 +file2 = open('D:\\Domonkos\\transfer_learning\\' + str(num_kmeans) + '_' + str(num_steps2) + '_' + str(keep_prob2) + '_' + str(batch_size2) + '_transferConstInitposterior_adam.txt', 'w') +file2.write('execution_time' + ' ' + 'minibatch_loss' + ' ' + 'minibatch_accuracy' + ' ' + 'validation_accuracy' + ' ' + 'num_kmeans' + ' ' + 'num_step' + ' ' + '\n') +metadata = np.loadtxt('D:\\Domonkos\\transfer_learning\\labels_tf.txt').astype(int) +metadata2 = np.loadtxt('D:\\Domonkos\\resting_raw\\metadata\\labels.txt') +for k in range(10): + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, name='train_data', shape=(batch_size1, img_size_x, img_size_y, img_size_z, num_channels)) + tf_train_labels = tf.placeholder(tf.float32, name='train_labels', shape=(batch_size1, num_labels)) + tf_keep_prob = tf.placeholder(tf.float32, name='keep_prob') + layer1_weights = tf.Variable(tf.truncated_normal([kernel_size_1, kernel_size_1, kernel_size_1, num_channels, depth_1], stddev=0.1), name='layer1_weights') + layer1_biases = tf.Variable(tf.zeros([depth_1]), name='layer1_biases') + layer2_weights = tf.Variable(tf.truncated_normal([kernel_size_1, kernel_size_1, kernel_size_1, depth_1, depth_1], stddev=0.1), name='layer2_weights') + layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth_1]), name='layer2_biases') + layer3_weights = tf.Variable(tf.truncated_normal([img_size_x // 4 * (img_size_y // 4) * (img_size_z // 4) * depth_1, num_hid_1], stddev=0.1), name='layer3_weights') + layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hid_1]), name='layer3_biases') + layer4_weights = tf.Variable(tf.truncated_normal([num_hid_1, num_labels], stddev=0.1), name='layer4_weights') + layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]), name='layer4_biases') + + def model(data): + conv = tf.nn.conv3d(data, layer1_weights, [1, 1, 1, 1, 1], padding='SAME') + hidden = tf.nn.relu(conv + layer1_biases) + pool_pre = tf.nn.max_pool3d(hidden, [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding='SAME') + pool = tf.nn.dropout(pool_pre, tf_keep_prob) + conv = tf.nn.conv3d(pool, layer2_weights, [1, 1, 1, 1, 1], padding='SAME') + hidden = tf.nn.relu(conv + layer2_biases) + pool_pre = tf.nn.max_pool3d(hidden, [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding='SAME') + pool = tf.nn.dropout(pool_pre, tf_keep_prob) + shape = pool.get_shape().as_list() + reshape = tf.reshape(pool, [shape[0], shape[1] * shape[2] * shape[3] * shape[4]]) + fully_connect_1 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases) + fully_connect_1_drop = tf.nn.dropout(fully_connect_1, 1) + return tf.matmul(fully_connect_1_drop, layer4_weights) + layer4_biases + logits = model(tf_train_dataset) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.AdamOptimizer(0.001).minimize(loss) + train_prediction = tf.nn.softmax(logits) + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized \n') + train_index = np.arange(92) + np.random.shuffle(train_index) + for step in range(num_steps1): + offset = step * batch_size1 % (num_samples1 - batch_size1) + batch_data = [] + batch_labels = [] + for i in range(batch_size1): + ind = train_index[(offset + i) % train_index.shape[0]] + batch_data.append(np.load('D:\\Domonkos\\transfer_learning\\numpys_tf_use2\\' + str(ind) + '_' + str(int(np.floor(rn.random() * metadata[ind, 6]))) + '.npy')) + batch_labels.append(metadata[ind, 2]) + batch_data = np.asarray(batch_data) + batch_labels = np.asarray(batch_labels) + batch_labels = (np.arange(num_labels) == batch_labels[:, (None)]).astype(np.float32) + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_keep_prob: keep_prob1} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + file.write(str(time.time() - start) + ' ' + str(l) + ' ' + str(accuracy(predictions, batch_labels)) + ' ' + ' ' + str(step) + ' ' + '\n') + layer1_weights_tf = session.run('layer1_weights:0') + layer1_biases_tf = session.run('layer1_biases:0') + layer2_weights_tf = session.run('layer2_weights:0') + layer2_biases_tf = session.run('layer2_biases:0') + layer3_weights_tf = session.run('layer3_weights:0') + layer3_biases_tf = session.run('layer3_biases:0') + layer4_weights_tf = session.run('layer4_weights:0') + layer4_biases_tf = session.run('layer4_biases:0') + print('variables are saved_' + str(k) + '\n') + labels = np.asarray(range(num_class)) + batch_size2 = 4 + num_samples2 = 60 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, name='train_data', shape=(batch_size2, img_size_x, img_size_y, img_size_z, num_channels)) + tf_train_labels = tf.placeholder(tf.float32, name='train_labels', shape=(batch_size2, num_labels)) + tf_keep_prob = tf.placeholder(tf.float32, name='keep_prob') + tf_valid_dataset = tf.placeholder(tf.float32, name='valid', shape=(num_samples2 // num_kmeans, img_size_x, img_size_y, img_size_z, num_channels)) + layer1_weights = tf.placeholder(tf.float32, name='layer1_weights', shape=(kernel_size_1, kernel_size_1, kernel_size_1, num_channels, depth_1)) + layer1_biases = tf.placeholder(tf.float32, name='layer1_biases', shape=depth_1) + layer2_weights = tf.placeholder(tf.float32, name='layer2_weights', shape=(kernel_size_1, kernel_size_1, kernel_size_1, depth_1, depth_1)) + layer2_biases = tf.placeholder(tf.float32, name='layer2_biases', shape=depth_1) + layer3_weights = tf.Variable(tf.truncated_normal([img_size_x // 4 * (img_size_y // 4) * (img_size_z // 4) * depth_1, num_hid_1], stddev=0.1), name='layer3_weights') + layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hid_1]), name='layer3_biases') + layer4_weights = tf.Variable(tf.truncated_normal([num_hid_1, num_labels], stddev=0.1), name='layer4_weights') + layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]), name='layer4_biases') + + def model(data): + conv = tf.nn.conv3d(data, layer1_weights, [1, 1, 1, 1, 1], padding='SAME') + hidden = tf.nn.relu(conv + layer1_biases) + pool_pre = tf.nn.max_pool3d(hidden, [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding='SAME') + pool = tf.nn.dropout(pool_pre, tf_keep_prob) + conv = tf.nn.conv3d(pool, layer2_weights, [1, 1, 1, 1, 1], padding='SAME') + hidden = tf.nn.relu(conv + layer2_biases) + pool_pre = tf.nn.max_pool3d(hidden, [1, 2, 2, 2, 1], [1, 2, 2, 2, 1], padding='SAME') + pool = tf.nn.dropout(pool_pre, tf_keep_prob) + shape = pool.get_shape().as_list() + reshape = tf.reshape(pool, [shape[0], shape[1] * shape[2] * shape[3] * shape[4]]) + fully_connect_1 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases) + fully_connect_1_drop = tf.nn.dropout(fully_connect_1, 1) + return tf.matmul(fully_connect_1_drop, layer4_weights) + layer4_biases + logits = model(tf_train_dataset) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.AdamOptimizer(0.001).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + kfold = KFold(num_samples2, num_kmeans, True, None) + ind1 = 0 + for train_index, test_index in kfold: + np.random.shuffle(train_index) + valid_dataset = [] + valid_labels = [] + for j in range(test_index.shape[0]): + valid_dataset.append(np.load('D:\\Domonkos\\resting_raw\\numpys_resamp_ss\\rss_' + str(test_index[-(j + 1)]) + '_' + str(int(np.floor(rn.random() * 100))) + '.npy')) + valid_labels.append(metadata2[test_index[-(j + 1)], 2]) + valid_dataset = np.asarray(valid_dataset) + valid_labels = np.asarray(valid_labels) + valid_labels = (np.arange(num_labels) == valid_labels[:, (None)]).astype(np.float32) + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized globally \n') + session.run(layer3_weights, feed_dict={layer3_weights: layer3_weights_tf}) + session.run(layer3_biases, feed_dict={layer3_biases: layer3_biases_tf}) + session.run(layer4_weights, feed_dict={layer4_weights: layer4_weights_tf}) + session.run(layer4_biases, feed_dict={layer4_biases: layer4_biases_tf}) + print('Initialized weights and biases_' + str(k) + '\n') + for step in range(num_steps2): + offset = step * batch_size2 % (num_samples2 - num_samples2 / num_kmeans - batch_size2) + batch_data = [] + batch_labels = [] + for i in range(batch_size2): + ind = train_index[(offset + i) % train_index.shape[0]] + batch_data.append(np.load('D:\\Domonkos\\resting_raw\\numpys_resamp_ss\\rss_' + str(ind) + '_' + str(int(np.floor(rn.random() * 100))) + '.npy')) + batch_labels.append(metadata2[ind, 2]) + batch_data = np.asarray(batch_data) + batch_labels = np.asarray(batch_labels) + batch_labels = (np.arange(num_labels) == batch_labels[:, (None)]).astype(np.float32) + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_valid_dataset: valid_dataset, tf_keep_prob: keep_prob2, layer1_weights: layer1_weights_tf, layer1_biases: layer1_biases_tf, layer2_weights: layer2_weights_tf, layer2_biases: layer2_biases_tf} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + file2.write(str(time.time() - start) + ' ' + str(l) + ' ' + str(accuracy(predictions, batch_labels)) + ' ' + str(accuracy(valid_prediction.eval(feed_dict=feed_dict), valid_labels)) + ' ' + str(ind1) + ' ' + str(step) + ' ' + '\n') + ind1 += 1 +end = time.time() +file.close() +file2.write(str(end - start)) +file2.close() diff --git a/dataset/preprocessed/457.txt b/dataset/preprocessed/457.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/457.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/458.txt b/dataset/preprocessed/458.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/458.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/46.txt b/dataset/preprocessed/46.txt new file mode 100644 index 0000000..f5c1f81 --- /dev/null +++ b/dataset/preprocessed/46.txt @@ -0,0 +1,51 @@ +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + data_d = tf.nn.dropout(data, keep_prob=0.9) + output1 = tf.matmul(data_d, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/460.txt b/dataset/preprocessed/460.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/460.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/461.txt b/dataset/preprocessed/461.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/461.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/462.txt b/dataset/preprocessed/462.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/462.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/463.txt b/dataset/preprocessed/463.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/463.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/464.txt b/dataset/preprocessed/464.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/464.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/465.txt b/dataset/preprocessed/465.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/465.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/466.txt b/dataset/preprocessed/466.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/466.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/467.txt b/dataset/preprocessed/467.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/467.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/468.txt b/dataset/preprocessed/468.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/468.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/469.txt b/dataset/preprocessed/469.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/469.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/47.txt b/dataset/preprocessed/47.txt new file mode 100644 index 0000000..a61aafb --- /dev/null +++ b/dataset/preprocessed/47.txt @@ -0,0 +1,134 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1.1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 100 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + print(test_numbers) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/470.txt b/dataset/preprocessed/470.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/470.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/471.txt b/dataset/preprocessed/471.txt new file mode 100644 index 0000000..fafed14 --- /dev/null +++ b/dataset/preprocessed/471.txt @@ -0,0 +1,139 @@ +"""Generative adversarial network.""" +from __future__ import print_function +import numpy as np +import tensorflow as tf +import matplotlib.pyplot as plt +from tensorflow import contrib +from tensorflow.contrib import layers + + +class Gan(object): + """Adversary based generator network. + """ + + def __init__(self, batch_size=32, ndims=784, cdim=1, nlatent=150, eta_d=0.0005, eta_g=0.0005): + """Initializes a GAN + + Args: + ndims(int): Number of dimensions in the feature. + nlatent(int): Number of dimensions in the latent space. + """ + self.batch_size = batch_size + self.ndims = ndims + self.in_shape = int(ndims ** 0.5), int(ndims ** 0.5) + self.n_latent = nlatent + self.colorspace_dim = cdim + self.x_placeholder = tf.placeholder(tf.float32, [None, ndims * cdim]) + self.z_placeholder = tf.placeholder(tf.float32, [None, nlatent]) + self.x_hat = self._generator(self.z_placeholder) + y_hat = self._discriminator(self.x_hat) + y = self._discriminator(self.x_placeholder, reuse=True) + self.d_loss = self._discriminator_loss(y, y_hat) + self.g_loss = self._generator_loss(y_hat) + discrim_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'discriminator') + print('Discriminator variables:', discrim_vars) + self.d_optimizer = tf.train.AdamOptimizer(eta_d).minimize(self.d_loss, var_list=discrim_vars) + gen_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'generator') + print('Generator variables:', gen_vars) + self.g_optimizer = tf.train.AdamOptimizer(eta_g).minimize(self.g_loss, var_list=gen_vars) + self.sample_gen = self.x_hat + self.session = tf.InteractiveSession() + self.session.run(tf.global_variables_initializer()) + + def _discriminator(self, x, reuse=False): + """Discriminator block of the network. + + Args: + x (tf.Tensor): The input tensor of dimension (None, 784). + reuse (Boolean): re use variables with same name in scope instead of creating + new ones, check Tensorflow documentation + Returns: + y (tf.Tensor): Scalar output prediction D(x) for true vs fake image(None, 1). + DO NOT USE AN ACTIVATION FUNCTION AT THE OUTPUT LAYER HERE. + + """ + with tf.variable_scope('discriminator', reuse=reuse) as scope: + h1 = layers.fully_connected(x, self.ndims * self.colorspace_dim, activation_fn=tf.nn.relu) + h2 = layers.fully_connected(h1, self.ndims * self.colorspace_dim, activation_fn=tf.nn.relu) + h3 = layers.fully_connected(h2, self.ndims * self.colorspace_dim, activation_fn=tf.nn.relu) + y = layers.fully_connected(h3, 1, activation_fn=None, scope=scope) + return y + + def _discriminator_loss(self, y, y_hat): + """Loss for the discriminator. + + Args: + y (tf.Tensor): The output tensor of the discriminator for true images of dimension (None, 1). + y_hat (tf.Tensor): The output tensor of the discriminator for fake images of dimension (None, 1). + Returns: + l (tf.Scalar): average batch loss for the discriminator. + + """ + loss_y = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y, labels=tf.ones_like(y))) + loss_yhat = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_hat, labels=tf.zeros_like(y_hat))) + return loss_y + loss_yhat + + def _generator(self, z, reuse=False): + """From a sampled z, generate an image. + + Args: + z(tf.Tensor): z from _sample_z of dimension (None, 2). + reuse (Boolean): re use variables with same name in scope instead of creating + new ones, check Tensorflow documentation + Returns: + x_hat(tf.Tensor): Fake image G(z) (None, 784). + """ + with tf.variable_scope('generator', reuse=reuse) as scope: + h1 = layers.fully_connected(z, self.ndims * self.colorspace_dim, activation_fn=tf.nn.leaky_relu) + h2 = layers.fully_connected(h1, self.ndims * self.colorspace_dim, activation_fn=tf.nn.leaky_relu) + h3 = layers.fully_connected(h2, self.ndims * self.colorspace_dim, activation_fn=tf.nn.leaky_relu) + raw = layers.fully_connected(h3, self.ndims * self.colorspace_dim, activation_fn=tf.nn.tanh, scope=scope) + return raw + + def _generator_loss(self, y_hat): + """Loss for the discriminator. + + Args: + y_hat (tf.Tensor): The output tensor of the discriminator for fake images of dimension (None, 1). + Returns: + l (tf.Scalar): average batch loss for the discriminator. + + """ + return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_hat, labels=tf.ones_like(y_hat))) + + def sample(self, z_mu): + img = self.session.run(self.x_hat, feed_dict={self.z_placeholder: z_mu}) + return img + + def generate_file(self, z_mus, step, n=20, img_dir='images/'): + out = np.empty((self.in_shape[0] * n, self.in_shape[1] * n, self.colorspace_dim)) + if self.colorspace_dim == 1: + out = np.empty((self.in_shape[0] * n, self.in_shape[1] * n)) + for x_idx in range(n): + for y_idx in range(n): + img = self.sample(z_mus[x_idx + y_idx * n]) + if self.colorspace_dim == 1: + out[x_idx * self.in_shape[0]:(x_idx + 1) * self.in_shape[0], y_idx * self.in_shape[1]:(y_idx + 1) * self.in_shape[1]] = img[0].reshape(self.in_shape) + else: + out[x_idx * self.in_shape[0]:(x_idx + 1) * self.in_shape[0], y_idx * self.in_shape[1]:(y_idx + 1) * self.in_shape[1]] = img[0].reshape([self.in_shape[0], self.in_shape[1], self.colorspace_dim]) + plt.imsave(img_dir + str(step) + '.png', out, cmap='hsv' if self.colorspace_dim == 3 else 'gray') + + def train(self, dataset, img_size=64, num_steps=5000, d_steps=1): + z_mus = [np.random.uniform(-1, 1, [1, self.n_latent]) for _ in range(400)] + print('Generated samples') + num_epochs = 0 + for step in range(num_steps): + if step % 10 == 0: + print('Step:', step) + batch_x, new_epoch = dataset.next_batch(self.batch_size) + batch_x = batch_x.reshape((self.batch_size, self.ndims * self.colorspace_dim)) + batch_x = (batch_x - 0.5) / 0.5 + for _ in range(d_steps): + batch_z = np.random.uniform(-1, 1, [self.batch_size, self.n_latent]) + self.session.run(self.d_optimizer, feed_dict={self.x_placeholder: batch_x, self.z_placeholder: batch_z}) + batch_z = np.random.uniform(-1, 1, [self.batch_size, self.n_latent]) + self.session.run(self.g_optimizer, feed_dict={self.z_placeholder: batch_z}) + if new_epoch: + num_epochs += 1 + if num_epochs % 5 == 0: + self.generate_file(z_mus, step) diff --git a/dataset/preprocessed/472.txt b/dataset/preprocessed/472.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/472.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/474.txt b/dataset/preprocessed/474.txt new file mode 100644 index 0000000..2b03454 --- /dev/null +++ b/dataset/preprocessed/474.txt @@ -0,0 +1,65 @@ +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.01) + return tf.Variable(initial) + + +def bias_variable(shape): + initial = tf.constant(0.01, shape=shape) + return tf.Variable(initial) + + +def conv2d(x, W, stride): + return tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +def createNetwork(action_count): + W_conv1 = weight_variable([8, 8, 4, 32]) + b_conv1 = bias_variable([32]) + W_conv2 = weight_variable([4, 4, 32, 64]) + b_conv2 = bias_variable([64]) + W_conv3 = weight_variable([3, 3, 64, 64]) + b_conv3 = bias_variable([64]) + W_fc1 = weight_variable([1600, 512]) + b_fc1 = bias_variable([512]) + W_fc2 = weight_variable([512, action_count]) + b_fc2 = bias_variable([action_count]) + s = tf.placeholder('float', [None, 80, 80, 4]) + h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + return s, readout, h_fc1 + + +class DQNAgent(object): + + def __init__(self, action_count): + self.action_count = action_count + self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + self.action_placeholder = tf.placeholder('float', [None, action_count]) + self.reward_placeholder = tf.placeholder('float', [None]) + self.readout_action = tf.reduce_sum(tf.mul(self.readout, self.action_placeholder), reduction_indices=1) + self.cost = tf.reduce_mean(tf.square(self.reward_placeholder - self.readout_action)) + self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.cost) + + def act(self, session, state): + scores = self.score_actions(session, [state])[0] + return np.argmax(scores) + + def score_actions(self, session, state_batch): + return session.run(self.readout, feed_dict={self.state_placeholder: state_batch}) + + def train(self, session, reward_batch, action_batch, state_batch): + _, loss = session.run([self.train_step, self.cost], feed_dict={self.reward_placeholder: reward_batch, self.action_placeholder: action_batch, self.state_placeholder: state_batch}) + return loss diff --git a/dataset/preprocessed/475.txt b/dataset/preprocessed/475.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/475.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/476.txt b/dataset/preprocessed/476.txt new file mode 100644 index 0000000..945d200 --- /dev/null +++ b/dataset/preprocessed/476.txt @@ -0,0 +1,319 @@ +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data + + +def constantTest(): + m1 = tf.constant([[3, 3]]) + m2 = tf.constant([[2], [3]]) + predect = tf.matmul(m1, m2) + print(predect) + with tf.Session() as sess: + print(sess.run(predect)) + + +def variableTest(): + a = tf.Variable(1.0) + newValue = tf.add(a, 1) + update = tf.assign(a, newValue) + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + print(sess.run(a)) + sess.run(update) + print(sess.run(a)) + + +def fetchTest(): + input1 = tf.constant(3.0) + input2 = tf.constant(2.0) + input3 = tf.constant(5.0) + intermed = tf.add(input2, input3) + with tf.Session() as sess: + result = sess.run([input1, intermed]) + print(result) + + +def feedTest(): + input1 = tf.placeholder(tf.float32) + input2 = tf.placeholder(tf.float32) + output = tf.add(input1, input2) + with tf.Session() as sess: + print(sess.run([output], feed_dict={input1: [7.0], input2: [2.0]})) + + +def test4liner(): + x_data = np.random.rand(100) + y_data = x_data * 0.1 + 0.2 + b = tf.Variable(0.0) + k = tf.Variable(0.0) + y = k * x_data + b + with tf.name_scope('loss'): + loss = tf.reduce_mean(tf.square(y - y_data)) + with tf.name_scope('train'): + optimizer = tf.train.GradientDescentOptimizer(0.2) + train = optimizer.minimize(loss) + init = tf.global_variables_initializer() + with tf.Session() as sess: + writer = tf.summary.FileWriter('logs', sess.graph) + sess.run(init) + for i in range(100): + sess.run(train) + if i % 10 == 0: + print(sess.run([k, b])) + + +def test4regression(): + x_data = np.linspace(-0.5, 0.5, 200)[:, (np.newaxis)] + noise = np.random.normal(0, 0.02, x_data.shape) + y_data = np.square(x_data) + noise + x = tf.placeholder(tf.float32, [None, 1]) + y = tf.placeholder(tf.float32, [None, 1]) + Weights_L1 = tf.Variable(tf.random_normal([1, 10])) + biases_L1 = tf.Variable(0.0) + Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + L1 = tf.nn.tanh(Wx_plus_b_L1) + Weights_L2 = tf.Variable(tf.random_normal([10, 1])) + biases_L2 = tf.Variable(tf.zeros([1, 1])) + Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + prediction = tf.nn.tanh(Wx_plus_b_L2) + loss = tf.reduce_mean(tf.square(y - prediction)) + train = tf.train.GradientDescentOptimizer(0.2).minimize(loss) + with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + for i in range(2000): + sess.run(train, feed_dict={x: x_data, y: y_data}) + prediction_value = sess.run(prediction, feed_dict={x: x_data}) + plt.figure() + plt.scatter(x_data, y_data) + plt.plot(x_data, prediction_value, 'r-') + plt.show() + + +def test4classifyMnist(): + mnist = input_data.read_data_sets('mnist_data', one_hot=True) + batch_size = 100 + n_batch = mnist.train.num_examples // batch_size + x = tf.placeholder(tf.float32, [None, 784]) + y = tf.placeholder(tf.float32, [None, 10]) + Weights_L1 = tf.Variable(tf.random_normal([784, 100])) + biases_L1 = tf.Variable(tf.zeros([100]) + 0.1) + Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + L1 = tf.nn.sigmoid(Wx_plus_b_L1) + Weights_L2 = tf.Variable(tf.random_normal([100, 100])) + biases_L2 = tf.Variable(tf.zeros([100])) + Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + L2 = tf.nn.sigmoid(Wx_plus_b_L2) + Weights_L3 = tf.Variable(tf.random_normal([100, 10])) + biases_L3 = tf.Variable(tf.zeros([10])) + Wx_plus_b_L3 = tf.matmul(L2, Weights_L3) + biases_L3 + prediction = tf.nn.softmax(Wx_plus_b_L3) + loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction) + train = tf.train.GradientDescentOptimizer(0.2).minimize(loss) + correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + for i in range(20): + for batch in range(n_batch): + batch_xs, batch_ys = mnist.train.next_batch(batch_size) + sess.run(train, feed_dict={x: batch_xs, y: batch_ys}) + acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}) + print('Iter ' + str(i) + ',Testing Accuracy ' + str(acc)) + + +def test4fitness2dropout(): + mnist = input_data.read_data_sets('mnist_data', one_hot=True) + batch_size = 100 + n_batch = mnist.train.num_examples // batch_size + keep_prob = tf.placeholder(tf.float32) + x = tf.placeholder(tf.float32, [None, 784]) + y = tf.placeholder(tf.float32, [None, 10]) + Weights_L1 = tf.Variable(tf.zeros([784, 100])) + biases_L1 = tf.Variable(tf.zeros([100]) + 0.1) + Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + L1 = tf.nn.sigmoid(Wx_plus_b_L1) + L1 = tf.nn.dropout(L1, keep_prob) + Weights_L2 = tf.Variable(tf.zeros([100, 100])) + biases_L2 = tf.Variable(tf.zeros([100])) + Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + L2 = tf.nn.sigmoid(Wx_plus_b_L2) + L2 = tf.nn.dropout(L2, keep_prob) + Weights_L3 = tf.Variable(tf.zeros([100, 10])) + biases_L3 = tf.Variable(tf.zeros([10])) + Wx_plus_b_L3 = tf.matmul(L2, Weights_L3) + biases_L3 + prediction = tf.nn.softmax(Wx_plus_b_L3) + loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction) + train = tf.train.AdamOptimizer().minimize(loss) + correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + for i in range(20): + for batch in range(n_batch): + batch_xs, batch_ys = mnist.train.next_batch(batch_size) + sess.run(train, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 0.7}) + acc_test = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0}) + acc_train = sess.run(accuracy, feed_dict={x: mnist.train.images, y: mnist.train.labels, keep_prob: 1.0}) + print('Iter ' + str(i) + ',Testing Accuracy ' + str(acc_test) + ',Training Accuracy ' + str(acc_train)) + + +def test4tensorboard(): + + def variable_summaries(var): + with tf.name_scope('summaries'): + mean = tf.reduce_mean(var) + tf.summary.scalar('mean', mean) + with tf.name_scope('stddex'): + stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) + tf.summary.scalar('stddev', stddev) + tf.summary.scalar('max', tf.reduce_max(var)) + tf.summary.scalar('max', tf.reduce_min(var)) + tf.summary.histogram('histogram', var) + mnist = input_data.read_data_sets('mnist_data', one_hot=True) + batch_size = 100 + n_batch = mnist.train.num_examples // batch_size + keep_prob = tf.placeholder(tf.float32) + x = tf.placeholder(tf.float32, [None, 784]) + y = tf.placeholder(tf.float32, [None, 10]) + Weights_L1 = tf.Variable(tf.zeros([784, 100])) + biases_L1 = tf.Variable(tf.zeros([100]) + 0.1) + variable_summaries(Weights_L1) + variable_summaries(biases_L1) + Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1 + L1 = tf.nn.sigmoid(Wx_plus_b_L1) + L1 = tf.nn.dropout(L1, keep_prob) + Weights_L2 = tf.Variable(tf.zeros([100, 100])) + biases_L2 = tf.Variable(tf.zeros([100])) + Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2 + L2 = tf.nn.sigmoid(Wx_plus_b_L2) + L2 = tf.nn.dropout(L2, keep_prob) + Weights_L3 = tf.Variable(tf.zeros([100, 10])) + biases_L3 = tf.Variable(tf.zeros([10])) + Wx_plus_b_L3 = tf.matmul(L2, Weights_L3) + biases_L3 + prediction = tf.nn.softmax(Wx_plus_b_L3) + loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction) + tf.summary.scalar('loss', tf.reduce_mean(loss)) + train = tf.train.AdamOptimizer().minimize(loss) + correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + tf.summary.scalar('accuracy', tf.reduce_mean(accuracy)) + merged = tf.summary.merge_all() + with tf.Session() as sess: + writer = tf.summary.FileWriter('logs', sess.graph) + sess.run(tf.global_variables_initializer()) + for i in range(20): + for batch in range(n_batch): + batch_xs, batch_ys = mnist.train.next_batch(batch_size) + sess.run(train, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 0.7}) + summary, acc_test = sess.run([merged, accuracy], feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0}) + writer.add_summary(summary, i) + acc_train = sess.run(accuracy, feed_dict={x: mnist.train.images, y: mnist.train.labels, keep_prob: 1.0}) + print('Iter ' + str(i) + ',Testing Accuracy ' + str(acc_test) + ',Training Accuracy ' + str(acc_train)) + + +def test4cnn(): + """cnn,mnist图像的卷积操作""" + mnist = input_data.read_data_sets('mnist_data', one_hot=True) + batch_size = 100 + n_batch = mnist.train.num_examples // batch_size + + 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) + + def conv2d(x, W): + """ + padding 补零操作,参数有 "SAME", "VALID" + strides + strides[0] = 1,也即在 batch 维度上的移动为 1,也就是不跳过任何一个样本,否则当初也不该把它们作为输入(input) + strides[3] = 1,也即在 channels 维度上的移动为 1,也就是不跳过任何一个颜色通道;1为灰度图,3为彩图 + strides[1],strides[2] 代表沿着x或者y的方向,每步移动的距离 + """ + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + def max_pool_2x2(x): + """ + max_pool:最大值采样 + ksize + ksize[0] = 1,也即在 batch 维度上的移动为 1,也就是不跳过任何一个样本,否则当初也不该把它们作为输入(input) + ksize[3] = 1,也即在 channels 维度上的移动为 1,也就是不跳过任何一个颜色通道; + ksize[1],ksize[2] 代表沿着x或者y的方向,每步移动的距离 + """ + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + x = tf.placeholder(tf.float32, [None, 784]) + y = tf.placeholder(tf.float32, [None, 10]) + x_image = tf.reshape(x, [-1, 28, 28, 1]) + W_convl = weight_variable([5, 5, 1, 32]) + b_convl = bias_variable([32]) + h_conv1 = tf.nn.relu(conv2d(x_image, W_convl) + b_convl) + h_pool1 = max_pool_2x2(h_conv1) + W_conv2 = weight_variable([5, 5, 32, 64]) + b_conv2 = bias_variable([64]) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = max_pool_2x2(h_conv2) + W_fcl = weight_variable([7 * 7 * 64, 1024]) + b_fcl = bias_variable([1024]) + h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + h_fcl = tf.nn.relu(tf.matmul(h_pool2_flat, W_fcl) + b_fcl) + keep_prob = tf.placeholder(tf.float32) + h_fcl_drop = tf.nn.dropout(h_fcl, keep_prob) + W_fc2 = weight_variable([1024, 10]) + b_fc2 = bias_variable([10]) + prediction = tf.nn.softmax(tf.matmul(h_fcl_drop, W_fc2) + b_fc2) + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction)) + train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + correct_prediction = tf.equal(tf.arg_max(prediction, 1), tf.arg_max(y, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + for i in range(20): + for batch in range(n_batch): + batch_xs, batch_ys = mnist.train.next_batch(batch_size) + sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 0.7}) + acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0}) + print('Iter ' + str(i) + ',Testing Accuracy= ' + str(acc)) + + +def test4rnnLstm(): + mnist = input_data.read_data_sets('mnist_data', one_hot=True) + n_inputs = 28 + max_time = 28 + lstm_size = 100 + n_classes = 10 + batch_size = 50 + n_batch = mnist.train.num_examples // batch_size + x = tf.placeholder(tf.float32, [None, 784]) + y = tf.placeholder(tf.float32, [None, 10]) + weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1)) + biases = tf.Variable(tf.constant(0.1, shape=[n_classes])) + + def RNN(X, weights, biases): + inputs = tf.reshape(X, [-1, max_time, n_inputs]) + lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size) + outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, inputs, dtype=tf.float32) + results = tf.nn.softmax(tf.matmul(final_state[1], weights) + biases) + return results + prediction = RNN(x, weights, biases) + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)) + train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + init = tf.global_variables_initializer() + with tf.Session() as sess: + sess.run(init) + for epoch in range(6): + for batch in range(n_batch): + batch_xs, batch_ys = mnist.train.next_batch(batch_size) + sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys}) + acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}) + print('Iter' + str(epoch) + ',Testing Accuracy= ' + str(acc)) + + +if __name__ == '__main__': + test4tensorboard() diff --git a/dataset/preprocessed/477.txt b/dataset/preprocessed/477.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/477.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/48.txt b/dataset/preprocessed/48.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/48.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/480.txt b/dataset/preprocessed/480.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/480.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/481.txt b/dataset/preprocessed/481.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/481.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/482.txt b/dataset/preprocessed/482.txt new file mode 100644 index 0000000..bfb803a --- /dev/null +++ b/dataset/preprocessed/482.txt @@ -0,0 +1,135 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +from six.moves import range +from time import time +from read_data import * +train_subset = 100000 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 801 + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +""" +with tf.Session(graph=graph) as session: + # This is a one-time operation which ensures the parameters get initialized as + # we described in the graph: random weights for the matrix, zeros for the + # biases. + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + # Run the computations. We tell .run() that we want to run the optimizer, + # and get the loss value and the training predictions returned as numpy + # arrays. + _, l, predictions = session.run([optimizer, loss, train_prediction]) + if (step % 100 == 0): + print('Loss at step %d: %f' % (step, l)) + print('Training accuracy: %.1f%%' % accuracy( + predictions, train_labels[:train_subset, :])) + # Calling .eval() on valid_prediction is basically like calling run(), but + # just to get that one numpy array. Note that it recomputes all its graph + # dependencies. + print('Validation accuracy: %.1f%%' % accuracy( + valid_prediction.eval(), valid_labels)) + print(' +') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +""" +print('') +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +print('') + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + output1 = tf.matmul(data, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/483.txt b/dataset/preprocessed/483.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/483.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/484.txt b/dataset/preprocessed/484.txt new file mode 100644 index 0000000..34cee1b --- /dev/null +++ b/dataset/preprocessed/484.txt @@ -0,0 +1,107 @@ +import numpy as np +import tensorflow as tf +""" +code for running tests of each model + +import numpy as np +X = np.random.randn(10000, 5) +Y = np.random.randn(10000, 1) + 0.5 * X[:, 0:1] - 0.25 * X[:, 1:2] +m = linReg(X[0::2, :], Y[0::2], X[1::2, :], Y[1::2]) + +""" + + +def linReg(Xtrain, Ytrain, Xtest, Ytest): + """ + Linear regression built in tensor flow + """ + numIter = 5000 + dim = Xtrain.shape[1] + totalTrainVar = np.mean(Ytrain ** 2) + totalTestVar = np.mean(Ytest ** 2) + with tf.Session() as sess: + x = tf.placeholder('float', shape=[None, dim]) + y_ = tf.placeholder('float', shape=[None, 1]) + W = tf.Variable(tf.random_uniform([dim, 1])) + b = tf.Variable(tf.random_uniform([1, 1])) + y = tf.matmul(x, W) + b + mse = tf.reduce_mean(tf.square(y_ - y)) + sess.run(tf.initialize_all_variables()) + train_step = tf.train.AdamOptimizer().minimize(mse) + sess.run(tf.initialize_all_variables()) + for i in range(numIter): + train_step.run(feed_dict={x: Xtrain, y_: Ytrain}, session=sess) + if i % 500 == 0: + trainError = mse.eval(feed_dict={x: Xtrain, y_: Ytrain}, session=sess) + trainR2 = (totalTrainVar - trainError) / totalTrainVar + print('Train r2: {:.2f}'.format(trainR2)) + testError = mse.eval(feed_dict={x: Xtest, y_: Ytest}, session=sess) + testR2 = (totalTestVar - testError) / totalTestVar + print('Test r2: {:.2f}'.format(testR2)) + statModel = [W.eval(session=sess), b.eval(session=sess), testR2] + return statModel + + +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) + + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +def convNet(Xtrain, Ytrain, Xtest, Ytest): + """ + Convolutional netowrk built in tensor flow + """ + numIter = 50000 + dim = Xtrain.shape[1] + imageDim = int(np.sqrt(dim)) + totalTrainVar = np.mean(Ytrain ** 2) + totalTestVar = np.mean(Ytest ** 2) + with tf.Session() as sess: + x = tf.placeholder('float', shape=[None, dim]) + y_ = tf.placeholder('float', shape=[None, 1]) + x_image = tf.reshape(x, [-1, imageDim, imageDim, 1]) + W_conv1 = weight_variable([10, 10, 1, 32]) + b_conv1 = bias_variable([32]) + h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + W_conv2 = weight_variable([10, 10, 32, 64]) + b_conv2 = bias_variable([64]) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = max_pool_2x2(h_conv2) + W_fc1 = weight_variable([7 * 7 * 64, 1024]) + b_fc1 = bias_variable([1024]) + h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + keep_prob = tf.placeholder('float') + h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + W_fc2 = weight_variable([1024, 1]) + b_fc2 = bias_variable([1]) + y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) + mse = tf.reduce_mean(tf.square(y_ - y_conv)) + sess.run(tf.initialize_all_variables()) + train_step = tf.train.AdamOptimizer(0.0001).minimize(mse) + sess.run(tf.initialize_all_variables()) + dataDict = {x: Xtrain, y_: Ytrain, keep_prob: 0.8} + for i in range(numIter): + train_step.run(feed_dict=dataDict, session=sess) + if i % 1000 == 0: + trainError = mse.eval(feed_dict=dataDict, session=sess) + trainR2 = (totalTrainVar - trainError) / totalTrainVar + print('Train r2: {:.2f}'.format(trainR2)) + testError = mse.eval(feed_dict={x: Xtest, y_: Ytest, keep_prob: 1.0}, session=sess) + testR2 = (totalTestVar - testError) / totalTestVar + print('Test r2: {:.2f}'.format(testR2)) + return testR2 diff --git a/dataset/preprocessed/485.txt b/dataset/preprocessed/485.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/485.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/486.txt b/dataset/preprocessed/486.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/486.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/487.txt b/dataset/preprocessed/487.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/487.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/488.txt b/dataset/preprocessed/488.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/488.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/489.txt b/dataset/preprocessed/489.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/489.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/49.txt b/dataset/preprocessed/49.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/49.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/490.txt b/dataset/preprocessed/490.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/490.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/491.txt b/dataset/preprocessed/491.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/491.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/492.txt b/dataset/preprocessed/492.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/492.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/493.txt b/dataset/preprocessed/493.txt new file mode 100644 index 0000000..ceada19 --- /dev/null +++ b/dataset/preprocessed/493.txt @@ -0,0 +1,367 @@ +from __future__ import print_function +from __future__ import absolute_import +from __future__ import division +import sys +if sys.version[0] == '2': + import cPickle as pkl +else: + import pickle as pkl +import numpy as np +import tensorflow as tf +import utils +dtype = utils.DTYPE + + +class Model: + + def __init__(self): + self.sess = None + self.X = None + self.y = None + self.layer_keeps = None + self.vars = None + self.keep_prob_train = None + self.keep_prob_test = None + + def run(self, fetches, X=None, y=None, mode='train'): + feed_dict = {} + if type(self.X) is list: + for i in range(len(X)): + feed_dict[self.X[i]] = X[i] + else: + feed_dict[self.X] = X + if y is not None: + feed_dict[self.y] = y + if self.layer_keeps is not None: + if mode == 'train': + feed_dict[self.layer_keeps] = self.keep_prob_train + elif mode == 'test': + feed_dict[self.layer_keeps] = self.keep_prob_test + return self.sess.run(fetches, feed_dict) + + def dump(self, model_path): + var_map = {} + for name, var in self.vars.iteritems(): + var_map[name] = self.run(var) + pkl.dump(var_map, open(model_path, 'wb')) + print('model dumped at', model_path) + + +class LR(Model): + + def __init__(self, input_dim=None, output_dim=1, init_path=None, opt_algo='gd', learning_rate=0.01, l2_weight=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + b = self.vars['b'] + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=logits)) + l2_weight * tf.nn.l2_loss(xw) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FM(Model): + + def __init__(self, input_dim=None, output_dim=1, factor_order=10, init_path=None, opt_algo='gd', learning_rate=0.01, l2_w=0, l2_v=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + v = self.vars['v'] + b = self.vars['b'] + X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + l2_w * tf.nn.l2_loss(xw) + l2_v * tf.nn.l2_loss(xv) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FNN(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + print('num_inputs:{0}\\t\tlayer_size:{1}'.format(num_inputs, layer_sizes)) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + node_in = num_inputs * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + print('init_vars:', init_vars) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + print('第{0}个隐藏层l.shape, wi.shape, bi.shape'.format(i), l.shape, wi.shape, bi.shape) + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class CCPM(Model): + + def __init__(self, field_sizes=None, embed_size=10, filter_sizes=None, layer_acts=None, drop_out=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + init_vars.append(('b1', [1], 'zero', dtype)) + print('init_vars: ', init_vars) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + f1 = self.vars['f1'] + l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + f2 = self.vars['f2'] + l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + b1 = self.vars['b1'] + l = tf.matmul(l, w1) + b1 + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN1(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + l = tf.concat([xw, ip], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN2(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None, layer_norm=True): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + node_in = num_inputs * embed_size + embed_size * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + z = tf.reduce_sum(tf.reshape(xw, [-1, num_inputs, embed_size]), 1) + op = tf.reshape(tf.matmul(tf.reshape(z, [-1, embed_size, 1]), tf.reshape(z, [-1, 1, embed_size])), [-1, embed_size * embed_size]) + if layer_norm: + p_mean, p_var = tf.nn.moments(op, [1], keep_dims=True) + op = (op - p_mean) / tf.sqrt(p_var) + p_g = tf.Variable(tf.ones([embed_size ** 2]), name='p_g') + p_b = tf.Variable(tf.zeros([embed_size ** 2]), name='p_b') + op = op * p_g + p_b + l = tf.concat([xw, op], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(tf.concat(w0, 0)) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + z = tf.reduce_sum(tf.reshape(xw, [-1, num_inputs, embed_size]), 1) + op = tf.reshape(tf.matmul(tf.reshape(z, [-1, embed_size, 1]), tf.reshape(z, [-1, 1, embed_size])), [-1, embed_size * embed_size]) + l = tf.concat([xw, ip, op], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) diff --git a/dataset/preprocessed/494.txt b/dataset/preprocessed/494.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/494.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/495.txt b/dataset/preprocessed/495.txt new file mode 100644 index 0000000..e3c9995 --- /dev/null +++ b/dataset/preprocessed/495.txt @@ -0,0 +1,224 @@ +import tensorflow as tf +import numpy as np +import layers_normal as nn + + +def dihedral(x, i): + if len(x.shape) == 3: + if i & 4: + y = np.transpose(x, (1, 0, 2)) + else: + y = x.copy() + if i & 3 == 0: + return y + if i & 3 == 1: + return y[:, ::-1] + if i & 3 == 2: + return y[::-1, :] + if i & 3 == 3: + return y[::-1, ::-1] + if len(x.shape) == 4: + if i & 4: + y = np.transpose(x, (0, 2, 1, 3)) + else: + y = x.copy() + if i & 3 == 0: + return y + if i & 3 == 1: + return y[:, :, ::-1] + if i & 3 == 2: + return y[:, ::-1, :] + if i & 3 == 3: + return y[:, ::-1, ::-1] + + +def summary_images(x, name): + for i in range(min(4, x.get_shape().as_list()[3])): + tf.summary.image('{}-{}'.format(name, i), x[:, :, :, i:i + 1]) + + +class CNN: + + def __init__(self): + self.tfx = None + self.tfy = None + self.tfp = None + self.xent = None + self.tftrain1 = None + self.tftrain2 = None + self.tftrain_all = None + self.tfkp = None + self.tfacc = None + self.train_counter = 0 + self.test = None + self.embedding_input = None + + def NN1(self, x): + assert x.get_shape().as_list()[:3] == [None, 101, 101] + x = nn.convolution(x, 16, w=4) + x = nn.convolution(x) + x = nn.max_pool(x) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 48, 48, 16] + x = nn.convolution(x, 32) + x = nn.convolution(x) + x = nn.max_pool(x) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 22, 22, 32] + x = nn.convolution(x, 64) + x = nn.convolution(x) + x = nn.max_pool(x) + x = nn.batch_normalization(x, self.tfacc) + x = tf.nn.dropout(x, self.tfkp) + assert x.get_shape().as_list() == [None, 9, 9, 64] + x = nn.convolution(x, 128) + x = tf.nn.dropout(x, self.tfkp) + x = nn.convolution(x) + summary_images(x, 'nn1') + x = nn.batch_normalization(x, self.tfacc) + x = tf.nn.dropout(x, self.tfkp) + assert x.get_shape().as_list() == [None, 5, 5, 128] + x = nn.convolution(x, 1024, w=5) + assert x.get_shape().as_list() == [None, 1, 1, 1024] + x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + x = tf.nn.dropout(x, self.tfkp) + x = nn.fullyconnected(x, 1024) + x = tf.nn.dropout(x, self.tfkp) + x = nn.fullyconnected(x, 1024) + x = nn.batch_normalization(x, self.tfacc) + self.test = x + x = nn.fullyconnected(x, 2, activation=None) + return x + + def NN2(self, x): + assert x.get_shape().as_list()[:3] == [None, 101, 101] + x = x[:, 28:73, 28:73, :] + assert x.get_shape().as_list()[:3] == [None, 45, 45] + x = nn.convolution(x, 16, w=5) + x = nn.convolution(x, 19, w=5) + x = nn.convolution(x, 23, w=5) + x = nn.batch_normalization(x, self.tfacc) + x = nn.convolution(x, 27, w=5) + x = nn.convolution(x, 33, w=5) + x = nn.convolution(x, 39, w=5) + x = nn.batch_normalization(x, self.tfacc) + x = nn.convolution(x, 47, w=5) + x = nn.convolution(x, 57, w=5) + x = nn.convolution(x, 68, w=5) + x = nn.batch_normalization(x, self.tfacc) + x = nn.convolution(x, 82, w=5) + summary_images(x, 'nn2') + assert x.get_shape().as_list() == [None, 5, 5, 82] + x = tf.nn.dropout(x, self.tfkp) + x = nn.convolution(x, 256, w=5) + assert x.get_shape().as_list() == [None, 1, 1, 256] + x = tf.reshape(x, [-1, 256]) + x = tf.nn.dropout(x, self.tfkp) + x = nn.fullyconnected(x, 512) + x = tf.nn.dropout(x, self.tfkp) + x = nn.fullyconnected(x, 512) + x = nn.batch_normalization(x, self.tfacc) + x = nn.fullyconnected(x, 2, activation=None) + return x + + def create_architecture(self, bands): + self.tfkp = tf.placeholder_with_default(tf.constant(1.0, tf.float32), [], name='kp') + self.tfacc = tf.placeholder_with_default(tf.constant(0.0, tf.float32), [], name='acc') + x = self.tfx = tf.placeholder(tf.float32, [None, 101, 101, bands], name='input') + summary_images(x, 'input') + with tf.name_scope('nn1'): + x1 = self.NN1(x) + tv1 = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) + with tf.name_scope('nn2'): + x2 = self.NN2(x) + tv2 = [x for x in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) if not x in tv1] + with tf.name_scope('nn3'): + x = tf.concat([x1, x2], 1) + self.embedding_input = x + x = nn.fullyconnected(x, 8) + x = nn.fullyconnected(x, 8) + x = nn.fullyconnected(x, 1, activation=None) + tv_join = [x for x in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) if not x in tv1 and not x in tv2] + assert x.get_shape().as_list() == [None, 1] + self.tfp = tf.nn.sigmoid(tf.reshape(x, [-1])) + with tf.name_scope('xent'): + self.tfy = tf.placeholder(tf.float32, [None]) + xent = tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=tf.reshape(self.tfy, [-1, 1])) + self.xent = tf.reduce_mean(xent) + with tf.name_scope('train'): + self.tftrain1 = tf.train.AdamOptimizer(0.0001).minimize(self.xent, var_list=tv1 + tv_join) + self.tftrain2 = tf.train.AdamOptimizer(0.0001).minimize(self.xent, var_list=tv2 + tv_join) + self.tftrain_all = tf.train.AdamOptimizer(0.0001).minimize(self.xent, var_list=tv1 + tv2 + tv_join) + + @staticmethod + def split_test_train(path): + import os + files = ['{}/{}'.format(path, f) for f in sorted(os.listdir(path))] + return files[:3000], files[3000:] + + @staticmethod + def load(files): + xs = np.stack([np.load(f)['image'] for f in files]) + return CNN.prepare(xs) + + @staticmethod + def prepare(images): + images[images == 100] = 0.0 + if images.shape[-1] == 1: + images = (images - 4.337e-13) / 5.504e-12 + elif images.shape[-1] == 4: + images = (images - 1.685e-12) / 5.122e-11 + else: + print('No statistics to prepare this kind of data') + return images + + @staticmethod + def batch(files, labels): + id0 = np.where(labels == 0)[0] + id1 = np.where(labels == 1)[0] + k = 15 + idn = np.random.choice(id0, k, replace=False) + idp = np.random.choice(id1, k, replace=False) + xs = CNN.load([files[i] for i in idp] + [files[i] for i in idn]) + ys = np.concatenate((labels[idp], labels[idn])) + for i in range(len(xs)): + s = np.random.uniform(0.8, 1.2) + u = np.random.uniform(-0.1, 0.1) + xs[i] = dihedral(xs[i], np.random.randint(8)) * s + u + return xs, ys + + def train(self, session, xs, ys, options=None, run_metadata=None, tensors=None): + if tensors is None: + tensors = [] + if self.train_counter < 10000: + train = self.tftrain1 + x = self.train_counter + elif self.train_counter < 20000: + train = self.tftrain2 + x = self.train_counter - 10000 + else: + train = self.tftrain_all + x = self.train_counter - 20000 + acc = 0.6 ** (x / 1000.0) + kp = 0.5 + 0.5 * 0.5 ** (x / 2000.0) + output = session.run([train, self.xent] + tensors, feed_dict={self.tfx: xs, self.tfy: ys, self.tfkp: kp, self.tfacc: acc}, options=options, run_metadata=run_metadata) + self.train_counter += 1 + return output[1], output[2:] + + def predict_naive(self, session, images): + return session.run(self.tfp, feed_dict={self.tfx: images}) + + def predict_naive_xentropy(self, session, images, labels): + return session.run([self.tfp, self.xent], feed_dict={self.tfx: images, self.tfy: labels}) + + def predict(self, session, images): + ps = self.predict_naive(session, images) + for i in range(1, 8): + ps *= self.predict_naive(session, dihedral(images, i)) + return ps + + def predict_xentropy(self, session, images, labels): + ps, xent = self.predict_naive_xentropy(session, images, labels) + for i in range(1, 8): + ps *= self.predict_naive(session, dihedral(images, i)) + return ps, xent diff --git a/dataset/preprocessed/496.txt b/dataset/preprocessed/496.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/496.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/497.txt b/dataset/preprocessed/497.txt new file mode 100644 index 0000000..2d0a6d0 --- /dev/null +++ b/dataset/preprocessed/497.txt @@ -0,0 +1,83 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) + image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +batch_size = 128 +hidden_nodes_1 = 1024 +hidden_nodes_2 = 512 +hidden_nodes_3 = 1024 +learning_rate = 0.0001 +beta = 0.005 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + keep_prob = tf.placeholder(tf.float32) + weights_1 = tf.Variable(tf.random_normal([image_size * image_size, hidden_nodes_1])) + biases_1 = tf.Variable(tf.zeros([hidden_nodes_1])) + weights_2 = tf.Variable(tf.random_normal([hidden_nodes_1, hidden_nodes_2])) + biases_2 = tf.Variable(tf.zeros([hidden_nodes_2])) + weights_3 = tf.Variable(tf.random_normal([hidden_nodes_2, hidden_nodes_3])) + biases_3 = tf.Variable(tf.zeros([hidden_nodes_3])) + weights_out = tf.Variable(tf.random_normal([hidden_nodes_3, num_labels])) + biases_out = tf.Variable(tf.zeros([num_labels])) + + def forward_prop(input): + h1 = tf.nn.dropout(tf.nn.relu(tf.matmul(input, weights_1) + biases_1), keep_prob) + h2 = tf.nn.dropout(tf.nn.relu(tf.matmul(h1, weights_2) + biases_2), keep_prob) + h3 = tf.nn.dropout(tf.nn.relu(tf.matmul(h2, weights_3) + biases_3), keep_prob) + return tf.matmul(h3, weights_out) + biases_out + logits = forward_prop(tf_train_dataset) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss += beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_out)) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(forward_prop(tf_valid_dataset)) + test_prediction = tf.nn.softmax(forward_prop(tf_test_dataset)) +num_steps = 10001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 1.0} + feed_dict_w_drop = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict_w_drop) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(feed_dict=feed_dict), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(feed_dict=feed_dict), test_labels)) diff --git a/dataset/preprocessed/498.txt b/dataset/preprocessed/498.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/498.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/499.txt b/dataset/preprocessed/499.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/499.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/5.txt b/dataset/preprocessed/5.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/5.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/50.txt b/dataset/preprocessed/50.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/50.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/500.txt b/dataset/preprocessed/500.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/500.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/501.txt b/dataset/preprocessed/501.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/501.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/502.txt b/dataset/preprocessed/502.txt new file mode 100644 index 0000000..a8863ff --- /dev/null +++ b/dataset/preprocessed/502.txt @@ -0,0 +1,150 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +def logic_L2_reg(num_steps=3001, batch_size=128, beta=0.01): + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('-------------------------------------------------------------------------------------------------------------') + print('Logic Unit-SGD-L2 Regularization') + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +def nn_L2_reg(num_steps=3001, batch_size=128, beta=0.01): + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_input = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + biases_input = tf.Variable(tf.zeros([1024])) + weights_layer1 = tf.Variable(tf.truncated_normal([1024, num_labels])) + biases_layer2 = tf.Variable(tf.zeros([num_labels])) + layer1 = tf.matmul(tf_train_dataset, weights_input) + biases_input + layer1_relu = tf.nn.relu(layer1) + layer2 = tf.matmul(layer1_relu, weights_layer1) + biases_layer2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=layer2) + beta * (tf.nn.l2_loss(weights_input) + tf.nn.l2_loss(weights_layer1))) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(layer2) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + with tf.Session(graph=graph) as session: + print('-------------------------------------------------------------------------------------------------------------') + print('Single Hidden Layer (Relu) NN') + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +def nn_L2_dropout(num_steps=3001, batch_size=128, keep_prob=0.5, beta=0.01): + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_input = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + biases_input = tf.Variable(tf.zeros([1024])) + weights_layer1 = tf.Variable(tf.truncated_normal([1024, num_labels])) + biases_layer2 = tf.Variable(tf.zeros([num_labels])) + layer1 = tf.matmul(tf_train_dataset, weights_input) + biases_input + layer1_relu = tf.nn.relu(layer1) + layer1_dropout = tf.nn.dropout(layer1_relu, keep_prob) + layer2 = tf.matmul(layer1_dropout, weights_layer1) + biases_layer2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=layer2) + beta * (tf.nn.l2_loss(weights_input) + tf.nn.l2_loss(weights_layer1))) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_input) + biases_input), weights_layer1) + biases_layer2) + with tf.Session(graph=graph) as session: + print('-------------------------------------------------------------------------------------------------------------') + print('Single Hidden Layer (Relu) NN---With Dropout') + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +nn_L2_dropout() diff --git a/dataset/preprocessed/503.txt b/dataset/preprocessed/503.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/503.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/504.txt b/dataset/preprocessed/504.txt new file mode 100644 index 0000000..83e839a --- /dev/null +++ b/dataset/preprocessed/504.txt @@ -0,0 +1,145 @@ +import tensorflow as tf +from tqdm import tqdm +from utils import BatchGenerator, make_dir +import os +import pdb + + +class NNClassifier: + + def _inference(self, X, is_train): + hidden_size = 30 + word_embedding = tf.constant(self._embedding) + embedding_b = tf.nn.embedding_lookup(word_embedding, X[:, (0), :]) + embedding_q = tf.nn.embedding_lookup(word_embedding, X[:, (1), :]) + with tf.variable_scope('Body_GRU'): + with tf.variable_scope('Body_GRU_fw'): + cell_b_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + with tf.variable_scope('Body_GRU_bw'): + cell_b_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + lengths_b = tf.reduce_sum(tf.sign(X[:, (0), :]), axis=-1) + output_b, out_state_b = tf.nn.bidirectional_dynamic_rnn(cell_b_fw, cell_b_fw, embedding_b, sequence_length=lengths_b, dtype=tf.float32) + output_b = tf.concat([output_b[0], output_b[1]], axis=-1, name='output_b') + out_state_b = tf.concat([out_state_b[0], out_state_b[1]], axis=-1) + with tf.variable_scope('Question_GRU'): + with tf.variable_scope('Question_GRU_fw'): + cell_q_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + with tf.variable_scope('Question_GRU_bw'): + cell_q_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + lengths_q = tf.reduce_sum(tf.sign(X[:, (1), :]), axis=-1) + output_q, out_state_q = tf.nn.bidirectional_dynamic_rnn(cell_q_fw, cell_q_fw, embedding_q, sequence_length=lengths_q, dtype=tf.float32) + output_q = tf.concat([output_q[0], output_q[1]], axis=-1) + out_state_q = tf.concat([out_state_q[0], out_state_q[1]], axis=-1, name='out_state_q') + similarity = tf.matmul(output_b, tf.reshape(out_state_q, [tf.shape(X)[0], -1, 1])) + mask = tf.reshape(tf.cast(tf.sign(X[:, (0), :]), dtype=tf.float32), [tf.shape(X)[0], -1, 1], name='mask') + attention_weight = tf.div(tf.exp(similarity) * mask, tf.reshape(tf.reduce_sum(tf.exp(similarity) * mask, axis=1), [tf.shape(X)[0], -1, 1]), name='attention_weight') + attention_b = tf.reduce_sum(attention_weight * output_b, axis=1) + output = tf.concat([out_state_q, attention_b], axis=-1) + output = tf.layers.dense(inputs=output, units=hidden_size) + output = tf.nn.elu(output) + output = tf.layers.dense(inputs=output, units=hidden_size) + output = tf.nn.elu(output) + output = tf.layers.dense(inputs=output, units=hidden_size) + output = tf.nn.elu(output) + dense = tf.layers.dense(inputs=output, units=self._n_classes) + return dense + + def _iter(self, X, y, tensor_loss, train_op, placeholder, metric_tensors): + self._session.run(tf.local_variables_initializer()) + metric_scores = {} + for metric in self._metrics: + metric_scores[metric] = 0 + batch_generator = BatchGenerator(X, y, self._batch_size) + for b in tqdm(range(X.shape[0] // self._batch_size + 1)): + batch = next(batch_generator) + feed_dict = {placeholder['x']: batch['x'], placeholder['y']: batch['y']} + if train_op is not None: + loss, _, metrics = self._session.run([tensor_loss, train_op, metric_tensors], feed_dict=feed_dict) + else: + loss, metrics = self._session.run([tensor_loss, metric_tensors], feed_dict=feed_dict) + if train_op == None: + self._history.append(metrics['accuracy'][0]) + if self._early_stop != None: + self._history[-1] = max(self._history[-1:-self._early_stop - 1:-1]) + summary = tf.Summary() + summary.value.add(tag='loss', simple_value=float(loss)) + print('loss=%f' % loss) + for metric in self._metrics: + score = float(metrics[metric][0]) + summary.value.add(tag=metric, simple_value=score) + print(', %s=%f' % (metric, score), end='') + print('\n', end='') + return summary + + def __init__(self, learning_rate=0.1, batch_size=1, n_iters=10, name='dnn', valid=None, embedding=None, early_stop=None): + self._batch_size = batch_size + self._n_iters = n_iters + self._metrics = {'accuracy': tf.metrics.accuracy} + self._global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') + self._optimizer = tf.train.AdamOptimizer(learning_rate) + self._loss = tf.losses.softmax_cross_entropy + self._name = name + self._valid = valid + self._embedding = embedding + self._history = [] + self._early_stop = early_stop + + def fit(self, X, y): + self._n_classes = y.shape[1] + make_dir(self._name) + make_dir(os.path.join(self._name, 'train')) + make_dir(os.path.join(self._name, 'valid')) + placeholder = {'x': tf.placeholder(tf.int32, shape=(None, X.shape[1], X.shape[2])), 'y': tf.placeholder(tf.int32, shape=(None, y.shape[1]))} + with tf.variable_scope('nn') as scope: + y_prob = self._inference(placeholder['x'], is_train=True) + loss = self._loss(placeholder['y'], y_prob) + reg_const = 0.0005 + l2 = reg_const * sum([tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()]) + loss += l2 + yp = tf.cast(placeholder['y'], tf.float32) + train_op = self._optimizer.minimize(loss, global_step=self._global_step) + metric_tensors = {} + for metric in self._metrics: + y_pred_argmax = tf.argmax(y_prob, axis=-1) + y_true_argmax = tf.argmax(placeholder['y'], axis=-1) + metric_tensors[metric] = self._metrics[metric](y_true_argmax, y_pred_argmax) + gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1) + self._session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) + summary_writer = {'train': tf.summary.FileWriter(os.path.join(self._name, 'train'), self._session.graph), 'valid': tf.summary.FileWriter(os.path.join(self._name, 'valid'), self._session.graph)} + self._session.run(tf.global_variables_initializer()) + saver = tf.train.Saver() + ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoint/')) + if ckpt and ckpt.model_checkpoint_path: + saver.restore(self._session, ckpt.model_checkpoint_path) + initial_step = self._global_step.eval(session=self._session) + for i in range(initial_step, self._n_iters): + print('training %i' % i) + summary = self._iter(X, y, loss, train_op, placeholder, metric_tensors) + summary_writer['train'].add_summary(summary, i) + summary_writer['train'].flush() + saver.save(self._session, 'checkpoint/session', i) + if self._valid is not None: + print('evaluating %i' % i) + summary = self._iter(self._valid['x'], self._valid['y'], loss, None, placeholder, metric_tensors) + summary_writer['valid'].add_summary(summary, i) + summary_writer['valid'].flush() + if self._early_stop is not None: + print(len(self._history)) + if len(self._history) > self._early_stop and self._history[-1] == self._history[-1 - self._early_stop]: + print(self._history) + return + + def predict(self, X, prob=False, remove_s=False): + with tf.variable_scope('nn', reuse=True): + X_placeholder = tf.placeholder(tf.int32, shape=(None, X.shape[1], X.shape[2])) + y_prob = self._inference(X_placeholder, is_train=False) + if remove_s: + y_prob = tf.slice(y_prob, [0, 0], [X.shape[0], 5]) + y_prob = tf.nn.softmax(y_prob) + y_max = tf.reduce_max(y_prob, axis=-1) + y_pred = tf.cast(tf.equal(y_prob, tf.reshape(y_max, (-1, 1))), dtype=tf.int32) + y_, y_prob = self._session.run([y_pred, y_prob], feed_dict={X_placeholder: X}) + if not prob: + return y_ + else: + return y_, y_prob diff --git a/dataset/preprocessed/505.txt b/dataset/preprocessed/505.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/505.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/506.txt b/dataset/preprocessed/506.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/506.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/507.txt b/dataset/preprocessed/507.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/507.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/508.txt b/dataset/preprocessed/508.txt new file mode 100644 index 0000000..dc65c38 --- /dev/null +++ b/dataset/preprocessed/508.txt @@ -0,0 +1,126 @@ +import random +import tensorflow as tf +import numpy as np + + +def sort_by_label(image, label): + tmp = {} + for i in range(10): + tmp[i] = [] + for x, y in zip(image, label): + tmp[y].append(x.reshape([28 * 28])) + result = {} + for k, v in tmp.items(): + result[k] = np.array(v, dtype=np.float32) + return result + + +def load_mnist(): + mnist = tf.keras.datasets.mnist + (x_train, y_train), (x_test, y_test) = mnist.load_data() + x_train, x_test = x_train / 255.0, x_test / 255.0 + train = sort_by_label(x_train, y_train) + test = sort_by_label(x_test, y_test) + return train, test + + +MARGIN = 1.0 +anchor_placeholder = tf.placeholder(shape=[None, 28 * 28], dtype=tf.float32) +positive_placeholder = tf.placeholder(shape=[None, 28 * 28], dtype=tf.float32) +negative_placeholder = tf.placeholder(shape=[None, 28 * 28], dtype=tf.float32) +center_placeholder = tf.placeholder(shape=[10, 64], dtype=tf.float32) +w0 = tf.Variable(tf.random_normal(shape=[28 * 28, 64], stddev=0.1)) +b0 = tf.Variable(tf.zeros(shape=[64])) +w1 = tf.Variable(tf.random_normal(shape=[64, 64], stddev=0.1)) +b1 = tf.Variable(tf.zeros(shape=[64])) +w2 = tf.Variable(tf.random_normal(shape=[64, 64], stddev=0.1)) +b2 = tf.Variable(tf.zeros(shape=[64])) + + +def encode_image(input_tensor): + h0 = tf.nn.relu(tf.matmul(input_tensor, w0) + b0) + h1 = tf.nn.relu(tf.matmul(h0, w1) + b1) + h2 = tf.matmul(h1, w2) + b2 + return tf.nn.l2_normalize(h2, axis=-1) + + +def sample_triplet(dataset, size): + labels = list(range(9)) + anchor_image = [] + positive_image = [] + negative_image = [] + for i in range(size): + positive_label, negative_label = random.choices(labels, k=2) + a, p = random.choices(dataset[positive_label], k=2) + n = random.choice(dataset[negative_label]) + anchor_image.append(a) + positive_image.append(p) + negative_image.append(n) + return np.array(anchor_image), np.array(positive_image), np.array(negative_image) + + +anchor_output = encode_image(anchor_placeholder) +positive_output = encode_image(positive_placeholder) +negative_output = encode_image(negative_placeholder) +d_pos = tf.reduce_sum(tf.square(anchor_output - positive_output), axis=-1) +d_neg = tf.reduce_sum(tf.square(anchor_output - negative_output), axis=-1) +triplet_loss = tf.maximum(0.0, MARGIN + d_pos - d_neg) +regularizer = tf.nn.l2_loss(w0) + tf.nn.l2_loss(w1) + tf.nn.l2_loss(w2) +loss = tf.reduce_mean(triplet_loss) + 1e-05 * regularizer +optimizer = tf.train.AdamOptimizer(learning_rate=1e-05) +train_ops = optimizer.minimize(loss) +init = tf.global_variables_initializer() + + +def calc_center(dataset, session): + result = [] + for i in range(10): + tmp = session.run(anchor_output, feed_dict={anchor_placeholder: dataset[i]}) + result.append(np.mean(tmp, axis=0)) + return np.array(result) + + +def calc_encoding(dataset, session): + result = [] + for i in range(10): + tmp = session.run(anchor_output, feed_dict={anchor_placeholder: dataset[i]}) + result.append(tmp) + return np.array(result) + + +def get_predict(embedding, center): + min_dist = None + result = 0 + for i in range(10): + dist = np.sum(np.square(embedding - center[i])) + if min_dist is None or dist < min_dist: + min_dist = dist + result = i + return result + + +def main(): + session = tf.Session() + session.run(init) + training_set, test_set = load_mnist() + batch_size = 100 + cost = 0.0 + for i in range(5): + a, p, n = sample_triplet(training_set, batch_size) + _, c = session.run([train_ops, loss], feed_dict={anchor_placeholder: a, positive_placeholder: p, negative_placeholder: n}) + cost += c / batch_size + if i % 1000 == 0: + print('%d, cost: %s' % (i, cost)) + cost = 0.0 + center = calc_center(training_set, session) + print('shape %s, value: %s' % (center[0].shape, center[0])) + error = 0 + test_encoding = calc_encoding(test_set, session) + for embedding in test_encoding[9]: + if get_predict(embedding, center) != 9: + error += 1 + print('Wrong prediction: %s, %s' % (error, error / len(test_encoding[9]))) + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/509.txt b/dataset/preprocessed/509.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/509.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/51.txt b/dataset/preprocessed/51.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/51.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/510.txt b/dataset/preprocessed/510.txt new file mode 100644 index 0000000..dd73378 --- /dev/null +++ b/dataset/preprocessed/510.txt @@ -0,0 +1,263 @@ +import numpy as np +import tensorflow as tf +import neural_network as nn + + +class Generator(nn.Generator): + """ + Example OpenAI-Gym Generator architecture. + """ + + def __init__(self, sess): + """ + Args + ---- + sess : the tensorflow session to be used + """ + self.sess_ = sess + with tf.variable_scope('gen'): + self.input_state_ = tf.placeholder(tf.float32, shape=[None, 4], name='input_state') + self.input_seed_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_seed') + self.concat = tf.concat([self.input_state_, self.input_seed_], 1, name='concat') + self.hidden = tf.layers.dense(self.concat, 8, activation=tf.nn.relu, name='hidden') + self.output_ = tf.layers.dense(self.hidden, 2, name='output') + self.sess.run(tf.global_variables_initializer()) + + @property + def input_state(self): + """ + The input state of shape [None, 4] + + Returns + ------- + A placeholder tensor: the input state's placeholder tensor + """ + return self.input_state_ + + @property + def output(self): + """ + The outputted action distribution of shape [None, 2] + + Returns + ------- + A tensor: the output tensor + """ + return self.output_ + + @property + def sess(self): + """ + The session used to create the graph + + Returns + ------- + A session: the graph's session + """ + return self.sess_ + + @property + def input_seed(self): + """ + The input random seed + + Returns + ------- + A placeholder: the input seed's placeholder tensor + """ + return self.input_seed_ + + @property + def trainable_variables(self): + """ + A list of the trainable variables in our generator + + Returns + ------- + A list of tensors: the trainable variables in this graph + """ + return tf.trainable_variables('gen') + + +class Discriminator(nn.Discriminator): + """ + Example OpenAI-Gym Discriminator Architecture + """ + + def __init__(self, sess): + """ + Args + ---- + sess : the tensorflow session to be used + """ + self.sess_ = sess + with tf.variable_scope('dis'): + self.input_state_ = tf.placeholder(tf.float32, shape=[None, 4], name='input_state') + self.input_reward_ = tf.placeholder(tf.float32, shape=[None], name='input_reward') + self.input_action_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_action') + self.input_reward_exp = tf.expand_dims(self.input_reward_, axis=-1, name='input_reward_expanded') + self.concat = tf.concat([self.input_state_, self.input_reward_exp, self.input_action_], axis=1, name='concat') + self.hidden = tf.layers.dense(self.concat, 8, activation=tf.nn.relu, name='hidden') + self.output_ = tf.layers.dense(self.hidden, 1, activation=tf.sigmoid, name='output') + self.sess.run(tf.global_variables_initializer()) + + @property + def input_state(self): + """ + The input state of shape [None, 4] + + Returns + ------- + A placeholder tensor: the input state's placeholder tensor + """ + return self.input_state_ + + @property + def input_action(self): + """ + The input action of shape [None, 1] + + Returns + ------- + A placeholder tensor: the input action's placeholder tensor + """ + return self.input_action_ + + @property + def output(self): + """ + The probability output of shape [None, 1] + + Returns + ------- + A tensor: the output's tensor + """ + return self.output_ + + @property + def sess(self): + """ + The session used to create a graph + + Returns + ------- + A session: the graph's session + """ + return self.sess_ + + @property + def input_reward(self): + """ + The input reward + + Returns + ------- + A placeholder tensor: the input reward's tensor + """ + return self.input_reward_ + + @property + def trainable_variables(self): + """ + A list of the trainable variables in our generator + + Returns + ------- + A list of tensors: the trainable variables in this graph + """ + return tf.trainable_variables('dis') + + +class Discriminator_copy(nn.Discriminator_copy): + """ + Example OpenAI-Gym Discriminator Copying method + """ + + def __init__(self, dis, new_rew_input): + """ + Initializes a discriminator_copy object + + Args + ---- + dis (Discriminator) : The discriminator to copy + new_rew_input (tf.placeholder) : a new reward input. + """ + self.sess_ = dis.sess + with tf.variable_scope('dis', reuse=tf.AUTO_REUSE): + self.input_state_ = tf.placeholder(tf.float32, shape=[None, 4], name='input_state') + self.input_reward_ = new_rew_input + self.input_action_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_action') + self.input_reward_exp = tf.expand_dims(self.input_reward_, axis=-1, name='input_reward_expanded') + self.concat = tf.concat([self.input_state_, self.input_reward_exp, self.input_action_], axis=1, name='concat_copy') + self.hidden_ker = tf.get_variable('hidden/kernel') + self.hidden_bias = tf.get_variable('hidden/bias') + self.output_ker = tf.get_variable('output/kernel') + self.output_bias = tf.get_variable('output/bias') + self.hidden = tf.matmul(self.concat, self.hidden_ker) + self.hidden_bias + self.output_ = tf.sigmoid(tf.matmul(self.hidden, self.output_ker) + self.output_bias) + + @property + def input_state(self): + """ + The input state of shape [None, 4] + + Returns + ------- + A placeholder tensor: the input state's placeholder tensor + """ + return self.input_state_ + + @property + def input_action(self): + """ + The input action of shape [None, 1] + + Returns + ------- + A placeholder tensor: the input action's placeholder tensor + """ + return self.input_action_ + + @property + def output(self): + """ + The probability output of shape [None, 1] + + Returns + ------- + A tensor: the output's tensor + """ + return self.output_ + + @property + def sess(self): + """ + The session used to create a graph + + Returns + ------- + A session: the graph's session + """ + return self.sess_ + + @property + def input_reward(self): + """ + The input reward + + Returns + ------- + A placeholder tensor: the input reward's tensor + """ + return self.input_reward_ + + @property + def trainable_variables(self): + """ + A list of the trainable variables in our generator + + Returns + ------- + A list of tensors: the trainable variables in this graph + """ + return tf.trainable_variables('dis') diff --git a/dataset/preprocessed/511.txt b/dataset/preprocessed/511.txt new file mode 100644 index 0000000..e153a20 --- /dev/null +++ b/dataset/preprocessed/511.txt @@ -0,0 +1,107 @@ +import numpy as np +import tensorflow as tf +import config + + +class NN: + + def __init__(self, session): + self.session = session + + def f_batch(self, state_batch): + return self.session.run([p_op, v_op], feed_dict={state: state_batch, training: False}) + + def train(self, state_batch, pi_batch, z_batch): + p_loss, v_loss, _ = self.session.run([p_loss_op, v_loss_op, train_step_op], feed_dict={state: state_batch, pi: pi_batch, z: z_batch, learning_rate: config.learning_rate, training: True}) + return p_loss, v_loss + + +def init_model(): + x = single_convolutional_block(state) + for i in range(config.residual_blocks_num): + x = residual_block(x) + p_op = policy_head(x) + v_op = value_head(x) + with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): + v_loss_op, p_loss_op, combined_loss_op = loss(pi, z, p_op, v_op) + train_step_op = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=config.momentum).minimize(combined_loss_op) + return p_op, v_op, p_loss_op, v_loss_op, train_step_op + + +def weight_variable(shape): + return tf.Variable(tf.truncated_normal(shape, stddev=0.1)) + + +def bias_variable(shape): + return tf.Variable(tf.constant(0.1, shape=shape)) + + +def conv2d(x, kernel_size, filter_num): + W = weight_variable([kernel_size, kernel_size, x.shape.dims[3].value, filter_num]) + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def batch_normalization(x): + return tf.layers.batch_normalization(x, training=training) + + +def rectifier_nonlinearity(x): + return tf.nn.relu(x) + + +def linear_layer(x, size): + W = weight_variable([x.shape.dims[1].value, size]) + b = bias_variable([size]) + return tf.matmul(x, W) + b + + +def single_convolutional_block(x): + x = conv2d(x, 3, 64) + x = batch_normalization(x) + return rectifier_nonlinearity(x) + + +def residual_block(x): + original_x = x + x = conv2d(x, 3, 64) + x = batch_normalization(x) + x = rectifier_nonlinearity(x) + x = conv2d(x, 3, 64) + x = batch_normalization(x) + x += original_x + return rectifier_nonlinearity(x) + + +def policy_head(x): + x = conv2d(x, 1, 2) + x = batch_normalization(x) + x = rectifier_nonlinearity(x) + x = tf.reshape(x, [-1, config.board_length * 2]) + return linear_layer(x, config.all_moves_num) + + +def value_head(x): + x = conv2d(x, 1, 1) + x = batch_normalization(x) + x = rectifier_nonlinearity(x) + x = tf.reshape(x, [-1, config.board_length]) + x = linear_layer(x, 64) + x = rectifier_nonlinearity(x) + x = linear_layer(x, 1) + return tf.nn.tanh(x) + + +def loss(pi, z, p, v): + v_loss = tf.reduce_mean(tf.square(z - v)) + p_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=p, labels=pi)) + variables = [v for v in tf.trainable_variables() if 'bias' not in v.name and 'beta' not in v.name] + l2 = tf.add_n([tf.nn.l2_loss(variable) for variable in variables]) + return v_loss, p_loss, v_loss + p_loss + config.l2_weight * l2 + + +state = tf.placeholder(tf.float32, [None, config.N, config.N, config.history_num * 2 + 1], name='state') +pi = tf.placeholder(tf.float32, [None, config.all_moves_num], name='pi') +z = tf.placeholder(tf.float32, [None, 1], name='z') +training = tf.placeholder(tf.bool, name='training') +learning_rate = tf.placeholder(tf.float32, name='learning_rate') +p_op, v_op, p_loss_op, v_loss_op, train_step_op = init_model() diff --git a/dataset/preprocessed/512.txt b/dataset/preprocessed/512.txt new file mode 100644 index 0000000..e614c05 --- /dev/null +++ b/dataset/preprocessed/512.txt @@ -0,0 +1,466 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(LSTMAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.tanh(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + return out, lstm_state + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + """ + + def __init__(self, hidden_size, dropout): + self.hidden_size = hidden_size + self.dropout = dropout + + def encode(self, inputs, masks, attention_inputs=None, initial_state=(None, None), model_type='gru', name='encoder', reuse=False): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(name, reuse=reuse): + if attention_inputs is None: + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + else: + raise Exception('Must specify model type.') + else: + assert model_type == 'lstm' + cell = LSTMAttnCell(self.hidden_size, attention_inputs) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + outputs, final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs, sequence_length=masks, initial_state_fw=initial_state[0], initial_state_bw=initial_state[1], dtype=tf.float32) + final_outputs = outputs[0] + outputs[1] + return final_outputs, final_state + + +class Decoder(object): + + def __init__(self, hidden_size, output_size, dropout): + self.hidden_size = hidden_size + self.output_size = output_size + self.dropout = dropout + + def decode(self, knowledge_rep, masks, initial_state=(None, None)): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + with vs.variable_scope('decoder'): + with vs.variable_scope('answer_start'): + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + start_states, start_final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, knowledge_rep, sequence_length=masks, initial_state_fw=initial_state[0], initial_state_bw=initial_state[1], dtype=tf.float32) + start_states = start_states[0] + start_states[1] + start_states_reshaped = tf.reshape(start_states, [-1, self.hidden_size]) + start_probs = tf.nn.rnn_cell._linear(start_states_reshaped, output_size=1, bias=True) + start_probs = tf.reshape(start_probs, [-1, self.output_size]) + with vs.variable_scope('answer_end'): + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + end_states, end_final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, knowledge_rep, sequence_length=masks, initial_state_fw=start_final_state[0], initial_state_bw=start_final_state[1], dtype=tf.float32) + end_states = end_states[0] + end_states[1] + end_states_reshaped = tf.reshape(end_states, [-1, self.hidden_size]) + end_probs = tf.nn.rnn_cell._linear(end_states_reshaped, output_size=1, bias=True) + end_probs = tf.reshape(end_probs, [-1, self.output_size]) + bool_masks = tf.cast(tf.sequence_mask(masks, maxlen=self.output_size), tf.float32) + add_mask = -1e+30 * (1.0 - bool_masks) + start_probs = tf.add(start_probs, add_mask) + end_probs = tf.add(end_probs, add_mask) + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, pretrained_embeddings, flags): + """ + Initializes your System + + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.flags = flags + self.h_size = self.flags.state_size + self.p_size = self.flags.output_size + self.q_size = self.flags.question_size + self.embed_size = self.flags.embedding_size + self.dropout = self.flags.dropout + self.encoder = Encoder(hidden_size=self.h_size, dropout=1.0 - self.flags.dropout) + self.decoder = Decoder(hidden_size=self.h_size, output_size=self.p_size, dropout=1.0 - self.flags.dropout) + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.p_size), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.q_size), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + self.saver = tf.train.Saver() + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def mixer(self, q_states, ctx_states): + with vs.variable_scope('mixer'): + A = tf.nn.softmax(batch_matmul(ctx_states, tf.transpose(q_states, perm=[0, 2, 1]))) + C_P = batch_matmul(A, q_states) + C_P = tf.reshape(C_P, [-1, self.h_size]) + P = tf.reshape(ctx_states, [-1, self.h_size]) + P_final = tf.nn.rnn_cell._linear([C_P, P], output_size=self.h_size, bias=True) + P_final = tf.reshape(P_final, [-1, self.p_size, self.h_size]) + return P_final + + def filter(self, Q, P): + with vs.variable_scope('filter'): + Qn = tf.nn.l2_normalize(Q, dim=2) + Pn = tf.nn.l2_normalize(P, dim=2) + R = batch_matmul(Qn, tf.transpose(Pn, perm=[0, 2, 1])) + r = tf.reduce_max(R, axis=1) + r = tf.expand_dims(r, axis=2) + P_filtered = P * r + return P_filtered + + def coattention(self, P, Q, masks): + P_t = tf.transpose(P, perm=[0, 2, 1]) + Q_t = tf.transpose(Q, perm=[0, 2, 1]) + L = tf.batch_matmul(P, Q_t) + AQ = tf.nn.softmax(L, dim=1) + AD = tf.nn.softmax(tf.transpose(L, perm=[0, 2, 1]), dim=1) + CQ = tf.batch_matmul(P_t, AQ) + contexts = tf.concat(1, [Q_t, CQ]) + CD = tf.batch_matmul(contexts, AD) + lstm_inputs = tf.transpose(tf.concat(1, [P_t, CD]), perm=[0, 2, 1]) + cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, output_keep_prob=self.dropout) + cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, output_keep_prob=self.dropout) + all_states, _ = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, lstm_inputs, sequence_length=masks, dtype=tf.float32) + U = tf.concat(2, all_states) + U = U[:, :self.p_size, :] + return U + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + P_filtered = self.context_embeddings + P_filtered = self.filter(Q=self.question_embeddings, P=self.context_embeddings) + question_states, final_question_state = self.encoder.encode(self.question_embeddings, self.mask_q_placeholder, attention_inputs=None, model_type=self.flags.model_type, reuse=False, name='q_encoder') + ctx_states, final_ctx_state = self.encoder.encode(P_filtered, self.mask_ctx_placeholder, attention_inputs=None, initial_state=final_question_state, model_type=self.flags.model_type, reuse=False, name='ctx_encoder') + feed_states = self.mixer(q_states=question_states, ctx_states=ctx_states) + self.start_probs, self.end_probs = self.decoder.decode(knowledge_rep=feed_states, masks=self.mask_ctx_placeholder, initial_state=final_question_state) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + """ + q_mean,q_var = tf.nn.moments(question_embeddings, axes=[2], shift=None, name=None, keep_dims=True) + ctx_mean,ctx_var = tf.nn.moments(context_embeddings, axes=[2], shift=None, name=None, keep_dims=True) + + + + # q_scale = tf.Variable(tf.zeros,name='q_scale',dtype=tf.float32,trainable=True) + # ctx_scale = tf.Variable(1.0,name='ctx_scale',dtype=tf.float32,trainable=True) + # q_offset = tf.Variable(1.0,name='q_offset',dtype=tf.float32,trainable=True) + # ctx_offset = tf.Variable(1.0,name='ctx_offset',dtype=tf.float32,trainable=True) + + + q_scale = tf.get_variable("q_scale", shape=[self.q_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + ctx_scale = tf.get_variable("ctx_scale", shape=[self.p_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + q_offset = tf.get_variable("q_offset", shape=[self.q_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + ctx_offset = tf.get_variable("ctx_offset", shape=[self.p_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + + + self.question_embeddings = tf.nn.batch_normalization(question_embeddings, q_mean, q_var, q_offset,q_scale, variance_epsilon=0.0000001) + self.context_embeddings = tf.nn.batch_normalization(context_embeddings, ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon=0.0000001) + """ + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs + + def answer(self, session, data): + yp_lst = [] + yp2_lst = [] + prog_train = Progbar(target=1 + int(len(data[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle=False)): + yp, yp2 = self.decode(session, *batch) + yp_lst.append(yp) + yp2_lst.append(yp2) + prog_train.update(i + 1, [('Answering Questions....', 0.0)]) + print('') + yp_all = np.concatenate(yp_lst, axis=0) + yp2_all = np.concatenate(yp2_lst, axis=0) + a_s = np.argmax(yp_all, axis=1) + a_e = np.argmax(yp2_all, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(session=sess, context_batch=context_batch, question_batch=question_batch, answer_span_batch=answer_span_batch, mask_ctx_batch=mask_ctx_batch, mask_q_batch=mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset[0]) + else: + inds = np.random.choice(len(dataset[0]), sample) + sampled = [elem[inds] for elem in dataset] + context = [context[i] for i in inds] + a_s, a_e = self.answer(session, sampled) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + f1 = [] + em = [] + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + cur_em = exact_match_score(pred_words, actual_words) + em.append(float(cur_em)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + return np.mean(f1), np.mean(em) + + def run_epoch(self, sess, train_set, val_set, train_context, val_context): + prog_train = Progbar(target=1 + int(len(train_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + print('') + prog_val = Progbar(target=1 + int(len(val_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + self.evaluate_answer(session=sess, dataset=train_set, context=train_context, sample=len(val_set[0]), log=True, eval_set='-Epoch TRAIN-') + self.evaluate_answer(session=sess, dataset=val_set, context=val_context, sample=None, log=True, eval_set='-Epoch VAL-') + + def train(self, session, dataset, val_dataset, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = [elem[:self.flags.batch_size * 1] for elem in train_dataset] + val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + num_epochs = 100 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, train_context=train_context, val_context=val_context) + logging.info('Saving model in %s', train_dir) + self.saver.save(session, train_dir + '/' + self.flags.run_name + '.ckpt') + + def minibatches(self, data, batch_size, shuffle=True): + num_data = len(data[0]) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + indices = np.arange(num_data) + if shuffle: + np.random.shuffle(indices) + for minibatch_start in np.arange(0, num_data, batch_size): + minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] diff --git a/dataset/preprocessed/513.txt b/dataset/preprocessed/513.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/513.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/514.txt b/dataset/preprocessed/514.txt new file mode 100644 index 0000000..20419a5 --- /dev/null +++ b/dataset/preprocessed/514.txt @@ -0,0 +1,60 @@ +import tensorflow as tf +import numpy as np +import math +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data/', one_hot=True, reshape=False, validation_size=0) +np.set_printoptions(threshold=np.nan) +x = tf.placeholder(tf.float32, [None, 28, 28, 1]) +y_ = tf.placeholder(tf.float32, [None, 10]) +pkeep = tf.placeholder(tf.float32) +lr = tf.placeholder(tf.float32) +K = 6 +L = 12 +M = 24 +N = 200 +w1 = tf.Variable(tf.truncated_normal([6, 6, 1, K], stddev=0.1)) +b1 = tf.Variable(tf.constant(0.1, tf.float32, [K])) +w2 = tf.Variable(tf.truncated_normal([5, 5, K, L], stddev=0.1)) +b2 = tf.Variable(tf.constant(0.1, tf.float32, [L])) +w3 = tf.Variable(tf.truncated_normal([4, 4, L, M], stddev=0.1)) +b3 = tf.Variable(tf.constant(0.1, tf.float32, [M])) +w4 = tf.Variable(tf.truncated_normal([7 * 7 * M, N], stddev=0.1)) +b4 = tf.Variable(tf.constant(0.1, tf.float32, [N])) +w5 = tf.Variable(tf.truncated_normal([N, 10], stddev=0.1)) +b5 = tf.Variable(tf.constant(0.1, tf.float32, [10])) +y1 = tf.nn.relu(tf.nn.conv2d(x, w1, strides=[1, 1, 1, 1], padding='SAME') + b1) +y2 = tf.nn.relu(tf.nn.conv2d(y1, w2, strides=[1, 2, 2, 1], padding='SAME') + b2) +y3 = tf.nn.relu(tf.nn.conv2d(y2, w3, strides=[1, 2, 2, 1], padding='SAME') + b3) +yy3 = tf.reshape(y3, shape=[-1, 7 * 7 * M]) +y4 = tf.nn.relu(tf.matmul(yy3, w4) + b4) +yy4 = tf.nn.dropout(y4, pkeep) +Ylogits = tf.matmul(yy4, w5) + b5 +y = tf.nn.softmax(Ylogits) +cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits, labels=y_) +cross_entropy = tf.reduce_mean(cross_entropy) * 100 +is_correct = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) +train_step = tf.train.AdamOptimizer(lr).minimize(cross_entropy) +init = tf.global_variables_initializer() +session = tf.Session() +session.run(init) +for i in range(10000): + batch_x, batch_y = mnist.train.next_batch(100) + train_data = {x: batch_x, y_: batch_y} + print(i) + max_learning_rate = 0.003 + min_learning_rate = 0.0001 + decay_speed = 2000.0 + learning_rate = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i / decay_speed) + session.run(train_step, {x: batch_x, y_: batch_y, lr: learning_rate, pkeep: 0.75}) +np.savetxt('test1.txt', session.run(w1).flatten()) +np.savetxt('bias1.txt', session.run(b1).flatten()) +np.savetxt('test2.txt', session.run(w2).flatten()) +np.savetxt('bias2.txt', session.run(b2).flatten()) +np.savetxt('test3.txt', session.run(w3).flatten()) +np.savetxt('bias3.txt', session.run(b3).flatten()) +np.savetxt('test4.txt', session.run(w4).flatten()) +np.savetxt('bias4.txt', session.run(b4).flatten()) +np.savetxt('test5.txt', session.run(w5).flatten()) +np.savetxt('bias5.txt', session.run(b5).flatten()) diff --git a/dataset/preprocessed/515.txt b/dataset/preprocessed/515.txt new file mode 100644 index 0000000..8dfc9f7 --- /dev/null +++ b/dataset/preprocessed/515.txt @@ -0,0 +1,148 @@ +import tensorflow as tf +import numpy as np + + +class Actor: + + def __init__(self, num_inputs, h_size, name): + """Constructor of Actor class. + + Args: + num_inputs (int): Number of the inputs of the actor + h_size (int): Size of the LSTM output. + name (str): name of the context. + """ + self.num_variables = 10 + self.cell = tf.contrib.rnn.BasicLSTMCell(num_units=h_size, state_is_tuple=True) + self.inp = tf.placeholder(shape=[None, num_inputs], dtype=tf.float32) + self.initializer = tf.contrib.layers.xavier_initializer() + self.batch_size = tf.placeholder(dtype=tf.int32, shape=[]) + self.train_length = tf.placeholder(dtype=tf.int32) + self.rnn_inp = tf.reshape(self.inp, [self.batch_size, self.train_length, num_inputs]) + self.state_in = self.cell.zero_state(self.batch_size, tf.float32) + self.rnn, self.rnn_state = tf.nn.dynamic_rnn(inputs=self.rnn_inp, cell=self.cell, dtype=tf.float32, initial_state=self.state_in, scope=name + '_rnn') + self.rnn = tf.reshape(self.rnn, shape=[-1, h_size]) + self.b1 = tf.Variable(self.initializer([1, 1000])) + self.W1 = tf.Variable(self.initializer([h_size, 1000])) + self.h1 = tf.nn.relu(tf.matmul(self.rnn, self.W1) + self.b1) + self.b2 = tf.Variable(self.initializer([1, 100])) + self.W2 = tf.Variable(self.initializer([1000, 100])) + self.h2 = tf.nn.relu(tf.matmul(self.h1, self.W2) + self.b2) + self.b3 = tf.Variable(self.initializer([1, 50])) + self.W3 = tf.Variable(self.initializer([100, 50])) + self.h3 = tf.nn.relu(tf.matmul(self.h2, self.W3) + self.b3) + self.b4 = tf.Variable(self.initializer([1, 1])) + self.W4 = tf.Variable(self.initializer([50, 1])) + self.a_unscaled = tf.nn.tanh(tf.matmul(self.h3, self.W4) + self.b4) + self.a = tf.multiply(self.a_unscaled, 0.05) + self.network_params = tf.trainable_variables()[-self.num_variables:] + self.critic_gradient = tf.placeholder(tf.float32, [None, 1]) + self.unnormalized_actor_gradients = tf.gradients(self.a, self.network_params, -self.critic_gradient) + self.actor_gradients = list(map(lambda x: tf.div(x, 32), self.unnormalized_actor_gradients)) + self.optimizer = tf.train.AdamOptimizer(0.0001) + self.upd = self.optimizer.apply_gradients(zip(self.actor_gradients, self.network_params)) + self.update_parameters = [] + + def create_op_holder(self, params, tau): + """ Use target network op holder if needed""" + self.update_parameters = [self.network_params[i].assign(tf.multiply(params[i], tau) + tf.multiply(self.network_params[i], 1.0 - tau)) for i in range(len(self.network_params))] + + +class Critic: + + def __init__(self, num_inputs, h_size, name): + """Constructor of Critic class. + + Args: + num_inputs (int): Number of the inputs of the critic + h_size (int): Size of the LSTM output. + name (str): name of the context. + """ + self.num_variables = 10 + self.cell = tf.contrib.rnn.BasicLSTMCell(num_units=h_size, state_is_tuple=True) + self.inp = tf.placeholder(shape=[None, num_inputs], dtype=tf.float32) + self.initializer = tf.contrib.layers.xavier_initializer() + self.batch_size = tf.placeholder(dtype=tf.int32, shape=[]) + self.train_length = tf.placeholder(dtype=tf.int32) + self.rnn_inp = tf.reshape(self.inp, [self.batch_size, self.train_length, num_inputs]) + self.state_in = self.cell.zero_state(self.batch_size, tf.float32) + self.rnn, self.rnn_state = tf.nn.dynamic_rnn(inputs=self.rnn_inp, cell=self.cell, dtype=tf.float32, initial_state=self.state_in, scope=name + '_rnn') + self.rnn = tf.reshape(self.rnn, shape=[-1, h_size]) + self.b1 = tf.Variable(self.initializer([1, 1000])) + self.W1 = tf.Variable(self.initializer([h_size, 1000])) + self.h1 = tf.nn.relu(tf.matmul(self.rnn, self.W1) + self.b1) + self.b2 = tf.Variable(self.initializer([1, 100])) + self.W2 = tf.Variable(self.initializer([1000, 100])) + self.h2 = tf.nn.relu(tf.matmul(self.h1, self.W2) + self.b2) + self.b3 = tf.Variable(self.initializer([1, 50])) + self.W3 = tf.Variable(self.initializer([100, 50])) + self.h3 = tf.nn.relu(tf.matmul(self.h2, self.W3) + self.b3) + self.b4 = tf.Variable(self.initializer([1, 1])) + self.W4 = tf.Variable(self.initializer([50, 1])) + self.q = tf.matmul(self.h3, self.W4) + self.b4 + self.network_params = tf.trainable_variables()[-self.num_variables:] + self.target_q = tf.placeholder(tf.float32, [None, 1]) + self.loss = tf.reduce_mean(tf.square(self.target_q - self.q)) + self.optimizer = tf.train.AdamOptimizer(0.0001) + self.upd = self.optimizer.minimize(self.loss) + self.critic_gradients = tf.gradients(self.q, self.inp) + self.update_parameters = [] + + def create_op_holder(self, params, tau): + """ Use target network op holder if needed""" + self.update_parameters = [self.network_params[i].assign(tf.multiply(params[i], tau) + tf.multiply(self.network_params[i], 1.0 - tau)) for i in range(len(self.network_params))] + + +class Agent: + + def __init__(self, a_dof, c_dof, h_size, name, batch_size, train_length, tau): + """Constructor of Agent class. Each agent is composed of the main actor-critic pair and the target actor-critic + pair. Target architectures help stabilizing the training of the agents. + + Args: + h_size (int): Size of the LSTM output. + name (str): name of the context. + """ + self.actor = Actor(a_dof, h_size, name + '_actor') + self.critic = Critic(c_dof, h_size, name + '_critic') + self.actor_target = Actor(a_dof, h_size, name + '_actor_target') + self.critic_target = Critic(c_dof, h_size, name + '_critic_target') + self.batch_size = batch_size + self.train_length = train_length + self.state_train = np.zeros([self.batch_size, h_size]), np.zeros([self.batch_size, h_size]) + self.actor_target.create_op_holder(self.actor.network_params, tau) + self.critic_target.create_op_holder(self.critic.network_params, tau) + + def a_actor_operation(self, session, inp, state): + return session.run([self.actor.a, self.actor.rnn_state], feed_dict={self.actor.inp: inp, self.actor.state_in: state, self.actor.batch_size: 1, self.actor.train_length: 1}) + + def a_actor_training(self, session, inp): + return session.run(self.actor.a, feed_dict={self.actor.inp: inp, self.actor.state_in: self.state_train, self.actor.batch_size: self.batch_size, self.actor.train_length: self.train_length}) + + def a_target_actor_training(self, session, inp): + return session.run(self.actor_target.a, feed_dict={self.actor_target.inp: inp, self.actor_target.state_in: self.state_train, self.actor_target.batch_size: self.batch_size, self.actor_target.train_length: self.train_length}) + + def q_target_critic(self, session, inp): + return session.run(self.critic_target.q, feed_dict={self.critic_target.inp: inp, self.critic_target.train_length: self.train_length, self.critic_target.batch_size: self.batch_size, self.critic_target.state_in: self.state_train}) + + def gradients_critic(self, session, inp): + return session.run(self.critic.critic_gradients, feed_dict={self.critic.inp: inp, self.critic.train_length: self.train_length, self.critic.batch_size: self.batch_size, self.critic.state_in: self.state_train}) + + def update_critic(self, session, inp, q): + session.run(self.critic.upd, feed_dict={self.critic.inp: inp, self.critic.target_q: q, self.critic.train_length: self.train_length, self.critic.batch_size: self.batch_size, self.critic.state_in: self.state_train}) + + def update_actor(self, session, inp, gradients): + session.run(self.actor.upd, feed_dict={self.actor.inp: inp, self.actor.state_in: self.state_train, self.actor.critic_gradient: gradients, self.actor.batch_size: self.batch_size, self.actor.train_length: self.train_length}) + + def update_targets(self, session): + session.run(self.actor_target.update_parameters) + session.run(self.critic_target.update_parameters) + + def initialize_targets(self, session): + session.run([self.actor_target.network_params[i].assign(self.actor.network_params[i]) for i in range(len(self.actor.network_params))]) + session.run([self.critic_target.network_params[i].assign(self.critic.network_params[i]) for i in range(len(self.critic.network_params))]) + + def importance(self, session, inp_q, inp_q_target, r, gamma, state, new_state): + act_q = session.run(self.critic_target.q, feed_dict={self.critic_target.inp: inp_q, self.critic_target.train_length: 1, self.critic_target.batch_size: 1, self.critic_target.state_in: state})[0, 0] + tar_q = session.run(self.critic_target.q, feed_dict={self.critic_target.inp: inp_q_target, self.critic_target.train_length: 1, self.critic_target.batch_size: 1, self.critic_target.state_in: new_state})[0, 0] + return np.abs(r + gamma * tar_q - act_q) diff --git a/dataset/preprocessed/516.txt b/dataset/preprocessed/516.txt new file mode 100644 index 0000000..0a9d08d --- /dev/null +++ b/dataset/preprocessed/516.txt @@ -0,0 +1,24 @@ +import numpy as np +import tensorflow as tf +X = tf.placeholder('float', [10, 10], name='X') +Y1 = tf.placeholder('float', [10, 20], name='Y1') +Y2 = tf.placeholder('float', [10, 20], name='Y2') +initial_shared_layer_weights = np.random.rand(10, 20) +initial_Y1_layer_weights = np.random.rand(20, 20) +initial_Y2_layer_weights = np.random.rand(20, 20) +shared_layer_weights = tf.Variable(initial_shared_layer_weights, name='share_W', dtype='float32') +Y1_layer_weights = tf.Variable(initial_Y1_layer_weights, name='share_Y1', dtype='float32') +Y2_layer_weights = tf.Variable(initial_Y2_layer_weights, name='share_Y2', dtype='float32') +shared_layer = tf.nn.relu(tf.matmul(X, shared_layer_weights)) +Y1_layer = tf.nn.relu(tf.matmul(shared_layer, Y1_layer_weights)) +Y2_layer = tf.nn.relu(tf.matmul(shared_layer, Y2_layer_weights)) +Y1_Loss = tf.nn.l2_loss(Y1 - Y1_layer) +Y2_Loss = tf.nn.l2_loss(Y2 - Y2_layer) +Joint_Loss = Y1_Loss + Y2_Loss +Optimiser = tf.train.AdamOptimizer().minimize(Joint_Loss) +Y1_op = tf.train.AdamOptimizer().minimize(Y1_Loss) +Y2_op = tf.train.AdamOptimizer().minimize(Y2_Loss) +with tf.Session() as session: + session.run(tf.initialize_all_variables()) + _, Joint_Loss = session.run([Optimiser, Joint_Loss], {X: np.random.rand(10, 10) * 10, Y1: np.random.rand(10, 20) * 10, Y2: np.random.rand(10, 20) * 10}) + print(Joint_Loss) diff --git a/dataset/preprocessed/517.txt b/dataset/preprocessed/517.txt new file mode 100644 index 0000000..0bf9ce7 --- /dev/null +++ b/dataset/preprocessed/517.txt @@ -0,0 +1,214 @@ +import os, sys, time +import numpy as np +import tensorflow as tf +import datagen as dg + + +class Config(object): + """ The model hyperparams and data information + + """ + + def __init__(self, config_list=[300, 64, 1000, 1024, 15, 0.9, 2e-05, 0.001, 25]): + [self.embed_size, self.batch_size, self.label_size, self.hidden_size, self.max_epochs, self.dropout, self.lr, self.l2, self.max_ques_len] = config_list + + +class CNN_LSTM_VQAModel: + """ Visual Question Answering using CNN and LSTM model. + + """ + + def load_data(self, debug=False): + """ load the image, question, answer data from dataset """ + self.train_set, self.test_set, self.vocab, self.ans_lk_table = dg.build_voc_and_get_data(self.config.max_ques_len) + self.wv, _, _ = dg.word_embed(self.vocab, 'glove.6B.300d') + label_size = len(self.ans_lk_table) + assert label_size == self.config.label_size + for i in range(len(self.train_set)): + ans_id = self.train_set[i][2] + ans_vec = np.zeros(label_size) + ans_vec[ans_id] = 1 + self.train_set[i][2] = ans_vec + if debug: + self.valid_set = self.train_set[1024:2048] + self.train_set = self.train_set[:1024] + self.test_set = self.test_set[:65] + else: + self.valid_set = self.train_set[:len(self.train_set) // 4] + self.train_set = self.train_set[len(self.train_set) // 4:] + + def add_placeholders(self): + """ placeholder tensors for input and labels + + input_placeholder: input tensor of shape + (None, max_ques_len), type tf.int32 + label_placeholder: label tensor of shape + (batch_size, label_size), type tf.float32 + dropout_placeholder: Dropout value placeholder (scalar) + type tf.float32 + """ + self.input_placeholder = tf.placeholder(tf.int32, shape=[None, self.config.max_ques_len], name='Input') + self.labels_placeholder = tf.placeholder(tf.float32, shape=[self.config.batch_size, self.config.label_size], name='Target') + self.dropout_placeholder = tf.placeholder(tf.float32, name='Dropout') + + def add_embedding(self, wv=None): + with tf.device('/cpu:0'): + if wv is not None: + embedding = tf.get_variable(name='Embed', shape=wv.shape, initializer=tf.constant_initializer(wv), trainable=False) + else: + embedding = tf.get_variable(name='Embed', shape=[len(self.vocab), self.config.embed_size], trainable=True) + inputs = tf.nn.embedding_lookup(embedding, self.input_placeholder) + tmp = tf.split(1, self.config.max_ques_len, inputs) + inputs = [tf.squeeze(x, [1]) for x in tmp] + return inputs + + def add_projection(self, output, cnn_outputs=None): + with tf.variable_scope('Projection'): + U = tf.get_variable('U', [self.config.hidden_size, self.config.label_size]) + b = tf.get_variable('b', [self.config.label_size]) + output = tf.matmul(output, U) + b + return output + + def add_lstm_model(self, inputs): + """ LSTM model for Question Encoding. + + inputs: list (length=max_ques_len) of input tensor of shape + (batch_size, embed_size), type tf.float32 + + lstm_output: final state of lstm, tensor of shape + (batch_size, hidden_size), type tf.float32 + """ + batch_size, hidden_size, embed_size = self.config.batch_size, self.config.hidden_size, self.config.embed_size + with tf.variable_scope('InputDropout'): + inputs = [tf.nn.dropout(x, self.dropout_placeholder) for x in inputs] + with tf.variable_scope('LSTM') as scope: + self.initial_state = tf.zeros([batch_size, hidden_size]) + ht = self.initial_state + Ct = ht + for step, xt in enumerate(inputs): + if step > 0: + scope.reuse_variables() + Wf = tf.get_variable('Wf', [embed_size, hidden_size]) + Uf = tf.get_variable('Uf', [hidden_size, hidden_size]) + bf = tf.get_variable('bf', [hidden_size]) + ft = tf.nn.sigmoid(tf.matmul(xt, Wf) + tf.matmul(ht, Uf) + bf) + Wi = tf.get_variable('Wi', [embed_size, hidden_size]) + Ui = tf.get_variable('Ui', [hidden_size, hidden_size]) + bi = tf.get_variable('bi', [hidden_size]) + it = tf.nn.sigmoid(tf.matmul(xt, Wi) + tf.matmul(ht, Ui) + bi) + Wc = tf.get_variable('Wc', [embed_size, hidden_size]) + Uc = tf.get_variable('Uc', [hidden_size, hidden_size]) + bc = tf.get_variable('bc', [hidden_size]) + _Ct = tf.nn.tanh(tf.matmul(xt, Wc) + tf.matmul(ht, Uc) + bc) + Ct = ft * Ct + it * _Ct + Wo = tf.get_variable('Wo', [embed_size, hidden_size]) + Uo = tf.get_variable('Uo', [hidden_size, hidden_size]) + bo = tf.get_variable('bo', [hidden_size]) + ot = tf.nn.sigmoid(tf.matmul(xt, Wo) + tf.matmul(ht, Uo) + bo) + ht = ot * tf.nn.tanh(Ct) + self.final_state = ht + with tf.variable_scope('LSTMDropout'): + lstm_output = tf.nn.dropout(self.final_state, self.dropout_placeholder) + return lstm_output + + def add_loss_op(self, y): + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, self.labels_placeholder)) + tf.add_to_collection('total_loss', cross_entropy) + loss = tf.add_n(tf.get_collection('total_loss')) + return loss + + def add_training_op(self, loss): + optimizer = tf.train.AdamOptimizer(self.config.lr) + train_op = optimizer.minimize(loss) + return train_op + + def __init__(self, config): + self.config = config + self.load_data(debug=False) + self.add_placeholders() + self.inputs = self.add_embedding(self.wv) + self.lstm_output = self.add_lstm_model(self.inputs) + self.output = self.add_projection(self.lstm_output) + self.loss = self.add_loss_op(self.output) + self.predictions = tf.nn.softmax(self.output) + one_hot_prediction = tf.argmax(self.predictions, 1) + correct_predictions = tf.equal(tf.argmax(self.labels_placeholder, 1), one_hot_prediction) + self.correct_predictions = tf.reduce_sum(tf.cast(correct_predictions, 'int32')) + self.train_step = self.add_training_op(self.loss) + + def run_epoch(self, session, input_data, train_op=None): + config = self.config + dropout = config.dropout + if not train_op: + train_op = tf.no_op() + dropout = 1 + total_steps = len(input_data) // self.config.batch_size + total_loss = [] + for step, (x, y) in enumerate(dg.data_iter(input_data, config.batch_size)): + feed = {self.input_placeholder: x, self.labels_placeholder: y, self.dropout_placeholder: dropout} + loss, _ = session.run([self.loss, train_op], feed_dict=feed) + total_loss.append(loss) + if step % 10 == 0: + sys.stdout.write('\r{} / {} : loss = {}'.format(step, total_steps, loss)) + sys.stdout.flush() + print('\n') + return np.mean(total_loss) + + def predict(self, session, data): + """ Prediction generator """ + dropout = 1 + pred_res = [] + confi_res = [] + total_steps = len(data) // self.config.batch_size + for step, (x, y) in enumerate(dg.data_iter(data, self.config.batch_size)): + if x.shape[0] < self.config.batch_size: + break + feed = {self.input_placeholder: x, self.dropout_placeholder: dropout} + preds = session.run(self.predictions, feed_dict=feed) + predicted_indices = preds.argmax(axis=1) + pred_res.extend(predicted_indices) + confident = preds.max(axis=1) + confi_res.extend(confident) + if step % 10 == 0: + sys.stdout.write('\r{} / {}'.format(step, total_steps)) + sys.stdout.flush() + print('\n') + return pred_res, confi_res + + +def test_VQA(): + config = Config() + with tf.variable_scope('CNN_LSTM_VQA') as scope: + model = CNN_LSTM_VQAModel(config) + init = tf.initialize_all_variables() + saver = tf.train.Saver() + with tf.Session() as session: + best_val_loss = float('inf') + best_val_epoch = 0 + session.run(init) + for epoch in range(config.max_epochs): + print('Epoch {}'.format(epoch)) + start = time.time() + train_loss = model.run_epoch(session, model.train_set, model.train_step) + valid_loss = model.run_epoch(session, model.valid_set) + print('Training loss: {}'.format(train_loss)) + print('Validation loss: {}'.format(valid_loss)) + print('Total time: {}'.format(time.time() - start)) + if valid_loss < best_val_loss: + best_val_loss = valid_loss + best_val_epoch = epoch + if not os.path.exists('./weights'): + os.makedirs('./weights') + saver.save(session, './weights/Vqa.weights') + saver.restore(session, './weights/Vqa.weights') + print('Test Model: ') + pred_indices, _ = model.predict(session, model.test_set) + test_ans_list = [x[2] for x in model.test_set] + num = len(test_ans_list) - len(test_ans_list) % config.batch_size + test_ans_list = test_ans_list[:num] + accuracy = np.equal(pred_indices, test_ans_list).sum() / len(pred_indices) + print('accuracy: {}'.format(accuracy)) + + +if __name__ == '__main__': + test_VQA() diff --git a/dataset/preprocessed/518.txt b/dataset/preprocessed/518.txt new file mode 100644 index 0000000..f41163b --- /dev/null +++ b/dataset/preprocessed/518.txt @@ -0,0 +1,64 @@ +import tensorflow as tf +import numpy as np +from data_exploration import batch_generator +import time +n_input = 4735 +n_hidden_1 = 900 +n_hidden_2 = 900 +n_hidden_3 = 90 +n_hidden_4 = 9 +n_classes = 1 +BATCHSIZE = 100 +batch_handle = batch_generator(BATCHSIZE) + + +def gen(): + while True: + print(batch_handle) + yield next(batch_handle) + + +def mlp(): + sequence1 = tf.placeholder(shape=(None, 4735), dtype=tf.float32) + sequence2 = tf.placeholder(shape=(None, 4735), dtype=tf.float32) + distance = tf.placeholder(shape=(None, 1), dtype=tf.float32) + layer11 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(sequence1, weights['h1']), biases['b1'])), 1) + layer12 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(sequence2, weights['h1']), biases['b1'])), 1) + layer21 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer11, weights['h2']), biases['b2'])), 1) + layer22 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer12, weights['h2']), biases['b2'])), 1) + layer31 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer21, weights['h3']), biases['b3'])), 1) + layer32 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer22, weights['h3']), biases['b3'])), 1) + layer41 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer31, weights['h4']), biases['b4'])), 1) + layer42 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.matmul(layer32, weights['h4']), biases['b4'])), 1) + layer5 = tf.nn.tanh(tf.add(layer41, layer42)) + predict = tf.reduce_sum(layer5, 1, keepdims=True) + error = tf.reduce_mean(tf.square(tf.subtract(predict, distance))) + optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(error) + init = tf.global_variables_initializer() + return sequence1, sequence2, init, distance, error, optimizer + + +weights = {'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=1)), 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=1)), 'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3], stddev=1)), 'h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4], stddev=1)), 'out': tf.Variable(tf.random_normal([n_hidden_4, n_classes], stddev=1))} +biases = {'b1': tf.Variable(tf.random_normal([n_hidden_1])), 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 'b3': tf.Variable(tf.random_normal([n_hidden_3])), 'b4': tf.Variable(tf.random_normal([n_hidden_4])), 'out': tf.Variable(tf.random_normal([n_classes]))} + + +def data_convert(): + a, b = next(batch_handle) + x = np.array(a) + y = np.array(b) + s1 = x[:, 0:4735] + s2 = x[:, 4735:] + d = y + return s1, s2, d + + +if __name__ == '__main__': + start = time.time() + s1, s2, init, distance, out, opt = mlp() + with tf.Session() as session: + session.run(init) + for i in range(0, 100): + seq1, seq2, dis = data_convert() + print(session.run([out], feed_dict={s1: seq1, s2: seq2, distance: dis})) + print(session.run([opt], feed_dict={s1: seq1, s2: seq2, distance: dis})) + print('elapsed time is equal:{}'.format(time.time() - start)) diff --git a/dataset/preprocessed/519.txt b/dataset/preprocessed/519.txt new file mode 100644 index 0000000..8d3f3e5 --- /dev/null +++ b/dataset/preprocessed/519.txt @@ -0,0 +1,135 @@ +import tensorflow as tf +import numpy as np +import time +import random +from src.attention_wrapper import BahdanauAttention, AttentionWrapper + + +class model(object): + + def __init__(self, params, embedding): + random.seed(params['seed']) + self.keep_prob_in = params['dropout_input'] + self.keep_prob_fc = params['dropout_fully_connected'] + self.hidden_unit_lstm = params['hidden_unit_lstm'] + self.hidden_unit_fc = params['hidden_unit_fully_connected'] + self.setup_placeholder() + self.setup_embedding(embedding) + self.setup_model() + self.setup_optimizer() + + def encoder(self): + with tf.variable_scope('encoded_headline'): + headline_lstm = tf.contrib.rnn.BasicLSTMCell(self.hidden_unit_lstm) + self.encoded_headline, _ = tf.nn.dynamic_rnn(headline_lstm, self.headline_embedding, self.H, dtype=tf.float32) + with tf.variable_scope('encoded_document'): + document_lstm = tf.contrib.rnn.BasicLSTMCell(self.hidden_unit_lstm) + self.encoded_document, _ = tf.nn.dynamic_rnn(document_lstm, self.document_embedding, self.D, dtype=tf.float32) + + def decoder(self): + with tf.variable_scope('attention'): + attention_mechanism = BahdanauAttention(self.encoded_headline.get_shape()[-1], self.encoded_headline, memory_sequence_length=self.H) + lstm = tf.contrib.rnn.BasicLSTMCell(self.hidden_unit_lstm) + attention = AttentionWrapper(lstm, attention_mechanism, output_attention=False, attention_input_fn=lambda x, y: tf.concat([x, y], axis=-1)) + weight, _ = tf.nn.dynamic_rnn(attention, self.encoded_document, dtype=tf.float32) + self.weight = tf.reduce_mean(weight, axis=1) + + def fully_connected(self): + with tf.variable_scope('fully_connected'): + h1 = tf.contrib.layers.fully_connected(self.weight, self.hidden_unit_fc, activation_fn=None) + h2 = tf.nn.dropout(tf.nn.relu(tf.contrib.layers.batch_norm(h1)), self.dropout_fc) + self.logits = tf.contrib.layers.fully_connected(h2, 2, activation_fn=tf.sigmoid) + + def setup_placeholder(self): + self.headline = tf.placeholder(tf.int32, shape=[None, None], name='headline') + self.document = tf.placeholder(tf.int32, shape=[None, None], name='document') + self.H = tf.placeholder(tf.int32, shape=[None], name='H') + self.D = tf.placeholder(tf.int32, shape=[None], name='D') + self.stance = tf.placeholder(tf.int32, shape=[None, 2], name='stance') + self.dropout_in = tf.placeholder(tf.float32, shape=[], name='dropout_in') + self.dropout_fc = tf.placeholder(tf.float32, shape=[], name='dropout_fc') + + def setup_embedding(self, embedding): + _embedding = tf.Variable(embedding, name='embedding', dtype=tf.float32, trainable=False) + headline_embedding = tf.nn.embedding_lookup(_embedding, self.headline, name='headline_embedding') + document_embedding = tf.nn.embedding_lookup(_embedding, self.document, name='document_embedding') + self.headline_embedding = tf.nn.dropout(headline_embedding, self.dropout_in) + self.document_embedding = tf.nn.dropout(document_embedding, self.dropout_in) + + def setup_model(self): + self.encoder() + self.decoder() + self.fully_connected() + + def setup_optimizer(self): + self.loss = tf.losses.mean_squared_error(self.stance, self.logits) + self.optimizer = tf.train.AdamOptimizer().minimize(self.loss) + + def feed(self, mini_batch, train=True): + headline, document, stance, H, D = mini_batch + input_feed = {self.headline: headline, self.document: document, self.H: H, self.D: D, self.stance: stance, self.dropout_in: self.keep_prob_in if train else 1, self.dropout_fc: self.keep_prob_fc if train else 1} + return input_feed + + def train(self, session, mini_batch): + _, loss = session.run([self.optimizer, self.loss], self.feed(mini_batch)) + return loss + + def test(self, session, mini_batch): + logits, loss = session.run([self.logits, self.loss], self.feed(mini_batch, False)) + return logits, loss + + def evaluate(self, session, dataset, batch_size, th): + prob = [] + acc = 0.0 + counter = {'agree': 0, 'disagree': 0, 'discuss': 0} + for j in range(0, len(dataset), batch_size): + data = self.create_minibatch(dataset, batch_size, j) + logits, loss = self.test(session, data) + if len(dataset) - j < batch_size: + logits = logits[:len(dataset) - j] + for i in logits: + prob.append(i) + stances = data[2] + for i in range(len(logits)): + if logits[i, 0] > th or logits[i, 1] > th: + s = np.argmax(logits[i]) + if stances[i, s] == 1: + acc += 1 + if s == 0: + counter['agree'] += 1 + elif s == 1: + counter['disagree'] += 1 + else: + counter['discuss'] += 1 + if np.sum(stances[i]) == 0: + acc += 1 + print('Accuracy: {:.5f} %'.format(acc / len(dataset))) + print('Counter:', counter, '\n') + return prob + + def create_minibatch(self, dataset, batch_size, start): + minibatch = list(dataset[start:start + batch_size]) + if len(minibatch) < batch_size: + minibatch.extend(dataset[-(batch_size - len(minibatch)):]) + headline_length = max(len(h) for h, d, s in minibatch) + document_length = max(len(d) for h, d, s in minibatch) + headlines, documents, stances = [], [], [] + for h, d, s in minibatch: + headlines.append(h + [(0) for _ in range(headline_length - len(h))]) + documents.append(d + [(0) for _ in range(document_length - len(d))]) + stances.append(s) + return np.array(headlines), np.array(documents), np.array(stances), np.array([headline_length for _ in range(batch_size)]), np.array([document_length for _ in range(batch_size)]) + + def run(self, session, dataset, num_epoch, batch_size, threshold): + train, test = dataset + start = time.time() + for i in range(num_epoch): + random.shuffle(train) + for j in range(0, len(train), batch_size): + mini_batch = self.create_minibatch(train, batch_size, j) + loss = self.train(session, mini_batch) + if j % (50 * batch_size) == 0: + print('epoch {} iteration {}, elapsed {:.2f} min, train loss: {:.5f}'.format(i, j / batch_size, (time.time() - start) / 60, loss)) + if i % 5 == 0: + self.evaluate(session, test, batch_size, threshold) + tf.train.Saver().save(session, './model/model_{}_epoch.chk'.format(i + 1)) diff --git a/dataset/preprocessed/52.txt b/dataset/preprocessed/52.txt new file mode 100644 index 0000000..98d734d --- /dev/null +++ b/dataset/preprocessed/52.txt @@ -0,0 +1,71 @@ +import numpy as np +import tensorflow as tf + + +class AutoEncoder: + + def __init__(self, model_name, keep_prob, learning_rate, dim): + self.dim = dim + self.keep_prob = keep_prob + self.model_name = model_name + self.learning_rate = learning_rate + self.declare_placeholder() + self.contruct_network() + self.define_loss() + self.define_optimizer() + self.define_saver() + self.create_session() + self.declare_init() + self.run_init() + + def declare_placeholder(self): + self.x = tf.placeholder(tf.float32, shape=(None, self.dim[0], self.dim[1])) + self.dropout = tf.placeholder(tf.float32) + + def contruct_network(self): + self.encoder_1W = tf.Variable(tf.truncated_normal([self.dim[0] * self.dim[1], int(self.dim[0] * self.dim[1] / 2)])) + self.encoder_1b = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2)])) + self.encoder_2W = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2), int(self.dim[0] * self.dim[1] / 4)])) + self.encoder_2b = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 4)])) + self.decoder_1W = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 4), int(self.dim[0] * self.dim[1] / 2)])) + self.decoder_1b = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2)])) + self.decoder_2W = tf.Variable(tf.truncated_normal([int(self.dim[0] * self.dim[1] / 2), self.dim[0] * self.dim[1]])) + self.decoder_2b = tf.Variable(tf.truncated_normal([self.dim[0] * self.dim[1]])) + self.enc1 = tf.nn.tanh(tf.nn.bias_add(tf.matmul(tf.reshape(self.x, [-1, self.dim[0] * self.dim[1]]), self.encoder_1W), self.encoder_1b)) + self.enc1 = tf.nn.dropout(self.enc1, keep_prob=self.dropout) + self.feat = tf.nn.tanh(tf.nn.bias_add(tf.matmul(self.enc1, self.encoder_2W), self.encoder_2b)) + self.feat = tf.nn.dropout(self.feat, keep_prob=self.dropout) + self.dec1 = tf.nn.tanh(tf.nn.bias_add(tf.matmul(self.feat, self.decoder_1W), self.decoder_1b)) + self.dec1 = tf.nn.dropout(self.dec1, keep_prob=self.dropout) + self.output = tf.reshape(tf.nn.bias_add(tf.matmul(self.dec1, self.decoder_2W), self.decoder_2b), [-1, self.dim[0], self.dim[1]]) + self.output = tf.nn.dropout(self.output, keep_prob=self.dropout) + + def define_loss(self): + self.loss = tf.reduce_mean(tf.square(self.output - self.x)) + + def define_optimizer(self): + self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss) + + def declare_init(self): + self.init = tf.global_variables_initializer() + + def run_init(self): + self.session.run(self.init) + + def create_session(self): + self.session = tf.Session() + + def define_saver(self): + self.saver = tf.train.Saver() + + def save_model(self): + self.saver.save(self.session, self.model_name) + + def restore_model(self): + self.saver.restore(self.session, self.model_name) + + def run_training(self, input): + self.session.run(self.optimizer, feed_dict={self.x: input, self.dropout: self.keep_prob}) + + def get_loss(self, input): + return self.session.run(self.loss, feed_dict={self.x: input, self.dropout: 1}) diff --git a/dataset/preprocessed/520.txt b/dataset/preprocessed/520.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/520.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/521.txt b/dataset/preprocessed/521.txt new file mode 100644 index 0000000..a243e13 --- /dev/null +++ b/dataset/preprocessed/521.txt @@ -0,0 +1,185 @@ +import numpy as np +import tensorflow as tf +import logging +from adam import ADAMOptimizer +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) + + +class RBM(object): + + def __init__(self, n_features, n_hidden, visible_placeholder=None, contrastive_divergence_level=1, learning_rate=0.001, persistent_contrastive_divergence=True, n_fantasy_states=100, name='rbm'): + """ + A restricted boltzmann machine implemented in tensorflow + + :param n_features: The input dimensionality + :param n_hidden: The hidden layer size + :param visible_placeholder: Optional. Is used for chaining. + A tensorflow tensor that represents the input [batch x n_features]. + :param contrastive_divergence_level: How often to sample to get the model distribution + :param learning_rate: The learning rate for gradient ascent on the likelihood + :param persistent_contrastive_divergence: Use a persistent model state for model distribution. Replaces + contrastive divergence + :param fantasy_states: Number of fantasy samples to keep in the persistent mode. + :param name: tensorflow name of this model + """ + if visible_placeholder is None: + self.visible_placeholder = tf.placeholder(tf.float32, shape=(None, n_features)) + else: + self.visible_placeholder = visible_placeholder + self.n_features = n_features + self.n_hidden = n_hidden + self.learning_rate = learning_rate + self.contrastive_divergence_level = contrastive_divergence_level + self.persistent_contrastive_divergence = persistent_contrastive_divergence + self.name = name + with tf.name_scope(name): + self.w = tf.Variable(tf.random_normal((self.n_features, self.n_hidden), mean=0.0, stddev=np.sqrt(6.0 / (self.n_features + self.n_hidden))), name='weights') + self.b_h = tf.Variable(tf.random_normal((self.n_hidden,), mean=0.0, stddev=0.1), name='hidden_bias') + self.b_v = tf.Variable(tf.random_normal((self.n_features,), mean=0.0, stddev=0.1), name='visible_bias') + self.hidden_data_probability = tf.nn.sigmoid(tf.matmul(self.visible_placeholder, self.w) + self.b_h) + self.hidden_state_plus = RBM._sample_from(self.hidden_data_probability, 'hidden_state_plus') + self.hidden_placeholder = tf.placeholder(dtype=tf.float32, shape=(None, self.n_hidden)) + visible_prop = tf.nn.sigmoid(tf.matmul(self.hidden_placeholder, self.w, transpose_b=True) + self.b_v) + self.visible_from_hidden = RBM._sample_from(visible_prop, 'visible_from_hidden') + if self.persistent_contrastive_divergence: + self.visible_state_persistent = tf.Variable(initial_value=tf.zeros((n_fantasy_states, self.n_features), dtype=tf.float32), trainable=False, name='visible_model_sampling_persistent_state') + self.hidden_state_persistent = tf.Variable(initial_value=tf.zeros((n_fantasy_states, self.n_hidden), dtype=tf.float32), trainable=False, name='hidden_model_sampling_persistent_state') + current_hidden_state = self.hidden_state_persistent + current_visible_state = self.visible_state_persistent + for i in range(self.contrastive_divergence_level): + hidden_probability = tf.nn.sigmoid(tf.matmul(current_visible_state, self.w) + self.b_h) + current_hidden_state = RBM._sample_from(hidden_probability, 'hidden_state_minus_{}'.format(i)) + visible_probability = tf.nn.sigmoid(tf.matmul(current_hidden_state, self.w, transpose_b=True) + self.b_v) + current_visible_state = RBM._sample_from(visible_probability, 'visible_state_minus_{}'.format(i)) + self.visible_state_minus = tf.assign(self.visible_state_persistent, current_visible_state, name='visible_state_minus') + self.hidden_state_minus = tf.assign(self.hidden_state_persistent, current_hidden_state, name='hidden_state_minus') + else: + current_hidden = self.hidden_data_probability + for i in range(self.contrastive_divergence_level): + visible_prop = tf.nn.sigmoid(tf.matmul(current_hidden, self.w, transpose_b=True) + self.b_v) + self.visible_state_minus = RBM._sample_from(visible_prop, 'visible_state_minus_{}'.format(i)) + hidden_prop = tf.nn.sigmoid(tf.matmul(self.visible_state_minus, self.w) + self.b_h) + self.hidden_state_minus = RBM._sample_from(hidden_prop, 'hidden_state_minus_{}'.format(i)) + gradient_w = (tf.matmul(self.visible_placeholder, self.hidden_state_plus, transpose_a=True) - tf.matmul(self.visible_state_minus, self.hidden_state_minus, transpose_a=True)) / tf.cast(tf.shape(self.visible_placeholder)[0], dtype=tf.float32) + gradient_b_h = tf.reduce_mean(self.hidden_state_plus, reduction_indices=0) - tf.reduce_mean(self.hidden_state_minus, reduction_indices=0) + gradient_b_v = tf.reduce_mean(self.visible_placeholder, reduction_indices=0) - tf.reduce_mean(self.visible_state_minus, reduction_indices=0) + self.reconstruction_error = tf.reduce_mean(tf.reduce_sum((self.visible_placeholder - self.visible_state_minus) ** 2, axis=-1)) + beta = 0.2 + self.update_w = ADAMOptimizer(self.w, minimize=False, beta1=beta, beta2=beta, learning_rate=self.learning_rate).train_step(gradient_w) + self.update_b_h = ADAMOptimizer(self.b_h, minimize=False, beta1=beta, beta2=beta, learning_rate=learning_rate).train_step(gradient_b_h) + self.update_b_v = ADAMOptimizer(self.b_v, minimize=False, beta1=beta, beta2=beta, learning_rate=learning_rate).train_step(gradient_b_v) + self.train_step = [self.update_w, self.update_b_h, self.update_b_v] + self.logarithmic_energy = tf.reduce_sum(tf.matmul(self.visible_placeholder, self.w) * self.hidden_placeholder, axis=-1) + tf.reduce_sum(self.visible_placeholder * self.b_v, axis=-1) + tf.reduce_sum(self.hidden_placeholder * self.b_h, axis=-1) + self.state_probabilities = tf.nn.softmax(self.logarithmic_energy) + + def _sample_from(vector, name): + return tf.cast(tf.less(tf.random_uniform(tf.shape(vector)), vector), tf.float32, name=name) + + def train(self, features, n_iterations=1000, batch_size=100, session=None): + """ + Perform training of the boltzmann machine given an input distribution + + :param features: numpy array representing the input distribution [n_samples x n_features] + :param n_iterations: Number of iterations to train + :param batch_size: Number of samples to use in one training iteration + :param session: tensorflow session to use for the training. If None, default will be used. + """ + if session is None: + session = tf.get_default_session() + train_range = range(n_iterations) + n_samples = features.shape[0] + for i in train_range: + random_indices = np.random.randint(0, n_samples, size=batch_size) + feed_dict = {self.visible_placeholder: features[random_indices]} + reconstruction_error, _, _, _ = session.run([self.reconstruction_error, self.update_b_h, self.update_w, self.update_b_v], feed_dict=feed_dict) + if i % 20 == 0: + logger.info('reconstruction error in iteration {} of {} is {}'.format(i, n_iterations, reconstruction_error)) + + def sample_visible(self, n_samples=None, hidden_states=None, session=None): + """ + Sample visible layer states from the boltzmann machine. Either use hidden_states for initializing the hidden + layer if given or use a binary random hidden_state. + + :param n_samples: Optional. Number of states to sample. + :param hidden_states: Optional. If given, sample the visible states from these hidden states. + :param session: tensorflow session to use. If None, use default session. + :return: An array with visible states. The number of states returned is n_samples if given or the row number of + hidden_states. If nothing was given, a single sample is returned. + """ + if session is None: + session = tf.get_default_session() + if hidden_states is None: + hidden_states = np.random.randint(0, 2, size=(n_samples if n_samples is not None else 1, self.n_hidden)) + return session.run(self.visible_from_hidden, feed_dict={self.hidden_placeholder: hidden_states}) + + def sample_hidden(self, n_samples=None, visible_states=None, session=None): + """ + Sample hidden layer states from the boltzmann machine. Either use visible_states for initializing the visible + layer if given or use a binary random hidden_state. + + :param n_samples: Optional. Number of states to sample. + :param visible_states: Optional. If given, sample the hidden states from these visible states. + :param session: tensorflow session to use. If None, use default session. + :return: An array with hidden states. The number of states returned is n_samples if given or the row number of + visible_states. If nothing was given, a single sample is returned. + """ + if session is None: + session = tf.get_default_session() + if visible_states is None: + visible_states = np.random.randint(0, 2, size=(n_samples if n_samples is not None else 1, self.n_features)) + return session.run(self.hidden_state_plus, feed_dict={self.visible_placeholder: visible_states}) + + +if __name__ == '__main__': + import sklearn.datasets as datasets + import sklearn.svm as svm + digits = datasets.load_digits() + features = np.array(digits.data >= 8, dtype=np.float) + target = digits.target + n_features = features.shape[1] + n_hidden = 300 + n_hidden2 = 300 + n_hidden3 = 300 + n_batch = 2000 + n_iterations = 1000 + input_placeholder = tf.placeholder(tf.float32, shape=(None, n_features)) + rbm = RBM(n_features, n_hidden, input_placeholder, name='rbm_1', n_fantasy_states=n_batch, persistent_contrastive_divergence=False, contrastive_divergence_level=10, learning_rate=0.001) + rbm2 = RBM(n_hidden, n_hidden2, rbm.hidden_state_plus, learning_rate=0.0001, name='rbm_2', n_fantasy_states=n_batch) + rbm3 = RBM(n_hidden2, n_hidden3, rbm2.hidden_state_plus, learning_rate=0.0001, name='rbm_3', n_fantasy_states=n_batch) + svc = svm.SVC(kernel='linear') + svc2 = svm.SVC(kernel='linear') + svc2.fit(features[:-100], target[:-100]) + direct_svc_score = svc2.score(features[-100:], target[-100:]) + session = tf.Session() + session.run(tf.initialize_all_variables()) + for i in range(n_iterations): + random_indices = np.random.randint(0, features.shape[0], size=n_batch) + session.run([rbm.update_w, rbm.update_b_h, rbm.update_b_v], feed_dict={input_placeholder: features[random_indices]}) + if i % 100 == 0: + hidden = session.run(rbm.hidden_state_plus, feed_dict={input_placeholder: features}) + svc.fit(hidden[:-100], target[:-100]) + print('svc score with rbm: {}, without rbm: {}'.format(svc.score(hidden[-100:], target[-100:]), direct_svc_score)) + print('reconstruction error: {}'.format(session.run(rbm.reconstruction_error, feed_dict={input_placeholder: features[random_indices]}))) + hd = session.run(rbm.hidden_state_plus, feed_dict={input_placeholder: features[random_indices]}) + p = session.run(rbm.state_probabilities, feed_dict={input_placeholder: features[random_indices], rbm.hidden_placeholder: hd}) + print(p) + for i in range(n_iterations): + random_indices = np.random.randint(0, features.shape[0], size=n_batch) + session.run([rbm2.update_w, rbm2.update_b_h, rbm2.update_b_v], feed_dict={input_placeholder: features[random_indices]}) + for i in range(n_iterations): + random_indices = np.random.randint(0, features.shape[0], size=n_batch) + session.run([rbm3.update_w, rbm3.update_b_h, rbm3.update_b_v], feed_dict={input_placeholder: features[random_indices]}) + if i % 500 == 0: + hidden = session.run(rbm3.hidden_state_plus, feed_dict={input_placeholder: features}) + svc.fit(hidden[:-100], target[:-100]) + print('svc score with rbm: {}, without rbm: {}'.format(svc.score(hidden[-100:], target[-100:]), direct_svc_score)) + import matplotlib.pyplot as plt + for i in range(20): + hidden = session.run(rbm2.hidden_state_plus, feed_dict={input_placeholder: features[0].reshape((1, 64))}) + hidden = np.random.randint(0, 2, size=(1, n_hidden3)) + hidden3 = session.run(rbm3.visible_from_hidden, feed_dict={rbm3.hidden_placeholder: hidden}) + hidden2 = session.run(rbm2.visible_from_hidden, feed_dict={rbm2.hidden_placeholder: hidden3}) + visible = session.run(rbm.visible_from_hidden, feed_dict={rbm.hidden_placeholder: hidden2}) + plt.imshow(visible.reshape(8, 8), cmap='gray') + plt.show() diff --git a/dataset/preprocessed/522.txt b/dataset/preprocessed/522.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/522.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/523.txt b/dataset/preprocessed/523.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/523.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/524.txt b/dataset/preprocessed/524.txt new file mode 100644 index 0000000..da3d2af --- /dev/null +++ b/dataset/preprocessed/524.txt @@ -0,0 +1,467 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class GRUAttnCell(tf.nn.rnn_cell.GRUCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(GRUAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(gru_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(tf.nn.rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + return out, gru_state + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(LSTMAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + return out, lstm_state + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + -vocab_dim: dimension of the embeddings + """ + + def __init__(self, size, vocab_dim, name): + self.size = size + self.vocab_dim = vocab_dim + self.name = name + + def encode(self, inputs, masks, encoder_state_input=None, attention_inputs=None, model_type='gru'): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(self.name): + if attention_inputs is None: + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(self.size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(self.size) + else: + raise Exception('Must specify model type.') + elif model_type == 'gru': + cell = GRUAttnCell(self.size, attention_inputs) + elif model_type == 'lstm': + cell = LSTMAttnCell(self.size, attention_inputs) + else: + raise Exception('Must specify model type.') + outputs, final_state = tf.nn.dynamic_rnn(cell, inputs, sequence_length=masks, dtype=tf.float32, initial_state=encoder_state_input) + return outputs, final_state + + +class Decoder(object): + + def __init__(self, output_size, name): + self.output_size = output_size + self.name = name + + def decode(self, knowledge_rep, masks, dropout, state_size, model_type='gru'): + """ + takes in a knowledge representation + # and output a probability estimation over + # all paragraph tokens on which token should be + # the start of the answer span, and which should be + # the end of the answer span. + + # :param knowledge_rep: it is a representation of the paragraph and question, + # decided by how you choose to implement the encoder + # :return: + # """ + with tf.variable_scope(self.name): + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(state_size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(state_size) + else: + raise Exception('Must specify model type.') + knowledge_rep = tf.squeeze(knowledge_rep, -1) + with vs.variable_scope('answer_start'): + start_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer1'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer2'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer3'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer4'): + start_probs = tf.nn.rnn_cell._linear(start_probs, self.output_size, True, 1.0) + with vs.variable_scope('answer_end'): + end_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer1'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer2'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer3'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer4'): + end_probs = tf.nn.rnn_cell._linear(end_probs, self.output_size, True, 1.0) + bool_masks = tf.cast(tf.sequence_mask(masks, maxlen=self.output_size), tf.float32) + a = tf.constant(-1e+30) + b = tf.constant(1.0) + add_mask = a * (b - bool_masks) + start_probs = start_probs + add_mask + end_probs = end_probs + add_mask + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, encoder, decoder, pretrained_embeddings, max_ctx_len, max_q_len, flags): + """ + Initializes your System + + :param encoder: tuple of 2 encoders that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.question_encoder, self.context_encoder = encoder + self.decoder = decoder + self.max_ctx_len = max_ctx_len + self.max_q_len = max_q_len + self.embed_size = encoder[0].vocab_dim + self.flags = flags + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.max_ctx_len), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.max_q_len), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def mixer(self, final_q_state, ctx_states, model_type='gru'): + if model_type == 'gru': + pass + elif model_type == 'lstm': + final_q_state = final_q_state[-1] + else: + raise Exception('Must specify model type.') + with vs.variable_scope('mixer'): + ht = tf.nn.rnn_cell._linear(final_q_state, self.flags.state_size, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(ctx_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + ctx_state_rep = ctx_states * scores + return ctx_state_rep + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + question_states, final_question_state = self.question_encoder.encode(self.question_embeddings, self.mask_q_placeholder, encoder_state_input=None, attention_inputs=None, model_type=self.flags.model_type) + ctx_states, final_ctx_state = self.context_encoder.encode(self.context_embeddings, self.mask_ctx_placeholder, encoder_state_input=None, attention_inputs=None, model_type=self.flags.model_type) + feed_states = batch_matmul(ctx_states, tf.expand_dims(final_question_state, 2)) + self.start_probs, self.end_probs = self.decoder.decode(feed_states, self.mask_ctx_placeholder, self.dropout_placeholder, self.flags.state_size, model_type=self.flags.model_type) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.question_embeddings = tf.reshape(question_embeddings, [-1, self.max_q_len, self.embed_size]) + context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + self.context_embeddings = tf.reshape(context_embeddings, [-1, self.max_ctx_len, self.embed_size]) + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = np.array(map(np.array, context_batch)) + input_feed[self.question_placeholder] = np.array(map(np.array, question_batch)) + input_feed[self.mask_ctx_placeholder] = np.array(map(np.array, mask_ctx_batch)) + input_feed[self.mask_q_placeholder] = np.array(mask_q_batch) + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs + + def answer(self, session, data): + data = np.array(data).T + yp, yp2 = self.decode(session, *data) + a_s = np.argmax(yp, axis=1) + a_e = np.argmax(yp2, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset) + else: + sampled = dataset[np.random.choice(dataset.shape[0], sample)] + a_s, a_e = self.answer(session, sampled) + f1 = [] + em = [] + sampled = sampled.T + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][sampled[2][i][0]:sampled[2][i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + em.append(exact_match_score(pred_words, actual_words)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), None, sample)) + return f1, em + + def run_epoch(self, sess, train_set, val_set, context): + prog_train = Progbar(target=1 + int(len(train_set) / self.flags.batch_size)) + for i, batch in enumerate(minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + if self.flags.debug == 0 or self.flags.debug == 1: + prog_val = Progbar(target=1 + int(len(val_set) / self.flags.batch_size)) + for i, batch in enumerate(minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + train_f1, train_em = self.evaluate_answer(sess, train_set, context=context[0], sample=100, log=True, eval_set='-Epoch TRAIN-') + val_f1, val_em = self.evaluate_answer(sess, val_set, context=context[1], sample=100, log=True, eval_set='-Epoch VAL-') + + def train(self, session, saver, dataset, contexts, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + train_dataset, val_dataset = dataset + train_mask = [None, None] + val_mask = [None, None] + train_dataset[0], train_mask[0] = self.pad(train_dataset[0], self.max_ctx_len) + train_dataset[1], train_mask[1] = self.pad(train_dataset[1], self.max_q_len) + val_dataset[0], val_mask[0] = self.pad(val_dataset[0], self.max_ctx_len) + val_dataset[1], val_mask[1] = self.pad(val_dataset[1], self.max_q_len) + for i in range(1, len(train_dataset[0])): + assert len(train_dataset[0][i]) == len(train_dataset[0][i - 1]), 'Incorrectly padded train context' + assert len(train_dataset[1][i]) == len(train_dataset[1][i - 1]), 'Incorrectly padded train question' + for i in range(1, len(val_dataset[0])): + assert len(val_dataset[0][i]) == len(val_dataset[0][i - 1]), 'Incorrectly padded val context' + assert len(val_dataset[1][i]) == len(val_dataset[1][i - 1]), 'Incorrectly padded val question' + print('Training/val data padding verification completed.') + train_dataset.extend(train_mask) + val_dataset.extend(val_mask) + train_dataset = np.array(train_dataset).T + val_dataset = np.array(val_dataset).T + train_context = contexts[0] + val_context = contexts[1] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = train_dataset[:self.flags.batch_size] + val_dataset = val_dataset[:self.flags.batch_size] + num_epochs = 20 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, context=val_context) + logging.info('Saving model in %s', train_dir) + saver.save(session, train_dir) + self.evaluate_answer(session, train_dataset, train_context, sample=None, log=True, eval_set='-FINAL TRAIN-') + self.evaluate_answer(session, val_dataset, val_context, sample=None, log=True, eval_set='-FINAL VAL-') diff --git a/dataset/preprocessed/525.txt b/dataset/preprocessed/525.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/525.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/526.txt b/dataset/preprocessed/526.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/526.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/527.txt b/dataset/preprocessed/527.txt new file mode 100644 index 0000000..5670359 --- /dev/null +++ b/dataset/preprocessed/527.txt @@ -0,0 +1,234 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +"""Analyzing solutions from https://github.com/hankcs/udacity-deep-learning/blob/master/3_regularization.py""" +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('L2 regularization(beta=%.5f) Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +print('Best beta=%f, accuracy=%.1f%%' % (beta_val[np.argmax(accuracy_val)], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Overfitting with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y1 = tf.nn.dropout(y1, 0.5) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + global_step = tf.Variable(0) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, fc1_size], stddev=np.sqrt(2.0 / (image_size * image_size)))) + b1 = tf.Variable(tf.zeros([fc1_size])) + W2 = tf.Variable(tf.truncated_normal([fc1_size, fc2_size], stddev=np.sqrt(2.0 / fc1_size))) + b2 = tf.Variable(tf.zeros([fc2_size])) + W3 = tf.Variable(tf.truncated_normal([fc2_size, fc3_size], stddev=np.sqrt(2.0 / fc2_size))) + b3 = tf.Variable(tf.zeros([fc3_size])) + W4 = tf.Variable(tf.truncated_normal([fc3_size, num_labels], stddev=np.sqrt(2.0 / fc3_size))) + b4 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + logits = tf.matmul(y3, W4) + b4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + valid_logits = tf.matmul(y3_valid, W4) + b4 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + test_logits = tf.matmul(y3_test, W4) + b4 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/528.txt b/dataset/preprocessed/528.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/528.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/529.txt b/dataset/preprocessed/529.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/529.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/53.txt b/dataset/preprocessed/53.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/53.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/530.txt b/dataset/preprocessed/530.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/530.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/531.txt b/dataset/preprocessed/531.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/531.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/532.txt b/dataset/preprocessed/532.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/532.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/533.txt b/dataset/preprocessed/533.txt new file mode 100644 index 0000000..3f9eff1 --- /dev/null +++ b/dataset/preprocessed/533.txt @@ -0,0 +1,253 @@ +import tensorflow as tf +from tqdm import tqdm +from utils import BatchGenerator +import os +import pdb + + +class NNClassifier: + + def _inference(self, X, is_train): + self.hidden_size = hidden_size = 30 + self.embed_placeholder = tf.placeholder(tf.float32, shape=[None, 300]) + dropout = 0.3 + word_embedding = self.embed_placeholder + embedding_b = tf.nn.embedding_lookup(word_embedding, X[:, (0), :]) + embedding_q = tf.nn.embedding_lookup(word_embedding, X[:, (1), :]) + with tf.variable_scope('Body_GRU'): + with tf.variable_scope('Body_GRU_fw'): + cell_b_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + with tf.variable_scope('Body_GRU_bw'): + cell_b_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + lengths_b = tf.reduce_sum(tf.sign(X[:, (0), :]), axis=-1) + output_b, out_state_b = tf.nn.bidirectional_dynamic_rnn(cell_b_fw, cell_b_fw, embedding_b, sequence_length=lengths_b, dtype=tf.float32) + output_b = tf.concat([output_b[0], output_b[1]], axis=-1) + out_state_b = tf.concat([out_state_b[0], out_state_b[1]], axis=-1) + with tf.variable_scope('Question_GRU'): + with tf.variable_scope('Question_GRU_fw'): + cell_q_fw = tf.nn.rnn_cell.GRUCell(hidden_size) + with tf.variable_scope('Question_GRU_bw'): + cell_q_bw = tf.nn.rnn_cell.GRUCell(hidden_size) + lengths_q = tf.reduce_sum(tf.sign(X[:, (1), :]), axis=-1) + output_q, out_state_q = tf.nn.bidirectional_dynamic_rnn(cell_q_fw, cell_q_fw, embedding_q, sequence_length=lengths_q, dtype=tf.float32) + output_q = tf.concat([output_q[0], output_q[1]], axis=-1) + out_state_q = tf.concat([out_state_q[0], out_state_q[1]], axis=-1) + C_d = self._dynamic_coattention(output_b, output_q) + with tf.variable_scope('match_lstm_GRU_dynamic_coattention'): + with tf.variable_scope('match_gru_fw'): + cell_m_fw = tf.nn.rnn_cell.GRUCell(2 * hidden_size) + with tf.variable_scope('match_gru_bw'): + cell_m_bw = tf.nn.rnn_cell.GRUCell(2 * hidden_size) + lengths_q = tf.reduce_sum(tf.sign(X[:, (1), :]), axis=-1) + output_m, out_state_m = tf.nn.bidirectional_dynamic_rnn(cell_m_fw, cell_m_fw, C_d, sequence_length=lengths_q, dtype=tf.float32) + output_m = tf.concat([output_m[0], output_m[1]], axis=-1) + out_state_m = tf.concat([out_state_m[0], out_state_m[1]], axis=-1) + output = tf.concat([out_state_m, out_state_q, out_state_b], axis=-1) + output = tf.layers.dense(inputs=output, units=10) + output = tf.nn.relu(output) + dense = tf.layers.dense(inputs=output, units=self._n_classes) + return dense + + def _dynamic_coattention(self, output_b, output_q): + with tf.variable_scope('dynamic_coattention'): + timestep = tf.shape(output_q)[1] + batch_size = tf.shape(output_q)[0] + L = tf.matmul(output_b, tf.transpose(output_q, [0, 2, 1])) + A_q = tf.nn.softmax(L, dim=2, name='A_q') + A_b = tf.transpose(tf.nn.softmax(L, dim=1), [0, 2, 1], name='A_b') + C_q = tf.matmul(tf.transpose(A_q, [0, 2, 1]), output_b) + C_b = tf.matmul(tf.transpose(A_b, [0, 2, 1]), output_q) + C_d = tf.matmul(tf.transpose(A_b, [0, 2, 1]), tf.concat([output_q, C_q], axis=-1)) + return C_d + + def _match_lstm(self, i, output_b, output_q, a_array): + hidden_size = self.hidden_size * 2 + timestep = tf.shape(output_b)[1] + output_q_i = tf.reshape(output_q[:, (i), :], [-1, 1, hidden_size]) + with tf.variable_scope('match_lstm'): + we = tf.get_variable(name='we', shape=[1, 1, hidden_size], dtype=tf.float32, initializer=tf.truncated_normal_initializer(0.0, 1)) + W_s = tf.get_variable(name='W_s', shape=[hidden_size, hidden_size], dtype=tf.float32, initializer=tf.truncated_normal_initializer(0.0, 1)) + W_t = tf.get_variable(name='W_t', shape=[hidden_size, hidden_size], dtype=tf.float32, initializer=tf.truncated_normal_initializer(0.0, 1)) + temp1 = tf.reshape(tf.matmul(tf.reshape(output_b, [-1, hidden_size]), W_s), [-1, timestep, hidden_size]) + temp2 = tf.reshape(tf.matmul(tf.reshape(output_q_i, [-1, hidden_size]), W_t), [-1, 1, hidden_size]) + middle = tf.tanh(temp1 + temp2) + e_i = tf.reduce_sum(we * middle, axis=-1, keep_dims=True) + a_i = tf.nn.softmax(e_i, dim=1) + a_i = tf.reduce_sum(a_i * output_b, axis=1, keep_dims=False) + a_array = a_array.write(i, tf.concat([a_i, tf.reshape(output_q_i, [-1, hidden_size])], axis=-1)) + i = tf.add(i, 1) + return i, output_b, output_q, a_array + + def _attention(self, inputs, attention_size, time_major=False, return_alphas=False): + """ + Attention mechanism layer which reduces RNN/Bi-RNN outputs with Attention vector. + The idea was proposed in the article by Z. Yang et al., "Hierarchical Attention Networks + for Document Classification", 2016: http://www.aclweb.org/anthology/N16-1174. + Args: + inputs: The Attention inputs. + Matches outputs of RNN/Bi-RNN layer (not final state): + In case of RNN, this must be RNN outputs `Tensor`: + If time_major == False (default), this must be a tensor of shape: + `[batch_size, max_time, cell.output_size]`. + If time_major == True, this must be a tensor of shape: + `[max_time, batch_size, cell.output_size]`. + In case of Bidirectional RNN, this must be a tuple (outputs_fw, outputs_bw) containing the forward and + the backward RNN outputs `Tensor`. + If time_major == False (default), + outputs_fw is a `Tensor` shaped: + `[batch_size, max_time, cell_fw.output_size]` + and outputs_bw is a `Tensor` shaped: + `[batch_size, max_time, cell_bw.output_size]`. + If time_major == True, + outputs_fw is a `Tensor` shaped: + `[max_time, batch_size, cell_fw.output_size]` + and outputs_bw is a `Tensor` shaped: + `[max_time, batch_size, cell_bw.output_size]`. + attention_size: Linear size of the Attention weights. + time_major: The shape format of the `inputs` Tensors. + If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`. + If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`. + Using `time_major = True` is a bit more efficient because it avoids + transposes at the beginning and end of the RNN calculation. However, + most TensorFlow data is batch-major, so by default this function + accepts input and emits output in batch-major form. + return_alphas: Whether to return attention coefficients variable along with layer's output. + Used for visualization purpose. + Returns: + The Attention output `Tensor`. + In case of RNN, this will be a `Tensor` shaped: + `[batch_size, cell.output_size]`. + In case of Bidirectional RNN, this will be a `Tensor` shaped: + `[batch_size, cell_fw.output_size + cell_bw.output_size]`. + """ + if isinstance(inputs, tuple): + inputs = tf.concat(inputs, 2) + if time_major: + inputs = tf.array_ops.transpose(inputs, [1, 0, 2]) + inputs_shape = inputs.shape + sequence_length = inputs_shape[1].value + hidden_size = inputs_shape[2].value + with tf.variable_scope('attention'): + W_omega = tf.get_variable(name='W', shape=[hidden_size, attention_size], dtype=tf.float32, initializer=tf.random_normal_initializer(0.0, 0.1)) + b_omega = tf.get_variable(name='b', shape=[attention_size], dtype=tf.float32, initializer=tf.random_normal_initializer(0.0, 0.1)) + u_omega = tf.get_variable(name='u', shape=[attention_size], dtype=tf.float32, initializer=tf.random_normal_initializer(0.0, 0.1)) + v = tf.tanh(tf.matmul(tf.reshape(inputs, [-1, hidden_size]), W_omega) + tf.reshape(b_omega, [1, -1])) + vu = tf.matmul(v, tf.reshape(u_omega, [-1, 1])) + exps = tf.reshape(tf.exp(vu), [-1, sequence_length]) + alphas = exps / tf.reshape(tf.reduce_sum(exps, 1), [-1, 1]) + output = tf.reduce_sum(inputs * tf.reshape(alphas, [-1, sequence_length, 1]), 1) + if not return_alphas: + return output + else: + return output, alphas + + def _iter(self, X, y, tensor_loss, train_op, placeholder, metric_tensors): + self._session.run(tf.local_variables_initializer()) + metric_scores = {} + for metric in self._metrics: + metric_scores[metric] = 0 + batch_generator = BatchGenerator(X, y, self._batch_size) + for b in tqdm(range(X.shape[0] // self._batch_size + 1)): + batch = next(batch_generator) + feed_dict = {placeholder['x']: batch['x'], placeholder['y']: batch['y'], self.embed_placeholder: self._embedding} + if train_op is not None: + loss, _, metrics = self._session.run([tensor_loss, train_op, metric_tensors], feed_dict=feed_dict) + else: + loss, metrics = self._session.run([tensor_loss, metric_tensors], feed_dict=feed_dict) + if train_op == None: + self._history.append(metrics['accuracy'][1]) + if self._early_stop != None: + self._history[-1] = max(self._history[-1:-self._early_stop - 1:-1]) + summary = tf.Summary() + summary.value.add(tag='loss', simple_value=float(loss)) + print('loss=%f' % loss) + for metric in self._metrics: + score = float(metrics[metric][1]) + summary.value.add(tag=metric, simple_value=score) + print(', %s=%f' % (metric, score), end='') + print('\n', end='') + return summary + + def __init__(self, learning_rate=0.1, batch_size=1, n_iters=10, name='dnn', valid=None, embedding=None, early_stop=None): + self._batch_size = batch_size + self._n_iters = n_iters + self._metrics = {'accuracy': tf.metrics.accuracy} + self._global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') + self._loss = tf.losses.softmax_cross_entropy + self._optimizer = tf.train.AdamOptimizer(learning_rate) + self._name = name + self._valid = valid + self._embedding = embedding + self._history = [] + self._early_stop = early_stop + + def __del__(self): + if not self._session._closed: + self._session.close() + tf.reset_default_graph() + + def fit(self, X, y): + self._n_classes = y.shape[1] + os.mkdir(self._name) + os.mkdir(os.path.join(self._name, 'train')) + os.mkdir(os.path.join(self._name, 'valid')) + placeholder = {'x': tf.placeholder(tf.int32, shape=(None, X.shape[1], X.shape[2])), 'y': tf.placeholder(tf.int32, shape=(None, y.shape[1]))} + with tf.variable_scope('nn') as scope: + y_prob = self._inference(placeholder['x'], is_train=True) + loss = self._loss(placeholder['y'], y_prob) + reg_const = 0.0005 + l2 = reg_const * sum([tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()]) + loss += l2 + yp = tf.cast(placeholder['y'], tf.float32) + yt = y_prob + pos = tf.reduce_sum(yp * yt, axis=1) + neg = tf.reduce_max((1 - yp) * yt, reduction_indices=[1]) + train_op = self._optimizer.minimize(loss) + add_global_step_op = tf.assign(self._global_step, self._global_step + 1) + metric_tensors = {} + for metric in self._metrics: + y_pred_argmax = tf.argmax(y_prob, axis=-1) + y_true_argmax = tf.argmax(placeholder['y'], axis=-1) + metric_tensors[metric] = self._metrics[metric](y_true_argmax, y_pred_argmax) + gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1) + self._session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) + summary_writer = {'train': tf.summary.FileWriter(os.path.join(self._name, 'train'), self._session.graph), 'valid': tf.summary.FileWriter(os.path.join(self._name, 'valid'), self._session.graph)} + self._session.run(tf.global_variables_initializer()) + saver = tf.train.Saver() + ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoint/')) + if ckpt and ckpt.model_checkpoint_path: + saver.restore(self._session, ckpt.model_checkpoint_path) + initial_step = self._global_step.eval(session=self._session) + print('initial step: {}'.format(initial_step)) + for i in range(initial_step, self._n_iters): + print('training %i' % i) + _ = self._session.run([add_global_step_op]) + summary = self._iter(X, y, loss, train_op, placeholder, metric_tensors) + summary_writer['train'].add_summary(summary, i) + summary_writer['train'].flush() + saver.save(self._session, 'checkpoint/session', i) + if self._valid is not None: + print('evaluating %i' % i) + summary = self._iter(self._valid['x'], self._valid['y'], loss, None, placeholder, metric_tensors) + summary_writer['valid'].add_summary(summary, i) + summary_writer['valid'].flush() + if self._early_stop is not None: + print(len(self._history)) + if len(self._history) > self._early_stop and self._history[-1] == self._history[-1 - self._early_stop]: + print(self._history) + return + + def predict(self, X, prob=False, remove_s=False): + with tf.variable_scope('nn', reuse=True): + X_placeholder = tf.placeholder(tf.int32, shape=(None, X.shape[1], X.shape[2])) + y_prob = self._inference(X_placeholder, is_train=False) + if remove_s: + y_prob = tf.slice(y_prob, [0, 0], [X.shape[0], 5]) + y_prob = tf.nn.softmax(y_prob) + y_max = tf.reduce_max(y_prob, axis=-1) + y_pred = tf.cast(tf.equal(y_prob, tf.reshape(y_max, (-1, 1))), dtype=tf.int32) + y_, y_prob = self._session.run([y_pred, y_prob], feed_dict={X_placeholder: X, self.embed_placeholder: self._embedding}) + if not prob: + return y_ + else: + return y_, y_prob diff --git a/dataset/preprocessed/534.txt b/dataset/preprocessed/534.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/534.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/535.txt b/dataset/preprocessed/535.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/535.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/536.txt b/dataset/preprocessed/536.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/536.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/537.txt b/dataset/preprocessed/537.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/537.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/538.txt b/dataset/preprocessed/538.txt new file mode 100644 index 0000000..74d1997 --- /dev/null +++ b/dataset/preprocessed/538.txt @@ -0,0 +1,514 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import random +import math +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.ops import rnn_cell +from tensorflow.python.ops.nn import bidirectional_dynamic_rnn +from tensorflow.python.ops.nn import dynamic_rnn +from tensorflow.python.ops.nn import sparse_softmax_cross_entropy_with_logits +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +from evaluate import exact_match_score, f1_score +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class GRUAttnCell(rnn_cell.GRUCell): + + def __init__(self, num_units, encoder_output, scope=None): + self.hs = encoder_output + super(GRUAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = rnn_cell._linear(gru_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.hs * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.hs * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + return out, out + + +def matrix_multiply_with_batch(matrix=None, batch=None, matrixByBatch=True): + ret = None + if matrixByBatch: + ret = tf.scan(lambda a, x: tf.matmul(matrix, x), batch) + else: + n = batch.get_shape().as_list()[1] + m = batch.get_shape().as_list()[2] + c = matrix.get_shape().as_list()[1] + batch = tf.reshape(batch, [-1, m]) + ret = tf.matmul(batch, matrix) + ret = tf.reshape(ret, [-1, n, c]) + return ret + + +class Encoder(object): + + def __init__(self, state_size, embedding_size, output_size): + self.state_size = state_size + self.embedding_size = embedding_size + self.output_size = output_size + self.h_q = None + self.h_p = None + self.H_q = None + self.H_p = None + + def encode_v2(self, question_embeddings, document_embeddings, question_mask, context_mask, encoderb_state_input, dropout_keep_prob): + """ encode_v2() + """ + with vs.variable_scope('encoder'): + lstm_cell = tf.nn.rnn_cell.LSTMCell(self.embedding_size) + question_length = tf.reduce_sum(tf.cast(question_mask, tf.int32), reduction_indices=1) + print('Question length: ', question_length) + Q_prime, _ = dynamic_rnn(lstm_cell, tf.transpose(question_embeddings, [0, 2, 1]), sequence_length=question_length, time_major=False, dtype=tf.float32) + Q_prime = tf.transpose(Q_prime, [0, 2, 1]) + print('Q_prime: ', Q_prime) + W_Q = tf.get_variable('W_Q', (self.embedding_size, self.embedding_size)) + b_Q = tf.get_variable('b_Q', (self.embedding_size, 1)) + Q = tf.tanh(matrix_multiply_with_batch(matrix=W_Q, batch=question_embeddings, matrixByBatch=True) + b_Q) + print('Q: ', Q) + tf.get_variable_scope().reuse_variables() + print('Context mask: ', context_mask) + context_length = tf.reduce_sum(tf.cast(context_mask, tf.int32), reduction_indices=1) + D, _ = dynamic_rnn(lstm_cell, tf.transpose(document_embeddings, [0, 2, 1]), sequence_length=context_length, time_major=False, dtype=tf.float32) + D = tf.transpose(D, [0, 2, 1]) + print('D: ', D) + L = tf.matmul(tf.transpose(D, [0, 2, 1]), Q) + A_Q = tf.nn.softmax(L) + A_D = tf.nn.softmax(tf.transpose(L, [0, 2, 1])) + print('A_Q: ', A_Q) + print('A_D: ', A_D) + C_Q = batch_matmul(D, A_Q) + print('C_Q: ', C_Q) + concat = tf.concat(1, [Q, C_Q]) + print('concat: ', concat) + C_D = batch_matmul(tf.concat(1, [Q, C_Q]), A_D) + print('C_D: ', C_D) + final_D = tf.concat(1, [D, C_D]) + print('final D: ', final_D) + return final_D + + def encode(self, question_embeddings, context_embeddings, question_mask, context_mask, encoder_state_input, dropout_keep_prob, batch_size): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with vs.variable_scope('encoder', True): + with vs.variable_scope('question', True): + lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, output_keep_prob=dropout_keep_prob) + question_length = tf.reduce_sum(tf.cast(question_mask, tf.int32), reduction_indices=1) + print('Question length: ', question_length) + self.H_q, _ = dynamic_rnn(lstm_cell, question_embeddings, sequence_length=question_length, time_major=False, dtype=tf.float64, swap_memory=True) + self.h_q = self.H_q[:, (1), :] + print('H_q: ', self.H_q) + print('h_q: ', self.h_q) + with vs.variable_scope('context', True): + context_length = tf.reduce_sum(tf.cast(context_mask, tf.int32), reduction_indices=1) + print('Context length: ', context_length) + context_lstm_cell = tf.nn.rnn_cell.LSTMCell(self.state_size) + context_lstm_cell = tf.nn.rnn_cell.DropoutWrapper(context_lstm_cell, output_keep_prob=dropout_keep_prob) + self.H_p, _ = dynamic_rnn(context_lstm_cell, context_embeddings, sequence_length=context_length, time_major=False, dtype=tf.float64, swap_memory=True) + self.h_p = self.H_p[:, (1), :] + print('H_p: ', self.H_p) + print('h_p: ', self.h_p) + return self.h_q, self.h_p + + +class Decoder(object): + + def __init__(self, output_size, state_size): + self.state_size = state_size + self.output_size = output_size + + def decode_v2(self, final_D, W, W_prime, context_mask): + with vs.variable_scope('answer_start'): + a_s = tf.squeeze(matrix_multiply_with_batch(matrix=W, batch=tf.transpose(final_D, [0, 2, 1]), matrixByBatch=False)) + print('a_s: ', a_s) + with vs.variable_scope('answer_end'): + lstm_cell = tf.nn.rnn_cell.LSTMCell(self.output_size) + context_length = tf.reduce_sum(tf.cast(context_mask, tf.int32), reduction_indices=1) + print('Context length: ', context_length) + final_D_prime, _ = dynamic_rnn(lstm_cell, final_D, sequence_length=context_length, time_major=False, dtype=tf.float32) + print('final D prime: ', final_D_prime) + a_e = matrix_multiply_with_batch(matrix=W_prime, batch=tf.transpose(final_D_prime, [0, 2, 1]), matrixByBatch=False) + print('a_e: ', a_e) + a_e = tf.squeeze(matrix_multiply_with_batch(matrix=W_prime, batch=tf.transpose(final_D_prime, [0, 2, 1]), matrixByBatch=False)) + print('a_e: ', a_e) + return a_s, a_e + + def decode(self, h_q, h_p): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + with vs.variable_scope('answer_start'): + a_s = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + with vs.variable_scope('answer_end'): + a_e = rnn_cell._linear([h_q, h_p], self.output_size, True, 1.0) + return a_s, a_e + + +class QASystem(object): + + def __init__(self, encoder, decoder, **kwargs): + """ + Initializes your System + + :param encoder: an encoder that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + self.max_question_len = 60 + self.max_context_len = 301 + self.max_answer_len = 46 + self.n_classes = 2 + self.saver = None + self.encoder = encoder + self.decoder = decoder + self.question_embeddings = None + self.context_embeddings = None + self.a_s_probs = None + self.a_e_probs = None + self.loss = None + self.train_op = None + self.grad_norm = None + self.gradients = None + self.clipped_gradients = None + self.state_size = kwargs['state_size'] + self.embed_path = kwargs['embed_path'] + self.embedding_size = kwargs['embedding_size'] + self.output_size = kwargs['output_size'] + self.optimizer = kwargs['optimizer'] + self.initial_learning_rate = kwargs['learning_rate'] + self.global_step = tf.Variable(0, trainable=False) + self.epochs = kwargs['epochs'] + self.batch_size = kwargs['batch_size'] + self.max_gradient_norm = kwargs['max_gradient_norm'] + self.dropout_keep_prob = kwargs['dropout_keep_prob'] + self.train_dir = kwargs['train_dir'] + self.question_input_placeholder = tf.placeholder(tf.int32, (None, self.max_question_len)) + self.context_input_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + self.question_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_question_len)) + self.context_mask_placeholder = tf.placeholder(tf.bool, (None, self.max_context_len)) + self.start_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + self.end_answer_placeholder = tf.placeholder(tf.int32, (None, self.max_context_len)) + self.dropout_placeholder = tf.placeholder(tf.float32, ()) + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.setup_training_op() + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + W = tf.get_variable('W', (3 * self.embedding_size, 1)) + W_prime = tf.get_variable('W_prime', (3 * self.embedding_size, 1)) + final_D = self.encoder.encode_v2(self.question_embeddings, self.context_embeddings, self.question_mask_placeholder, self.context_mask_placeholder, None, self.dropout_keep_prob) + self.a_s_probs, self.a_e_probs = self.decoder.decode_v2(final_D, W, W_prime, self.context_mask_placeholder) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + l1 = tf.nn.softmax_cross_entropy_with_logits(logits=self.a_s_probs, labels=self.start_answer_placeholder) + l2 = tf.nn.softmax_cross_entropy_with_logits(logits=self.a_e_probs, labels=self.end_answer_placeholder) + self.loss = l1 + l2 + + def setup_training_op(self): + """ + Sets up the training ops. + + Creates an optimizer and applies the gradients to all trainable variables. + Clips the global norm of the gradients. + """ + lr = tf.train.exponential_decay(self.initial_learning_rate, self.global_step, 1000, 0.96) + opt = get_optimizer(self.optimizer)(learning_rate=lr) + self.gradients, params = zip(*opt.compute_gradients(self.loss)) + for param in params: + print('Param: ', param) + self.clipped_gradients, _ = tf.clip_by_global_norm(self.gradients, self.max_gradient_norm) + grads_and_params = zip(self.clipped_gradients, params) + self.grad_norm = tf.global_norm(self.clipped_gradients) + self.train_op = opt.apply_gradients(grads_and_params, global_step=self.global_step) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + pretrained_embeddings = np.load(self.embed_path)['glove'] + self.question_embeddings = tf.constant(pretrained_embeddings, name='question_embeddings', dtype=tf.float32) + self.question_embeddings = tf.nn.embedding_lookup(self.question_embeddings, self.question_input_placeholder) + self.question_embeddings = tf.reshape(self.question_embeddings, [-1, self.embedding_size, self.max_question_len]) + print('Question embeddings: ', self.question_embeddings) + self.context_embeddings = tf.constant(pretrained_embeddings, name='context_embeddings', dtype=tf.float32) + self.context_embeddings = tf.nn.embedding_lookup(self.context_embeddings, self.context_input_placeholder) + self.context_embeddings = tf.reshape(self.context_embeddings, [-1, self.embedding_size, self.max_context_len]) + print('Context embeddings: ', self.context_embeddings) + + def optimize(self, session, train_batch, dropout_keep_prob=1): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*train_batch) + feed = {} + feed[self.question_input_placeholder] = question_batch + feed[self.context_input_placeholder] = context_batch + feed[self.question_mask_placeholder] = question_mask_batch + feed[self.context_mask_placeholder] = context_mask_batch + feed[self.start_answer_placeholder] = start_answer_batch + feed[self.end_answer_placeholder] = end_answer_batch + feed[self.dropout_placeholder] = dropout_keep_prob + _, loss, grad_norm = session.run([self.train_op, self.loss, self.grad_norm], feed_dict=feed) + return loss, grad_norm + + def test(self, session, valid_x, valid_y): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed['valid_x'] = valid_x + if valid_y is not None: + input_feed['valid_y'] = valid_y + output_feed = [] + outputs = session.run(output_feed, input_feed) + return outputs + + def decode(self, session, test_x): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*test_x) + input_feed = {} + input_feed[self.question_input_placeholder] = question_batch + input_feed[self.context_input_placeholder] = context_batch + input_feed[self.question_mask_placeholder] = question_mask_batch + input_feed[self.context_mask_placeholder] = context_mask_batch + input_feed[self.start_answer_placeholder] = start_answer_batch + input_feed[self.end_answer_placeholder] = end_answer_batch + input_feed[self.dropout_placeholder] = 1.0 + _, loss, grad_norm = session.run([self.train_op, self.loss, self.grad_norm], feed_dict=input_feed) + a_s = self.a_s_probs.eval(feed_dict=input_feed, session=session) + a_e = self.a_e_probs.eval(feed_dict=input_feed, session=session) + return a_s, a_e + + def answer(self, session, test_x): + yp, yp2 = self.decode(session, test_x) + a_s = np.argmax(yp, axis=1) + a_e = np.argmax(yp2, axis=1) + return a_s, a_e + + def validate(self, session, dataset, model_path, model_name): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + new_saver = tf.train.import_meta_graph(model_path + model_name) + new_saver.restore(session, tf.train.latest_checkpoint(model_path)) + f1s = [] + ems = [] + step = 1000 + for start_idx in range(0, len(dataset['val']), step): + end_idx = min(start_idx + step, len(dataset['val'])) + f1s_one_batch, ems_one_batch = self.evaluate_answer(session, dataset['val'][start_idx:end_idx], sample=None, log=True) + f1s += f1s_one_batch + ems += ems_one_batch + f1_total = sum(f1s) / float(len(f1s)) + em_total = sum(ems) / float(len(ems)) + print('Total f1: ', f1_total) + print('Total em: ', em_total) + + def evaluate_answer(self, session, dataset, sample=100, log=False): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + batch = dataset + if sample is None: + sample = len(dataset) + else: + random_indices = [random.randint(0, len(dataset)) for _ in range(sample)] + batch = [dataset[idx] for idx in random_indices] + question_batch, context_batch, question_mask_batch, context_mask_batch, start_answer_batch, end_answer_batch = zip(*batch) + a_s, a_e = self.answer(session, batch) + true_a_s = np.argmax(start_answer_batch, axis=1) + true_a_e = np.argmax(end_answer_batch, axis=1) + print('predicted a_s: ', a_s) + print('predicted a_e: ', a_e) + print('true start answer: ', true_a_s) + print('true end answer: ', true_a_e) + answers = [context_batch[i][a_s[i]:a_e[i] + 1] for i in range(len(a_s))] + true_answers = [context_batch[i][true_a_s[i]:true_a_e[i] + 1] for i in range(len(true_a_s))] + f1s = [] + ems = [] + for i in range(len(true_answers)): + answer = answers[i] + true_answer = true_answers[i] + f1_one_example = f1_score(answer, true_answer) + f1s.append(f1_one_example) + em_one_example = exact_match_score(answer, true_answer) + ems.append(em_one_example) + f1 = np.sum(f1s) / float(sample) + em = np.sum(ems) / float(sample) + if log: + logging.info('F1: {}, EM: {}, for {} samples'.format(f1, em, sample)) + return f1s, ems + + def train(self, session, dataset, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious appoarch can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + dataset = self.preprocess_dataset(dataset) + init = tf.global_variables_initializer() + session.run(init) + self.validate(session, dataset, 'saved_models/', 'baselinev2_model_epoch_9_iter_8000.meta') + exit() + for epoch in range(self.epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.epochs) + self.run_epoch(session, dataset['train'], epoch) + self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch)) + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + evaluate_answer(session, dataset['val'], sample=none, log=True) + + def preprocess_dataset(self, dataset): + for data_subset_name in ['train', 'val']: + for i in range(len(dataset[data_subset_name])): + question = dataset[data_subset_name][i][0] + context = dataset[data_subset_name][i][1] + answer = dataset[data_subset_name][i][2] + padded_question, question_mask = self.pad_sequence(question, self.max_question_len) + padded_context, context_mask = self.pad_sequence(context, self.max_context_len) + start_answer = [0] * self.max_context_len + if answer[0] < self.output_size: + start_answer[answer[0]] = 1 + end_answer = [0] * self.max_context_len + if answer[1] < self.output_size: + end_answer[answer[1]] = 1 + dataset[data_subset_name][i] = padded_question, padded_context, question_mask, context_mask, start_answer, end_answer + return dataset + + def pad_sequence(self, sequence, max_length): + new_sequence = [] + mask = [] + if len(sequence) >= max_length: + new_sequence = sequence[0:max_length] + mask = [True] * max_length + elif len(sequence) < max_length: + delta = max_length - len(sequence) + new_sequence = sequence + [0] * delta + mask = [True] * len(sequence) + [False] * delta + return new_sequence, mask + + def run_epoch(self, session, train_examples, epoch_no): + for i, batch in enumerate(self.minibatches(train_examples, self.batch_size, shuffle=True)): + print('Global step: ', self.global_step.eval()) + loss, grad_norm = self.optimize(session, batch, self.dropout_keep_prob) + print('Loss: ', loss, ' , grad norm: ', grad_norm) + if i % 100 == 0: + self.evaluate_answer(session, train_examples, sample=100, log=True) + if i % 100 == 0: + self.saver.save(session, self.train_dir + '/baselinev2_model_epoch_' + str(epoch_no) + '_iter_' + str(i)) + + def get_tiny_batches(self, data): + return [data[0:10], data[10:20]] + + def minibatches(self, data, batch_size, shuffle=True): + if shuffle: + random.shuffle(data) + num_batches = int(math.ceil(len(data) / batch_size)) + q, r = divmod(len(data), num_batches) + indices = [(q * i + min(i, r)) for i in xrange(num_batches + 1)] + return [data[indices[i]:indices[i + 1]] for i in xrange(num_batches)] diff --git a/dataset/preprocessed/539.txt b/dataset/preprocessed/539.txt new file mode 100644 index 0000000..86356b6 --- /dev/null +++ b/dataset/preprocessed/539.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/54.txt b/dataset/preprocessed/54.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/54.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/540.txt b/dataset/preprocessed/540.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/540.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/541.txt b/dataset/preprocessed/541.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/541.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/542.txt b/dataset/preprocessed/542.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/542.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/543.txt b/dataset/preprocessed/543.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/543.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/544.txt b/dataset/preprocessed/544.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/544.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/545.txt b/dataset/preprocessed/545.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/545.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/546.txt b/dataset/preprocessed/546.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/546.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/549.txt b/dataset/preprocessed/549.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/549.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/55.txt b/dataset/preprocessed/55.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/55.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/550.txt b/dataset/preprocessed/550.txt new file mode 100644 index 0000000..b76cc03 --- /dev/null +++ b/dataset/preprocessed/550.txt @@ -0,0 +1,161 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from neural.full_connect import load_reformat_not_mnist, accuracy + + +def tf_better_nn(offset_range=-1, regular=False, drop_out=False, lrd=False): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_node_count = 1024 + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count])) + biases1 = tf.Variable(tf.zeros([hidden_node_count])) + weights2 = tf.Variable(tf.truncated_normal([hidden_node_count, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + ys = tf.matmul(tf_train_dataset, weights1) + biases1 + hidden = tf.nn.relu(ys) + h_fc = hidden + valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + valid_hidden1 = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + test_hidden1 = tf.nn.relu(test_y0) + keep_prob = tf.placeholder(tf.float32) + if drop_out: + hidden_drop = tf.nn.dropout(hidden, keep_prob) + h_fc = hidden_drop + logits = tf.matmul(h_fc, weights2) + biases2 + logits_predict = tf.matmul(hidden, weights2) + biases2 + valid_predict = tf.matmul(valid_hidden1, weights2) + biases2 + test_predict = tf.matmul(test_hidden1, weights2) + biases2 + l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(biases1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(biases2) + if not regular: + l2_loss = 0 + beta = 0.002 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta * l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + if lrd: + cur_step = tf.Variable(0) + starter_learning_rate = 0.1 + learning_rate = tf.train.exponential_decay(starter_learning_rate, cur_step, 10000, 0.96, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=cur_step) + train_prediction = tf.nn.softmax(logits_predict) + valid_prediction = tf.nn.softmax(valid_predict) + test_prediction = tf.nn.softmax(test_predict) + num_steps = 30001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + if offset_range == -1: + offset_range = train_labels.shape[0] - batch_size + offset = step * batch_size % offset_range + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +def tf_deep_nn(regular=False, drop_out=False, lrd=False, layer_cnt=2): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_node_count = 1024 + hidden_stddev = np.sqrt(2.0 / 784) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count], stddev=hidden_stddev)) + biases1 = tf.Variable(tf.zeros([hidden_node_count])) + weights = [] + biases = [] + hidden_cur_cnt = hidden_node_count + for i in range(layer_cnt - 2): + if hidden_cur_cnt > 2: + hidden_next_cnt = int(hidden_cur_cnt / 2) + else: + hidden_next_cnt = 2 + hidden_stddev = np.sqrt(2.0 / hidden_cur_cnt) + weights.append(tf.Variable(tf.truncated_normal([hidden_cur_cnt, hidden_next_cnt], stddev=hidden_stddev))) + biases.append(tf.Variable(tf.zeros([hidden_next_cnt]))) + hidden_cur_cnt = hidden_next_cnt + y0 = tf.matmul(tf_train_dataset, weights1) + biases1 + hidden = tf.nn.relu(y0) + hidden_drop = hidden + keep_prob = 0.5 + if drop_out: + hidden_drop = tf.nn.dropout(hidden, keep_prob) + valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + valid_hidden = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + test_hidden = tf.nn.relu(test_y0) + for i in range(layer_cnt - 2): + y1 = tf.matmul(hidden_drop, weights[i]) + biases[i] + hidden_drop = tf.nn.relu(y1) + if drop_out: + keep_prob += 0.5 * i / (layer_cnt + 1) + hidden_drop = tf.nn.dropout(hidden_drop, keep_prob) + y0 = tf.matmul(hidden, weights[i]) + biases[i] + hidden = tf.nn.relu(y0) + valid_y0 = tf.matmul(valid_hidden, weights[i]) + biases[i] + valid_hidden = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(test_hidden, weights[i]) + biases[i] + test_hidden = tf.nn.relu(test_y0) + weights2 = tf.Variable(tf.truncated_normal([hidden_cur_cnt, num_labels], stddev=hidden_stddev / 2)) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_drop, weights2) + biases2 + logits_predict = tf.matmul(hidden, weights2) + biases2 + valid_predict = tf.matmul(valid_hidden, weights2) + biases2 + test_predict = tf.matmul(test_hidden, weights2) + biases2 + l2_loss = 0 + if regular: + l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + for i in range(len(weights)): + l2_loss += tf.nn.l2_loss(weights[i]) + beta = 0.25 / batch_size + beta = 1e-05 + l2_loss *= beta + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + l2_loss + if lrd: + cur_step = tf.Variable(0, trainable=False) + starter_learning_rate = 0.4 + learning_rate = tf.train.exponential_decay(starter_learning_rate, cur_step, 100000, 0.96, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=cur_step) + else: + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_predict) + valid_prediction = tf.nn.softmax(valid_predict) + test_prediction = tf.nn.softmax(test_predict) + num_steps = 20001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset_range = train_labels.shape[0] - batch_size + offset = step * batch_size % offset_range + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +if __name__ == '__main__': + image_size = 28 + num_labels = 10 + train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = load_reformat_not_mnist(image_size, num_labels) + tf_deep_nn(layer_cnt=6, lrd=True, drop_out=True, regular=True) diff --git a/dataset/preprocessed/551.txt b/dataset/preprocessed/551.txt new file mode 100644 index 0000000..896b1f8 --- /dev/null +++ b/dataset/preprocessed/551.txt @@ -0,0 +1,324 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import argparse +import math +import random +import os +import tensorflow as tf +import numpy as np +from six.moves import xrange as range +from utils import * +import pdb +from time import gmtime, strftime +NUM_CLASSES = len(get_chars_to_index_mapping()) +NUM_HIDDEN_LAYERS = 2 +DROPOUT_KEEP_PROB = 1 + + +class Config: + """Holds model hyperparams and data information. + + The config class is used to store various hyperparameters and dataset + information parameters. Model objects are passed a Config() object at + instantiation. + """ + context_size = 0 + num_mfcc_features = 13 + num_final_features = num_mfcc_features * (2 * context_size + 1) + batch_size = 16 + num_classes = NUM_CLASSES + num_hidden = 50 + num_epochs = 100 + l2_lambda = 0.0001 + lr = 0.001 + + +class CTCModel: + """ + Implements a recursive neural network with a single hidden layer attached to CTC loss. + This network will predict a sequence of TIDIGITS (e.g. z1039) for a given audio wav file. + """ + + def add_placeholders(self): + """Generates placeholder variables to represent the input tensors + + These placeholders are used as inputs by the rest of the model building and will be fed + data during training. Note that when "None" is in a placeholder's shape, it's flexible + (so we can use different batch sizes without rebuilding the model). + + Adds following nodes to the computational graph: + + inputs_placeholder: Input placeholder tensor of shape (None, None, num_final_features), type tf.float32 + targets_placeholder: Sparse placeholder, type tf.int32. You don't need to specify shape dimension. + seq_lens_placeholder: Sequence length placeholder tensor of shape (None), type tf.int32 + + TODO: Add these placeholders to self as the instance variables + self.inputs_placeholder + self.targets_placeholder + self.seq_lens_placeholder + + HINTS: + - Use tf.sparse_placeholder(tf.int32) for targets_placeholder. This is required by TF's ctc_loss op. + - Inputs is of shape [batch_size, max_timesteps, num_final_features], but we allow flexible sizes for + batch_size and max_timesteps (hence the shape definition as [None, None, num_final_features]. + + (Don't change the variable names) + """ + inputs_placeholder = tf.placeholder(tf.float32, shape=(None, None, Config.num_final_features), name='inputs_placeholder') + targets_placeholder = tf.sparse_placeholder(tf.int32, name='targets_placeholder') + seq_lens_placeholder = tf.placeholder(tf.int32, shape=None, name='seq_lens_placeholder') + keep_prob_placeholder = tf.placeholder(tf.float32, name='keep_prob_placeholder') + self.inputs_placeholder = inputs_placeholder + self.targets_placeholder = targets_placeholder + self.seq_lens_placeholder = seq_lens_placeholder + self.keep_prob_placeholder = keep_prob_placeholder + + def create_feed_dict(self, inputs_batch, targets_batch, seq_lens_batch, keep_prob): + """Creates the feed_dict for the digit recognizer. + + A feed_dict takes the form of: + + feed_dict = { + : , + .... + } + + Hint: The keys for the feed_dict should be a subset of the placeholder + tensors created in add_placeholders. + + Args: + inputs_batch: A batch of input data. + targets_batch: A batch of targets data. + seq_lens_batch: A batch of seq_lens data. + Returns: + feed_dict: The feed dictionary mapping from placeholders to values. + """ + feed_dict = {self.inputs_placeholder: inputs_batch, self.targets_placeholder: targets_batch, self.seq_lens_placeholder: seq_lens_batch, self.keep_prob_placeholder: keep_prob} + return feed_dict + + def add_prediction_op(self, train): + """Applies a GRU RNN over the input data, then an affine layer projection. Steps to complete + in this function: + + - Roll over inputs_placeholder with GRUCell, producing a Tensor of shape [batch_s, max_timestep, + num_hidden]. + - Apply a W * f + b transformation over the data, where f is each hidden layer feature. This + should produce a Tensor of shape [batch_s, max_timesteps, num_classes]. Set this result to + "logits". + + Remember: + * Use the xavier initialization for matrices (W, but not b). + * W should be shape [num_hidden, num_classes]. num_classes for our dataset is 12 + * tf.contrib.rnn.GRUCell, tf.contrib.rnn.MultiRNNCell and tf.nn.dynamic_rnn are of interest + """ + forward_cell_multi = [] + backward_cell_multi = [] + for _ in range(NUM_HIDDEN_LAYERS): + forward_cell = tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.GRUCell(Config.num_hidden, activation=tf.nn.relu), input_keep_prob=self.keep_prob_placeholder, output_keep_prob=self.keep_prob_placeholder) + forward_cell_multi.append(forward_cell) + backward_cell = tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.GRUCell(Config.num_hidden, activation=tf.nn.relu), input_keep_prob=self.keep_prob_placeholder, output_keep_prob=self.keep_prob_placeholder) + backward_cell_multi.append(backward_cell) + forward_cell_multi = tf.contrib.rnn.MultiRNNCell(forward_cell_multi) + backward_cell_multi = tf.contrib.rnn.MultiRNNCell(backward_cell_multi) + outputs, _ = tf.nn.bidirectional_dynamic_rnn(forward_cell_multi, backward_cell_multi, self.inputs_placeholder, sequence_length=self.seq_lens_placeholder, dtype=tf.float32) + outputs = tf.concat(outputs, 2) + W = tf.get_variable(name='W', shape=[Config.num_hidden * 2, Config.num_classes], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + b = tf.get_variable(name='b', shape=(Config.num_classes,), dtype=tf.float32, initializer=tf.zeros_initializer()) + max_timesteps = tf.shape(outputs)[1] + num_hidden = tf.shape(outputs)[2] + f = tf.reshape(outputs, [-1, num_hidden]) + logits = tf.add(tf.matmul(f, W), b) + logits = tf.reshape(logits, [-1, max_timesteps, Config.num_classes]) + self.logits = logits + + def add_loss_op(self): + """Adds Ops for the loss function to the computational graph. + + - Use tf.nn.ctc_loss to calculate the CTC loss for each example in the batch. You'll need self.logits, + self.targets_placeholder, self.seq_lens_placeholder for this. Set variable ctc_loss to + the output of tf.nn.ctc_loss + - You will need to first tf.transpose the data so that self.logits is shaped [max_timesteps, batch_s, + num_classes]. + - Configure tf.nn.ctc_loss so that identical consecutive labels are allowed + - Compute L2 regularization cost for all trainable variables. Use tf.nn.l2_loss(var). + + """ + ctc_loss = [] + l2_cost = 0.0 + self.logits = tf.transpose(self.logits, perm=[1, 0, 2]) + tf.identity(self.logits, name='logits') + ctc_loss = tf.nn.ctc_loss(self.targets_placeholder, self.logits, self.seq_lens_placeholder, ctc_merge_repeated=False, preprocess_collapse_repeated=False) + for var in tf.trainable_variables(): + if len(var.get_shape().as_list()) > 1: + l2_cost += tf.nn.l2_loss(var) + loss_without_invalid_paths = tf.boolean_mask(ctc_loss, tf.less(ctc_loss, tf.constant(10000.0))) + self.num_valid_examples = tf.cast(tf.shape(loss_without_invalid_paths)[0], tf.int32) + cost = tf.reduce_mean(loss_without_invalid_paths) + self.loss = Config.l2_lambda * l2_cost + cost + + def add_training_op(self): + """Sets up the training Ops. + + Creates an optimizer and applies the gradients to all trainable variables. The Op returned by this + function is what must be passed to the `sess.run()` call to cause the model to train. See + + https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html#Optimizer + + for more information. + + Use tf.train.AdamOptimizer for this model. Call optimizer.minimize() on self.loss. + + """ + optimizer = tf.train.AdamOptimizer(learning_rate=Config.lr).minimize(self.loss) + self.optimizer = optimizer + + def add_decoder_and_cer_op(self): + """Setup the decoder and add the word error rate calculations here. + + Tip: You will find tf.nn.ctc_beam_search_decoder and tf.edit_distance methods useful here. + Also, report the mean cer over the batch in variable cer + + """ + decoded_sequence = None + cer = None + decoded_sequence = tf.nn.ctc_beam_search_decoder(self.logits, self.seq_lens_placeholder, merge_repeated=False)[0][0] + cer = tf.reduce_mean(tf.edit_distance(tf.to_int32(decoded_sequence), self.targets_placeholder), name='cer') + tf.summary.scalar('loss', self.loss) + tf.summary.scalar('cer', cer) + self.decoded_sequence = decoded_sequence + self.cer = cer + + def add_summary_op(self): + self.merged_summary_op = tf.summary.merge_all() + + def build(self, train): + self.add_placeholders() + self.add_prediction_op(train) + self.add_loss_op() + self.add_training_op() + self.add_decoder_and_cer_op() + self.add_summary_op() + + def train_on_batch(self, session, train_inputs_batch, train_targets_batch, train_seq_len_batch, train=True): + keep_prob = 1.0 + if train: + keep_prob = DROPOUT_KEEP_PROB + feed = self.create_feed_dict(train_inputs_batch, train_targets_batch, train_seq_len_batch, keep_prob) + batch_cost, cer, batch_num_valid_ex, summary = session.run([self.loss, self.cer, self.num_valid_examples, self.merged_summary_op], feed) + if math.isnan(batch_cost): + return 0 + if train: + _ = session.run([self.optimizer], feed) + return batch_cost, cer, summary + + def print_results(self, session, train_inputs_batch, train_targets_batch, train_seq_len_batch): + train_feed = self.create_feed_dict(train_inputs_batch, train_targets_batch, train_seq_len_batch, 1.0) + train_first_batch_preds = session.run(self.decoded_sequence, feed_dict=train_feed) + compare_predicted_to_true(train_first_batch_preds, train_targets_batch) + + def __init__(self, train=True): + self.build(train) + + +def load_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--train_path', nargs='?', default='./data/cmu/cmu_all6_train.dat', type=str, help='Give path to training data') + parser.add_argument('--val_path', nargs='?', default='./data/cmu/cmu_all6_val.dat', type=str, help='Give path to val data') + parser.add_argument('--save_every', nargs='?', default=None, type=int, help='Save model every x iterations. Default is not saving at all.') + parser.add_argument('--print_every', nargs='?', default=10, type=int, help='Print some training and val examples (true and predicted sequences) every x iterations. Default is 10') + parser.add_argument('--save_to_file', nargs='?', default='saved_models/saved_model_epoch', type=str, help='Provide filename prefix for saving intermediate models') + parser.add_argument('--load_from_file', nargs='?', default=None, type=str, help='Provide filename to load saved model') + parser.add_argument('--test_path', nargs='?', default='no', type=str, help='Provide test filename to do test classification') + return parser.parse_args() + + +def train_model(logs_path, num_batches_per_epoch, train_feature_minibatches, train_labels_minibatches, train_seqlens_minibatches, val_feature_minibatches, val_labels_minibatches, val_seqlens_minibatches): + with tf.Graph().as_default(): + model = CTCModel(train=True) + init = tf.global_variables_initializer() + saver = tf.train.Saver(max_to_keep=None) + with tf.Session() as session: + session.run(init) + train_writer = tf.summary.FileWriter(logs_path + '/train', session.graph) + val_writer = tf.summary.FileWriter(logs_path + '/dev', session.graph) + global_start = time.time() + step_ii = 0 + curr_epoch = 0 + while True: + total_train_cost = total_train_cer = 0 + start = time.time() + for batch in random.sample(range(num_batches_per_epoch), num_batches_per_epoch): + cur_batch_size = len(train_seqlens_minibatches[batch]) + batch_cost, batch_cer, summary = model.train_on_batch(session, train_feature_minibatches[batch], train_labels_minibatches[batch], train_seqlens_minibatches[batch], train=True) + total_train_cost += batch_cost * cur_batch_size + total_train_cer += batch_cer * cur_batch_size + train_writer.add_summary(summary, step_ii) + step_ii += 1 + train_cost = total_train_cost / num_examples + train_cer = total_train_cer / num_examples + val_batch_cost, val_batch_cer, summary = model.train_on_batch(session, val_feature_minibatches[0], val_labels_minibatches[0], val_seqlens_minibatches[0], train=False) + val_writer.add_summary(summary, step_ii) + log = 'Epoch {}/{}, train_cost = {:.3f}, train_ed = {:.3f}, val_cost = {:.3f}, val_ed = {:.3f}, time = {:.3f}' + print(log.format(curr_epoch + 1, Config.num_epochs, train_cost, train_cer, val_batch_cost, val_batch_cer, time.time() - start)) + if args.print_every is not None and (curr_epoch + 1) % args.print_every == 0: + batch_ii = 0 + print('Train set sample predictions:') + model.print_results(session, train_feature_minibatches[batch_ii], train_labels_minibatches[batch_ii], train_seqlens_minibatches[batch_ii]) + print('Val set sample predictions:') + model.print_results(session, val_feature_minibatches[batch_ii][:10], val_labels_minibatches[batch_ii][:10], val_seqlens_minibatches[batch_ii][:10]) + if args.save_every is not None and args.save_to_file is not None and (curr_epoch + 1) % args.save_every == 0: + saver.save(session, args.save_to_file, global_step=curr_epoch + 1) + curr_epoch += 1 + + +def test(test_dataset, trained_weights_file): + test_feature_minibatches, test_labels_minibatches, test_seqlens_minibatches = make_batches(test_dataset, batch_size=len(test_dataset[0])) + test_feature_minibatches = pad_all_batches(test_feature_minibatches) + with tf.Graph().as_default(): + with tf.Session() as session: + new_saver = tf.train.import_meta_graph('%s.meta' % args.load_from_file, clear_devices=True) + new_saver.restore(session, trained_weights_file) + print('model restored with the %s checkpoint' % trained_weights_file) + start = time.time() + cer = tf.get_default_graph().get_tensor_by_name('cer:0') + test_feed_dict = {'inputs_placeholder:0': test_feature_minibatches[0], 'targets_placeholder/indices:0': test_labels_minibatches[0][0], 'targets_placeholder/values:0': test_labels_minibatches[0][1], 'targets_placeholder/shape:0': test_labels_minibatches[0][2], 'seq_lens_placeholder:0': test_seqlens_minibatches[0], 'keep_prob_placeholder:0': 1.0} + test_cer = session.run([cer], test_feed_dict) + log = 'test_cer = {:.3f}, time = {:.3f}' + print(log.format(test_cer[0], time.time() - start)) + if args.print_every is not None: + batch_ii = 0 + logits = tf.get_default_graph().get_tensor_by_name('logits:0') + seq_lens_placeholder = tf.get_default_graph().get_tensor_by_name('seq_lens_placeholder:0') + decoded_sequence = tf.nn.ctc_beam_search_decoder(logits, seq_lens_placeholder, merge_repeated=False)[0][0] + test_results_feed_dict = {'inputs_placeholder:0': test_feature_minibatches[batch_ii], 'targets_placeholder/indices:0': test_labels_minibatches[batch_ii][0], 'targets_placeholder/values:0': test_labels_minibatches[batch_ii][1], 'targets_placeholder/shape:0': test_labels_minibatches[batch_ii][2], 'seq_lens_placeholder:0': test_seqlens_minibatches[batch_ii], + 'keep_prob_placeholder:0': 1.0} + train_first_batch_preds = session.run(decoded_sequence, feed_dict=test_results_feed_dict) + compare_predicted_to_true(train_first_batch_preds, test_labels_minibatches[batch_ii]) + + +def pad_all_batches(batch_feature_array): + for batch_num in range(len(batch_feature_array)): + batch_feature_array[batch_num] = pad_sequences(batch_feature_array[batch_num])[0] + return batch_feature_array + + +if __name__ == '__main__': + args = load_args() + if args.test_path == 'no': + logs_path = 'tensorboard/' + strftime('%Y_%m_%d_%H_%M_%S', gmtime()) + '_lr=%f' % Config.lr + train_dataset = load_dataset(args.train_path) + train_feature_minibatches, train_labels_minibatches, train_seqlens_minibatches = make_batches(train_dataset, batch_size=Config.batch_size) + val_dataset = load_dataset(args.val_path) + val_feature_minibatches, val_labels_minibatches, val_seqlens_minibatches = make_batches(val_dataset, batch_size=len(val_dataset[0])) + train_feature_minibatches = pad_all_batches(train_feature_minibatches) + val_feature_minibatches = pad_all_batches(val_feature_minibatches) + num_examples = np.sum([batch.shape[0] for batch in train_feature_minibatches]) + num_batches_per_epoch = int(math.ceil(num_examples / Config.batch_size)) + train_model(logs_path, num_batches_per_epoch, train_feature_minibatches, train_labels_minibatches, train_seqlens_minibatches, val_feature_minibatches, val_labels_minibatches, val_seqlens_minibatches) + else: + if args.load_from_file is None: + raise ValueError('must specify weights to load through --load_from_file') + test_dataset = load_dataset(args.test_path) + test(test_dataset, args.load_from_file) diff --git a/dataset/preprocessed/552.txt b/dataset/preprocessed/552.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/552.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/553.txt b/dataset/preprocessed/553.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/553.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/554.txt b/dataset/preprocessed/554.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/554.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/555.txt b/dataset/preprocessed/555.txt new file mode 100644 index 0000000..f20d8c7 --- /dev/null +++ b/dataset/preprocessed/555.txt @@ -0,0 +1,367 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import sys +if sys.version[0] == '2': + import cPickle as pkl +else: + import pickle as pkl +import numpy as np +import tensorflow as tf +from PNN.author import utils +dtype = utils.DTYPE + + +class Model: + + def __init__(self): + self.sess = None + self.X = None + self.y = None + self.layer_keeps = None + self.vars = None + self.keep_prob_train = None + self.keep_prob_test = None + + def run(self, fetches, X=None, y=None, mode='train'): + feed_dict = {} + if type(self.X) is list: + for i in range(len(X)): + feed_dict[self.X[i]] = X[i] + else: + feed_dict[self.X] = X + if y is not None: + feed_dict[self.y] = y + if self.layer_keeps is not None: + if mode == 'train': + feed_dict[self.layer_keeps] = self.keep_prob_train + elif mode == 'test': + feed_dict[self.layer_keeps] = self.keep_prob_test + return self.sess.run(fetches, feed_dict) + + def dump(self, model_path): + var_map = {} + for name, var in self.vars.iteritems(): + var_map[name] = self.run(var) + pkl.dump(var_map, open(model_path, 'wb')) + print('model dumped at', model_path) + + +class LR(Model): + + def __init__(self, input_dim=None, output_dim=1, init_path=None, opt_algo='gd', learning_rate=0.01, l2_weight=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + b = self.vars['b'] + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=logits)) + l2_weight * tf.nn.l2_loss(xw) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FM(Model): + + def __init__(self, input_dim=None, output_dim=1, factor_order=10, init_path=None, opt_algo='gd', learning_rate=0.01, l2_w=0, l2_v=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + v = self.vars['v'] + b = self.vars['b'] + X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + l2_w * tf.nn.l2_loss(xw) + l2_v * tf.nn.l2_loss(xv) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FNN(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + node_in = num_inputs * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + print(l.shape, wi.shape, bi.shape) + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class DeepFM(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + init_vars.append(('weight_%d' % i, [field_sizes[i], 1], 'xavier', dtype)) + init_vars.append(('bias', [1], 'zero', dtype)) + node_in = num_inputs * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = [self.vars['weight_%d' % i] for i in range(num_inputs)] + v = [self.vars['embed_%d' % i] for i in range(num_inputs)] + b = self.vars['bias'] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w[i]) for i in range(num_inputs)], 1) + xv = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], v[i]) for i in range(num_inputs)], 1) + l = xv + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + print(l.shape, wi.shape, bi.shape) + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + xv = tf.reshape(xv, [-1, num_inputs, embed_size]) + p = 0.5 * tf.reduce_sum(tf.square(tf.reduce_sum(xv, 1)) - tf.reduce_sum(tf.square(xv), 1), 1) + xw = tf.reduce_sum(xw, 1) + logits = tf.reshape(l + xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class CCPM(Model): + + def __init__(self, field_sizes=None, embed_size=10, filter_sizes=None, layer_acts=None, drop_out=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + init_vars.append(('b1', [1], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + f1 = self.vars['f1'] + l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + f2 = self.vars['f2'] + l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + b1 = self.vars['b1'] + l = tf.matmul(l, w1) + b1 + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN1(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + l = tf.concat([xw, ip], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN2(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None, layer_norm=True): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + init_vars.append(('kernel', [embed_size, num_pairs, embed_size], 'xavier', dtype)) + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + k = self.vars['kernel'] + p = tf.expand_dims(p, 1) + kp = tf.reduce_sum(tf.multiply(tf.transpose(tf.reduce_sum(tf.multiply(p, k), -1), [0, 2, 1]), q), -1) + l = tf.concat([xw, kp], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) diff --git a/dataset/preprocessed/556.txt b/dataset/preprocessed/556.txt new file mode 100644 index 0000000..8e23342 --- /dev/null +++ b/dataset/preprocessed/556.txt @@ -0,0 +1,267 @@ +import tensorflow as tf +import tensorflow.contrib.rnn as rnn +from math import sqrt +from numpy import argmax +dropout_prob = 0.5 +n_kept_model = 2 +k_word_candidate = 3 +k_bpos_candidate = 3 +buffer_max_len = 300 +stack_max_len = 200 +actions_max_len = 400 +lstm_dim = 100 +subword_lstm_dim = 64 +output_dim = 20 +word_vocab_size = 100004 +subword_vocab_size = 100004 +word_emb_dim = 64 +param_emb_dim = 30 +candidate_emb_dim = 20 +n_pos = 16 +n_bpos = 61 +n_label = 38 +n_class = 106 +buffer = tf.Variable(tf.zeros([1, buffer_max_len, lstm_dim]), trainable=False, name='input_buffer') +stack = tf.Variable(tf.zeros([1, stack_max_len, lstm_dim]), trainable=False, name='input_stack') +actions = tf.Variable(tf.zeros([1, actions_max_len, lstm_dim]), trainable=False, name='input_actions') +subword_ph = tf.placeholder(tf.int32, [None], name='placeholder_subword') +word_candidates_ph = tf.placeholder(tf.int32, [None, None], name='placeholder_word_candidates') +bpos_ph = tf.placeholder(tf.int32, [None, None], name='placeholder_bpos') +word_ph = tf.placeholder(tf.int32, None, name='placeholder_word') +subword_list_ph = tf.placeholder(tf.int32, [None], name='placeholder_subword_list') +pos_ph = tf.placeholder(tf.int32, None, name='placeholder_pos') +relation_action_ph = tf.placeholder(tf.int32, [None], name='placeholder_rel_action') +action_ph = tf.placeholder(tf.int32, None, name='placeholder_action') +output_mask = tf.placeholder(tf.float32, [None], name='placeholder_output_mask') +label = tf.placeholder(tf.int32, None, name='placeholder_output') +stack_len = tf.placeholder(tf.int32, name='placeholder_stack_len') +buffer_len = tf.placeholder(tf.int32, name='placeholder_buffer_len') +actions_len = tf.placeholder(tf.int32, name='placeholder_actions_len') +subwords_len = tf.placeholder(tf.int32, name='placeholder_subwords_len') +word_lm_emb = tf.Variable(tf.zeros([word_vocab_size, word_emb_dim]), trainable=False, name='embedding_word_lm') +subword_emb = tf.Variable(tf.zeros([subword_vocab_size, word_emb_dim]), trainable=False, name='embedding_subword') +word_emb_ph = tf.placeholder(tf.float32, [word_vocab_size, word_emb_dim]) +subword_emb_ph = tf.placeholder(tf.float32, [word_vocab_size, word_emb_dim]) +assign_word_embedding = tf.assign(word_lm_emb, word_emb_ph, validate_shape=False) +assign_subword_embedding = tf.assign(subword_emb, subword_emb_ph, validate_shape=False) +word_emb = tf.Variable(tf.random_uniform([word_vocab_size, candidate_emb_dim], minval=-0.1, maxval=0.1), name='embedding_word') +bpos_emb = tf.Variable(tf.random_uniform([n_bpos, candidate_emb_dim], minval=-0.1, maxval=0.1), name='embedding_bpos') +pos_emb = tf.Variable(tf.random_uniform([n_pos, param_emb_dim], minval=-0.1, maxval=0.1), name='embedding_pos') +action_emb = tf.Variable(tf.random_uniform([n_label, param_emb_dim], minval=-0.1, maxval=0.1), name='embedding_action') + + +def nn_run_lstm_input(input_vec, dim, seq_len, scope_name): + with tf.variable_scope(scope_name) as vs: + lstm_fw_cell = rnn.BasicLSTMCell(dim, reuse=tf.get_variable_scope().reuse) + lstm_outputs, _ = tf.nn.dynamic_rnn(lstm_fw_cell, input_vec, dtype=tf.float32, scope=vs, sequence_length=seq_len) + outputs = tf.reshape(lstm_outputs[:, (0), :], [1, dim]) + return outputs + + +with tf.name_scope('input_layer'): + padded_buffer = tf.pad(buffer, [[0, 0], [0, buffer_max_len - buffer_len], [0, 0]]) + padded_stack = tf.pad(stack, [[0, 0], [0, stack_max_len - stack_len], [0, 0]]) + reshaped_padded_buffer = tf.reshape(padded_buffer, [1, buffer_max_len, lstm_dim]) + reshaped_padded_stack = tf.reshape(padded_stack, [1, stack_max_len, lstm_dim]) + lstm_buffer = nn_run_lstm_input(reshaped_padded_buffer, lstm_dim, buffer_len, 'lstm_buffer') + lstm_stack = nn_run_lstm_input(reshaped_padded_stack, lstm_dim, stack_len, 'lstm_stack') + input_concat_vec = tf.concat([lstm_stack, lstm_buffer], axis=-1) + input_dim = 2 * lstm_dim + input_weight = tf.Variable(tf.truncated_normal([input_dim, output_dim], stddev=1.0 / sqrt(output_dim)), name='weight_input') + input_bias = tf.Variable(tf.zeros(output_dim), name='bias_input') + input_out = tf.nn.relu(tf.matmul(input_concat_vec, input_weight) + input_bias) +with tf.name_scope('output_layer'): + output_weight = tf.Variable(tf.truncated_normal([output_dim, n_class], stddev=1.0 / sqrt(n_class)), name='weight_output') + output_bias = tf.Variable(tf.zeros(n_class), name='bias_output') + out = tf.matmul(input_out, output_weight) + output_bias + prediction = tf.multiply(tf.nn.softmax(out), output_mask) +with tf.name_scope('calculate_loss'): + dropped_hidden = tf.nn.dropout(input_out, dropout_prob) + dropped_output = tf.matmul(dropped_hidden, output_weight) + output_bias + one_hot_labels = tf.one_hot(label, n_class, on_value=1.0, off_value=0.0) + ce_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=dropped_output, labels=one_hot_labels)) + loss = ce_loss +with tf.name_scope('initial_buffer'): + mapped_subwords_b = tf.nn.embedding_lookup(subword_emb, subword_ph) + mapped_word_can = tf.nn.embedding_lookup(word_emb, word_candidates_ph) + reshaped_word_can = tf.reshape(mapped_word_can, [tf.shape(mapped_word_can)[0], -1]) + mapped_bpos = tf.nn.embedding_lookup(bpos_emb, bpos_ph) + reshaped_bpos = tf.reshape(mapped_bpos, [tf.shape(mapped_bpos)[0], -1]) + buffer_concat_vec = tf.concat([mapped_subwords_b, reshaped_word_can, reshaped_bpos], axis=-1) + input_dim = (k_word_candidate + k_bpos_candidate) * candidate_emb_dim + word_emb_dim + buffer_weight = tf.Variable(tf.truncated_normal([input_dim, lstm_dim], stddev=1.0 / sqrt(lstm_dim)), name='weight_buffer') + buffer_bias = tf.Variable(tf.zeros([lstm_dim]), name='bias_buffer') + buffer_list = tf.nn.relu(tf.matmul(buffer_concat_vec, buffer_weight) + buffer_bias) + reshaped_buffer = tf.expand_dims(buffer_list, 0) + assign_buffer = tf.assign(buffer, reshaped_buffer, validate_shape=False) +with tf.name_scope('remove_buffer'): + shorten_buffer = buffer[:, :-1, :] + remove_from_buffer = tf.assign(buffer, shorten_buffer, validate_shape=False) +with tf.name_scope('stack_config'): + stack_word_dim = word_emb_dim + subword_lstm_dim + param_emb_dim + stack_word_weight = tf.Variable(tf.truncated_normal([stack_word_dim, lstm_dim], stddev=1.0 / sqrt(lstm_dim)), name='weight_stack_word') + stack_word_bias = tf.Variable(tf.zeros([lstm_dim]), name='bias_stack_word') + stack_rel_dim = 2 * lstm_dim + param_emb_dim + stack_rel_weight = tf.Variable(tf.truncated_normal([stack_rel_dim, lstm_dim], stddev=1.0 / sqrt(lstm_dim)), name='weight_stack_rel') + stack_rel_bias = tf.Variable(tf.zeros([lstm_dim]), name='bias_stack_rel') +with tf.name_scope('action_config'): + action_weight = tf.Variable(tf.truncated_normal([param_emb_dim, lstm_dim], stddev=1.0 / sqrt(lstm_dim)), name='weight_action') + action_bias = tf.Variable(tf.zeros(lstm_dim), name='bias_action') +with tf.name_scope('stack_word_node'): + mapped_word = tf.nn.embedding_lookup(word_lm_emb, word_ph) + mapped_subwords_s = tf.nn.embedding_lookup(subword_emb, subword_list_ph) + subword_lstm = nn_run_lstm_input(tf.expand_dims(mapped_subwords_s, 0), subword_lstm_dim, subwords_len, 'lstm_subword') + mapped_pos = tf.nn.embedding_lookup(pos_emb, pos_ph) + stack_concat_vec = tf.concat([mapped_word, subword_lstm, mapped_pos], axis=-1) + stack_vec = tf.nn.relu(tf.matmul(stack_concat_vec, stack_word_weight) + stack_word_bias) + reshaped_stack_vec = tf.expand_dims(stack_vec, 0) +with tf.name_scope('left_arc'): + left_dep_node = stack[:, (-2), :] + right_head_node = stack[:, (-1), :] + la_mapped_action = tf.nn.embedding_lookup(action_emb, relation_action_ph) + left_arc_concat = tf.concat([right_head_node, left_dep_node, la_mapped_action], axis=-1) + composed_left_arc = tf.nn.tanh(tf.matmul(left_arc_concat, stack_rel_weight) + stack_rel_bias) + reshaped_left_arc_vec = tf.expand_dims(composed_left_arc, 0) +with tf.name_scope('right_arc'): + left_head_node = stack[:, (-2), :] + right_dep_node = stack[:, (-1), :] + ra_mapped_action = tf.nn.embedding_lookup(action_emb, relation_action_ph) + right_arc_concat = tf.concat([left_head_node, right_dep_node, ra_mapped_action], axis=-1) + composed_right_arc = tf.nn.tanh(tf.matmul(right_arc_concat, stack_rel_weight) + stack_rel_bias) + reshaped_right_arc_vec = tf.expand_dims(composed_right_arc, 0) +with tf.name_scope('modify_stack'): + initial_stack = tf.assign(stack, reshaped_stack_vec, validate_shape=False) + new_word_stack = tf.concat([stack, reshaped_stack_vec], axis=1) + add_word_to_stack = tf.assign(stack, new_word_stack, validate_shape=False) + new_replaced_word = tf.concat([stack[:, :-1, :], reshaped_stack_vec], axis=1) + replace_word_tos = tf.assign(stack, new_replaced_word, validate_shape=False) + new_stack_left_arc = tf.concat([stack[:, :-2, :], reshaped_left_arc_vec], axis=1) + left_arc_replace = tf.assign(stack, new_stack_left_arc, validate_shape=False) + new_stack_right_arc = tf.concat([stack[:, :-2, :], reshaped_right_arc_vec], axis=1) + right_arc_replace = tf.assign(stack, new_stack_right_arc, validate_shape=False) +with tf.name_scope('modify_actions'): + mapped_actions = tf.nn.embedding_lookup(action_emb, action_ph) + action_vec = tf.nn.relu(tf.matmul(mapped_actions, action_weight) + action_bias) + reshaped_action_vec = tf.expand_dims(action_vec, 0) + initial_actions = tf.assign(actions, reshaped_action_vec, validate_shape=False) + new_actions = tf.concat([actions, reshaped_action_vec], axis=1) + add_to_actions = tf.assign(actions, new_actions, validate_shape=False) +with tf.name_scope('optimize'): + optimizer = tf.train.AdamOptimizer(name='parser_opt') + variable_list = tf.trainable_variables() + compute_grad = optimizer.compute_gradients(loss, var_list=variable_list) + gradient_tensor_ph = tf.placeholder(tf.float32, name='placeholder_gradient') + calc_gradient_sum = tf.reduce_sum(gradient_tensor_ph, 0) + gradient_ph = [(tf.placeholder(tf.float32), grad_info[1]) for grad_info in compute_grad] + apply_grad = optimizer.apply_gradients(gradient_ph) +init = tf.global_variables_initializer() +saver = tf.train.Saver(max_to_keep=n_kept_model) + + +class ParserModel: + + def __init__(self, params, embeddings=None, model_path=None): + self.params = params + self.session = tf.Session() + if model_path is not None: + saver.restore(self.session, model_path) + else: + if embeddings is None: + raise Exception('You must feed embeddings value if model_path is not defined.') + feed_dict = {word_emb_ph: embeddings['word'], subword_emb_ph: embeddings['subword']} + self.session.run(init) + self.session.run([assign_word_embedding, assign_subword_embedding], feed_dict=feed_dict) + self.subwords = list() + self.tos_subwords = list() + self.stack_len = 0 + self.buffer_len = 0 + self.actions_len = 0 + self.gradients = [list() for _ in range(len(compute_grad))] + self.stack_states = list() + self.action_state = None + + def calc_loss(self, action_label, feasible_actions): + feed_dict = {label: [action_label], output_mask: feasible_actions, stack_len: self.stack_len, buffer_len: self.buffer_len, actions_len: self.actions_len} + train_loss = self.session.run(loss, feed_dict=feed_dict) + grad_op = [grad[0] for grad in compute_grad if grad[0] is not None] + grad_idx = [idx for idx, grad in enumerate(compute_grad) if grad[0] is not None] + gradients = self.session.run(grad_op, feed_dict=feed_dict) + for idx, value in zip(grad_idx, gradients): + self.gradients[idx].append(value) + return train_loss + + def train(self): + feed_dict = dict() + for grad_idx, grad_value in enumerate(self.gradients): + if len(grad_value) > 0: + computed_value = self.session.run(calc_gradient_sum, feed_dict={gradient_tensor_ph: grad_value}) + feed_dict[gradient_ph[grad_idx][0]] = computed_value + else: + feed_dict[gradient_ph[grad_idx][0]] = self.session.run(tf.zeros(tf.shape(compute_grad[grad_idx][1]))) + self.session.run(apply_grad, feed_dict=feed_dict) + + def predict(self, feasible_actions): + feed_dict = {output_mask: feasible_actions, stack_len: self.stack_len, buffer_len: self.buffer_len, actions_len: self.actions_len} + pred = self.session.run(prediction, feed_dict=feed_dict) + return argmax(pred) + + def save_model(self, model_path, global_step): + saver.save(self.session, model_path, global_step=global_step) + print('Model at epoch', global_step, 'is saved.') + + def initial_parser_model(self, subwords, word_candidates, bpos_candidates, real_subword): + self.tos_subwords = list() + self.subwords = real_subword + self.buffer_len = len(real_subword) + 1 + self.stack_len = 1 + self.actions_len = 1 + for grad_list in self.gradients: + grad_list.clear() + feed_dict = {subword_ph: [1] + list(reversed(subwords)), word_candidates_ph: [[1] * k_word_candidate] + list(reversed(word_candidates)), bpos_ph: [[n_bpos - 1] * k_bpos_candidate] + list(reversed(bpos_candidates)), buffer_len: self.buffer_len} + self.session.run(assign_buffer, feed_dict=feed_dict) + feed_dict = {word_ph: [1], subword_list_ph: [1], pos_ph: [n_pos - 1], stack_len: self.buffer_len, subwords_len: 1} + self.session.run(initial_stack, feed_dict=feed_dict) + + def take_action(self, action_index): + action, params = self.params['reverse_action_map'][action_index] + if action == 'SHIFT': + self.take_action_shift(params) + elif action == 'APPEND': + self.take_action_append(params) + elif action == 'LEFT-ARC': + self.take_action_left_arc(params) + else: + self.take_action_right_arc(params) + self.actions_len += 1 + + def take_action_shift(self, pos_tag): + word = self.subwords[0] + self.tos_subwords = [word] + self.subwords = self.subwords[1:] + self.stack_len += 1 + self.buffer_len -= 1 + feed_dict = self.get_word_stack_feed_dict(word, self.tos_subwords, pos_tag) + self.session.run([add_word_to_stack, remove_from_buffer], feed_dict=feed_dict) + + def take_action_append(self, pos_tag): + subword = self.subwords[0] + self.tos_subwords.append(subword) + word = ''.join(self.tos_subwords) + self.subwords = self.subwords[1:] + self.buffer_len -= 1 + feed_dict = self.get_word_stack_feed_dict(word, self.tos_subwords, pos_tag) + self.session.run([replace_word_tos, remove_from_buffer], feed_dict=feed_dict) + + def take_action_left_arc(self, rel): + self.stack_len -= 1 + feed_dict = {relation_action_ph: [self.params['dep_label_map'][rel]], stack_len: self.stack_len} + self.session.run(left_arc_replace, feed_dict=feed_dict) + + def take_action_right_arc(self, rel): + self.stack_len -= 1 + feed_dict = {relation_action_ph: [self.params['dep_label_map'][rel]], stack_len: self.stack_len} + self.session.run(right_arc_replace, feed_dict=feed_dict) + + def get_word_stack_feed_dict(self, word, subwords, pos): + feed_dict = {word_ph: [self.params['word_map'].get(word, 0)], subword_list_ph: [self.params['subword_map'].get(subword, 0) for subword in subwords], pos_ph: [self.params['pos_map'][pos]], buffer_len: self.buffer_len, stack_len: self.buffer_len, subwords_len: len(self.subwords)} + return feed_dict diff --git a/dataset/preprocessed/557.txt b/dataset/preprocessed/557.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/557.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/558.txt b/dataset/preprocessed/558.txt new file mode 100644 index 0000000..124962a --- /dev/null +++ b/dataset/preprocessed/558.txt @@ -0,0 +1,158 @@ +import tensorflow as tf +import numpy as np +import datetime +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data/') + + +def discriminator(image, reuse=False): + if reuse: + tf.get_variable_scope().reuse_variables() + weights1 = tf.get_variable('discriminator_weights1', [5, 5, 1, 32], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais1 = tf.get_variable('discriminator_bais1', [32], initializer=tf.constant_initializer(0)) + layer1 = tf.nn.conv2d(input=image, filter=weights1, strides=[1, 1, 1, 1], padding='SAME') + layer1 = layer1 + bais1 + layer1 = tf.nn.relu(layer1) + layer1 = pooling_layer(layer1, 5, 2) + weights2 = tf.get_variable('discriminator_weights2', [5, 5, 32, 64], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais2 = tf.get_variable('discriminator_bais2', [64], initializer=tf.constant_initializer(0)) + layer2 = tf.nn.conv2d(input=layer1, filter=weights2, strides=[1, 1, 1, 1], padding='SAME') + layer2 = layer2 + bais2 + layer2 = tf.nn.relu(layer2) + layer2 = pooling_layer(layer2, 5, 2) + weights3 = tf.get_variable('discriminator_weights3', [7 * 7 * 64, 1024], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais3 = tf.get_variable('discriminator_bais3', [1024], initializer=tf.constant_initializer(0)) + layer3 = tf.reshape(layer2, [-1, 7 * 7 * 64]) + layer3 = tf.matmul(layer3, weights3) + layer3 = layer3 + bais3 + layer3 = tf.nn.relu(layer3) + weights4 = tf.get_variable('discriminator_weights4', [1024, 1], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais4 = tf.get_variable('discriminator_bais4', [1], initializer=tf.constant_initializer(0)) + layer4 = tf.matmul(layer3, weights4) + bais4 + return layer4 + + +def pooling_layer(layer, kernel_size, stride_size): + kernel = [1, kernel_size, kernel_size, 1] + stride = [1, stride_size, stride_size, 1] + return tf.nn.max_pool(layer, ksize=kernel, strides=stride, padding='SAME') + + +def conv_layer(layer, kernel, input_shape, output_shape, stride_size, biases): + weights = self.add_weights([kernel, kernel, input_shape, output_shape]) + biases = self.add_biases([output_shape]) + stride = [1, stride_size, stride_size, 1] + layer = tf.nn.conv2d(layer, weights, strides=stride, padding='SAME') + biases + return layer + + +def generator(batch_size, dimension): + z = tf.truncated_normal([batch_size, dimension], mean=0, stddev=1, name='z') + weights1 = tf.get_variable('generator_weights1', [dimension, 3136], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais1 = tf.get_variable('generator_bais1', [3136], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer1 = tf.matmul(z, weights1) + bais1 + layer1 = tf.reshape(layer1, [-1, 56, 56, 1]) + layer1 = tf.contrib.layers.batch_norm(layer1, epsilon=1e-05, scope='bn1') + layer1 = tf.nn.relu(layer1) + weights2 = tf.get_variable('generator_weights2', [3, 3, 1, dimension / 2], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais2 = tf.get_variable('generator_bais2', [dimension / 2], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer2 = tf.nn.conv2d(layer1, weights2, strides=[1, 2, 2, 1], padding='SAME') + layer2 = layer2 + bais2 + layer2 = tf.contrib.layers.batch_norm(layer2, epsilon=1e-05, scope='bn2') + layer2 = tf.nn.relu(layer2) + layer2 = tf.image.resize_images(layer2, [56, 56]) + weights3 = tf.get_variable('generator_weights3', [3, 3, dimension / 2, dimension / 4], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais3 = tf.get_variable('generator_bais3', [dimension / 4], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer3 = tf.nn.conv2d(layer2, weights3, strides=[1, 2, 2, 1], padding='SAME') + layer3 = layer3 + bais3 + layer3 = tf.contrib.layers.batch_norm(layer3, epsilon=1e-05, scope='bn3') + layer3 = tf.nn.relu(layer3) + layer3 = tf.image.resize_images(layer3, [56, 56]) + weights4 = tf.get_variable('generator_weights4', [1, 1, dimension / 4, 1], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais4 = tf.get_variable('generator_bais4', [1], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer4 = tf.nn.conv2d(layer3, weights4, strides=[1, 2, 2, 1], padding='SAME') + layer4 = layer4 + bais4 + layer4 = tf.sigmoid(layer4) + return layer4 + + +session = tf.Session() +batch_size = 50 +dimensions = 100 +x_placeholder = tf.placeholder('float', shape=[None, 28, 28, 1], name='x_placeholder') +generator_z = generator(batch_size, dimensions) +discriminator_x = discriminator(x_placeholder) +discriminator_g = discriminator(generator_z, reuse=True) +generator_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=discriminator_g, labels=tf.ones_like(discriminator_g))) +discriminator_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=discriminator_x, labels=tf.fill([batch_size, 1], 0.9))) +discriminator_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=discriminator_g, labels=tf.zeros_like(discriminator_g))) +discriminator_loss = discriminator_loss_real + discriminator_loss_fake +tvars = tf.trainable_variables() +discriminator_vars = [var for var in tvars if 'discriminator_' in var.name] +generator_vars = [var for var in tvars if 'generator_' in var.name] +with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE) as scope: + discriminator_trainer_fake = tf.train.AdamOptimizer(0.0001).minimize(discriminator_loss_fake, var_list=discriminator_vars) + discriminator_trainer_real = tf.train.AdamOptimizer(0.0001).minimize(discriminator_loss_real, var_list=discriminator_vars) + generator_trainer = tf.train.AdamOptimizer(0.0001).minimize(generator_loss, var_list=generator_vars) +tf.summary.scalar('Generator_loss', generator_loss) +tf.summary.scalar('Discriminator_loss_real', discriminator_loss_real) +tf.summary.scalar('Discriminator_loss_fake', discriminator_loss_fake) +discriminator_real_count_ph = tf.placeholder(tf.float32) +discriminator_fake_count_ph = tf.placeholder(tf.float32) +generator_count_ph = tf.placeholder(tf.float32) +tf.summary.scalar('d_real_count', discriminator_real_count_ph) +tf.summary.scalar('d_fake_count', discriminator_fake_count_ph) +tf.summary.scalar('g_count', generator_count_ph) +discriminator_on_generated = tf.reduce_mean(discriminator(generator(batch_size, dimensions))) +discriminator_on_real = tf.reduce_mean(discriminator(x_placeholder)) +tf.summary.scalar('d_on_generated_eval', discriminator_on_generated) +tf.summary.scalar('d_on_real_eval', discriminator_on_real) +images_for_tensorboard = generator(batch_size, dimensions) +tf.summary.image('Generated_images', images_for_tensorboard, 10) +merged = tf.summary.merge_all() +logdir = 'tensorboard/gan/' +writer = tf.summary.FileWriter(logdir, session.graph) +print(logdir) +saver = tf.train.Saver() +session.run(tf.global_variables_initializer()) +generatorLoss = 0 +discriminatorLossReal, discriminatorLossFake = 1, 1 +discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 +for i in range(100000): + real_image_batch = mnist.train.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + if discriminatorLossFake > 0.6: + _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_fake, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + discriminator_fake_count += 1 + if generatorLoss > 0.5: + _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([generator_trainer, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + generator_count += 1 + if discriminatorLossReal > 0.45: + _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_real, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + discriminator_real_count += 1 + if i % 10 == 0: + real_image_batch = mnist.validation.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + summary = session.run(merged, {x_placeholder: real_image_batch, discriminator_real_count_ph: discriminator_real_count, discriminator_fake_count_ph: discriminator_fake_count, generator_count_ph: generator_count}) + writer.add_summary(summary, i) + discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 + if i % 1000 == 0: + images = session.run(generator(3, dimensions)) + discriminator_result = session.run(discriminator(x_placeholder), {x_placeholder: images}) + print('TRAINING STEP', i, 'AT', datetime.datetime.now()) + for j in range(3): + print('Discriminator classification', discriminator_result[j]) + im = images[(j), :, :, (0)] + plt.imsave('imageAt' + str(i) + 'TyreeMath.png', im.reshape([28, 28]), cmap='Greys') + if i % 5000 == 0: + save_path = saver.save(session, 'models/pretrained_gan.ckpt', global_step=i) + print('saved to %s' % save_path) +test_images = session.run(generator(10, 100)) +test_eval = session.run(discriminator(x_placeholder), {x_placeholder: test_images}) +real_images = mnist.validation.next_batch(10)[0].reshape([10, 28, 28, 1]) +real_eval = session.run(discriminator(x_placeholder), {x_placeholder: real_images}) +for i in range(10): + print(test_eval[i]) + plt.imsave('generated_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap='Greys') +for i in range(10): + print(real_eval[i]) + plt.imsave('real_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap='Greys') diff --git a/dataset/preprocessed/559.txt b/dataset/preprocessed/559.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/559.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/56.txt b/dataset/preprocessed/56.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/56.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/561.txt b/dataset/preprocessed/561.txt new file mode 100644 index 0000000..c730faa --- /dev/null +++ b/dataset/preprocessed/561.txt @@ -0,0 +1,66 @@ +import tensorflow as tf +import numpy as np + + +class CNN_TwoConvLayers: + + def __init__(self, model_name, session, learning_rate, optimizer, learning_decay_rate, filter_sizes, num_filters, max_sentence_length, num_classes, embeddings, new_embeddings, embedding_dim, vocabulary_size, static, max_l2_norm, regularization_lambda, dropout_keep_prob): + self.model_name = model_name + self.session = session + self.learning_rate = learning_rate + self.optimizer = optimizer + self.dropout_keep_prob_train = dropout_keep_prob + self.regularization_lambda = regularization_lambda + self.input_x = tf.placeholder(shape=(None, max_sentence_length), dtype=tf.int32, name='input_x') + self.input_y = tf.placeholder(shape=(None, num_classes), dtype=tf.float32, name='input_y') + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, name='dropout_keep_prob') + self.embeddings_placeholder = tf.placeholder(tf.float32, shape=(vocabulary_size, embedding_dim)) + self.embeddings = tf.Variable(self.embeddings_placeholder, trainable=not static) + self.new_embeddings = tf.Variable(new_embeddings, trainable=True) + self.all_embeddings = tf.concat([self.embeddings, self.new_embeddings], axis=0) + self.embedded_words = tf.nn.embedding_lookup(self.all_embeddings, self.input_x) + self.input_x_expanded = tf.expand_dims(self.embedded_words, -1) + num_filters_conv1 = int(num_filters / 3) + num_filters_conv2 = num_filters - num_filters_conv1 + self.pool_results = [] + for i, filter_size in enumerate(filter_sizes): + filter1 = tf.get_variable('filter1_' + str(i), shape=(filter_size, embedding_dim, 1, num_filters_conv1), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + if max_l2_norm != 0: + filter1 = tf.clip_by_norm(filter1, max_l2_norm) + bias1 = tf.Variable(tf.constant(0.0, shape=(num_filters_conv1,))) + conv1 = tf.nn.conv2d(input=self.input_x_expanded, filter=filter1, strides=[1, 1, 1, 1], padding='VALID') + relu1 = tf.nn.relu(tf.nn.bias_add(conv1, bias1)) + conv1_dim = max_sentence_length - filter_size + 1 + filter2 = tf.get_variable('filter2_' + str(i), shape=(filter_size, 1, num_filters_conv1, num_filters_conv2), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + if max_l2_norm != 0: + filter2 = tf.clip_by_norm(filter2, max_l2_norm) + bias2 = tf.Variable(tf.constant(0.0, shape=(num_filters_conv2,))) + conv2 = tf.nn.conv2d(input=relu1, filter=filter2, strides=[1, 1, 1, 1], padding='VALID') + relu2 = tf.nn.relu(tf.nn.bias_add(conv2, bias2)) + conv2_dim = conv1_dim - filter_size + 1 + pooled = tf.nn.max_pool(relu2, ksize=[1, conv2_dim, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + self.pool_results.append(pooled) + num_filters_total = num_filters_conv2 * len(filter_sizes) + self.flat = tf.reshape(tf.concat(self.pool_results, 3), [-1, num_filters_total]) + self.dropout = tf.nn.dropout(self.flat, self.dropout_keep_prob) + W = tf.get_variable('W', shape=(num_filters_total, num_classes), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + b = tf.Variable(tf.constant(0.1, shape=(num_classes,))) + self.output = tf.nn.xw_plus_b(self.dropout, W, b) + self.predictions = tf.argmax(self.output, 1) + losses = tf.nn.softmax_cross_entropy_with_logits(labels=self.input_y, logits=self.output) + if regularization_lambda != 0: + l2_loss = tf.nn.l2_loss(W) + self.loss = tf.reduce_mean(tf.add(losses, tf.multiply(self.regularization_lambda, l2_loss)), name='loss') + else: + self.loss = tf.reduce_mean(losses, name='loss') + self.optimizer = optimizer(learning_rate=self.learning_rate) + self.train_op = self.optimizer.minimize(self.loss) + self.saver = tf.train.Saver() + self.session.run(tf.global_variables_initializer(), feed_dict={self.embeddings_placeholder: embeddings}) + + def train_step(self, input_x, input_y): + _, loss = self.session.run([self.train_op, self.loss], feed_dict={self.input_x: input_x, self.input_y: input_y, self.dropout_keep_prob: self.dropout_keep_prob_train}) + return loss + + def feed(self, input_x): + return self.session.run([self.output, self.predictions], feed_dict={self.input_x: input_x, self.dropout_keep_prob: 1}) diff --git a/dataset/preprocessed/562.txt b/dataset/preprocessed/562.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/562.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/563.txt b/dataset/preprocessed/563.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/563.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/564.txt b/dataset/preprocessed/564.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/564.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/565.txt b/dataset/preprocessed/565.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/565.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/566.txt b/dataset/preprocessed/566.txt new file mode 100644 index 0000000..8ffa93d --- /dev/null +++ b/dataset/preprocessed/566.txt @@ -0,0 +1,468 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class GRUAttnCell(tf.nn.rnn_cell.GRUCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(GRUAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + gru_out, gru_state = super(GRUAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(gru_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(tf.nn.rnn_cell._linear([context, gru_out], self._num_units, True, 1.0)) + return out, gru_state + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(LSTMAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.relu(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + return out, lstm_state + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + -vocab_dim: dimension of the embeddings + """ + + def __init__(self, size, vocab_dim, name): + self.size = size + self.vocab_dim = vocab_dim + self.name = name + + def encode(self, inputs, masks, encoder_state_input=None, attention_inputs=None, model_type='gru'): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(self.name): + if attention_inputs is None: + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(self.size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(self.size) + else: + raise Exception('Must specify model type.') + elif model_type == 'gru': + cell = GRUAttnCell(self.size, attention_inputs) + elif model_type == 'lstm': + cell = LSTMAttnCell(self.size, attention_inputs) + else: + raise Exception('Must specify model type.') + outputs, final_state = tf.nn.dynamic_rnn(cell, inputs, sequence_length=masks, dtype=tf.float32, initial_state=encoder_state_input) + return outputs, final_state + + +class Decoder(object): + + def __init__(self, output_size, name): + self.output_size = output_size + self.name = name + + def decode(self, knowledge_rep, masks, dropout, state_size, model_type='gru'): + """ + takes in a knowledge representation + # and output a probability estimation over + # all paragraph tokens on which token should be + # the start of the answer span, and which should be + # the end of the answer span. + + # :param knowledge_rep: it is a representation of the paragraph and question, + # decided by how you choose to implement the encoder + # :return: + # """ + with tf.variable_scope(self.name): + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(state_size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(state_size) + else: + raise Exception('Must specify model type.') + knowledge_rep = tf.squeeze(knowledge_rep, -1) + with vs.variable_scope('answer_start'): + start_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer1'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer2'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer3'): + start_probs = tf.nn.rnn_cell._linear(start_probs, state_size, True, 1.0) + start_probs = tf.nn.dropout(start_probs, 1 - dropout) + with vs.variable_scope('startlayer4'): + start_probs = tf.nn.rnn_cell._linear(start_probs, self.output_size, True, 1.0) + with vs.variable_scope('answer_end'): + end_probs = tf.nn.rnn_cell._linear(knowledge_rep, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer1'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer2'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer3'): + end_probs = tf.nn.rnn_cell._linear(end_probs, state_size, True, 1.0) + end_probs = tf.nn.dropout(end_probs, 1 - dropout) + with vs.variable_scope('endlayer4'): + end_probs = tf.nn.rnn_cell._linear(end_probs, self.output_size, True, 1.0) + bool_masks = tf.cast(tf.sequence_mask(masks, maxlen=self.output_size), tf.float32) + a = tf.constant(-1e+30) + b = tf.constant(1.0) + add_mask = a * (b - bool_masks) + start_probs = start_probs + add_mask + end_probs = end_probs + add_mask + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, encoder, decoder, pretrained_embeddings, max_ctx_len, max_q_len, flags): + """ + Initializes your System + + :param encoder: tuple of 2 encoders that you constructed in train.py + :param decoder: a decoder that you constructed in train.py + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.question_encoder, self.context_encoder = encoder + self.decoder = decoder + self.max_ctx_len = max_ctx_len + self.max_q_len = max_q_len + self.embed_size = encoder[0].vocab_dim + self.flags = flags + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.max_ctx_len), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.max_q_len), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def mixer(self, final_q_state, ctx_states, model_type='gru'): + if model_type == 'gru': + pass + elif model_type == 'lstm': + final_q_state = final_q_state[-1] + else: + raise Exception('Must specify model type.') + with vs.variable_scope('mixer'): + ht = tf.nn.rnn_cell._linear(final_q_state, self.flags.state_size, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(ctx_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + ctx_state_rep = ctx_states * scores + return ctx_state_rep + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + question_states, final_question_state = self.question_encoder.encode(self.question_embeddings, self.mask_q_placeholder, encoder_state_input=None, attention_inputs=None, model_type=self.flags.model_type) + ctx_states, final_ctx_state = self.context_encoder.encode(self.context_embeddings, self.mask_ctx_placeholder, encoder_state_input=None, attention_inputs=None, model_type=self.flags.model_type) + feed_states = batch_matmul(ctx_states, tf.expand_dims(final_question_state, 2)) + self.start_probs, self.end_probs = self.decoder.decode(feed_states, self.mask_ctx_placeholder, self.dropout_placeholder, self.flags.state_size, model_type=self.flags.model_type) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.question_embeddings = tf.reshape(question_embeddings, [-1, self.max_q_len, self.embed_size]) + context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + self.context_embeddings = tf.reshape(context_embeddings, [-1, self.max_ctx_len, self.embed_size]) + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = np.array(map(np.array, context_batch)) + input_feed[self.question_placeholder] = np.array(map(np.array, question_batch)) + input_feed[self.mask_ctx_placeholder] = np.array(map(np.array, mask_ctx_batch)) + input_feed[self.mask_q_placeholder] = np.array(mask_q_batch) + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs + + def answer(self, session, data): + data = np.array(data).T + yp, yp2 = self.decode(session, *data) + a_s = np.argmax(yp, axis=1) + a_e = np.argmax(yp2, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset) + else: + sampled = dataset[np.random.choice(dataset.shape[0], sample)] + a_s, a_e = self.answer(session, sampled) + f1 = [] + em = [] + sampled = sampled.T + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][sampled[2][i][0]:sampled[2][i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + em.append(exact_match_score(pred_words, actual_words)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), None, sample)) + return f1, em + + def run_epoch(self, sess, train_set, val_set, context): + prog_train = Progbar(target=1 + int(len(train_set) / self.flags.batch_size)) + for i, batch in enumerate(minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + print('') + if self.flags.debug == 0: + prog_val = Progbar(target=1 + int(len(val_set) / self.flags.batch_size)) + for i, batch in enumerate(minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + train_f1, train_em = self.evaluate_answer(sess, train_set, context=context[0], sample=100, log=True, eval_set='-Epoch TRAIN-') + val_f1, val_em = self.evaluate_answer(sess, val_set, context=context[1], sample=100, log=True, eval_set='-Epoch VAL-') + + def train(self, session, saver, dataset, contexts, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + train_dataset, val_dataset = dataset + train_mask = [None, None] + val_mask = [None, None] + train_dataset[0], train_mask[0] = self.pad(train_dataset[0], self.max_ctx_len) + train_dataset[1], train_mask[1] = self.pad(train_dataset[1], self.max_q_len) + val_dataset[0], val_mask[0] = self.pad(val_dataset[0], self.max_ctx_len) + val_dataset[1], val_mask[1] = self.pad(val_dataset[1], self.max_q_len) + for i in range(1, len(train_dataset[0])): + assert len(train_dataset[0][i]) == len(train_dataset[0][i - 1]), 'Incorrectly padded train context' + assert len(train_dataset[1][i]) == len(train_dataset[1][i - 1]), 'Incorrectly padded train question' + for i in range(1, len(val_dataset[0])): + assert len(val_dataset[0][i]) == len(val_dataset[0][i - 1]), 'Incorrectly padded val context' + assert len(val_dataset[1][i]) == len(val_dataset[1][i - 1]), 'Incorrectly padded val question' + print('Training/val data padding verification completed.') + train_dataset.extend(train_mask) + val_dataset.extend(val_mask) + train_dataset = np.array(train_dataset).T + val_dataset = np.array(val_dataset).T + train_context = contexts[0] + val_context = contexts[1] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = train_dataset[:self.flags.batch_size] + val_dataset = val_dataset[:self.flags.batch_size] + num_epochs = 20 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, context=val_context) + logging.info('Saving model in %s', train_dir) + saver.save(session, train_dir) + self.evaluate_answer(session, train_dataset, train_context, sample=None, log=True, eval_set='-FINAL TRAIN-') + self.evaluate_answer(session, val_dataset, val_context, sample=None, log=True, eval_set='-FINAL VAL-') diff --git a/dataset/preprocessed/567.txt b/dataset/preprocessed/567.txt new file mode 100644 index 0000000..f5c1f81 --- /dev/null +++ b/dataset/preprocessed/567.txt @@ -0,0 +1,51 @@ +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + data_d = tf.nn.dropout(data, keep_prob=0.9) + output1 = tf.matmul(data_d, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/568.txt b/dataset/preprocessed/568.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/568.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/569.txt b/dataset/preprocessed/569.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/569.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/57.txt b/dataset/preprocessed/57.txt new file mode 100644 index 0000000..374bf63 --- /dev/null +++ b/dataset/preprocessed/57.txt @@ -0,0 +1,112 @@ +import tensorflow as tf + + +class sigGF: + + def __init__(self, image_shape, person_num): + self.image_shape = image_shape + self.person_num = person_num + self.X = tf.placeholder(tf.float32, [None, self.image_shape[0], self.image_shape[1], self.image_shape[2]]) + self.Y_M = tf.placeholder(tf.float32, [None, person_num]) + self.Y_D = tf.placeholder(tf.float32, [None, 2]) + self.keep_prob = tf.placeholder(tf.float32) + self.global_step = tf.Variable(0, trainable=False, name='global_step') + self.feature_vec = None + self.cost_M = None + self.cost_D = None + self.logit_M = None + self.logit_D = None + self.optimizer = None + self.build_model() + self.saver = tf.train.Saver(tf.global_variables()) + + def build_model(self): + _input = self.X + W1 = tf.Variable(tf.random_normal([3, 3, 3, 32], stddev=0.01)) + L1 = tf.nn.conv2d(_input, W1, strides=[1, 1, 1, 1], padding='SAME') + L1 = tf.nn.relu(L1) + L1 = tf.nn.max_pool(L1, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME') + W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01)) + L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') + L2 = tf.nn.relu(L2) + L2 = tf.nn.max_pool(L2, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME') + L3 = tf.contrib.layers.flatten(L2) + L3 = tf.layers.dense(L3, 512, activation=tf.nn.relu) + L3 = tf.layers.dropout(L3, self.keep_prob) + L4 = tf.contrib.layers.flatten(L3) + L4 = tf.layers.dense(L4, 256, activation=tf.nn.relu) + L4 = tf.layers.dropout(L4, self.keep_prob) + self.feature_vec = L4 + self.logit_M = tf.layers.dense(L4, self.person_num, activation=None, trainable=False) + self.cost_M = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.logit_M, labels=self.Y_M)) + self.logit_D = tf.layers.dense(L4, 2, activation=None, trainable=False) + self.cost_D = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.logit_D, labels=self.Y_D)) + self.optimizer = tf.train.AdamOptimizer(0.001).minimize(self.cost_M + self.cost_D) + + def train(self, session, X, Y_M, Y_D): + return session.run([self.optimizer, self.cost_M, self.cost_D], feed_dict={self.X: X, self.Y_M: Y_M, self.Y_D: Y_D, self.keep_prob: 0.7}) + + def predict(self, session, X): + return session.run([self.logit_M, self.logit_D], feed_dict={self.X: X, self.keep_prob: 1}) + + def test(self, session, X, Y_M, Y_D): + self.is_correct = tf.equal(tf.argmax(self.logit_M, 1), tf.argmax(self.Y_M, 1)) + self.acc_M = tf.reduce_mean(tf.cast(self.is_correct, tf.float32)) + self.is_correct2 = tf.equal(tf.argmax(self.logit_D, 1), tf.argmax(self.Y_D, 1)) + self.acc_D = tf.reduce_mean(tf.cast(self.is_correct2, tf.float32)) + return session.run([self.acc_M, self.acc_D], feed_dict={self.X: X, self.Y_M: Y_M, self.Y_D: Y_D, self.keep_prob: 1}) + + def get_feature(self, session, X): + return session.run(self.feature_vec, feed_dict={self.X: X, self.keep_prob: 1}) + + +class sigG: + + def __init__(self, image_shape, person_num): + self.image_shape = image_shape + self.person_num = person_num + self.X = tf.placeholder(tf.float32, [None, self.image_shape[0], self.image_shape[1], self.image_shape[2]]) + self.Y_M = tf.placeholder(tf.float32, [None, person_num]) + self.keep_prob = tf.placeholder(tf.float32) + self.global_step = tf.Variable(0, trainable=False, name='global_step') + self.feature_vec = None + self.cost_M = None + self.logit_M = None + self.optimizer = None + self.build_model() + self.saver = tf.train.Saver(tf.global_variables()) + + def build_model(self): + _input = self.X + W1 = tf.Variable(tf.random_normal([3, 3, 3, 32], stddev=0.01)) + L1 = tf.nn.conv2d(_input, W1, strides=[1, 1, 1, 1], padding='SAME') + L1 = tf.nn.relu(L1) + L1 = tf.nn.max_pool(L1, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME') + W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01)) + L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') + L2 = tf.nn.relu(L2) + L2 = tf.nn.max_pool(L2, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME') + L3 = tf.contrib.layers.flatten(L2) + L3 = tf.layers.dense(L3, 512, activation=tf.nn.relu) + L3 = tf.layers.dropout(L3, self.keep_prob) + L4 = tf.contrib.layers.flatten(L3) + L4 = tf.layers.dense(L4, 256, activation=tf.nn.relu) + L4 = tf.layers.dropout(L4, self.keep_prob) + self.feature_vec = L4 + self.logit_M = tf.layers.dense(L4, self.person_num, activation=None, trainable=False) + self.cost_M = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.logit_M, labels=self.Y_M)) + self.optimizer = tf.train.AdamOptimizer(0.001).minimize(self.cost_M) + + def train(self, session, X, Y_M): + return session.run([self.optimizer, self.cost_M], feed_dict={self.X: X, self.Y_M: Y_M, self.keep_prob: 0.7}) + + def predict(self, session, X): + return session.run(self.logit_M, feed_dict={self.X: X, self.keep_prob: 1}) + + def test(self, session, X, Y_M): + self.is_correct = tf.equal(tf.argmax(self.logit_M, 1), tf.argmax(self.Y_M, 1)) + self.acc_M = tf.reduce_mean(tf.cast(self.is_correct, tf.float32)) + return session.run(self.acc_M, feed_dict={self.X: X, self.Y_M: Y_M, self.keep_prob: 1}) + + def get_feature(self, session, X): + return session.run(self.feature_vec, feed_dict={self.X: X, self.keep_prob: 1}) diff --git a/dataset/preprocessed/570.txt b/dataset/preprocessed/570.txt new file mode 100644 index 0000000..99fa9f0 --- /dev/null +++ b/dataset/preprocessed/570.txt @@ -0,0 +1,175 @@ +import tensorflow as tf +import numpy as np +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + self.rewards = tf.placeholder(tf.float32, shape=(None,), name='reward') + self.indices = tf.placeholder(tf.int32, shape=(None, 2), name='indices') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.tanh(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + self.select_prob = tf.gather_nd(self.prob, self.indices) + self.result = tf.multiply(self.select_prob, self.rewards) + self.entropy = -tf.reduce_sum(tf.multiply(self.prob, tf.nn.softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)))) + self.pg_loss = tf.reduce_mean(self.result) + l2 * self.entropy + self.pg_optimization = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(self.pg_loss) + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + self.saver = tf.train.Saver() + self.saved_path = './saved_model' + self.count = 0 + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def policy_train(self, session, inputs, outputs, rewards): + form, pos, deprel = inputs + indices = [] + for aid in range(len(outputs)): + indices.append([aid, outputs[aid]]) + _, cost, indices, prob, res = session.run([self.pg_optimization, self.pg_loss, self.indices, self.prob, self.result], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) + self.count += 1 + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + def variable_output(self, session): + form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + def save(self, session): + self.saved_path = self.saver.save(session, './saved_model') + + def restore(self, session): + self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.ebedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + session.run(self.update_op) diff --git a/dataset/preprocessed/571.txt b/dataset/preprocessed/571.txt new file mode 100644 index 0000000..09e1acc --- /dev/null +++ b/dataset/preprocessed/571.txt @@ -0,0 +1,924 @@ +import tensorflow as tf +import numpy as np +import os + + +class PG_Crypto(object): + + def __init__(self, feature_number, hidden_units_number=[64, 32, 2], learning_rate=0.001): + tf.reset_default_graph() + self.s = tf.placeholder(dtype=tf.float32, shape=[None, feature_number], name='environment_features') + self.a = tf.placeholder(dtype=tf.int32, shape=[None], name='a') + self.r = tf.placeholder(dtype=tf.float32, shape=[None], name='r') + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_keep_prob') + with tf.variable_scope('rnn', initializer=tf.contrib.layers.xavier_initializer(uniform=False), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.a_prob = self._add_dense_layer(inputs=self.s, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.a_out = tf.nn.softmax(self.a_prob) + with tf.variable_scope('reward'): + negative_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.a_prob, labels=self.a) + with tf.variable_scope('train'): + optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) + self.loss = tf.reduce_mean(negative_cross_entropy * self.r) + self.train_op = optimizer.minimize(self.loss) + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act=tf.nn.relu, use_bias=True): + output = inputs + for n in output_shape: + output = tf.layers.dense(output, n, activation=act, use_bias=use_bias) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + def _add_gru_cell(self, units_number, activation=tf.nn.relu): + return tf.contrib.rnn.GRUCell(num_units=units_number, activation=activation) + + def train(self, drop=0.85): + feed = {self.a: np.array(self.a_buffer), self.r: np.array(self.r_buffer), self.s: np.array(self.s_buffer), self.dropout_keep_prob: drop} + _, loss = self.session.run([self.train_op, self.loss], feed_dict=feed) + return loss + + def restore_buffer(self): + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + + def save_transation(self, s, a, r): + self.a_buffer.append(a) + self.r_buffer.append(r) + self.s_buffer.append(s) + + def trade(self, s, train=False, drop=1.0): + feed = {self.s: s, self.dropout_keep_prob: drop} + a_prob = self.session.run([self.a_out], feed_dict=feed) + a_prob = a_prob[0].flatten() + if train: + a_indices = np.arange(a_prob.shape[0]) + return np.random.choice(a_indices, p=a_prob) + else: + return np.argmax(a_prob) + + def load_model(self, model_path='./PGModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./PGModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) + + +class PG_Crypto_portfolio(object): + + def __init__(self, feature_number, action_size=1, hidden_units_number=[300, 300, 128], learning_rate=0.001): + tf.reset_default_graph() + self.s = tf.placeholder(dtype=tf.float32, shape=[None, feature_number], name='s') + self.a = tf.placeholder(dtype=tf.int32, shape=[None, action_size], name='a') + self.r = tf.placeholder(dtype=tf.float32, shape=[None], name='r') + self.action_size = action_size + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_keep_prob') + with tf.variable_scope('policy', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.a_prob = self._add_dense_layer(inputs=self.s, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.a_prob = self._add_dense_layer(inputs=self.a_prob, output_shape=[self.action_size], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.a_out = tf.nn.softmax(self.a_prob) + with tf.variable_scope('reward'): + negative_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.a_prob, labels=self.a) + with tf.variable_scope('train'): + optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) + self.loss = tf.reduce_mean(negative_cross_entropy * self.r) + self.train_op = optimizer.minimize(self.loss) + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act=tf.nn.relu, use_bias=True): + output = inputs + for n in output_shape: + output = tf.layers.dense(output, n, activation=act, use_bias=use_bias) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + def train(self, drop=0.85): + random_index = np.arange(len(self.a_buffer)) + np.random.shuffle(random_index) + feed = {self.a: np.array(self.a_buffer)[random_index], self.r: np.array(self.r_buffer)[random_index], self.s: np.array(self.s_buffer)[random_index], self.dropout_keep_prob: drop} + _, loss = self.session.run([self.train_op, self.loss], feed_dict=feed) + return loss + + def restore_buffer(self): + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + + def save_transation(self, s, a, r): + self.a_buffer.append(a) + self.r_buffer.append(r) + self.s_buffer.append(s) + + def trade(self, s, train=False, drop=1.0): + feed = {self.s: s, self.dropout_keep_prob: drop} + a_prob = self.session.run([self.a_out], feed_dict=feed) + a_prob = a_prob[0].flatten() + if train: + a_indices = np.arange(a_prob.shape[0]) + target_index = np.random.choice(a_indices, p=a_prob) + a = np.zeros(a_prob.shape[0]) + a[target_index] = 1.0 + return a + else: + target_index = np.argmax(a_prob) + a = np.zeros(a_prob.shape[0]) + a[target_index] = 1.0 + return a + + def load_model(self, model_path='./PGModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./PGModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) + + +class RPG_Crypto_portfolio(object): + + def __init__(self, feature_number, action_size=1, hidden_units_number=[128, 64], learning_rate=0.001): + tf.reset_default_graph() + self.s = tf.placeholder(dtype=tf.float32, shape=[None, feature_number], name='s') + self.a = tf.placeholder(dtype=tf.int32, shape=[None, action_size], name='a') + self.r = tf.placeholder(dtype=tf.float32, shape=[None], name='r') + self.s_next = tf.placeholder(dtype=tf.float32, shape=[None, feature_number], name='s_next') + self.action_size = action_size + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.s_next_buffer = [] + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_keep_prob') + with tf.variable_scope('rnn_encoder', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + cell = self._add_GRU(units_number=128, activation=tf.nn.tanh, keep_prob=self.dropout_keep_prob) + self.rnn_input = tf.expand_dims(self.s, axis=0) + self.rnn_output, _ = tf.nn.dynamic_rnn(inputs=self.rnn_input, cell=cell, dtype=tf.float32) + self.rnn_output = tf.unstack(self.rnn_output, axis=0)[0] + with tf.variable_scope('supervised', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=[feature_number], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + with tf.variable_scope('policy_gradient', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.a_prob = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.a_prob = self._add_dense_layer(inputs=self.a_prob, output_shape=[action_size], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.a_out = tf.nn.softmax(self.a_prob, axis=-1) + self.negative_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.a_prob, labels=self.a) + with tf.variable_scope('train'): + optimizer_rl = tf.train.AdamOptimizer(learning_rate=learning_rate) + optimizer_sl = tf.train.AdamOptimizer(learning_rate=learning_rate * 2) + self.rlloss = tf.reduce_mean(self.negative_cross_entropy * self.r) + self.slloss = tf.reduce_mean(self.state_loss) + self.rltrain_op = optimizer_rl.minimize(self.rlloss) + self.sltrain_op = optimizer_sl.minimize(self.slloss) + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act=tf.nn.relu, use_bias=True): + output = inputs + for n in output_shape: + output = tf.layers.dense(output, n, activation=act, use_bias=use_bias) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + def _add_GRU(self, units_number, activation=tf.nn.relu, keep_prob=1.0): + cell = tf.contrib.rnn.GRUCell(units_number, activation=activation) + cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) + return cell + + def _add_GRUs(self, units_number, activation, keep_prob=1.0): + cells = tf.contrib.rnn.MultiRNNCell(cells=[self._add_GRU(units_number=n, activation=a) for n, a in zip(units_number, activation)]) + return cells + + def _add_gru_cell(self, units_number, activation=tf.nn.relu): + return tf.contrib.rnn.GRUCell(num_units=units_number, activation=activation) + + def train(self, drop=0.85): + feed = {self.a: np.array(self.a_buffer), self.r: np.array(self.r_buffer), self.s: np.array(self.s_buffer), self.s_next: np.array(self.s_next_buffer), self.dropout_keep_prob: drop} + self.session.run([self.rltrain_op, self.sltrain_op], feed_dict=feed) + + def restore_buffer(self): + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.s_next_buffer = [] + + def save_current_state(self, s): + self.s_buffer.append(s) + + def save_transation(self, a, r, s_next): + self.a_buffer.append(a) + self.r_buffer.append(r) + self.s_next_buffer.append(s_next) + + def trade(self, s, train=False, drop=1.0, prob=False): + feed = {self.s: np.array(self.s_buffer), self.dropout_keep_prob: drop} + a_prob = self.session.run([self.a_out], feed_dict=feed) + a_prob = a_prob[-1][-1].flatten() + if train: + a_indices = np.arange(a_prob.shape[0]) + target_index = np.random.choice(a_indices, p=a_prob) + a = np.zeros(a_prob.shape[0]) + a[target_index] = 1.0 + return a + else: + if prob: + return a_prob + target_index = np.argmax(a_prob) + a = np.zeros(a_prob.shape[0]) + a[target_index] = 1.0 + return a + + def load_model(self, model_path='./RPGModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./RPGModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) + + +class DRL_Crypto_portfolio(object): + + def __init__(self, feature_number, action_size=1, c=1e-05, hidden_units_number=[128, 64], learning_rate=0.001): + tf.reset_default_graph() + self.s = tf.placeholder(dtype=tf.float32, shape=[None, feature_number], name='s') + self.d = tf.placeholder(dtype=tf.float32, shape=[None, action_size - 1], name='d') + self.s_buffer = [] + self.d_buffer = [] + self.c = c + self.action_size = action_size + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_keep_prob') + with tf.variable_scope('rnn_encoder', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + cells = self._add_GRUs(units_number=[128, action_size], activation=[tf.nn.relu, tf.nn.relu]) + self.rnn_input = tf.expand_dims(self.s, axis=0) + self.rnn_output, _ = tf.nn.dynamic_rnn(inputs=self.rnn_input, cell=cells, dtype=tf.float32) + self.a_prob = tf.unstack(self.rnn_output, axis=0)[0] + with tf.variable_scope('direct_RL', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.a_out = tf.nn.softmax(self.a_prob, axis=-1) + self.a_out = tf.concat((tf.zeros(dtype=tf.float32, shape=[1, self.action_size]), self.a_out), axis=0) + self.reward = tf.reduce_sum(self.d * self.a_out[:-1, :-1] - self.c * tf.abs(self.a_out[1:, :-1] - self.a_out[:-1, :-1]), axis=1) + self.total_reward = tf.reduce_sum(self.reward) + self.mean_reward = tf.reduce_mean(self.reward) + with tf.variable_scope('train'): + optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) + self.train_op = optimizer.minimize(-self.mean_reward) + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act=tf.nn.relu, use_bias=True): + output = inputs + for n in output_shape: + output = tf.layers.dense(output, n, activation=act, use_bias=use_bias) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + def _add_GRU(self, units_number, activation=tf.nn.relu, keep_prob=1.0): + cell = tf.contrib.rnn.LSTMCell(units_number, activation=activation) + cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) + return cell + + def _add_GRUs(self, units_number, activation, keep_prob=1.0): + cells = tf.contrib.rnn.MultiRNNCell(cells=[self._add_GRU(units_number=n, activation=a) for n, a in zip(units_number, activation)]) + return cells + + def _add_gru_cell(self, units_number, activation=tf.nn.relu): + return tf.contrib.rnn.GRUCell(num_units=units_number, activation=activation) + + def train(self, drop=0.85): + feed = {self.s: np.array(self.s_buffer), self.d: np.array(self.d_buffer), self.dropout_keep_prob: drop} + self.session.run([self.train_op], feed_dict=feed) + + def restore_buffer(self): + self.s_buffer = [] + self.d_buffer = [] + + def save_current_state(self, s, d): + self.s_buffer.append(s) + self.d_buffer.append(d) + + def trade(self, train=False, drop=1.0, prob=False): + feed = {self.s: np.array(self.s_buffer), self.dropout_keep_prob: drop} + a_prob = self.session.run([self.a_out], feed_dict=feed) + a_prob = a_prob[-1][-1].flatten() + return a_prob + + def load_model(self, model_path='./RPGModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./RPGModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) + + +class DuelingDQN_portfolio(object): + + def __init__(self, a_dim, s_dim, buffer_size, batch_size, update_target_interval=50, epsilon=0.9, gamma=0.9, learning_rate=0.001): + tf.reset_default_graph() + self.a_dim, self.s_dim = a_dim, s_dim + self.buffer_size = buffer_size + self.batch_size = batch_size + self.buffer = np.zeros((self.buffer_size, self.s_dim * 2 + 2), dtype=np.float32) + self.buffer_length = 0 + self.update_target_interval = update_target_interval + self.critic_loss = 0 + self.total_step = 0 + self.epsilon = epsilon + self.learning_rate = learning_rate + self.gamma = gamma + self.s = tf.placeholder(tf.float32, [None, self.s_dim], name='s') + self.s_next = tf.placeholder(tf.float32, [None, self.s_dim], name='s_next') + self.q_next = tf.placeholder(tf.float32, [None, self.a_dim], name='q_next') + with tf.variable_scope('q_eval', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)) as scope: + self.q_eval = self._build_net(self.s, scope) + with tf.variable_scope('q_target', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)) as scope: + self.q_target = self._build_net(self.s_next, scope) + with tf.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.squared_difference(self.q_next, self.q_eval)) + with tf.variable_scope('train'): + self.train_op = tf.train.RMSPropOptimizer(self.learning_rate).minimize(self.loss) + self.e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_eval') + self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_target') + self.update_q_target_op = [tf.assign(t, e) for t, e in zip(self.t_params, self.e_params)] + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _build_net(self, s, scope): + with tf.variable_scope(scope): + net = tf.layers.dense(s, 64, activation=tf.nn.tanh, name='l1') + net = tf.layers.dense(net, 32, activation=tf.nn.tanh, name='l2') + value = tf.layers.dense(net, 1, activation=None, name='a') + advantage = tf.layers.dense(net, self.a_dim, activation=None, name='advantage') + q = value + (advantage - tf.reduce_mean(advantage, axis=1, keep_dims=True)) + return q + + def trade(self, s, train=False): + q = self.session.run(self.q_eval, {self.s: s}) + a = np.argmax(q) + action = np.zeros(self.a_dim) + if train: + if np.random.uniform() < self.epsilon: + action[a] = 1.0 + return action + else: + action[np.random.randint(0, self.a_dim)] = 1.0 + return action + action[a] = 1.0 + return action + + def update_target(self): + self.session.run(self.update_q_target_op) + + def train(self): + if self.buffer_length < self.buffer_size: + return + if self.total_step % self.update_target_interval == 0: + self.session.run(self.update_q_target_op) + s, a, r, s_next = self.get_transition_batch() + q_eval, q_target = self.session.run([self.q_eval, self.q_target], {self.s: s, self.s_next: s_next}) + b_indices = np.arange(self.batch_size, dtype=np.int32) + q_next = q_eval.copy() + q_next[b_indices, a.astype(np.int)] = r + self.gamma * np.max(q_target, axis=1) + _, self.critic_loss = self.session.run([self.train_op, self.loss], {self.s: s, self.q_next: q_next}) + self.total_step += 1 + + def save_transition(self, s, a, r, s_next): + a = np.argmax(a) + transition = np.hstack((s, [a], [r], s_next)) + self.buffer[(self.buffer_length % self.buffer_size), :] = transition + self.buffer_length += 1 + + def get_transition_batch(self): + indices = np.random.choice(self.buffer_size, size=self.batch_size) + batch = self.buffer[(indices), :] + s = batch[:, :self.s_dim] + a = batch[:, self.s_dim:self.s_dim + 1] + r = batch[:, -self.s_dim - 1:-self.s_dim] + s_next = batch[:, -self.s_dim:] + return s, a, r, s_next + + def restore_buffer(self): + self.buffer = np.zeros((self.buffer_size, self.s_dim + 1 + 1 + self.s_dim)) + self.buffer_length = 0 + + def load_model(self, model_path='./DRLModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./DRLModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) + + +class RPG_CryptoNG(object): + + def __init__(self, feature_number, action_size=1, hidden_units_number=[128, 64], learning_rate=0.001): + tf.reset_default_graph() + self.s = tf.placeholder(dtype=tf.float32, shape=[None, None, feature_number], name='s') + self.a = tf.placeholder(dtype=tf.int32, shape=[None, None, action_size], name='a') + self.r = tf.placeholder(dtype=tf.float32, shape=[None, None], name='r') + self.s_next = tf.placeholder(dtype=tf.float32, shape=[None, None, feature_number], name='s_next') + self.action_size = action_size + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.s_next_buffer = [] + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_keep_prob') + with tf.variable_scope('rnn_encoder', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + cell = self._add_GRU(units_number=128, activation=tf.nn.tanh, keep_prob=self.dropout_keep_prob) + self.rnn_output, _ = tf.nn.dynamic_rnn(inputs=self.s, cell=cell, dtype=tf.float32) + with tf.variable_scope('supervised', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=[feature_number], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + with tf.variable_scope('policy_gradient', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.a_prob = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.a_prob = self._add_dense_layer(inputs=self.a_prob, output_shape=[action_size], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.a_out = tf.nn.softmax(self.a_prob, axis=-1) + self.negative_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.a_prob, labels=self.a) + with tf.variable_scope('train'): + optimizer_rl = tf.train.AdamOptimizer(learning_rate=learning_rate) + optimizer_sl = tf.train.AdamOptimizer(learning_rate=learning_rate * 2) + self.rlloss = tf.reduce_mean(self.negative_cross_entropy * self.r) + self.slloss = tf.reduce_mean(self.state_loss) + self.rltrain_op = optimizer_rl.minimize(self.rlloss) + self.sltrain_op = optimizer_sl.minimize(self.slloss) + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act=tf.nn.relu, use_bias=True): + output = inputs + for n in output_shape: + output = tf.layers.dense(output, n, activation=act, use_bias=use_bias) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + def _add_GRU(self, units_number, activation=tf.nn.relu, keep_prob=1.0): + cell = tf.contrib.rnn.GRUCell(units_number, activation=activation) + cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) + return cell + + def _add_GRUs(self, units_number, activation, keep_prob=1.0): + cells = tf.contrib.rnn.MultiRNNCell(cells=[self._add_GRU(units_number=n, activation=a) for n, a in zip(units_number, activation)]) + return cells + + def _add_gru_cell(self, units_number, activation=tf.nn.relu): + return tf.contrib.rnn.GRUCell(num_units=units_number, activation=activation) + + def train(self, drop=0.85): + feed = {self.a: np.transpose(np.array(self.a_buffer), axes=[1, 0, 2]), self.r: np.transpose(np.array(self.r_buffer), axes=[1, 0]), self.s: np.transpose(np.array(self.s_buffer), axes=[1, 0, 2]), self.s_next: np.transpose(np.array(self.s_next_buffer), axes=[1, 0, 2]), self.dropout_keep_prob: drop} + self.session.run([self.rltrain_op, self.sltrain_op], feed_dict=feed) + + def restore_buffer(self): + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.s_next_buffer = [] + + def save_current_state(self, s): + self.s_buffer.append(s) + + def save_transation(self, a, r, s_next): + self.a_buffer.append(a) + self.r_buffer.append(r) + self.s_next_buffer.append(s_next) + + def trade(self, train=False, drop=1.0, prob=False): + feed = {self.s: np.transpose(np.array(self.s_buffer), axes=[1, 0, 2]), self.dropout_keep_prob: drop} + a_prob = self.session.run([self.a_out], feed_dict=feed)[0] + a_prob = a_prob[:, (-1), :] + actions = [] + if train: + for ap in a_prob: + if prob: + np.clip(np.random.normal(0.5, 0.25), 0, 1) + else: + a_indices = np.arange(ap.shape[0]) + target_index = np.random.choice(a_indices, p=ap) + a = np.zeros(ap.shape[0]) + a[target_index] = 1.0 + actions.append(a) + return np.array(actions) + else: + if prob: + return a_prob + actions = [] + for ap in a_prob: + target_index = np.argmax(ap) + a = np.zeros(ap.shape[0]) + a[target_index] = 1.0 + actions.append(a) + return np.array(actions) + + def load_model(self, model_path='./RPGModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./RPGModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) + + +class RPG_Portfolio_Stable(object): + + def __init__(self, feature_number, action_size=1, hidden_units_number=[128, 64], learning_rate=0.001): + tf.reset_default_graph() + self.s = tf.placeholder(dtype=tf.float32, shape=[None, None, feature_number], name='s') + self.a = tf.placeholder(dtype=tf.int32, shape=[None, None, action_size], name='a') + self.r = tf.placeholder(dtype=tf.float32, shape=[None, None], name='r') + self.s_next = tf.placeholder(dtype=tf.float32, shape=[None, None, feature_number], name='s_next') + self.action_size = action_size + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.s_next_buffer = [] + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_keep_prob') + with tf.variable_scope('rnn_encoder', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + cell = self._add_GRU(units_number=128, activation=tf.nn.tanh, keep_prob=self.dropout_keep_prob) + self.rnn_output, _ = tf.nn.dynamic_rnn(inputs=self.s, cell=cell, dtype=tf.float32) + with tf.variable_scope('supervised', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=[feature_number], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + with tf.variable_scope('policy_gradient', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.a_prob = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.a_prob = self._add_dense_layer(inputs=self.a_prob, output_shape=[action_size], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.a_out = tf.nn.softmax(self.a_prob, axis=-1) + self.negative_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.a_prob, labels=self.a) + with tf.variable_scope('train'): + optimizer_rl = tf.train.AdamOptimizer(learning_rate=learning_rate) + optimizer_sl = tf.train.AdamOptimizer(learning_rate=learning_rate * 2) + self.rl_loss = tf.reduce_mean(self.negative_cross_entropy * self.r) + self.sl_loss = tf.reduce_mean(self.state_loss) + self.rl_train_op = optimizer_rl.minimize(self.rl_loss) + self.sl_train_op = optimizer_sl.minimize(self.sl_loss) + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act=tf.nn.relu, use_bias=True): + output = inputs + for n in output_shape: + output = tf.layers.dense(output, n, activation=act, use_bias=use_bias) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + def _add_GRU(self, units_number, activation=tf.nn.relu, keep_prob=1.0): + cell = tf.contrib.rnn.GRUCell(units_number, activation=activation) + cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) + return cell + + def _add_GRUs(self, units_number, activation, keep_prob=1.0): + cells = tf.contrib.rnn.MultiRNNCell(cells=[self._add_GRU(units_number=n, activation=a) for n, a in zip(units_number, activation)]) + return cells + + def _add_gru_cell(self, units_number, activation=tf.nn.relu): + return tf.contrib.rnn.GRUCell(num_units=units_number, activation=activation) + + def train(self, drop=0.85): + feed = {self.a: np.transpose(np.array(self.a_buffer), axes=[1, 0, 2]), self.r: np.transpose(np.array(self.r_buffer), axes=[1, 0]), self.s: np.transpose(np.array(self.s_buffer), axes=[1, 0, 2]), self.s_next: np.transpose(np.array(self.s_next_buffer), axes=[1, 0, 2]), self.dropout_keep_prob: drop} + self.session.run([self.rl_train_op, self.sl_train_op], feed_dict=feed) + + def restore_buffer(self): + self.a_buffer = [] + self.r_buffer = [] + self.s_buffer = [] + self.s_next_buffer = [] + + def save_current_state(self, s): + self.s_buffer.append(s) + + def save_transation(self, a, r, s_next): + self.a_buffer.append(a) + self.r_buffer.append(r) + self.s_next_buffer.append(s_next) + + def trade(self, train=False, kp=1.0, prob=False): + feed = {self.s: np.transpose(np.array(self.s_buffer), axes=[1, 0, 2]), self.dropout_keep_prob: kp} + a_prob = self.session.run(self.a_out, feed_dict=feed)[:, (-1), :] + actions = [] + if train: + for ap in a_prob: + if prob: + np.clip(np.random.normal(0.5, 0.25), 0, 1) + else: + a_indices = np.arange(ap.shape[0]) + target_index = np.random.choice(a_indices, p=ap) + a = np.zeros(ap.shape[0]) + a[target_index] = 1.0 + actions.append(a) + return np.array(actions) + else: + if prob: + return a_prob + actions = [] + for ap in a_prob: + target_index = np.argmax(ap) + a = np.zeros(ap.shape[0]) + a[target_index] = 1.0 + actions.append(a) + return np.array(actions) + + def load_model(self, model_path='./RPGModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./RPGModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) + + +class DDRPG(object): + + def __init__(self, s_dim, asset_number, buffer_size=1600, batch_size=64, tau=0.05, softmax_tau=1, gamma=0.99, actor_rnn_units=128, critic_rnn_units=128, actor_dnn_units=[64], critic_dnn_units=[64], learning_rate_a=0.001, learning_rate_c=0.002): + tf.reset_default_graph() + self.buffer_size = buffer_size + self.batch_size = batch_size + self.pointer = 0 + self.asset_number = asset_number + self.a_dim, self.s_dim = 1, s_dim + self.tau = tau + self.softmax_tau = softmax_tau + self.gamma = gamma + self.lr_a = learning_rate_a + self.lr_c = learning_rate_c + self.actor_rnn_units = actor_rnn_units + self.critic_rnn_units = critic_rnn_units + self.actor_dnn_units = actor_dnn_units + self.critic_dnn_units = critic_dnn_units + self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.r_buffer = np.zeros((self.asset_number, self.buffer_size, 1)) + self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + self.s = tf.placeholder(tf.float32, [None, None, self.s_dim], 's') + self.s_next = tf.placeholder(tf.float32, [None, None, self.s_dim], 's_next') + self.r = tf.placeholder(tf.float32, [None, None, 1], 'r') + self.keep_prob = tf.placeholder(tf.float32, [], 'dropout') + with tf.variable_scope('actor', initializer=tf.truncated_normal_initializer(dtype=tf.float32, mean=0, stddev=1)): + self.a = self._build_a(s=self.s, rnn_units=self.actor_rnn_units, dnn_units=self.actor_dnn_units, scope='predict', keep_prob=self.keep_prob, trainable=True) + a_next = self._build_a(s=self.s_next, rnn_units=self.actor_rnn_units, dnn_units=self.actor_dnn_units, scope='target', keep_prob=self.keep_prob, trainable=False) + with tf.variable_scope('critic', initializer=tf.truncated_normal_initializer(dtype=tf.float32, mean=0, stddev=1)): + q = self._build_c(s=self.s, a=self.a, rnn_units=self.critic_rnn_units, dnn_units=self.critic_dnn_units, scope='predict', keep_prob=self.keep_prob, trainable=True) + q_next = self._build_c(s=self.s_next, a=a_next, rnn_units=self.critic_rnn_units, dnn_units=self.critic_dnn_units, scope='target', keep_prob=self.keep_prob, trainable=False) + self.ap_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='actor/predict') + self.at_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='actor/target') + self.cp_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='critic/predict') + self.ct_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='critic/target') + self.soft_replace = [[tf.assign(ta, (1 - self.tau) * ta + self.tau * pa), tf.assign(tc, (1 - self.tau) * tc + self.tau * pc)] for ta, pa, tc, pc in zip(self.at_params, self.ap_params, self.ct_params, self.cp_params)] + q_target = self.r + self.gamma * q_next + with tf.variable_scope('actor_loss'): + a_loss = -tf.reduce_mean(q) + with tf.variable_scope('critic_loss'): + c_loss = tf.losses.mean_squared_error(labels=q_target, predictions=q) + with tf.variable_scope('actor_train'): + self.a_train = tf.train.AdamOptimizer(self.lr_a).minimize(a_loss, var_list=self.ap_params) + with tf.variable_scope('critic_loss'): + self.c_train = tf.train.AdamOptimizer(self.lr_c).minimize(c_loss, var_list=self.cp_params) + self.sess = tf.Session() + self.sess.run(tf.global_variables_initializer()) + + def trade(self, train=False, kp=1.0, epsilon=0.9): + start_point = self.pointer + 1 - self.batch_size + action = self.sess.run(self.a, {self.s: self.s_buffer[:, 0 if start_point < 0 else start_point:self.pointer + 1, :], self.keep_prob: kp})[:, (-1), :].flatten() + if train: + if np.random.rand() < epsilon: + return np.exp(action / self.softmax_tau) / np.sum(np.exp(action / self.softmax_tau)) + action = np.random.normal(action, scale=1 - action) + action = np.exp(action / self.softmax_tau) / np.sum(np.exp(action / self.softmax_tau)) + return action + else: + return np.exp(action / self.softmax_tau) / np.sum(np.exp(action / self.softmax_tau)) + + def train(self, kp=0.85): + if self.pointer < self.batch_size: + return + sample_start = np.random.randint(0, self.pointer - self.batch_size + 1) + self.sess.run(self.soft_replace) + self.sess.run(self.a_train, {self.s: self.s_buffer[:, sample_start:sample_start + self.batch_size], self.keep_prob: kp}) + self.sess.run(self.c_train, {self.s: self.s_buffer[:, sample_start:sample_start + self.batch_size], self.a: self.a_buffer[:, sample_start:sample_start + self.batch_size], self.r: self.r_buffer[:, sample_start:sample_start + self.batch_size], self.s_next: self.s_next_buffer[:, sample_start:sample_start + self.batch_size], self.keep_prob: kp}) + + def save_current_state(self, s): + self.s_buffer[:, (self.pointer), :] = s + + def save_transition(self, a, r, s_next): + self.a_buffer[:, (self.pointer), :] = a[:, (None)] + self.s_next_buffer[:, (self.pointer), :] = s_next + self.r_buffer[:, (self.pointer), :] = r[:, (None)] + + def settle(self): + self.pointer += 1 + + def restore_buffer(self): + self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.r_buffer = np.zeros((self.asset_number, self.buffer_size, 1)) + self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + self.pointer = 0 + + def _build_a(self, s, rnn_units, dnn_units, scope, keep_prob, trainable): + with tf.variable_scope(scope): + cell = self._add_GRU(units_number=rnn_units, activation=tf.nn.tanh, keep_prob=keep_prob, trainable=trainable) + out, _ = tf.nn.dynamic_rnn(inputs=s, cell=cell, dtype=tf.float32) + out = self._add_dense_layer(inputs=out, output_shape=dnn_units + [1], activations=[tf.nn.relu] * len(dnn_units) + [tf.nn.sigmoid], drop_keep_prob=keep_prob, trainable=trainable) + return out + + def _build_c(self, s, a, rnn_units, dnn_units, scope, keep_prob, trainable): + with tf.variable_scope(scope): + rnn_input = tf.concat([s, a], axis=-1) + cell = self._add_GRU(units_number=rnn_units, activation=tf.nn.tanh, keep_prob=keep_prob, trainable=trainable) + out, _ = tf.nn.dynamic_rnn(inputs=rnn_input, cell=cell, dtype=tf.float32) + out = self._add_dense_layer(inputs=out, output_shape=dnn_units, activations=[tf.nn.relu] * len(dnn_units), drop_keep_prob=keep_prob, trainable=trainable) + q = self._add_dense_layer(inputs=out, output_shape=[1], activations=[tf.nn.tanh], drop_keep_prob=keep_prob, trainable=trainable) + return q + + def _add_GRU(self, units_number, activation=tf.nn.relu, keep_prob=1.0, trainable=True): + cell = tf.contrib.rnn.GRUCell(units_number, activation=activation) + cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) + return cell + + def _add_dense_layer(self, inputs, output_shape, activations, drop_keep_prob, use_bias=True, trainable=True): + output = inputs + for n, a in zip(output_shape, activations): + output = tf.layers.dense(output, n, activation=a, use_bias=use_bias, trainable=trainable) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + +class RPG_CryptoNG_ShareVNG(object): + + def __init__(self, s_dim, asset_number, a_dim, buffer_size=64, batch_size=64, hidden_units_number=[128, 64], learning_rate=0.001): + tf.reset_default_graph() + self.a_dim = a_dim + self.buffer_size = buffer_size + self.batch_size = batch_size + self.asset_number = asset_number + self.s_dim = s_dim + self.pointer = 0 + self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + self.r_buffer = np.zeros((self.asset_number, self.buffer_size)) + self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.s = tf.placeholder(dtype=tf.float32, shape=[None, None, self.s_dim], name='s') + self.a = tf.placeholder(dtype=tf.int32, shape=[None, None, self.a_dim], name='a') + self.r = tf.placeholder(dtype=tf.float32, shape=[None, None], name='r') + self.s_next = tf.placeholder(dtype=tf.float32, shape=[None, None, s_dim], name='s_next') + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_keep_prob') + with tf.variable_scope('rnn_encoder', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + cell = self._add_GRU(units_number=128, activation=tf.nn.tanh, keep_prob=self.dropout_keep_prob) + self.rnn_output, _ = tf.nn.dynamic_rnn(inputs=self.s, cell=cell, dtype=tf.float32) + with tf.variable_scope('supervised', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.state_predict = self._add_dense_layer(inputs=self.rnn_output, output_shape=[s_dim], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.state_loss = tf.losses.mean_squared_error(self.state_predict, self.s_next) + with tf.variable_scope('policy_gradient', initializer=tf.contrib.layers.xavier_initializer(uniform=True), regularizer=tf.contrib.layers.l2_regularizer(0.01)): + self.a_prob = self._add_dense_layer(inputs=self.rnn_output, output_shape=hidden_units_number, drop_keep_prob=self.dropout_keep_prob, act=tf.nn.relu, use_bias=True) + self.a_prob = self._add_dense_layer(inputs=self.a_prob, output_shape=[2], drop_keep_prob=self.dropout_keep_prob, act=None, use_bias=True) + self.a_out = tf.nn.softmax(self.a_prob, axis=-1) + self.negative_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.a_prob, labels=self.a) + with tf.variable_scope('train'): + optimizer_rl = tf.train.AdamOptimizer(learning_rate=learning_rate) + optimizer_sl = tf.train.AdamOptimizer(learning_rate=learning_rate * 2) + self.rl_loss = tf.reduce_mean(self.negative_cross_entropy * self.r) + self.sl_loss = tf.reduce_mean(self.state_loss) + self.rl_train_op = optimizer_rl.minimize(self.rl_loss) + self.sl_train_op = optimizer_sl.minimize(self.sl_loss) + self.init_op = tf.global_variables_initializer() + self.session = tf.Session() + self.saver = tf.train.Saver() + + def init_model(self): + self.session.run(self.init_op) + + def _add_dense_layer(self, inputs, output_shape, drop_keep_prob, act=tf.nn.relu, use_bias=True): + output = inputs + for n in output_shape: + output = tf.layers.dense(output, n, activation=act, use_bias=use_bias) + output = tf.nn.dropout(output, drop_keep_prob) + return output + + def _add_GRU(self, units_number, activation=tf.nn.relu, keep_prob=1.0): + cell = tf.contrib.rnn.GRUCell(units_number, activation=activation) + cell = tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) + return cell + + def _add_GRUs(self, units_number, activation, keep_prob=1.0): + cells = tf.contrib.rnn.MultiRNNCell(cells=[self._add_GRU(units_number=n, activation=a) for n, a in zip(units_number, activation)]) + return cells + + def _add_gru_cell(self, units_number, activation=tf.nn.relu): + return tf.contrib.rnn.GRUCell(num_units=units_number, activation=activation) + + def train(self, kp=0.85): + if self.pointer < self.buffer_size - 1: + return + max_upper_bound = self.buffer_size - self.batch_size + 1 + lower = np.random.randint(low=0, high=max_upper_bound) + upper = lower + +self.batch_size + feed = {self.a: self.a_buffer[:, lower:upper, :], self.r: self.r_buffer[:, lower:upper], self.s: self.s_buffer[:, lower:upper, :], self.s_next: self.s_next_buffer[:, lower:upper, :], self.dropout_keep_prob: kp} + self.session.run([self.rl_train_op, self.sl_train_op], feed_dict=feed) + + def restore_buffer(self): + self.a_buffer = np.zeros((self.asset_number, self.buffer_size, self.a_dim)) + self.r_buffer = np.zeros((self.asset_number, self.buffer_size)) + self.s_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.s_next_buffer = np.zeros((self.asset_number, self.buffer_size, self.s_dim)) + self.pointer = 0 + + def save_current_state(self, s): + if self.pointer < self.buffer_size - 1: + self.s_buffer[:, (self.pointer), :] = s + else: + self.s_buffer[:, :-1, :] = self.s_buffer[:, 1:, :] + self.s_buffer[:, (-1), :] = s + + def save_transition(self, a, r, s_next): + if self.pointer < self.buffer_size - 1: + self.a_buffer[:, (self.pointer), :] = a + self.s_next_buffer[:, (self.pointer), :] = s_next + self.r_buffer[:, (self.pointer)] = r + self.pointer += 1 + else: + self.a_buffer[:, :-1, :] = self.a_buffer[:, 1:, :] + self.a_buffer[:, (-1), :] = a + self.r_buffer[:, :-1] = self.r_buffer[:, 1:] + self.r_buffer[:, (-1)] = r + self.s_next_buffer[:, :-1, :] = self.s_next_buffer[:, 1:, :] + self.s_next_buffer[:, (-1), :] = s_next + + def trade(self, train=False, kp=1.0, prob=False): + feed = {self.s: self.s_buffer, self.dropout_keep_prob: kp} + a_prob = self.session.run(self.a_out, feed_dict=feed)[:, (self.pointer), :] + action = [] + if train: + for ap in a_prob: + if prob: + action.append(np.clip(np.random.normal(ap, 0.5), 0, 1)) + else: + a_indices = np.arange(ap.shape[0]) + target_index = np.random.choice(a_indices, p=ap) + a = np.zeros(ap.shape[0]) + a[target_index] = 1.0 + action.append(a) + return np.array(action) + else: + if prob: + return a_prob + action = [] + for ap in a_prob: + target_index = np.argmax(ap) + a = np.zeros(ap.shape[0]) + a[target_index] = 1.0 + action.append(a) + return np.array(action) + + def load_model(self, model_path='./RPGModel'): + self.saver.restore(self.session, model_path + '/model') + + def save_model(self, model_path='./RPGModel'): + if not os.path.exists(model_path): + os.mkdir(model_path) + model_file = model_path + '/model' + self.saver.save(self.session, model_file) diff --git a/dataset/preprocessed/572.txt b/dataset/preprocessed/572.txt new file mode 100644 index 0000000..5c4b12e --- /dev/null +++ b/dataset/preprocessed/572.txt @@ -0,0 +1,115 @@ +from tensorflow.examples.tutorials.mnist import input_data +import tensorflow as tf +import os +mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) +log_dir = '/tmp/tensorflow/mnist/logs/sample' + + +def weight_variable(shape): + with tf.name_scope('hidden1'): + initial = tf.truncated_normal(shape, stddev=0.1, name='weight1') + return tf.Variable(initial) + + +def bias_variable(shape): + initial = tf.constant(0.1, shape=shape) + return tf.Variable(initial) + + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +def run_deep_nn_with_interactive_session(): + x = tf.placeholder(tf.float32, [None, 784]) + x_image = tf.reshape(x, [-1, 28, 28, 1]) + y_ = tf.placeholder(tf.float32, [None, 10]) + W_conv1 = weight_variable([5, 5, 1, 32]) + b_conv1 = bias_variable([32]) + h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + print('h_conv1') + print(h_conv1) + h_pool1 = max_pool_2x2(h_conv1) + print('h_pool1') + print(h_pool1) + W_conv2 = weight_variable([5, 5, 32, 64]) + b_conv2 = bias_variable([64]) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + print('h_conv2') + print(h_conv2) + h_pool2 = max_pool_2x2(h_conv2) + print('h_pool2') + print(h_pool2) + W_fc1 = weight_variable([7 * 7 * 64, 1024]) + b_fc1 = bias_variable([1024]) + h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + print('h_fc1') + print(h_fc1) + keep_prob = tf.placeholder(tf.float32) + h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + W_fc2 = weight_variable([1024, 10]) + b_fc2 = bias_variable([10]) + y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 + print('y_conv') + print(y_conv) + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) + train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + session = tf.InteractiveSession() + session.run(tf.global_variables_initializer()) + for i in range(200): + batch = mnist.train.next_batch(50) + if i % 100 == 0: + train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) + print('step %d, training accuracy %g' % (i, train_accuracy)) + train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) + print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) + + +def run_deep_nn_with_session(): + x = tf.placeholder(tf.float32, [None, 784]) + x_image = tf.reshape(x, [-1, 28, 28, 1]) + y_ = tf.placeholder(tf.float32, [None, 10]) + W_conv1 = weight_variable([5, 5, 1, 32]) + b_conv1 = bias_variable([32]) + h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + W_conv2 = weight_variable([5, 5, 32, 64]) + b_conv2 = bias_variable([64]) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = max_pool_2x2(h_conv2) + W_fc1 = weight_variable([7 * 7 * 64, 1024]) + b_fc1 = bias_variable([1024]) + h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) + h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) + keep_prob = tf.placeholder(tf.float32) + h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + W_fc2 = weight_variable([1024, 10]) + b_fc2 = bias_variable([10]) + y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) + train_step_operation = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) + correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + with tf.Session() as session: + session.run(tf.global_variables_initializer()) + for i in range(200): + batch = mnist.train.next_batch(50) + feed_dict = {x: batch[0], y_: batch[1], keep_prob: 0.5} + _, loss_value = session.run([train_step_operation, cross_entropy], feed_dict=feed_dict) + print('loss_value %f' % loss_value) + if i % 100 == 0: + feed_dict = {x: batch[0], y_: batch[1], keep_prob: 1.0} + train_accuracy = accuracy.eval(feed_dict=feed_dict) + print('step %d, training accuracy %g' % (i, train_accuracy)) + print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) + + +if __name__ == '__main__': + run_deep_nn_with_session() diff --git a/dataset/preprocessed/573.txt b/dataset/preprocessed/573.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/573.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/574.txt b/dataset/preprocessed/574.txt new file mode 100644 index 0000000..8bc92de --- /dev/null +++ b/dataset/preprocessed/574.txt @@ -0,0 +1,233 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('L2 regularization(beta=%.5f) Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +print('Best beta=%f, accuracy=%.1f%%' % (beta_val[np.argmax(accuracy_val)], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Overfitting with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y1 = tf.nn.dropout(y1, 0.5) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + global_step = tf.Variable(0) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, fc1_size], stddev=np.sqrt(2.0 / (image_size * image_size)))) + b1 = tf.Variable(tf.zeros([fc1_size])) + W2 = tf.Variable(tf.truncated_normal([fc1_size, fc2_size], stddev=np.sqrt(2.0 / fc1_size))) + b2 = tf.Variable(tf.zeros([fc2_size])) + W3 = tf.Variable(tf.truncated_normal([fc2_size, fc3_size], stddev=np.sqrt(2.0 / fc2_size))) + b3 = tf.Variable(tf.zeros([fc3_size])) + W4 = tf.Variable(tf.truncated_normal([fc3_size, num_labels], stddev=np.sqrt(2.0 / fc3_size))) + b4 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + logits = tf.matmul(y3, W4) + b4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + valid_logits = tf.matmul(y3_valid, W4) + b4 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + test_logits = tf.matmul(y3_test, W4) + b4 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/575.txt b/dataset/preprocessed/575.txt new file mode 100644 index 0000000..86356b6 --- /dev/null +++ b/dataset/preprocessed/575.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/576.txt b/dataset/preprocessed/576.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/576.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/577.txt b/dataset/preprocessed/577.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/577.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/579.txt b/dataset/preprocessed/579.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/579.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/58.txt b/dataset/preprocessed/58.txt new file mode 100644 index 0000000..607ef32 --- /dev/null +++ b/dataset/preprocessed/58.txt @@ -0,0 +1,278 @@ +import tensorflow as tf +import numpy as np +import os +import re +path_file = 'C:\\Users\\stecose\\Documents\\Documents\\AiHearingModel' +save_path = 'C:\\Users\\stecose\\Documents\\Documents\\AiHearingModel/models/md0' +names = set() +for line in open(os.path.join(path_file, 'words.txt')): + if len(line.strip()) > 3 and len(line.strip()) < 15: + names.add(line.strip().lower()) +NAMES = names +chars = list('abcdefghijklmnopqrstuvwxyz') + ['', ''] +indices_for_chars = {c: i for i, c in enumerate(chars)} +NAME_MAX_LEN = 15 + + +def name_to_vec(name, maxlen=NAME_MAX_LEN): + v = np.zeros(maxlen, dtype=int) + null_idx = indices_for_chars[''] + v.fill(null_idx) + for i, c in enumerate(name): + if i >= maxlen: + break + n = indices_for_chars.get(c, null_idx) + v[i] = n + v[min(len(name), maxlen - 1)] = indices_for_chars[''] + return v + + +def vec_to_name(vec): + name = '' + for x in vec: + char = chars[x] + if len(char) == 1: + name += char + elif char == '': + return name + return name + + +print('name_to_vec("nate") --> ', name_to_vec('nate')) +name_vecs = np.array([name_to_vec(n) for n in names]) + + +def weight_var(shape, stddev=0.1, weight_decay=0.0, name=None): + initial = tf.truncated_normal(shape, stddev=stddev) + v = tf.Variable(initial, name=name) + if weight_decay > 0: + l2 = tf.nn.l2_loss(v) * weight_decay + tf.add_to_collection('losses', l2) + return v + + +def leaky_relu(x, leak=0.2, name='lrelu'): + with tf.variable_scope(name): + f1 = 0.5 * (1 + leak) + f2 = 0.5 * (1 - leak) + return f1 * x + f2 * abs(x) + + +def relu(x): + return leaky_relu(x) + + +def create_conv(input, out_channels, patch_size=5, stride=1, batch_norm=False, dropout=False): + in_channels = input.get_shape()[-1].value + w = weight_var([patch_size, patch_size, in_channels, out_channels]) + b = weight_var([out_channels], stddev=0) + conv = tf.nn.conv2d(input, w, strides=[1, stride, stride, 1], padding='SAME') + activation = relu(conv + b) + return activation + + +def text_conv(input, out_channels, patch_size=5, stride=1, dropout=False, pool_size=1): + in_channels = input.get_shape()[-1].value + w = weight_var([patch_size, in_channels, out_channels]) + b = weight_var([out_channels], stddev=0) + conv = tf.nn.conv1d(input, w, stride=stride, padding='SAME') + activation = relu(conv + b) + return activation + + +def create_fc(input, out_size): + in_size = input.get_shape()[-1].value + w = weight_var([in_size, out_size], weight_decay=0.004) + b = weight_var([out_size], weight_decay=0.004) + x = tf.matmul(input, w) + return relu(x + b) + + +name_placeholder = tf.placeholder(shape=[None, NAME_MAX_LEN], dtype=tf.int32, name='names') +Z_SIZE = 64 + + +def encoder_lstm(names): + with tf.variable_scope('encoder'): + cells = [tf.nn.rnn_cell.LSTMCell(size, state_is_tuple=True) for size in [len(chars), Z_SIZE]] + lstm = tf.nn.rnn_cell.MultiRNNCell(cells, state_is_tuple=True) + one_hot = tf.one_hot(names, len(chars), dtype=tf.float32) + outputs, state = tf.nn.dynamic_rnn(lstm, one_hot, dtype=tf.float32) + outputs_flat = tf.reshape(outputs, [-1, Z_SIZE * NAME_MAX_LEN]) + z_mean = create_fc(outputs_flat, Z_SIZE) + z_stddev = create_fc(outputs_flat, Z_SIZE) + return z_mean, z_stddev + + +z_mean, z_stddev = encoder_lstm(name_placeholder) +session = tf.Session() +session.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) + + +def sample_z(z_mean, z_stddev): + samples = tf.random_normal(tf.shape(z_stddev), 0, 1, dtype=tf.float32) + return z_mean + samples * z_stddev + + +z_vals = sample_z(z_mean, z_stddev) + + +def decoder_lstm(z): + z_repeated_over_time = tf.tile(tf.reshape(z, [-1, 1, Z_SIZE]), [1, NAME_MAX_LEN, 1]) + cells = [tf.nn.rnn_cell.LSTMCell(size, state_is_tuple=True) for size in [Z_SIZE, 2 * 2 * Z_SIZE, len(chars)]] + lstm = tf.nn.rnn_cell.MultiRNNCell(cells, state_is_tuple=True) + outputs, state = tf.nn.dynamic_rnn(lstm, z_repeated_over_time, dtype=tf.float32) + return outputs + + +z_input = tf.placeholder(tf.float32, [None, Z_SIZE], name='z_input') +use_z_input = tf.placeholder(tf.int32, shape=[], name='use_z_input_condition') +decoder_input = tf.cond(use_z_input > 0, lambda : z_input, lambda : z_vals) +decoded = decoder_lstm(decoder_input) +diff_loss = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(decoded, name_placeholder)) +kl_divergence = tf.reduce_mean(0.5 * tf.reduce_sum(tf.square(z_mean) + tf.square(z_stddev) - tf.log(tf.square(z_stddev)) - 1, 1)) +loss = diff_loss + kl_divergence +decoded_vecs = tf.argmax(decoded, axis=2) +learn_rate = tf.placeholder(tf.float32, name='learning_rate') +optimizer = tf.train.AdamOptimizer(learn_rate) +global_step = tf.contrib.framework.get_or_create_global_step() +train_step = optimizer.minimize(loss, global_step=global_step) +session.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) +session = tf.Session() +init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) +session.run(init_op) +saver = None +if save_path: + if not os.path.exists(save_path): + os.mkdir(save_path) + saver = tf.train.Saver() + ckpt = tf.train.get_checkpoint_state(save_path) + if ckpt and ckpt.model_checkpoint_path: + saver.restore(session, ckpt.model_checkpoint_path) + print('Restored from checkpoint', ckpt.model_checkpoint_path) + else: + print('Did not restore from checkpoint') +else: + print('Will not save progress') +train = True +while train: + nnames = name_vecs[(np.random.randint(name_vecs.shape[0], size=64)), :] + feed_dict = {name_placeholder: nnames, z_input: np.zeros((64, Z_SIZE)), use_z_input: 0, learn_rate: 0.001} + _, loss_, step_ = session.run([train_step, loss, global_step], feed_dict=feed_dict) + if step_ % 200 == 0: + output_ = session.run(decoded_vecs, feed_dict=feed_dict) + print('Step: {0}; loss: {1}'.format(step_, loss_)) + print(' example encoding: {} -> {}'.format(vec_to_name(nnames[0]), vec_to_name(output_[0]))) + if step_ % 600 == 0: + saver.save(session, save_path + '/model.ckpt', global_step=step_) + print('Saved') + if step_ > 1000000: + train = False + + +def reconstruct(name): + feed_dict = {name_placeholder: np.array([name_to_vec(name)]), z_input: np.zeros((64, Z_SIZE)), use_z_input: 0, learn_rate: 0.01} + output_ = session.run(decoded_vecs, feed_dict=feed_dict) + return vec_to_name(output_[0]) + + +import itertools +top5 = itertools.islice(names, 5) +for name in list(names)[:10]: + print(name, '->', reconstruct(name)) +for name in ['word', 'happy', 'winter', 'candle', 'cherish']: + print(name, '->', reconstruct(name)) +for name in ['embedding', 'automobile', 'air', 'larynx']: + print(name, '->', reconstruct(name)) +for name in ['ufhoe', 'xyzy', 'ihwrfoecoei']: + print(name, '->', reconstruct(name)) + + +def nameliness(word): + r = reconstruct(word) + return sum([(1 if a == b else 0) for a, b in zip(word, r)]) / float(len(word)) + + +for name in ['nate', 'july', 'fridge', 'gienigoe', 'chzsiucf', 'xyxyzzy']: + print(name, ':', nameliness(name)) + + +def make_batches(list, size=128): + batches = [] + while len(list): + batches.append(list[:min(len(list), size)]) + list = list[len(batches[-1]):] + return batches + + +embeddings = {} +for batch in make_batches(list(names)): + feed_dict = {name_placeholder: np.array([name_to_vec(name) for name in batch]), z_input: np.zeros((len(batch), Z_SIZE)), use_z_input: 0} + output_ = session.run(z_mean, feed_dict=feed_dict) + for name, vec in list(zip(batch, output_)): + embeddings[tuple(name)] = vec + + +def embed(name): + feed_dict = {name_placeholder: np.array([name_to_vec(name)]), z_input: np.zeros((1, Z_SIZE)), use_z_input: 0} + output_ = session.run(z_mean, feed_dict=feed_dict) + return output_[0] + + +def nearest(embedding): + + def distance(name): + return np.linalg.norm(embedding - embeddings[name]) + return ''.join(min(embeddings.keys(), key=distance)) + + +def unembed(embedding): + feed_dict = {name_placeholder: np.zeros((1, NAME_MAX_LEN)), z_input: np.array([embedding]), use_z_input: 1} + output_ = session.run(decoded_vecs, feed_dict=feed_dict) + return vec_to_name(output_[0]) + + +print(unembed(embed('nate')) == 'nate') +for name in ['nate', 'yikes', 'panda', 'ixzhxzi', 'justxn']: + print(name, 'is closest to', nearest(embed(name))) + + +def blend_names(name1, name2): + e1 = embed(name1) + e2 = embed(name2) + for i in range(11): + blend = i / 10.0 + print(unembed(e1 * (1 - blend) + e2 * blend)) + + +blend_names('amy', 'francisco') +blend_names('nathaniel', 'chen') +blend_names('will', 'william') +print(nearest(np.zeros(Z_SIZE))) +print(unembed(np.zeros(Z_SIZE))) +for name in ['nate', 'willy', 'sam', 'polly', 'jacob']: + print(name, '* 2 =', unembed(embed(name) * 2)) +for name in ['nancy', 'barry', 'chance', 'rachel', 'gloria']: + print('-' + name, '=', unembed(-embed(name))) +print(unembed(embed('alberta') - embed('albert') + embed('robert'))) +print(unembed(embed('alberta') - embed('albert') + embed('justin'))) +print(unembed(embed('alberta') - embed('albert') + embed('joseph'))) +print(unembed(embed('alberta') - embed('albert') + embed('nate'))) + + +def generate(): + return unembed(np.random.normal(size=Z_SIZE)) + + +for _ in range(10): + print(generate()) + + +def variations_on(name): + z = embed(name) + for i in range(10): + noise = np.random.normal(z.shape) + print(unembed(z + noise * i * 0.01)) + + +variations_on('nate') diff --git a/dataset/preprocessed/580.txt b/dataset/preprocessed/580.txt new file mode 100644 index 0000000..9339e4f --- /dev/null +++ b/dataset/preprocessed/580.txt @@ -0,0 +1,245 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +""" +Reformat into a shape that's more adapted to the models we're going to train: + + data as a flat matrix, + labels as float 1-hot encodings. + +""" +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +""" +Problem 1 + +Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor t using nn.l2_loss(t). The right amount of regularization should improve your validation / test accuracy. +""" +beta_val = np.logspace(-4, -2, 20) +batch_size = 128 +accuracy_val = [] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +for beta in beta_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: beta} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + print('L2 regularization(beta=%.5f) Test accuracy: %.1f%%' % (beta, accuracy(test_prediction.eval(), test_labels))) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +print('Best beta=%f, accuracy=%.1f%%' % (beta_val[np.argmax(accuracy_val)], max(accuracy_val))) +plt.semilogx(beta_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Overfitting with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y1 = tf.nn.dropout(y1, 0.5) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + global_step = tf.Variable(0) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, fc1_size], stddev=np.sqrt(2.0 / (image_size * image_size)))) + b1 = tf.Variable(tf.zeros([fc1_size])) + W2 = tf.Variable(tf.truncated_normal([fc1_size, fc2_size], stddev=np.sqrt(2.0 / fc1_size))) + b2 = tf.Variable(tf.zeros([fc2_size])) + W3 = tf.Variable(tf.truncated_normal([fc2_size, fc3_size], stddev=np.sqrt(2.0 / fc2_size))) + b3 = tf.Variable(tf.zeros([fc3_size])) + W4 = tf.Variable(tf.truncated_normal([fc3_size, num_labels], stddev=np.sqrt(2.0 / fc3_size))) + b4 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + logits = tf.matmul(y3, W4) + b4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + valid_logits = tf.matmul(y3_valid, W4) + b4 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + test_logits = tf.matmul(y3_test, W4) + b4 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001438} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) +print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/581.txt b/dataset/preprocessed/581.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/581.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/582.txt b/dataset/preprocessed/582.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/582.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/584.txt b/dataset/preprocessed/584.txt new file mode 100644 index 0000000..30b7279 --- /dev/null +++ b/dataset/preprocessed/584.txt @@ -0,0 +1,122 @@ +import tensorflow as tf +import numpy as np +import dihedral as nn + + +class CNN: + + def __init__(self): + self.tfx = None + self.tfp = None + self.tfy = None + self.tftrain_step = None + self.mse = None + self.tfkp = None + self.tfacc = None + self.train_counter = 0 + self.test = None + + def NN(self, x): + assert x.get_shape().as_list() == [None, 424, 424, 3] + x = nn.relu(nn.convolution(x, 8 * 4, w=4, s=2, input_repr='invariant')) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 209, 209, 8 * 4] + x = nn.relu(nn.convolution(x, 8 * 8, w=5, s=2)) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 101, 101, 8 * 8] + x = nn.relu(nn.convolution(x, 8 * 16, w=5, s=2)) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 47, 47, 8 * 16] + x = nn.relu(nn.convolution(x, 8 * 32, w=5, s=2)) + x = nn.relu(nn.convolution(x)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 20, 20, 8 * 32] + x = nn.relu(nn.convolution(x, 8 * 64, w=4, s=2)) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 9, 9, 8 * 64] + x = nn.relu(nn.convolution(x, 128, output_repr='invariant')) + x = nn.relu(nn.convolution(x, 8 * 256, w=7, input_repr='invariant')) + x = tf.nn.dropout(x, self.tfkp) + assert x.get_shape().as_list() == [None, 1, 1, 8 * 256] + x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + x = nn.relu(nn.fullyconnected(x, 8 * 256)) + x = tf.nn.dropout(x, self.tfkp) + x = nn.relu(nn.fullyconnected(x, 8 * 256)) + x = nn.batch_normalization(x, self.tfacc) + self.test = x + x = nn.fullyconnected(x, 37, output_repr='invariant') + assert x.get_shape().as_list() == [None, 37] + c1 = tf.nn.softmax(x[:, 0:3]) + c2 = tf.nn.softmax(x[:, 3:5]) * c1[:, 1:2] + c3 = tf.nn.softmax(x[:, 5:7]) * c2[:, 1:2] + c4 = tf.nn.softmax(x[:, 7:9]) * c2[:, 1:2] + c5 = tf.nn.softmax(x[:, 9:13]) * c2[:, 1:2] + c6 = tf.nn.softmax(x[:, 13:15]) + c7 = tf.nn.softmax(x[:, 15:18]) * c1[:, 0:1] + c8 = tf.nn.softmax(x[:, 18:25]) * c6[:, 0:1] + c9 = tf.nn.softmax(x[:, 25:28]) * c2[:, 0:1] + c10 = tf.nn.softmax(x[:, 28:31]) * c4[:, 0:1] + c11 = tf.nn.softmax(x[:, 31:37]) * c4[:, 0:1] + return tf.concat(1, [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11]) + + def create_architecture(self): + self.tfkp = tf.placeholder_with_default(tf.constant(1.0, tf.float32), []) + self.tfacc = tf.placeholder_with_default(tf.constant(0.0, tf.float32), []) + x = self.tfx = tf.placeholder(tf.float32, [None, 424, 424, 3]) + self.tfp = self.NN(x) + self.tfy = tf.placeholder(tf.float32, [None, 37]) + self.mse = tf.reduce_mean(tf.square(self.tfp - self.tfy)) + self.tftrain_step = tf.train.AdamOptimizer(0.001, epsilon=1e-06).minimize(self.mse) + + @staticmethod + def split_test_train(images_path, labels_csv): + import csv + import os + with open(labels_csv) as f: + reader = csv.reader(f) + rows = [row for row in reader] + labels = np.array([[float(x) for x in r[1:]] for r in rows[1:]]).astype(np.float32) + files = [(images_path + '/' + f) for f in sorted(os.listdir(images_path))] + n = 2000 + return (files[:n], labels[:n]), (files[n:], labels[n:]) + + @staticmethod + def load(files): + from scipy.ndimage import imread + n = len(files) + xs = np.zeros((n, 424, 424, 3), dtype=np.float32) + for i in range(n): + xs[i] = imread(files[i], mode='RGB').astype(np.float32) / 256.0 + return CNN.prepare(xs) + + @staticmethod + def prepare(images): + images = images - np.array([0.04543276, 0.04002843, 0.02984124]) + images = images / np.array([0.08930177, 0.0741211, 0.0656323]) + return images + + @staticmethod + def batch(files, labels): + ids = np.random.choice(len(files), 20, replace=False) + xs = CNN.load([files[i] for i in ids]) + ys = labels[ids] + for i in range(len(xs)): + s = np.random.uniform(0.8, 1.2) + u = np.random.uniform(-0.1, 0.1) + xs[i] = xs[i] * s + u + return xs, ys + + def train(self, session, xs, ys, options=None, run_metadata=None): + acc = 0.8 ** (self.train_counter / 1000.0) + _, mse = session.run([self.tftrain_step, self.mse], feed_dict={self.tfx: xs, self.tfy: ys, self.tfkp: 0.5, self.tfacc: acc}, options=options, run_metadata=run_metadata) + self.train_counter += 1 + return mse + + def predict(self, session, xs): + return session.run(self.tfp, feed_dict={self.tfx: xs, self.tfkp: 1.0, self.tfacc: 0.0}) + + def predict_mse(self, session, xs, ys): + return session.run([self.tfp, self.mse], feed_dict={self.tfx: xs, self.tfy: ys, self.tfkp: 1.0, self.tfacc: 0.0}) diff --git a/dataset/preprocessed/585.txt b/dataset/preprocessed/585.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/585.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/586.txt b/dataset/preprocessed/586.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/586.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/587.txt b/dataset/preprocessed/587.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/587.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/589.txt b/dataset/preprocessed/589.txt new file mode 100644 index 0000000..7d4c957 --- /dev/null +++ b/dataset/preprocessed/589.txt @@ -0,0 +1,324 @@ +import sys +if sys.version[0] == '2': + import cPickle as pkl +else: + import pickle as pkl +import numpy as np +import tensorflow as tf +import utils +dtype = utils.DTYPE + + +class Model: + + def __init__(self): + self.sess = None + self.X = None + self.y = None + self.layer_keeps = None + self.vars = None + self.keep_prob_train = None + self.keep_prob_test = None + + def run(self, fetches, X=None, y=None, mode='train'): + feed_dict = {} + if type(self.X) is list: + for i in range(len(X)): + feed_dict[self.X[i]] = X[i] + else: + feed_dict[self.X] = X + if y is not None: + feed_dict[self.y] = y + if self.layer_keeps is not None: + if mode == 'train': + feed_dict[self.layer_keeps] = self.keep_prob_train + elif mode == 'test': + feed_dict[self.layer_keeps] = self.keep_prob_test + return self.sess.run(fetches, feed_dict) + + def dump(self, model_path): + var_map = {} + for name, var in self.vars.iteritems(): + var_map[name] = self.run(var) + pkl.dump(var_map, open(model_path, 'wb')) + print('model dumped at', model_path) + + +class LR(Model): + + def __init__(self, input_dim=None, output_dim=1, init_path=None, opt_algo='gd', learning_rate=0.01, l2_weight=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + b = self.vars['b'] + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=logits)) + l2_weight * tf.nn.l2_loss(xw) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FM(Model): + + def __init__(self, input_dim=None, output_dim=1, factor_order=10, init_path=None, opt_algo='gd', learning_rate=0.01, l2_w=0, l2_v=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'tnormal', dtype), ('v', [input_dim, factor_order], 'tnormal', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + v = self.vars['v'] + b = self.vars['b'] + X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + l2_w * tf.nn.l2_loss(xw) + l2_v * tf.nn.l2_loss(xv) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FNN(Model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + factor_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = factor_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + for i in range(2, len(layer_sizes) - 1): + layer_input = layer_sizes[i] + layer_output = layer_sizes[i + 1] + init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class CCPM(Model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + embedding_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = embedding_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('f1', [embedding_order, layer_sizes[2], 1, 2], 'tnormal', dtype)) + init_vars.append(('f2', [embedding_order, layer_sizes[3], 2, 2], 'tnormal', dtype)) + init_vars.append(('w1', [2 * 3 * embedding_order, 1], 'tnormal', dtype)) + init_vars.append(('b1', [1], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + l = tf.nn.dropout(utils.activate(tf.concat([(tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) + b0[i]) for i in range(num_inputs)], 1), layer_acts[0]), self.layer_keeps[0]) + l = tf.transpose(tf.reshape(l, [-1, num_inputs, embedding_order, 1]), [0, 2, 1, 3]) + f1 = self.vars['f1'] + l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), num_inputs / 2), [0, 1, 3, 2]) + f2 = self.vars['f2'] + l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embedding_order * 3 * 2]), layer_acts[1]), self.layer_keeps[1]) + w1 = self.vars['w1'] + b1 = self.vars['b1'] + l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1, layer_acts[2]), self.layer_keeps[2]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN1(Model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, layer_l2=None, kernel_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + factor_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = factor_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('k1', [num_inputs, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + for i in range(2, len(layer_sizes) - 1): + layer_input = layer_sizes[i] + layer_output = layer_sizes[i + 1] + init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + k1 = self.vars['k1'] + b1 = self.vars['b1'] + p = tf.reduce_sum(tf.reshape(tf.matmul(tf.reshape(tf.transpose(tf.reshape(l, [-1, num_inputs, factor_order]), [0, 2, 1]), [-1, num_inputs]), k1), [-1, factor_order, layer_sizes[2]]), 1) + l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + b1 + p, layer_acts[1]), self.layer_keeps[1]) + for i in range(2, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + if kernel_l2 is not None: + self.loss += kernel_l2 * tf.nn.l2_loss(k1) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN2(Model): + + def __init__(self, layer_sizes=None, layer_acts=None, drop_out=None, layer_l2=None, kernel_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(layer_sizes[0]) + factor_order = layer_sizes[1] + for i in range(num_inputs): + layer_input = layer_sizes[0][i] + layer_output = factor_order + init_vars.append(('w0_%d' % i, [layer_input, layer_output], 'tnormal', dtype)) + init_vars.append(('b0_%d' % i, [layer_output], 'zero', dtype)) + init_vars.append(('w1', [num_inputs * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('k1', [factor_order * factor_order, layer_sizes[2]], 'tnormal', dtype)) + init_vars.append(('b1', [layer_sizes[2]], 'zero', dtype)) + for i in range(2, len(layer_sizes) - 1): + layer_input = layer_sizes[i] + layer_output = layer_sizes[i + 1] + init_vars.append(('w%d' % i, [layer_input, layer_output], 'tnormal')) + init_vars.append(('b%d' % i, [layer_output], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['w0_%d' % i] for i in range(num_inputs)] + b0 = [self.vars['b0_%d' % i] for i in range(num_inputs)] + xw = [tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)] + x = tf.concat([(xw[i] + b0[i]) for i in range(num_inputs)], 1) + l = tf.nn.dropout(utils.activate(x, layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + k1 = self.vars['k1'] + b1 = self.vars['b1'] + z = tf.reduce_sum(tf.reshape(l, [-1, num_inputs, factor_order]), 1) + p = tf.reshape(tf.matmul(tf.reshape(z, [-1, factor_order, 1]), tf.reshape(z, [-1, 1, factor_order])), [-1, factor_order * factor_order]) + l = tf.nn.dropout(utils.activate(tf.matmul(l, w1) + tf.matmul(p, k1) + b1, layer_acts[1]), self.layer_keeps[1]) + for i in range(2, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.reshape(l, [-1]) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += layer_l2[0] * tf.nn.l2_loss(tf.concat(xw, 1)) + for i in range(1, len(layer_sizes) - 1): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + if kernel_l2 is not None: + self.loss += kernel_l2 * tf.nn.l2_loss(k1) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) diff --git a/dataset/preprocessed/59.txt b/dataset/preprocessed/59.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/59.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/590.txt b/dataset/preprocessed/590.txt new file mode 100644 index 0000000..e57f826 --- /dev/null +++ b/dataset/preprocessed/590.txt @@ -0,0 +1,84 @@ +import tensorflow as tf + + +class VFAConfig(object): + height_dim = 84 + width_dim = 84 + action_space = [0, 1, 2, 3, 4, 5] + lr = 0.001 + discount = 0.99 + alpha_reg = 0.0001 + beta_reg = 0.0001 + nb_steps = 4 + + +class ValueFunctionApproximation: + """Base class for value Function Approximation. Shouldn't be used separately.""" + + def __init__(self, vfa_config): + self.vfa_config = vfa_config + self.add_placeholders() + self.add_variables() + self.Q_inputs = self.add_comp_graph(self.input_placeholder) + self.next_actions = tf.argmax(self.Q_inputs, 1) + self.max_Q_inputs = tf.reduce_max(self.Q_inputs, 1) + self.max_Q_inputs = tf.mul(self.max_Q_inputs, self.terminal_placeholder) + self.Q_inputs = tf.reduce_sum(tf.mul(self.Q_inputs, self.action_placeholder), 1) + self.calculate_loss = self.add_loss_op(self.Q_inputs) + self.train_step = self.add_training_op(self.calculate_loss) + + def add_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=[None, self.vfa_config.height_dim, self.vfa_config.width_dim, 3], name='Input') + self.action_placeholder = tf.placeholder(tf.float32, shape=[None, len(self.vfa_config.action_space)], name='Action') + self.output_placeholder = tf.placeholder(tf.float32, shape=[None], name='Output') + self.terminal_placeholder = tf.placeholder(tf.float32, shape=[None], name='Terminal') + self.reward_placeholder = tf.placeholder(tf.float32, shape=[None], name='Reward') + + def add_variables(self): + self.weights = {'wc1': tf.Variable(tf.random_normal([8, 8, 3, 32])), 'wc2': tf.Variable(tf.random_normal([4, 4, 32, 64])), 'wc3': tf.Variable(tf.random_normal([3, 3, 64, 64])), 'wd1': tf.Variable(tf.random_normal([11 * 11 * 64, 512])), 'out': tf.Variable(tf.random_normal([512, len(self.vfa_config.action_space)]))} + self.biases = {'bc1': tf.Variable(tf.random_normal([32])), 'bc2': tf.Variable(tf.random_normal([64])), 'bc3': tf.Variable(tf.random_normal([64])), 'bd1': tf.Variable(tf.random_normal([512])), 'out': tf.Variable(tf.random_normal([len(self.vfa_config.action_space)]))} + + def conv2d(self, inputs, W, b, strides=1): + x = tf.nn.conv2d(inputs, W, strides=[1, strides, strides, 1], padding='SAME') + x = tf.nn.bias_add(x, b) + return tf.nn.relu(x) + + def fc2d(self, inputs, W, b): + fc1 = tf.reshape(inputs, [-1, W.get_shape().as_list()[0]]) + fc1 = tf.add(tf.matmul(fc1, W), b) + return tf.nn.relu(fc1) + + def add_comp_graph(self, inputs): + conv1 = self.conv2d(inputs, self.weights['wc1'], self.biases['bc1'], strides=4) + conv2 = self.conv2d(conv1, self.weights['wc2'], self.biases['bc2'], strides=2) + conv3 = self.conv2d(conv2, self.weights['wc3'], self.biases['bc3']) + fc1 = self.fc2d(conv3, self.weights['wd1'], self.biases['bd1']) + output = tf.add(tf.matmul(fc1, self.weights['out']), self.biases['out']) + return output + + def add_loss_op(self, inputs): + loss = tf.nn.l2_loss(self.reward_placeholder + self.vfa_config.discount ** self.vfa_config.nb_steps * self.output_placeholder - inputs) + tf.add_to_collection('total_loss', loss) + total_loss = tf.add_n(tf.get_collection('total_loss')) + return total_loss + + def add_training_op(self, loss): + optimizer = tf.train.AdamOptimizer(self.vfa_config.lr) + train_op = optimizer.minimize(loss) + return train_op + + def run_batch(self, session, inputs, actions, outputs, terminal_states, rewards): + feed = {self.input_placeholder: outputs, self.terminal_placeholder: terminal_states} + Q_outputs = session.run(self.max_Q_inputs, feed_dict=feed) + feed = {self.input_placeholder: inputs, self.action_placeholder: actions, self.output_placeholder: Q_outputs, self.reward_placeholder: rewards} + session.run(self.train_step, feed_dict=feed) + + def best_next_actions(self, session, inputs): + feed = {self.input_placeholder: inputs} + next_actions = session.run(self.next_actions, feed_dict=feed) + return next_actions + + def value_function(self, session, inputs, actions): + feed = {self.input_placeholder: inputs, self.action_placeholder: actions} + value_function = session.run(self.Q_inputs, feed_dict=feed) + return value_function diff --git a/dataset/preprocessed/591.txt b/dataset/preprocessed/591.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/591.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/592.txt b/dataset/preprocessed/592.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/592.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/593.txt b/dataset/preprocessed/593.txt new file mode 100644 index 0000000..d3f9fed --- /dev/null +++ b/dataset/preprocessed/593.txt @@ -0,0 +1,160 @@ +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import logz +import time +import math + + +class Config(object): + n_features = 11 + n_classes = 3 + dropout = 0.5 + hidden_size_1 = 128 + hidden_size_2 = 256 + hidden_size_3 = 64 + batch_size = 256 + lr = 0.0005 + itera = 20 + train_itera = 20 + envname = 'Hopper-v1' + max_steps = 1000 + + +class NN(object): + + def add_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_features), name='input') + self.labels_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_classes), name='label') + self.dropout_placeholder = tf.placeholder(tf.float32, name='drop') + self.is_training = tf.placeholder(tf.bool) + + def create_feed_dict(self, inputs_batch, labels_batch=None, dropout=1, is_training=False): + if labels_batch is None: + feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + else: + feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + return feed_dict + + def add_prediction_op(self): + self.global_step = tf.Variable(0) + with tf.name_scope('layer1'): + hidden1 = tf.contrib.layers.fully_connected(self.input_placeholder, num_outputs=Config.hidden_size_1, activation_fn=tf.nn.relu) + with tf.name_scope('layer2'): + hidden2 = tf.contrib.layers.fully_connected(hidden1, num_outputs=Config.hidden_size_2, activation_fn=tf.nn.relu) + with tf.name_scope('layer3'): + hidden3 = tf.contrib.layers.fully_connected(hidden2, num_outputs=Config.hidden_size_3, activation_fn=tf.nn.relu) + with tf.name_scope('output'): + pred = tf.contrib.layers.fully_connected(hidden3, num_outputs=Config.n_classes, activation_fn=None) + return pred + + def add_loss_op(self, pred): + loss = tf.losses.mean_squared_error(predictions=pred, labels=self.labels_placeholder) + tf.summary.scalar('loss', loss) + return loss + + def add_training_op(self, loss): + extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + with tf.control_dependencies(extra_update_ops): + learning_rate = tf.train.exponential_decay(Config.lr, self.global_step, 1000, 0.8, staircase=True) + train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=self.global_step) + return train_op + + def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + rs, _, loss = sess.run([merged, self.train_op, self.loss], feed_dict=feed) + train_writer.add_summary(rs, i) + return loss + + def __init__(self, config): + self.config = config + self.build() + + def fit(self, sess, train_x, train_y): + loss = self.train_on_batch(sess, train_x, train_y) + + def build(self): + with tf.name_scope('inputs'): + self.add_placeholders() + with tf.name_scope('predict'): + self.pred = self.add_prediction_op() + with tf.name_scope('loss'): + self.loss = self.add_loss_op(self.pred) + with tf.name_scope('train'): + self.train_op = self.add_training_op(self.loss) + + def get_pred(self, sess, inputs_batch): + feed = self.create_feed_dict(inputs_batch, dropout=1, is_training=False) + p = sess.run(self.pred, feed_dict=feed) + return p + + +def load(path): + all = np.load(path) + X = all['arr_0'] + y = all['arr_1'] + y1 = y.reshape(y.shape[0], y.shape[2]) + return X, y1 + + +def main(): + PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_BC_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + X_train, y_train = load(train_path) + print('train size :', X_train.shape, y_train.shape) + print('start training') + with tf.Graph().as_default(): + config = Config() + nn = NN(config) + init = tf.global_variables_initializer() + saver = tf.train.Saver(max_to_keep=10, keep_checkpoint_every_n_hours=0.5) + shuffle_batch_x, shuffle_batch_y = tf.train.shuffle_batch([X_train, y_train], batch_size=Config.batch_size, capacity=10000, min_after_dequeue=5000, enqueue_many=True) + with tf.Session() as session: + merged = tf.summary.merge_all() + train_writer = tf.summary.FileWriter(train_log_path, session.graph) + session.run(init) + coord = tf.train.Coordinator() + threads = tf.train.start_queue_runners(session, coord) + for j in tqdm.tqdm(range(Config.itera)): + i = 0 + try: + for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + batch_x, batch_y = session.run([shuffle_batch_x, shuffle_batch_y]) + loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + i += 1 + if i % 1000 == 0: + print('step:', i, 'loss:', loss) + saver.save(session, os.path.join(PROJECT_ROOT, 'model/model_ckpt'), global_step=i) + except tf.errors.OutOfRangeError: + print('') + finally: + coord.request_stop() + coord.join(threads) + env = gym.make(Config.envname) + rollouts = 20 + returns = [] + for _ in range(rollouts): + obs = env.reset() + done = False + totalr = 0.0 + steps = 0 + while not done: + action = nn.get_pred(session, obs[(None), :]) + obs, r, done, _ = env.step(action) + totalr += r + steps += 1 + if steps >= Config.max_steps: + break + returns.append(totalr) + logz.log_tabular('Iteration', j) + logz.log_tabular('AverageReturn', np.mean(returns)) + logz.log_tabular('StdReturn', np.std(returns)) + logz.dump_tabular() + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/594.txt b/dataset/preprocessed/594.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/594.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/595.txt b/dataset/preprocessed/595.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/595.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/596.txt b/dataset/preprocessed/596.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/596.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/597.txt b/dataset/preprocessed/597.txt new file mode 100644 index 0000000..93ee5be --- /dev/null +++ b/dataset/preprocessed/597.txt @@ -0,0 +1,468 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class DecoderCell(tf.nn.rnn_cell.BasicLSTMCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, U, masks, scope=None): + self.U = U + U_shape = U.get_shape() + self.p_size = int(U_shape[1]) + self.hid_dim = int(U_shape[2]) + self.masks = masks + super(DecoderCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + with vs.variable_scope(scope or type(self).__name__): + c_tot, h_tot = state + c_prev = c_tot[:, :self.hid_dim] + h_prev = h_tot[:, :self.hid_dim] + alpha = h_tot[:, self.hid_dim:self.hid_dim + self.p_size] + beta = h_tot[:, self.hid_dim + self.p_size:] + s_prev = tf.argmax(alpha, axis=1) + e_prev = tf.argmax(beta, axis=1) + s_prev = tf.cast(s_prev, dtype=tf.int32) + e_prev = tf.cast(e_prev, dtype=tf.int32) + with vs.variable_scope('index_into_U'): + batch_size = tf.shape(self.U)[0] + a = tf.range(0, batch_size, dtype=tf.int32) + a = tf.expand_dims(a, axis=1) + s_prev = tf.expand_dims(s_prev, axis=1) + e_prev = tf.expand_dims(e_prev, axis=1) + s_prev = tf.concat(1, [a, s_prev]) + e_prev = tf.concat(1, [a, e_prev]) + U_s = tf.gather_nd(self.U, s_prev) + U_e = tf.gather_nd(self.U, e_prev) + lstm_input = tf.concat(1, [U_s, U_e]) + self._num_units = self.hid_dim + prev_state = tf.nn.rnn_cell.LSTMStateTuple(c_prev, h_prev) + lstm_out, lstm_state = super(DecoderCell, self).__call__(lstm_input, prev_state, scope) + self._num_units = self.hid_dim + self.p_size + c_state, h_state = lstm_state + with vs.variable_scope(scope or type(self).__name__): + H_i = tf.expand_dims(h_state, 1) + H_i = tf.tile(H_i, [1, self.p_size, 1]) + U_s = tf.expand_dims(U_s, 1) + U_s = tf.tile(U_s, [1, self.p_size, 1]) + U_e = tf.expand_dims(U_e, 1) + U_e = tf.tile(U_e, [1, self.p_size, 1]) + U_reshaped = tf.reshape(self.U, [-1, self.hid_dim]) + H_i_reshaped = tf.reshape(H_i, [-1, self.hid_dim]) + U_s_reshaped = tf.reshape(U_s, [-1, self.hid_dim]) + U_e_reshaped = tf.reshape(U_e, [-1, self.hid_dim]) + with vs.variable_scope('start_probs'): + start_probs = tf.nn.rnn_cell._linear([U_reshaped, H_i_reshaped, U_s_reshaped, U_e_reshaped], output_size=self.hid_dim, bias=True) + start_probs = tf.nn.tanh(start_probs) + with vs.variable_scope('second_layer'): + start_probs = tf.nn.rnn_cell._linear(start_probs, output_size=1, bias=True) + start_probs = tf.reshape(start_probs, [-1, self.p_size]) + with vs.variable_scope('end_probs'): + end_probs = tf.nn.rnn_cell._linear([U_reshaped, H_i_reshaped, U_s_reshaped, U_e_reshaped], output_size=self.hid_dim, bias=True) + end_probs = tf.nn.tanh(end_probs) + with vs.variable_scope('second_layer'): + end_probs = tf.nn.rnn_cell._linear(end_probs, output_size=1, bias=True) + end_probs = tf.reshape(end_probs, [-1, self.p_size]) + bool_masks = tf.cast(tf.sequence_mask(self.masks, maxlen=self.p_size), tf.float32) + add_mask = -1e+30 * (1.0 - bool_masks) + start_probs = tf.add(start_probs, add_mask) + end_probs = tf.add(end_probs, add_mask) + c_state = tf.concat(1, [c_state, c_tot[:, self.hid_dim:]]) + h_state = tf.concat(1, [h_state, start_probs, end_probs]) + probs = tf.concat(1, [start_probs, end_probs]) + return h_state, tf.nn.rnn_cell.LSTMStateTuple(c_state, h_state) + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + """ + + def __init__(self, hidden_size, dropout): + self.hidden_size = hidden_size + self.dropout = dropout + + def encode(self, inputs, masks, attention_inputs=None, model_type='gru', name='encoder', reuse=False): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(name, reuse=reuse): + if attention_inputs is None: + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + else: + raise Exception('Must specify model type.') + else: + raise Exception('Attention not implemented.') + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + outputs, final_state = tf.nn.dynamic_rnn(cell, inputs, sequence_length=masks, dtype=tf.float32) + return outputs, final_state + + +class Decoder(object): + + def __init__(self, hidden_size, output_size, dropout, num_iter=4): + self.hidden_size = hidden_size + self.output_size = output_size + self.dropout = dropout + self.num_iter = num_iter + + def decode(self, knowledge_rep, masks, model_type='gru'): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + with vs.variable_scope('decoder'): + cell = DecoderCell(num_units=self.hidden_size + 2 * self.output_size, U=knowledge_rep, masks=masks) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + all_probs, final_probs = tf.nn.dynamic_rnn(cell, knowledge_rep[:, :self.num_iter, :], dtype=tf.float32) + start_probs = all_probs[:, :, self.hidden_size:self.hidden_size + self.output_size] + end_probs = all_probs[:, :, self.hidden_size + self.output_size:] + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, pretrained_embeddings, flags): + """ + Initializes your System + + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.flags = flags + self.h_size = self.flags.state_size + self.p_size = self.flags.output_size + self.q_size = self.flags.question_size + self.dropout = 1.0 - self.flags.dropout + self.embed_size = self.flags.embedding_size + self.encoder = Encoder(hidden_size=self.h_size, dropout=1.0 - self.flags.dropout) + self.decoder = Decoder(hidden_size=2 * self.h_size, output_size=self.p_size, dropout=1.0 - self.flags.dropout) + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.p_size), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.q_size), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + self.saver = tf.train.Saver() + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def coattention(self, P, Q, masks): + P_t = tf.transpose(P, perm=[0, 2, 1]) + Q_t = tf.transpose(Q, perm=[0, 2, 1]) + L = tf.batch_matmul(P, Q_t) + AQ = tf.nn.softmax(L, dim=1) + AD = tf.nn.softmax(tf.transpose(L, perm=[0, 2, 1]), dim=1) + CQ = tf.batch_matmul(P_t, AQ) + contexts = tf.concat(1, [Q_t, CQ]) + CD = tf.batch_matmul(contexts, AD) + lstm_inputs = tf.transpose(tf.concat(1, [P_t, CD]), perm=[0, 2, 1]) + cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, output_keep_prob=self.dropout) + cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, output_keep_prob=self.dropout) + all_states, _ = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, lstm_inputs, sequence_length=masks, dtype=tf.float32) + U = tf.concat(2, all_states) + U = U[:, :self.p_size, :] + return U + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + question_states, final_question_state = self.encoder.encode(self.question_embeddings, self.mask_q_placeholder, attention_inputs=None, model_type=self.flags.model_type, reuse=False) + batch_size = tf.shape(question_states)[0] + sent_vec = tf.get_variable('q_sent', shape=(1, 1, self.h_size), initializer=tf.contrib.layers.xavier_initializer()) + sent_vec = tf.tile(sent_vec, [batch_size, 1, 1]) + question_states = tf.concat(1, [question_states, sent_vec]) + Q = tf.reshape(question_states, [-1, self.h_size]) + Q = tf.nn.rnn_cell._linear(Q, output_size=self.h_size, bias=True) + Q = tf.nn.tanh(tf.reshape(Q, [-1, self.q_size + 1, self.h_size])) + ctx_states, final_ctx_state = self.encoder.encode(self.context_embeddings, self.mask_ctx_placeholder, attention_inputs=None, model_type=self.flags.model_type, reuse=True) + sent_vec = tf.get_variable('p_sent', shape=(1, 1, self.h_size), initializer=tf.contrib.layers.xavier_initializer()) + sent_vec = tf.tile(sent_vec, [batch_size, 1, 1]) + P = tf.concat(1, [ctx_states, sent_vec]) + U = self.coattention(P=P, Q=Q, masks=self.mask_ctx_placeholder) + self.start_probs, self.end_probs = self.decoder.decode(knowledge_rep=U, masks=self.mask_ctx_placeholder, model_type=self.flags.model_type) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (0), :], self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (0), :], self.answer_span_placeholder[:, (1)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (1), :], self. + answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (1), :], self.answer_span_placeholder[:, (1)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (2), :], self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (2 + ), :], self.answer_span_placeholder[:, (1)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs[:, (3), :], self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs[:, (3), :], self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs[0][:, (-1), :], outputs[1][:, (-1), :] + + def answer(self, session, data): + yp_lst = [] + yp2_lst = [] + prog_train = Progbar(target=1 + int(len(data[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle=False)): + yp, yp2 = self.decode(session, *batch) + yp_lst.append(yp) + yp2_lst.append(yp2) + prog_train.update(i + 1, [('computing F1...', 1)]) + print('') + yp_all = np.concatenate(yp_lst, axis=0) + yp2_all = np.concatenate(yp2_lst, axis=0) + a_s = np.argmax(yp_all, axis=1) + a_e = np.argmax(yp2_all, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(session=sess, context_batch=context_batch, question_batch=question_batch, answer_span_batch=answer_span_batch, mask_ctx_batch=mask_ctx_batch, mask_q_batch=mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset[0]) + else: + inds = np.random.choice(len(dataset[0]), sample) + sampled = [elem[inds] for elem in dataset] + context = [context[i] for i in inds] + a_s, a_e = self.answer(session, sampled) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + f1 = [] + em = [] + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + cur_em = exact_match_score(pred_words, actual_words) + em.append(float(cur_em)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + return np.mean(f1), np.mean(em) + + def run_epoch(self, sess, train_set, val_set, train_context, val_context): + prog_train = Progbar(target=1 + int(len(train_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + print('') + prog_val = Progbar(target=1 + int(len(val_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + self.evaluate_answer(session=sess, dataset=train_set, context=train_context, sample=len(val_set[0]), log=True, eval_set='-Epoch TRAIN-') + self.evaluate_answer(session=sess, dataset=val_set, context=val_context, sample=None, log=True, eval_set='-Epoch VAL-') + + def train(self, session, dataset, val_dataset, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = [elem[:self.flags.batch_size] for elem in train_dataset] + val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + num_epochs = 100 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, train_context=train_context, val_context=val_context) + logging.info('Saving model in %s', train_dir) + self.saver.save(session, train_dir + '/qa.ckpt') + + def minibatches(self, data, batch_size, shuffle=True): + num_data = len(data[0]) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + indices = np.arange(num_data) + if shuffle: + np.random.shuffle(indices) + for minibatch_start in np.arange(0, num_data, batch_size): + minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] diff --git a/dataset/preprocessed/598.txt b/dataset/preprocessed/598.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/598.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/599.txt b/dataset/preprocessed/599.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/599.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/6.txt b/dataset/preprocessed/6.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/6.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/60.txt b/dataset/preprocessed/60.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/60.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/601.txt b/dataset/preprocessed/601.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/601.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/602.txt b/dataset/preprocessed/602.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/602.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/603.txt b/dataset/preprocessed/603.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/603.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/604.txt b/dataset/preprocessed/604.txt new file mode 100644 index 0000000..9300c71 --- /dev/null +++ b/dataset/preprocessed/604.txt @@ -0,0 +1,161 @@ +""" +Created on Oct 6, 2017 +This file contains sequence models for training on skeleton data +@author: Amin +""" +import os +from mpl_toolkits.mplot3d import Axes3D +import matplotlib.pyplot as plt +from shutil import copyfile +import matlab.engine +import re +import time +import numpy as np +import tensorflow as tf +import sys +from network_params import * +from tensorflow.python.ops.init_ops import TruncatedNormal +from tensorflow.python.ops.rnn_cell_impl import * +cnt = 0 + + +class JointLSTM: + """ + custom lstm cells comprised of num_joints seperate lstm cells + """ + + def __init__(self, num_joints=4, num_units_each_cell=5): + self.cells = [tf.nn.rnn_cell.LSTMCell(num_units_each_cell, state_is_tuple=True, initializer=TruncatedNormal()) for i in range(num_joints)] + self.num_cells = num_joints + self.mem_size_each = num_units_each_cell + self.mem_size_global = num_units_each_cell * num_joints + self.cell_op_states = [None] * self.num_cells + + def __call__(self, global_mem, inputs): + for i in range(self.num_cells): + if self.cell_op_states[i] == None: + init_state = self.cells[i].zero_state(batch_size, tf.float32) + self.cell_op_states[i] = self.cells[i](inputs[i], init_state, scope='cell_' + str(i))[0] + else: + self.cell_op_states[i] = self.cells[i](inputs[i], (self.cell_op_states[i], global_mem[i * self.mem_size_each:(i + 1) * self.mem_size_each]), scope='cell_' + str(i))[0] + with tf.variable_scope('whole_op_gate'): + W = tf.get_variable('W', [self.mem_size_global + self.num_cells * 3, self.mem_size_global]) + b = tf.get_variable('b', [self.mem_size_global], initializer=tf.constant_initializer(0.0)) + input_to_op_gate = tf.concat([global_mem, tf.reshape(inputs, [batch_size, 3 * self.num_cells])], axis=1) + output_from_op_gate = tf.nn.sigmoid(tf.matmul(input_to_op_gate, W) + b) + concatenated_mem = tf.nn.tanh(tf.concat([self.cell_op_states[i] for i in range(self.num_cells)], axis=1)) + final_mem = tf.multiply(output_from_op_gate, concatenated_mem) + return final_mem + + +def reset_graph(): + if 'sess' in globals() and sess: + sess.close() + tf.reset_default_graph() + + +def build_rnn_graph_per_joint(state_size=10, num_classes=10, batch_size=33, num_steps=15, learning_rate=0.0001): + """ + considering only one joints using dynamic rnn and lstm cells + """ + reset_graph() + num_joints = len(considered_joints) + b_size = tf.placeholder(tf.int32, [], name='batch_size_placeholder') + x = tf.placeholder(tf.float32, [batch_size, num_joints, num_steps, 3], name='input_placeholder') + y = tf.placeholder(tf.int32, [batch_size], name='labels_placeholder') + rnn_inputs = tf.transpose(x, [2, 1, 0, 3]) + num_units_each_cell = 10 + cell = JointLSTM(num_joints=num_joints, num_units_each_cell=num_units_each_cell) + final_mems = tf.scan(lambda a, x: cell(a, x), rnn_inputs, initializer=tf.ones([batch_size, num_joints * num_units_each_cell])) + print('here') + final_layer_op = tf.squeeze(final_mems[(-1), :, :]) + with tf.variable_scope('softmax'): + W = tf.get_variable('W', [num_joints * num_units_each_cell, num_classes]) + b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0)) + final_logits = tf.matmul(final_layer_op, W) + b + total_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=final_logits, labels=tf.one_hot(y, num_classes))) + train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + predictions = tf.nn.softmax(final_logits) + pred2 = tf.argmax(predictions, 1) + correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + return dict(x=x, y=y, total_loss=total_loss, train_step=train_step, preds=predictions, saver=tf.train.Saver(), b_size=b_size, acc=accuracy_, pred2=pred2, final_layer_op_=final_layer_op) + + +def build_basic_rnn_graph_with_list3(state_size=10, num_classes=10, batch_size=33, num_steps=15, learning_rate=0.0001): + """ + considering only one joints using dynamic rnn and lstm cells + """ + reset_graph() + num_joints = len(considered_joints) + b_size = tf.placeholder(tf.int32, [], name='batch_size_placeholder') + x = tf.placeholder(tf.float32, [batch_size, num_joints, num_steps, 3], name='input_placeholder') + y = tf.placeholder(tf.int32, [batch_size], name='labels_placeholder') + rnn_inputs = tf.squeeze(x[:, (-2), :]) + cell = tf.nn.rnn_cell.LSTMCell(state_size, state_is_tuple=True, initializer=TruncatedNormal()) + init_state = cell.zero_state(b_size, tf.float32) + rnn_outputs, final_state = tf.nn.dynamic_rnn(cell, rnn_inputs, dtype=tf.float32) + rnn_op_final = tf.squeeze(rnn_outputs[:, (-1), :]) + with tf.variable_scope('softmax'): + W = tf.get_variable('W', [state_size, num_classes]) + b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0)) + final_logits = tf.matmul(rnn_op_final, W) + b + total_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=final_logits, labels=tf.one_hot(y, num_classes))) + train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + predictions = tf.nn.softmax(final_logits) + pred2 = tf.argmax(predictions, 1) + correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + return dict(x=x, y=y, init_state=init_state, final_state=final_state, total_loss=total_loss, train_step=train_step, preds=predictions, saver=tf.train.Saver(), b_size=b_size, acc=accuracy_, pred2=pred2) + + +def build_basic_rnn_graph_with_list2(state_size=10, num_classes=10, batch_size=33, num_steps=15, learning_rate=0.0001): + """ + considering only one joints + """ + reset_graph() + num_joints = len(considered_joints) + b_size = tf.placeholder(tf.int32, [], name='batch_size_placeholder') + x = tf.placeholder(tf.float32, [batch_size, num_joints, num_steps, 3], name='input_placeholder') + y = tf.placeholder(tf.int32, [batch_size], name='labels_placeholder') + rnn_inputs = tf.unstack(tf.squeeze(x[:, (-2), :]), axis=1) + cell = tf.nn.rnn_cell.BasicRNNCell(state_size) + init_state = cell.zero_state(b_size, tf.float32) + rnn_outputs, final_state = tf.contrib.rnn.static_rnn(cell, rnn_inputs, dtype=tf.float32) + rnn_op_last = tf.squeeze(rnn_outputs[-1]) + with tf.variable_scope('softmax'): + W = tf.get_variable('W', [state_size, num_classes]) + b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0)) + logits = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, state_size]), W) + b, [-1, num_steps, num_classes]) + final_logits = tf.matmul(rnn_op_last, W) + b + total_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=final_logits, labels=tf.one_hot(y, num_classes))) + train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + predictions = tf.nn.softmax(final_logits) + pred2 = tf.argmax(predictions, 1) + correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + return dict(x=x, y=y, init_state=init_state, final_state=final_state, total_loss=total_loss, train_step=train_step, preds=predictions, saver=tf.train.Saver(), b_size=b_size, acc=accuracy_, pred2=pred2) + + +def build_basic_rnn_graph_with_list(state_size=30, num_classes=10, batch_size=33, num_steps=10, learning_rate=0.0001): + reset_graph() + num_joints = len(considered_joints) + b_size = tf.placeholder(tf.int32, [], name='batch_size_placeholder') + x = tf.placeholder(tf.float32, [batch_size, num_joints, num_steps, 3], name='input_placeholder') + y = tf.placeholder(tf.int32, [batch_size], name='labels_placeholder') + rnn_inputs = tf.unstack(tf.reshape(tf.unstack(tf.transpose(x, perm=[0, 2, 1, 3]), num_steps, axis=1), [-1, num_steps, num_joints * 3]), num_steps, axis=1) + cell = tf.nn.rnn_cell.BasicRNNCell(state_size) + init_state = cell.zero_state(b_size, tf.float32) + rnn_outputs, final_state = tf.contrib.rnn.static_rnn(cell, rnn_inputs, initial_state=init_state) + with tf.variable_scope('softmax'): + W = tf.get_variable('W', [state_size, num_classes]) + b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0)) + logits = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, state_size]), W) + b, [-1, num_steps, num_classes]) + final_logits = tf.squeeze(tf.split(logits, num_or_size_splits=num_steps, axis=1)[-1]) + total_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=final_logits, labels=y)) + train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss) + predictions = tf.nn.softmax(final_logits) + pred2 = tf.argmax(predictions, 1) + correct_pred = tf.equal(pred2, tf.cast(y, tf.int64)) + accuracy_ = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) + return dict(x=x, y=y, init_state=init_state, final_state=final_state, total_loss=total_loss, train_step=train_step, preds=predictions, saver=tf.train.Saver(), b_size=b_size, acc=accuracy_, pred2=pred2) diff --git a/dataset/preprocessed/605.txt b/dataset/preprocessed/605.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/605.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/606.txt b/dataset/preprocessed/606.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/606.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/607.txt b/dataset/preprocessed/607.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/607.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/608.txt b/dataset/preprocessed/608.txt new file mode 100644 index 0000000..c3945f4 --- /dev/null +++ b/dataset/preprocessed/608.txt @@ -0,0 +1,63 @@ +from __future__ import print_function +import tensorflow as tf +import numpy as np + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +def randomize(dataset, labels): + permutation = np.random.permutation(labels.shape[0]) + shuffled_dataset = dataset[(permutation), :] + shuffled_labels = labels[permutation] + return shuffled_dataset, shuffled_labels + + +def make_neural_network(train_dataset, train_labels, test_dataset, test_labels, num_labels, features): + train_dataset, train_labels = randomize(train_dataset, train_labels) + batch_size = 64 + beta = 0.001 + hidden_nodes1 = 128 + hidden_nodes2 = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, features)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_test_dataset = tf.constant(test_dataset) + hidden_weights = tf.Variable(tf.truncated_normal([features, hidden_nodes1])) + hidden_biases = tf.Variable(tf.zeros([hidden_nodes1])) + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + keep_prob = tf.placeholder('float') + hidden_layer_drop = tf.nn.dropout(hidden_layer, keep_prob) + hidden_weights2 = tf.Variable(tf.truncated_normal([hidden_nodes1, hidden_nodes2])) + hidden_biases2 = tf.Variable(tf.zeros([hidden_nodes2])) + hidden_layer2 = tf.nn.relu(tf.matmul(hidden_layer_drop, hidden_weights2) + hidden_biases2) + hidden_layer_drop2 = tf.nn.dropout(hidden_layer2, keep_prob) + weights = tf.Variable(tf.truncated_normal([hidden_nodes2, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_layer_drop2, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = tf.reduce_mean(loss + beta * tf.nn.l2_loss(weights)) + global_step = tf.Variable(0) + learnr = tf.placeholder('float') + learning_rate = tf.train.exponential_decay(learnr, global_step, 100000, 0.95, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + test_relu1 = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + test_relu2 = tf.nn.relu(tf.matmul(test_relu1, hidden_weights2) + hidden_biases2) + test_prediction = tf.nn.softmax(tf.matmul(test_relu2, weights) + biases) + num_steps = 2000 + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 1.0, learnr: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/61.txt b/dataset/preprocessed/61.txt new file mode 100644 index 0000000..828c181 --- /dev/null +++ b/dataset/preprocessed/61.txt @@ -0,0 +1,230 @@ +import numpy as np +import tensorflow as tf +import Utility as util +import math + + +class ConvolutionNeuralNetwork: + + def __init__(self, d, k): + """ + :param d: dimensionality + :param k: number of classes + """ + self.D = d + self.K = k + self.NR_VALIDATION_DATA = 50 + self.NR_ITERATION = 20000 + self.BATCH_SIZE = 500 + self.SHOW_ACC = 100 + self.TRAIN_STEP = 0.0001 + self.EPSILON = 0.001 + self.W1_SHAPE = [5, 5, 3, 16] + self.B1_SHAPE = [16] + self.W2_SHAPE = [5, 5, 16, 20] + self.B2_SHAPE = [20] + self.W3_SHAPE = [5, 5, 20, 20] + self.B3_SHAPE = [20] + self.WFC_SHAPE = [320, k] + self.BFC_SHAPE = [k] + + def training(self, features, labels): + """ + Training the Convolutional Neural Network + :param features: the training data [50000 x 3072] + :param labels: the true label for X [50000 x 1] + :return: return a dictionary which contains all learned parameters + """ + features = self.__preprocessing(features) + train_features = features[self.NR_VALIDATION_DATA:] + train_labels = labels[self.NR_VALIDATION_DATA:] + validation_features = features[0:self.NR_VALIDATION_DATA] + validation_labels = labels[0:self.NR_VALIDATION_DATA] + sess = tf.InteractiveSession() + x = tf.placeholder(tf.float32, shape=[None, self.D]) + y_ = tf.placeholder(tf.int64, shape=[None]) + x_image = tf.reshape(x, [-1, 32, 32, 3]) + W1 = self.__weight_variable(self.W1_SHAPE) + scale1 = tf.Variable(tf.ones(self.B1_SHAPE)) + beta1 = tf.Variable(tf.zeros(self.B1_SHAPE)) + W2 = self.__weight_variable(self.W2_SHAPE) + scale2 = tf.Variable(tf.ones(self.B2_SHAPE)) + beta2 = tf.Variable(tf.zeros(self.B2_SHAPE)) + W3 = self.__weight_variable(self.W3_SHAPE) + scale3 = tf.Variable(tf.ones(self.B3_SHAPE)) + beta3 = tf.Variable(tf.zeros(self.B3_SHAPE)) + WFC = self.__weight_variable(self.WFC_SHAPE) + bFC = self.__bias_variable(self.BFC_SHAPE) + Z1_conv = self.__convolution(x_image, W1) + batch_mean1, batch_var1 = tf.nn.moments(Z1_conv, [0]) + BN1 = tf.nn.batch_normalization(Z1_conv, batch_mean1, batch_var1, beta1, scale1, self.EPSILON) + H1_conv = self.__activation(BN1) + H1_pool = self.__pool(H1_conv) + Z2_conv = self.__convolution(H1_pool, W2) + batch_mean2, batch_var2 = tf.nn.moments(Z2_conv, [0]) + BN2 = tf.nn.batch_normalization(Z2_conv, batch_mean2, batch_var2, beta2, scale2, self.EPSILON) + H2_conv = self.__activation(BN2) + H2_pool = self.__pool(H2_conv) + Z3_conv = self.__convolution(H2_pool, W3) + batch_mean3, batch_var3 = tf.nn.moments(Z3_conv, [0]) + BN3 = tf.nn.batch_normalization(Z3_conv, batch_mean3, batch_var3, beta3, scale3, self.EPSILON) + H3_conv = self.__activation(BN3) + H3_pool = self.__pool(H3_conv) + H3_pool_flatten = tf.reshape(H3_pool, [-1, self.WFC_SHAPE[0]]) + HFC = tf.matmul(H3_pool_flatten, WFC) + bFC + y_conv = tf.nn.softmax(HFC) + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y_conv, y_) + cross_entropy_mean = tf.reduce_mean(cross_entropy) + train_step = tf.train.AdamOptimizer(self.TRAIN_STEP).minimize(cross_entropy_mean) + correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + sess.run(tf.initialize_all_variables()) + for i in range(self.NR_ITERATION): + batch = util.generate_batch(train_features, train_labels, self.BATCH_SIZE) + train_step.run(feed_dict={x: batch[0], y_: batch[1]}) + if i % self.SHOW_ACC == 0: + train_accuracy = accuracy.eval(feed_dict={x: validation_features, y_: validation_labels}) + print('Step - ', i, ' - Acc : ', train_accuracy) + W1_final = W1.eval() + beta1_final = beta1.eval() + scale1_final = scale1.eval() + W2_final = W2.eval() + beta2_final = beta2.eval() + scale2_final = scale2.eval() + W3_final = W3.eval() + beta3_final = beta3.eval() + scale3_final = scale3.eval() + WFC_final = WFC.eval() + bFC_final = bFC.eval() + sess.close() + return {'W1': W1_final, 'beta1': beta1_final, 'scale1': scale1_final, 'W2': W2_final, 'beta2': beta2_final, 'scale2': scale2_final, 'W3': W3_final, 'beta3': beta3_final, 'scale3': scale3_final, 'WFC': WFC_final, 'bFC': bFC_final} + + def predict(self, test_features, test_labels, nn): + """ + Predict data + :param test_features: testing data + :param test_labels: labels for test_features + :param nn: it is a dictionary which contains a Neural Network + :return: return the predicted labels and the accuracy + """ + test_features = self.__preprocessing(test_features) + x = tf.placeholder(tf.float32, shape=[None, self.D]) + W1 = tf.placeholder(tf.float32, shape=self.W1_SHAPE) + beta1 = tf.placeholder(tf.float32, shape=self.B1_SHAPE) + scale1 = tf.placeholder(tf.float32, shape=self.B1_SHAPE) + W2 = tf.placeholder(tf.float32, shape=self.W2_SHAPE) + beta2 = tf.placeholder(tf.float32, shape=self.B2_SHAPE) + scale2 = tf.placeholder(tf.float32, shape=self.B2_SHAPE) + W3 = tf.placeholder(tf.float32, shape=self.W3_SHAPE) + beta3 = tf.placeholder(tf.float32, shape=self.B3_SHAPE) + scale3 = tf.placeholder(tf.float32, shape=self.B3_SHAPE) + WFC = tf.placeholder(tf.float32, shape=self.WFC_SHAPE) + bFC = tf.placeholder(tf.float32, shape=self.BFC_SHAPE) + x_image = tf.reshape(x, [-1, 32, 32, 3]) + Z1_conv = self.__convolution(x_image, W1) + batch_mean1, batch_var1 = tf.nn.moments(Z1_conv, [0]) + BN1 = tf.nn.batch_normalization(Z1_conv, batch_mean1, batch_var1, beta1, scale1, self.EPSILON) + H1_conv = self.__activation(BN1) + H1_pool = self.__pool(H1_conv) + Z2_conv = self.__convolution(H1_pool, W2) + batch_mean2, batch_var2 = tf.nn.moments(Z2_conv, [0]) + BN2 = tf.nn.batch_normalization(Z2_conv, batch_mean2, batch_var2, beta2, scale2, self.EPSILON) + H2_conv = self.__activation(BN2) + H2_pool = self.__pool(H2_conv) + Z3_conv = self.__convolution(H2_pool, W3) + batch_mean3, batch_var3 = tf.nn.moments(Z3_conv, [0]) + BN3 = tf.nn.batch_normalization(Z3_conv, batch_mean3, batch_var3, beta3, scale3, self.EPSILON) + H3_conv = self.__activation(BN3) + H3_pool = self.__pool(H3_conv) + H3_pool_flatten = tf.reshape(H3_pool, [-1, self.WFC_SHAPE[0]]) + HFC = tf.matmul(H3_pool_flatten, WFC) + bFC + y_conv = tf.nn.softmax(HFC) + sess = tf.InteractiveSession() + feed_dict = {x: test_features, W1: nn['W1'], beta1: nn['beta1'], scale1: nn['scale1'], W2: nn['W2'], beta2: nn['beta2'], scale2: nn['scale2'], W3: nn['W3'], beta3: nn['beta3'], scale3: nn['scale3'], WFC: nn['WFC'], bFC: nn['bFC']} + predicted_labels = sess.run(y_conv, feed_dict=feed_dict) + sess.close() + predicted_labels = np.argmax(predicted_labels, axis=1) + acc = np.mean(predicted_labels == test_labels) + return predicted_labels, acc + + def __preprocessing(self, X): + """ + Preprocessing the X data by zero-centered and normalized them. + :param X: the data. + :return: return the new zero-centered and normalized data. + """ + X = X.astype(np.float64) + X = X - np.mean(X, dtype=np.float64) + X = X / np.std(X, dtype=np.float64) + return X + + def __weight_variable(self, shape): + """ + Initialize the weights variable. + :param shape: the shape. + :return: return a TensorFlow variable + """ + if len(shape) == 4: + initial = np.random.randn(shape[0], shape[1], shape[2], shape[3]) * math.sqrt(2.0 / (shape[0] * shape[1] * shape[2] * shape[3])) + else: + initial = np.random.randn(shape[0], shape[1]) * math.sqrt(2.0 / (shape[0] * shape[1])) + return tf.Variable(initial, dtype=tf.float32) + + def __bias_variable(self, shape): + """ + Initialize the biases variable. + :param shape:t he shape. + :return: return a TensorFlow variable + """ + initial = tf.constant(0.1, shape=shape) + return tf.Variable(initial) + + def __convolution(self, x, W): + """ + The convolution layer calculation. + :param x: the data. + :param W: the weights. + :return: return the output of the convolution layer. + """ + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + def __activation(self, x): + """ + The activation function. + :param x: the data. + :return: return the data after apply the activation. + """ + return tf.nn.relu(x) + + def __pool(self, x): + """ + The pool layer. + :param x: the data. + :return: return the output of the pool layer. + """ + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +if __name__ == '__main__': + D = 3072 + K = 10 + learn_data = 'result/CNN1_BN/cifar_10' + final_accuracy = 0 + batch_size = 500 + cnn = ConvolutionNeuralNetwork(3072, 10) + X, y, X_test, y_test = util.load_CIFAR10('data/') + if util.file_exist(learn_data): + nn_parameter = util.unpickle(learn_data) + else: + nn_parameter = cnn.training(X, y) + util.pickle_nn(learn_data, nn_parameter) + util.create_file('result/CNN1_BN/submission.csv') + for i in range(0, X_test.shape[0], batch_size): + batch_test_feature = X_test[i:i + batch_size, :] + batch_test_labels = y_test[i:i + batch_size] + predicted_labels, accuracy = cnn.predict(batch_test_feature, batch_test_labels, nn_parameter) + final_accuracy += accuracy + util.append_data_to_file('result/CNN1_BN/submission.csv', predicted_labels, i) + nr_iteration = X_test.shape[0] / batch_size + final_accuracy /= nr_iteration + print('The final accuracy is : ', final_accuracy) diff --git a/dataset/preprocessed/610.txt b/dataset/preprocessed/610.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/610.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/611.txt b/dataset/preprocessed/611.txt new file mode 100644 index 0000000..fc9fbdb --- /dev/null +++ b/dataset/preprocessed/611.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(text=''): + print(text + ' Tests Passed ...') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message('Flatten') + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message('Fully Connected') + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message('Output') + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/612.txt b/dataset/preprocessed/612.txt new file mode 100644 index 0000000..4caa465 --- /dev/null +++ b/dataset/preprocessed/612.txt @@ -0,0 +1,187 @@ +""" +Created on Tue May 16 12:30:50 2017 + +@author: Seagle + +""" +import os +os.chdir('C:/SeagleDLTrial') +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +lambda_l2 = 0.05 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + lambda_l2 * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(valid_prediction.eval(), valid_labels)) +import matplotlib.pyplot as plt +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(valid_prediction.eval(), valid_labels)) +import matplotlib.pyplot as plt +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +accuracy_val.index(max(accuracy_val)) +aa = [0, 2, 5, 5, 4, -1, 3] +[i for i, j in enumerate(aa) if j == max(aa)] +for i, j in enumerate(aa): + print(i, j) diff --git a/dataset/preprocessed/613.txt b/dataset/preprocessed/613.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/613.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/614.txt b/dataset/preprocessed/614.txt new file mode 100644 index 0000000..ae0bcdb --- /dev/null +++ b/dataset/preprocessed/614.txt @@ -0,0 +1,71 @@ +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.01) + return tf.Variable(initial) + + +def bias_variable(shape): + initial = tf.constant(0.01, shape=shape) + return tf.Variable(initial) + + +def conv2d(x, W, stride): + return tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +def createNetwork(action_count): + W_conv1 = weight_variable([8, 8, 4, 32]) + b_conv1 = bias_variable([32]) + W_conv2 = weight_variable([4, 4, 32, 64]) + b_conv2 = bias_variable([64]) + W_conv3 = weight_variable([3, 3, 64, 64]) + b_conv3 = bias_variable([64]) + W_fc1 = weight_variable([1600, 512]) + b_fc1 = bias_variable([512]) + W_fc2 = weight_variable([512, action_count]) + b_fc2 = bias_variable([action_count]) + s = tf.placeholder('float', [None, 80, 80, 4]) + h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + return s, readout, h_fc1 + + +class DQNAgent(object): + + def __init__(self, action_count): + self.action_count = action_count + self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + self.action_placeholder = tf.placeholder('float', [None, action_count]) + self.reward_placeholder = tf.placeholder('float', [None]) + self.readout_action = tf.reduce_sum(tf.multiply(self.readout, self.action_placeholder), reduction_indices=1) + self.losses = tf.square(self.reward_placeholder - self.readout_action) + self.loss = tf.reduce_mean(self.losses) + self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.loss) + tf.summary.scalar('loss', self.loss), + tf.summary.histogram('loss_hist', self.losses), + tf.summary.histogram('q_values_hist', self.readout), + tf.summary.scalar('max_q_value', tf.reduce_max(self.readout)) + self.summaries = tf.summary.merge_all() + + def act(self, session, state): + scores = self.score_actions(session, [state])[0] + return np.argmax(scores) + + def score_actions(self, session, state_batch): + return session.run(self.readout, feed_dict={self.state_placeholder: state_batch}) + + def train(self, session, reward_batch, action_batch, state_batch): + summaries, _, loss = session.run([self.summaries, self.train_step, self.loss], feed_dict={self.reward_placeholder: reward_batch, self.action_placeholder: action_batch, self.state_placeholder: state_batch}) + return summaries, loss diff --git a/dataset/preprocessed/615.txt b/dataset/preprocessed/615.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/615.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/616.txt b/dataset/preprocessed/616.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/616.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/617.txt b/dataset/preprocessed/617.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/617.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/618.txt b/dataset/preprocessed/618.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/618.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/619.txt b/dataset/preprocessed/619.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/619.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/62.txt b/dataset/preprocessed/62.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/62.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/620.txt b/dataset/preprocessed/620.txt new file mode 100644 index 0000000..b1c9861 --- /dev/null +++ b/dataset/preprocessed/620.txt @@ -0,0 +1,419 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +from itertools import islice +from sys import stdout +from tempfile import NamedTemporaryFile +import tensorflow as tf +from utils.model import Model +from data import load_and_preprocess_data +from data import score_arcs +from initialization import xavier_weight_init +from parser import minibatch_parse +from utils.generic_utils import Progbar +tf.flags.DEFINE_float('lr', 0.001, 'learning rate') +tf.flags.DEFINE_integer('hidden', 0, 'number of hidden layers if hidden > 0') +tf.flags.DEFINE_integer('hidden_size', 200, 'hidden size for each layer') +tf.flags.DEFINE_integer('epochs', 10, 'number of epochs') +tf.flags.DEFINE_float('l2_beta', 0, 'beta for computing l2 regularization') +tf.flags.DEFINE_string('activation', 'relu', 'activation function, can be relu or cube') +tf.flags.DEFINE_string('optimizer', 'adam', 'optimizer, can be adam or adagrad') +tf.flags.DEFINE_string('output', '', 'output filename for arcs') +FLAGS = tf.app.flags.FLAGS + + +class Config(object): + """Holds model hyperparams and data information. + + The config class is used to store various hyperparameters and dataset + information parameters. Model objects are passed a Config() object at + instantiation. + """ + n_word_ids = None + n_tag_ids = None + n_deprel_ids = None + n_word_features = None + n_tag_features = None + n_deprel_features = None + n_classes = None + dropout = 0.5 + embed_size = None + hidden_size = FLAGS.hidden_size + batch_size = 2048 + n_epochs = FLAGS.epochs + lr = FLAGS.lr + l2_beta = FLAGS.l2_beta + l2_loss = 0 + + +class ParserModel(Model): + """ + Implements a feedforward neural network with an embedding layer and single hidden layer. + This network will predict which transition should be applied to a given partial parse + configuration. + """ + + def add_placeholders(self): + """Generates placeholder variables to represent the input tensors + + These placeholders are used as inputs by the rest of the model + building and will be fed data during training. Note that when + "None" is in a placeholder's shape, it's flexible (so we can use + different batch sizes without rebuilding the model). + + Adds following nodes to the computational graph + + word_id_placeholder: + Word feature placeholder of shape (None, n_word_features), + type tf.int32 + tag_id_placeholder: + POS tag feature placeholder of shape (None, n_tag_features), + type tf.int32 + deprel_id_placeholder: + Dependency relation feature placeholder of shape + (None, n_deprel_features), type tf.int32 + class_placeholder: + Labels placeholder tensor of shape (None, n_classes), + type tf.float32 + dropout_placeholder: Dropout value placeholder (scalar), type + tf.float32 + + Add these placeholders to self as attributes + self.word_id_placeholder + self.tag_id_placeholder + self.deprel_id_placeholder + self.class_placeholder + self.dropout_placeholder + """ + self.word_id_placeholder = tf.placeholder(tf.int32, shape=(None, self.config.n_word_features)) + self.tag_id_placeholder = tf.placeholder(tf.int32, shape=(None, self.config.n_tag_features)) + self.deprel_id_placeholder = tf.placeholder(tf.int32, shape=(None, self.config.n_deprel_features)) + self.class_placeholder = tf.placeholder(tf.float32, shape=(None, self.config.n_classes)) + self.dropout_placeholder = tf.placeholder(tf.float32, shape=()) + + def create_feed_dict(self, word_id_batch, tag_id_batch, deprel_id_batch, class_batch=None, dropout=1): + """Creates the feed_dict for the dependency parser. + + A feed_dict takes the form of: + + feed_dict = { + : , + .... + } + + The keys for the feed_dict should be a subset of the placeholder + tensors created in add_placeholders. When an argument is None, + don't add it to the feed_dict. + + Args: + word_id_batch: A batch of word id features + tag_id_batch: A batch of POS tag id features + deprel_id_batch: A batch of dependency relation id features + class_batch: A batch of class label data + dropout: The dropout rate + Returns: + feed_dict: The feed dictionary mapping from placeholders to values. + """ + feed_dict = {} + if type(word_id_batch) != type(None): + feed_dict[self.word_id_placeholder] = word_id_batch + if type(tag_id_batch) != type(None): + feed_dict[self.tag_id_placeholder] = tag_id_batch + if type(deprel_id_batch) != type(None): + feed_dict[self.deprel_id_placeholder] = deprel_id_batch + if type(class_batch) != type(None): + feed_dict[self.class_placeholder] = class_batch + if type(dropout) != type(None): + feed_dict[self.dropout_placeholder] = dropout + return feed_dict + + def add_embeddings(self): + """Creates embeddings that map word, tag, deprels to vectors + + Embedding layers convert (sparse) ID representations to dense, + lower-dimensional representations. Inputs are integers, outputs + are floats. + + - Create 3 embedding matrices, one for each of the input types. + Input values index the rows of the matrices to extract. The + max bound (exclusive) on the values in the input can be found + in {n_word_ids, n_tag_ids, n_deprel_ids} + After lookup, the resulting tensors should each be of shape + (None, n, embed_size), where n is one of + {n_word_features, n_tag_features, n_deprel_features}. + - Initialize the word_id embedding matrix with + self.word_embeddings. Initialize the other two matrices + with the Xavier initialization you implemented + - Reshape the embedding tensors into shapes + (None, n * embed_size) + + ** Embedding matrices should be variables, not constants! ** + + Use tf.nn.embedding_lookup. Also take a look at tf.reshape + + Returns: + word_embeddings : tf.Tensor of type tf.float32 and shape + (None, n_word_features * embed_size) + tag_embeddings : tf.float32 (None, n_tag_features * embed_size) + deprel_embeddings : tf.float32 + (None, n_deprel_features * embed_size) + """ + word_variables = tf.Variable(self.word_embeddings) + word_embeddings = tf.nn.embedding_lookup(word_variables, self.word_id_placeholder) + word_embeddings = tf.reshape(word_embeddings, shape=(-1, self.config.n_word_features * self.config.embed_size)) + xavier_initializer = xavier_weight_init() + tag_variable = tf.Variable(xavier_initializer((self.config.n_tag_ids, self.config.embed_size))) + tag_embeddings = tf.nn.embedding_lookup(tag_variable, self.tag_id_placeholder) + tag_embeddings = tf.reshape(tag_embeddings, shape=(-1, self.config.n_tag_features * self.config.embed_size)) + deprel_variable = tf.Variable(xavier_initializer((self.config.n_deprel_ids, self.config.embed_size))) + deprel_embeddings = tf.nn.embedding_lookup(deprel_variable, self.deprel_id_placeholder) + deprel_embeddings = tf.reshape(deprel_embeddings, shape=(-1, self.config.n_deprel_features * self.config.embed_size)) + if self.config.l2_beta: + self.config.l2_loss += tf.nn.l2_loss(tag_variable) + tf.nn.l2_loss(deprel_variable) + return word_embeddings, tag_embeddings, deprel_embeddings + + def add_prediction_op(self): + """Adds the single layer neural network + + The l + h = Relu(W_w x_w + W_t x_t + W_d x_d + b1) + h_drop = Dropout(h, dropout_rate) + pred = h_drop U + b2 + + Note that we are not applying a softmax to pred. The softmax + will instead be done in the add_loss_op function, which improves + efficiency because we can use + tf.nn.softmax_cross_entropy_with_logits + Excluding the softmax in predictions won't change the expected + transition. + + Use the Xavier initializer from initialization.py for W_ and + U. Initialize b1 and b2 with zeros. + + The dimensions of the various variables you will need to create + are: + W_w : (n_word_features * embed_size, hidden_size) + W_t : (n_tag_features * embed_size, hidden_size) + W_d : (n_deprel_features * embed_size, hidden_size) + b1: (hidden_size,) + U: (hidden_size, n_classes) + b2: (n_classes) + + Use the value self.dropout_placeholder in tf.nn.dropout directly + + Returns: + pred: tf.Tensor of shape (batch_size, n_classes) + """ + x_w, x_t, x_d = self.add_embeddings() + xavier_initializer = xavier_weight_init() + W_w = tf.Variable(xavier_initializer((self.config.n_word_features * self.config.embed_size, self.config.hidden_size))) + W_t = tf.Variable(xavier_initializer((self.config.n_tag_features * self.config.embed_size, self.config.hidden_size))) + W_d = tf.Variable(xavier_initializer((self.config.n_deprel_features * self.config.embed_size, self.config.hidden_size))) + b1 = tf.Variable(tf.zeros((self.config.hidden_size,))) + U = tf.Variable(xavier_initializer((self.config.hidden_size, self.config.n_classes))) + b2 = tf.Variable(tf.zeros(self.config.n_classes)) + x = tf.matmul(x_w, W_w) + tf.matmul(x_t, W_t) + tf.matmul(x_d, W_d) + b1 + print('\n\t' + FLAGS.activation + ' activation function') + if FLAGS.activation == 'cube': + h = tf.pow(x, tf.constant(3, dtype=tf.float32)) + else: + h = tf.nn.relu(x) + if self.config.l2_beta: + print('\tl2 regularization with beta ' + str(self.config.l2_beta)) + self.config.l2_loss += tf.nn.l2_loss(W_w) + tf.nn.l2_loss(W_t) + tf.nn.l2_loss(W_d) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(U) + tf.nn.l2_loss(b2) + print('\t' + str(FLAGS.hidden + 1) + ' hidden layer(s) with size ' + str(FLAGS.hidden_size)) + if FLAGS.hidden: + w, b = {}, {} + for i in range(FLAGS.hidden): + w[i] = tf.Variable(xavier_initializer((self.config.hidden_size, self.config.hidden_size))) + b[i] = tf.Variable(tf.random_normal([self.config.hidden_size])) + + def hidden_layers(x): + layer = tf.nn.relu(tf.matmul(x, w[0]) + b[0]) + for i in range(1, FLAGS.hidden): + layer = tf.nn.relu(tf.matmul(layer, w[i]) + b[i]) + return layer + h_drop = tf.nn.dropout(h, self.dropout_placeholder) + layers = hidden_layers(h_drop) + pred = tf.matmul(layers, U) + b2 + if self.config.l2_beta: + for i in range(FLAGS.hidden): + self.config.l2_loss += tf.nn.l2_loss(w[i]) + tf.nn.l2_loss(b[i]) + else: + h_drop = tf.nn.dropout(h, self.dropout_placeholder) + pred = tf.matmul(h_drop, U) + b2 + return pred + + def add_loss_op(self, pred): + """Adds Ops for the loss function to the computational graph. + + In this case we are using cross entropy loss. The loss should be + averaged over all examples in the current minibatch. + + Use tf.nn.softmax_cross_entropy_with_logits to simplify your + implementation. You might find tf.reduce_mean useful. + + Args: + pred: + A tensor of shape (batch_size, n_classes) containing + the output of the neural network before the softmax layer. + Returns: + loss: A 0-d tensor (scalar) + """ + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.class_placeholder, logits=pred)) + if self.config.l2_beta: + loss = tf.reduce_mean(loss + self.config.l2_beta * self.config.l2_loss) + return loss + + def add_training_op(self, loss): + """Sets up the training Ops. + + Creates an optimizer and applies the gradients to all trainable + variables. The Op returned by this function is what must be + passed to the `sess.run()` call to cause the model to train. + + Use tf.train.AdamOptimizer for this model. + Calling optimizer.minimize() will return a train_op object. + + Args: + loss: Loss tensor, from cross_entropy_loss. + Returns: + train_op: The Op for training. + """ + print('\t' + FLAGS.optimizer + ' optimizer with learning rate ' + str(self.config.lr)) + if FLAGS.optimizer == 'adagrad': + optimizer = tf.train.AdagradOptimizer(self.config.lr) + else: + optimizer = tf.train.AdamOptimizer(self.config.lr) + train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step()) + return train_op + + def fit_batch(self, word_id_batch, tag_id_batch, deprel_id_batch, class_batch): + feed = self.create_feed_dict(word_id_batch, tag_id_batch, deprel_id_batch, class_batch=class_batch, dropout=self.config.dropout) + _, loss = self.sess.run([self.train_op, self.loss], feed_dict=feed) + return loss + + def fit_epoch(self, train_data, batch_size=None, incl_progbar=True): + """Fit on training data for an epoch""" + if incl_progbar: + progbar = Progbar(target=len(train_data) * batch_size if batch_size else len(train_data)) + for (word_id_batch, tag_id_batch, deprel_id_batch), class_batch in train_data: + loss = self.fit_batch(word_id_batch, tag_id_batch, deprel_id_batch, class_batch) + if incl_progbar: + progbar.add(word_id_batch.shape[0], [('Cross-entropy', loss)]) + + def predict_on_batch(self, inputs_batch): + feed = self.create_feed_dict(*inputs_batch) + predictions = self.sess.run(self.pred, feed_dict=feed) + return predictions + + def predict(self, partial_parses): + """Use this model to predict the next transitions/deprels of pps""" + feats = self.transducer.pps2feats(partial_parses) + td_vecs = self.predict_on_batch(feats) + preds = [self.transducer.td_vec2trans_deprel(td_vec) for td_vec in td_vecs] + return preds + + def eval(self, sentences, ex_arcs): + """LAS on either training or test sets""" + act_arcs = minibatch_parse(sentences, self, self.config.batch_size) + ex_arcs = tuple([(a[0], a[1], self.transducer.id2deprel[a[2]]) for a in pp] for pp in ex_arcs) + if FLAGS.output: + import json + with open(FLAGS.output, 'w+') as f: + for row in act_arcs: + f.write('%s\n' % json.dumps(row)) + return score_arcs(act_arcs, ex_arcs) + + def __init__(self, transducer, sess, config, word_embeddings): + self.transducer = transducer + self.sess = sess + self.word_embeddings = word_embeddings + self.config = config + self.build() + + +def main(debug): + """Main function + + Args: + debug : + whether to use a fraction of the data. Make sure to set to False + when you're ready to train your model for real! + """ + print(80 * '=') + print('INITIALIZING') + print(80 * '=') + config = Config() + data = load_and_preprocess_data(max_batch_size=config.batch_size) + transducer, word_embeddings, train_data = data[:3] + dev_sents, dev_arcs = data[3:5] + test_sents, test_arcs = data[5:] + config.n_word_ids = len(transducer.id2word) + 1 + config.n_tag_ids = len(transducer.id2tag) + 1 + config.n_deprel_ids = len(transducer.id2deprel) + 1 + config.embed_size = word_embeddings.shape[1] + for (word_batch, tag_batch, deprel_batch), td_batch in train_data.get_iterator(shuffled=False): + config.n_word_features = word_batch.shape[-1] + config.n_tag_features = tag_batch.shape[-1] + config.n_deprel_features = deprel_batch.shape[-1] + config.n_classes = td_batch.shape[-1] + break + print('Word feat size: {}, tag feat size: {}, deprel feat size: {}, classes size: {}'.format(config.n_word_features, config.n_tag_features, config.n_deprel_features, config.n_classes)) + if debug: + dev_sents = dev_sents[:500] + dev_arcs = dev_arcs[:500] + test_sents = test_sents[:500] + test_arcs = test_arcs[:500] + if not debug: + weight_file = NamedTemporaryFile(suffix='.weights') + with tf.Graph().as_default(), tf.Session() as session: + print('Building model...', end=' ') + start = time.time() + model = ParserModel(transducer, session, config, word_embeddings) + print('took {:.2f} seconds\n'.format(time.time() - start)) + init = tf.global_variables_initializer() + session.run(init) + saver = None if debug else tf.train.Saver() + print(80 * '=') + print('TRAINING') + print(80 * '=') + best_las = 0.0 + for epoch in range(config.n_epochs): + print('Epoch {}'.format(epoch)) + if debug: + model.fit_epoch(list(islice(train_data, 3)), config.batch_size) + else: + model.fit_epoch(train_data) + stdout.flush() + dev_las, dev_uas = model.eval(dev_sents, dev_arcs) + best = dev_las > best_las + if best: + best_las = dev_las + if not debug: + saver.save(session, weight_file.name) + print('Validation LAS: ', end='') + print('{:.2f}{}'.format(dev_las, ' (BEST!), ' if best else ', ')) + print('Validation UAS: ', end='') + print('{:.2f}'.format(dev_uas)) + if not debug: + print() + print(80 * '=') + print('TESTING') + print(80 * '=') + print('Restoring the best model weights found on the dev set') + saver.restore(session, weight_file.name) + stdout.flush() + las, uas = model.eval(test_sents, test_arcs) + if las: + print('Test LAS: ', end='') + print('{:.2f}'.format(las), end=', ') + print('Test UAS: ', end='') + print('{:.2f}'.format(uas)) + print('Done!') + return 0 + + +if __name__ == '__main__': + main(False) diff --git a/dataset/preprocessed/621.txt b/dataset/preprocessed/621.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/621.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/622.txt b/dataset/preprocessed/622.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/622.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/623.txt b/dataset/preprocessed/623.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/623.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/624.txt b/dataset/preprocessed/624.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/624.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/625.txt b/dataset/preprocessed/625.txt new file mode 100644 index 0000000..c3ed112 --- /dev/null +++ b/dataset/preprocessed/625.txt @@ -0,0 +1,126 @@ +import random +import tensorflow as tf +import numpy as np +import gym +actor_network_verbose = False +update_gradients_verbose = False +actor_learning_rate = 0.0003 +critic_learning_rate = 0.00025 +n_episode = 1500 +gamma = 0.99 +num_tests = 50 +observation_placeholder = tf.placeholder(tf.float32, shape=[None, 4]) +W1_actor = tf.get_variable('W1_actor', shape=[4, 64]) +b1_actor = tf.get_variable('b1_actor', shape=[64]) +W2_actor = tf.get_variable('W2_actor', shape=[64, 64]) +b2_actor = tf.get_variable('b2_actor', shape=[64]) +W3_actor = tf.get_variable('W3_actor', shape=[64, 2]) +b3_actor = tf.get_variable('b3_actor', shape=[2]) +hidden1_actor = tf.nn.relu(tf.matmul(observation_placeholder, W1_actor) + b1_actor) +hidden2_actor = tf.nn.relu(tf.matmul(hidden1_actor, W2_actor) + b2_actor) +scores = tf.nn.relu(tf.matmul(hidden2_actor, W3_actor) + b3_actor) +probs = tf.nn.softmax(scores) +discount_placeholder = tf.placeholder(tf.float32) +gain_placeholder = tf.placeholder(tf.float32) +action_placeholder = tf.placeholder(tf.float32, shape=[None, 2]) +log_probs = tf.log(probs) * action_placeholder +grads_actor = tf.gradients(log_probs, [W1_actor, b1_actor, W2_actor, b2_actor, W3_actor, b3_actor]) +update_W1_actor = W1_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[0]) +update_b1_actor = b1_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[1]) +update_W2_actor = W2_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[2]) +update_b2_actor = b2_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[3]) +update_W3_actor = W3_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[4]) +update_b3_actor = b3_actor.assign_add(actor_learning_rate * discount_placeholder * gain_placeholder * grads_actor[5]) +W1_critic = tf.get_variable('W1_critic', shape=[4, 64]) +b1_critic = tf.get_variable('b1_critic', shape=[64]) +W2_critic = tf.get_variable('W2_critic', shape=[64, 64]) +b2_critic = tf.get_variable('b2_critic', shape=[64]) +W3_critic = tf.get_variable('W3_critic', shape=[64, 1]) +b3_critic = tf.get_variable('b3_critic', shape=[1]) +hidden1_critic = tf.nn.relu(tf.matmul(observation_placeholder, W1_critic) + b1_critic) +hidden2_critic = tf.nn.relu(tf.matmul(hidden1_critic, W2_critic) + b2_critic) +values = tf.matmul(hidden2_critic, W3_critic) + b3_critic +grads_critic = tf.gradients(values, [W1_critic, b1_critic, W2_critic, b2_critic, W3_critic, b3_critic]) +update_W1_critic = W1_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[0]) +update_b1_critic = b1_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[1]) +update_W2_critic = W2_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[2]) +update_b2_critic = b2_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[3]) +update_W3_critic = W3_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[4]) +update_b3_critic = b3_critic.assign_add(critic_learning_rate * gain_placeholder * grads_critic[5]) + + +def actor_network(observation, session, verbose=False): + probabilities = session.run(probs, feed_dict={observation_placeholder: [observation]}) + if verbose: + print(probabilities) + if random.random() <= probabilities[0][0]: + return 0 + else: + return 1 + + +def update_actor_gradients(discount, gain, observation, action, session, verbose=False): + if action == 0: + mask = [[1, 0]] + else: + mask = [[0, 1]] + result = session.run([probs, log_probs, grads_actor, update_W1_actor, update_b1_actor, update_W2_actor, update_b2_actor, update_W3_actor, update_b3_actor], feed_dict={observation_placeholder: [observation], discount_placeholder: discount, gain_placeholder: gain, action_placeholder: mask}) + if verbose: + print(result[2]) + + +def update_critic_gradients(gain, observation, session, verbose=False): + result = session.run([grads_critic, update_W1_critic, update_b1_critic, update_W2_critic, update_b2_critic, update_W3_critic, update_b3_critic], feed_dict={observation_placeholder: [observation], gain_placeholder: gain}) + if verbose: + print(result[0]) + + +init = tf.global_variables_initializer() +with tf.Session() as sess: + sess.run(init) + env = gym.make('CartPole-v1') + print('Training...') + for episode in range(n_episode): + observation = env.reset() + reward = 0 + done = False + total_reward = 0.0 + memory = [] + discount = 1.0 + step = 0 + while not done: + action = actor_network(observation, sess, verbose=actor_network_verbose) + next_observation, next_reward, next_done, _ = env.step(action) + if next_done: + next_reward = -100 + gain_with_critic = next_reward - sess.run(values, feed_dict={observation_placeholder: [observation]})[0][0] + if not next_done: + gain_with_critic += gamma * sess.run(values, feed_dict={observation_placeholder: [next_observation]})[0][0] + update_actor_gradients(discount=discount, gain=gain_with_critic, observation=observation, action=action, session=sess, verbose=update_gradients_verbose) + update_critic_gradients(gain=gain_with_critic, observation=observation, session=sess, verbose=update_gradients_verbose) + discount *= gamma + total_reward += next_reward + observation, reward, done = next_observation, next_reward, next_done + step += 1 + print('Episode {0} Reward: {1}'.format(episode, total_reward)) + print('Training done!') + print() + input('Press Enter to start testing!') + print('Testing...') + rewards_sum = 0.0 + for episode in range(num_tests): + observation = env.reset() + reward = 0 + done = False + total_reward = 0.0 + while not done: + env.render() + action = actor_network(observation, sess) + next_observation, next_reward, next_done, _ = env.step(action) + total_reward += next_reward + observation, reward, done = next_observation, next_reward, next_done + print('Reward:', total_reward) + rewards_sum += total_reward + print('Testing done!') + print('Average reward:', rewards_sum / num_tests) + env.close() diff --git a/dataset/preprocessed/626.txt b/dataset/preprocessed/626.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/626.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/627.txt b/dataset/preprocessed/627.txt new file mode 100644 index 0000000..54a7394 --- /dev/null +++ b/dataset/preprocessed/627.txt @@ -0,0 +1,230 @@ +""" +Created on Tue Aug 22 13:23:07 2017 +Chapter 5 +basic of tensorflow +@author: gihun +""" +import numpy as np +import tensorflow as tf +np.set_printoptions(linewidth=100, threshold=2000) +""" +basic concept and principle +@ graph and session +graph: define skeleton "cast" (only sturcture declaration) for "metal" (real memory in GPU) + building graph is "complile process of the structure" +session: object managing "metal" (real memory in GPU) + calling session.run is "runtime stage of the structure + +@ there is 3 type of tensor object +1. placeholder : interface for CPU --> GPU memory copy. it serves as gateway from numpy value to tensor value + placeholder is used for input. you must "feed" the input value (numpy value) to placebolder + when you evaluate tensor that depnds on the placeholder. +2. Variable : it holds its own GPU memory. it is not temporary. + Variable is used as weights in network. It is the only we can save after training. +3. Tensor : it holds temporary GPU memory. tensor exists in order to represent intermediate result + The type of all intermediate result is tensor. +ex) + a : placeholder + b : variable + c = a+b --> c is tensor +""" +tf.reset_default_graph() +tf.ones([3, 3]) +tf.zeros([3, 3]) +x = tf.placeholder(name='x', dtype=np.float32, shape=[3, 3]) +w = tf.get_variable(name='w', initializer=np.ones([3, 3], dtype=np.float32)) +y = x + w +sess = tf.Session() +sess.run(tf.global_variables_initializer()) +val = sess.run(y, feed_dict={x: np.ones([3, 3], dtype=np.float32)}) +print(val) + + +class Eval: + + def __init__(self): + self.sess = tf.Session() + + def global_var_init(self): + self.sess.run(tf.global_variables_initializer()) + + def __call__(self, x, feed_dict=None): + return self.sess.run(x, feed_dict) + + +tf.reset_default_graph() +x = tf.placeholder(name='x', dtype=tf.float32, shape=[1, 3, 3, 1]) +feed_dict = {x: np.ones([1, 3, 3, 1], np.float32)} +e = Eval() +print(x.get_shape().as_list()) +print(tf.shape(x)) +print(e(tf.shape(x), feed_dict)) +tf.reset_default_graph() +x2 = tf.placeholder(name='x2', dtype=tf.float32, shape=[None, 3, 3, 1]) +feed_dict = {x2: np.ones([2, 3, 3, 1], np.float32)} +e = Eval() +print(x2.get_shape().as_list()) +print(tf.shape(x2)) +print(e(tf.shape(x2), feed_dict)) +tf.reset_default_graph() +x3 = tf.placeholder(name='x3', dtype=tf.float32, shape=[None, None, None, None]) +feed_dict = {x3: np.ones([4, 10, 10, 3], np.float32)} +e = Eval() +print(x3.get_shape().as_list()) +print(tf.shape(x3)) +print(e(tf.shape(x3), feed_dict)) +tf.reset_default_graph() +x = tf.placeholder(name='x', dtype=tf.float32, shape=[3, 3, 3, 1]) +x_ = np.reshape(np.arange(0, 27, 1, np.float32), [3, 3, 3, 1]) +feed_dict = {x: x_} +e = Eval() +print(e(tf.reduce_sum(x, reduction_indices=[1, 2]), feed_dict)) +print(e(tf.reduce_sum(x), feed_dict)) +print(np.sum(x_)) +print(e(tf.reduce_sum(x, axis=0), feed_dict)) +print(np.sum(x_, axis=0)) +print(e(tf.reduce_mean(x), feed_dict)) +print(np.mean(x_)) +print(e(tf.reduce_mean(x, axis=0, keep_dims=True), feed_dict)) +print(np.mean(x_, axis=0, keepdims=True)) +print(e(tf.reduce_prod(x, axis=0), feed_dict)) +print(np.prod(x_, axis=0)) +tf.reset_default_graph() +x = tf.placeholder(name='x', dtype=tf.float32, shape=[3, 3]) +x_ = np.reshape(np.arange(0, 9, 1, np.float32), [3, 3]) +feed_dict = {x: x_} +e = Eval() +print(x_) +print(e(x, feed_dict)) +print(e(tf.reshape(x, [9]), feed_dict)) +print(np.reshape(x_, [9])) +print(e(tf.tile(x, [2, 2]), feed_dict)) +print(tf.tile(x, [2, 2]).get_shape().as_list()) +print(np.tile(x_, [2, 2])) +tf.reset_default_graph() +x = tf.placeholder(name='x', dtype=tf.float32, shape=[3, 3]) +y = tf.placeholder(name='x', dtype=tf.float32, shape=[3, 3]) +x_ = np.reshape(np.arange(0, 9, 1, np.float32), [3, 3]) +y_ = np.reshape(np.arange(0, 9, 1, np.float32), [3, 3]) +feed_dict = {x: x_, y: y_} +e = Eval() +print(x_) +print(y_) +print(e(tf.concat([x, y], axis=1), feed_dict)) +print(np.concatenate([x_, y_], axis=1)) +print(e(tf.stack([x, y], axis=0), feed_dict)) +print(np.stack([x_, y_], axis=0)) +print(e(tf.unstack(x, axis=0), feed_dict)) +for a in e(tf.unstack(x, axis=0), feed_dict): + print(a) +print(e(tf.split(x, 3, axis=0), feed_dict)) +for a in e(tf.split(x, 3, axis=0), feed_dict): + print(a) +print(np.split(x_, [1, 2], axis=0)) +tf.reset_default_graph() +x = tf.placeholder(name='x', dtype=tf.float32, shape=[None, None, None, 1]) +w = tf.placeholder(name='w', dtype=tf.float32, shape=[3, 3, 1, 3]) +b = tf.placeholder(name='b', dtype=tf.float32, shape=[3]) +conv = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME', name='conv') +add = conv + b +relu = tf.nn.relu(add, name='relu') +pool = tf.nn.max_pool(relu, [1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool') +e = Eval() +x_ = np.tile(-np.identity(3, np.float32), [3, 3])[(None), :, :, (None)] +x_.shape +w_ = np.array([[1, 1, 1], [0, 1, 0], [0, 0, 0]], np.float32)[:, :, (None), (None)] +w_ = np.concatenate([w_] * 3, axis=3) +w_.shape +feed_dict = {x: x_, w: w_, b: np.array([1.5] * 3, np.float32)} +print(x_[(0), :, :, (0)]) +print(w_[:, :, (0), (0)]) +print(e(tf.shape(x), feed_dict)) +print(e(conv, feed_dict)[(0), :, :, (0)]) +print(e(add, feed_dict)[(0), :, :, (0)]) +print(e(relu, feed_dict)[(0), :, :, (0)]) +print(e(pool, feed_dict)[(0), :, :, (0)]) +print(relu.get_shape()) +print(e(tf.shape(relu), feed_dict)) +print(pool.get_shape()) +print(e(tf.shape(pool), feed_dict)) +tf.reset_default_graph() +x = tf.placeholder(name='x', dtype=tf.float32, shape=[None, 10]) +w = tf.placeholder(name='w', dtype=tf.float32, shape=[10, 10]) +b = tf.placeholder(name='b', dtype=tf.float32, shape=[10]) +full = tf.add(tf.matmul(x, w), b, name='full') +x_ = np.ones([3, 10], np.float32) +w_ = np.ones([10, 10], np.float32) +feed_dict = {x: x_, w: w_, b: [0.5] * 10} +e = Eval() +print(x_) +print(w_) +print(e(full, feed_dict)) +tf.reset_default_graph() +x = tf.placeholder(name='x', dtype=tf.float32, shape=[None, None, None, 1]) +w = tf.placeholder(name='w', dtype=tf.float32, shape=[3, 3, 3, 1]) +b = tf.placeholder(name='b', dtype=tf.float32, shape=[3]) +conv = tf.nn.conv2d_transpose(x, w, strides=[1, 1, 1, 1], output_shape=[1, 5, 5, 3], padding='SAME', name='conv') +add = conv + b +relu = tf.nn.relu(add, name='relu') +e = Eval() +x_ = -np.identity(5, np.float32)[(None), :, :, (None)] +w_ = np.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]], np.float32)[:, :, (None), (None)] +w_ = np.concatenate([w_] * 3, axis=2) +feed_dict = {x: x_, w: w_, b: np.array([0.5] * 3, np.float32)} +print(x_[(0), :, :, (0)]) +print(w_[:, :, (0), (0)]) +print(e(conv, feed_dict)[(0), :, :, (0)]) +print(e(add, feed_dict)[(0), :, :, (0)]) +print(e(relu, feed_dict)[(0), :, :, (0)]) +""" +conv2d_transpose needs output_shape and it should be static shape. +so I recomend you not to use conv2d_transpose, but conv2d. +""" +tf.reset_default_graph() +x3 = tf.placeholder(name='x3', dtype=tf.float32, shape=[None, None, None, None, 1]) +w3 = tf.placeholder(name='w3', dtype=tf.float32, shape=[3, 3, 3, 1, 3]) +b3 = tf.placeholder(name='b3', dtype=tf.float32, shape=[3]) +conv3d = tf.nn.conv3d(x3, w3, strides=[1, 1, 1, 1, 1], padding='SAME', name='conv3d') +add = conv3d + b3 +relu = tf.nn.relu(add, name='relu') +e = Eval() +x_ = -np.zeros([5, 5, 5])[(None), :, :, :, (None)] +for i in range(5): + x_[0, i, i, i, 0] = -1 +w_ = np.zeros([3, 3, 3])[:, :, :, (None), (None)] +w_[1, 1, 1, 0, 0] = 1 +w_[0, 1, 1, 0, 0] = 1 +w_ = np.concatenate([w_] * 3, axis=4) +feed_dict = {x3: x_, w3: w_, b3: np.array([0.5] * 3, np.float32)} +print(x_[(0), :, :, :, (0)]) +print(w_[:, :, :, (0), (0)]) +print(e(conv3d, feed_dict)[(0), :, :, :, (0)]) +print(e(add, feed_dict)[(0), :, :, :, (0)]) +print(e(relu, feed_dict)[(0), :, :, :, (0)]) +tf.reset_default_graph() +x = tf.Variable(np.ones([3, 3], dtype=np.float32)) +e = Eval() +e.global_var_init() +e(x) +""" +variable must initialize! if not, error : Attemptimg to use uninitialized value Variable +On the other hand, in case of placeholder, we must use it with feed_dict(like initializing) +""" +tf.shape +y.get_shape() +y.get_shape().as_list() +tf.reduce_sum +tf.reduce_mean +tf.reduce_prod +tf.reshape +tf.tile +tf.concat +tf.stack +tf.unstack +tf.nn.relu +tf.nn.conv2d +tf.nn.conv2d_transpose +tf.nn.conv3d +tf.nn.conv3d_transpose +tf.nn.max_pool +tf.nn.moments diff --git a/dataset/preprocessed/63.txt b/dataset/preprocessed/63.txt new file mode 100644 index 0000000..335455f --- /dev/null +++ b/dataset/preprocessed/63.txt @@ -0,0 +1,254 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import math as math +pickle_file = 'data/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +lambd = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + reg_loss = tf.nn.l2_loss(weights) + loss = tf.reduce_mean(loss + lambd * reg_loss) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_units = 1024 +lambd = 0.01 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + weights2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + biases1 = tf.Variable(tf.zeros([hidden_units])) + biases2 = tf.Variable(tf.zeros([num_labels])) + parameters = {'W1': weights1, 'b1': biases1, 'W2': weights2, 'b2': biases2} + + def forward_propagation(tf_train_dataset, parameters): + weights1 = parameters['W1'] + weights2 = parameters['W2'] + biases1 = parameters['b1'] + biases2 = parameters['b2'] + Z1 = tf.matmul(tf_train_dataset, weights1) + biases1 + A1 = tf.nn.relu(Z1) + logits = tf.matmul(A1, weights2) + biases2 + return logits + logits = forward_propagation(tf_train_dataset, parameters) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + reg_loss = tf.nn.l2_loss(parameters['W1']) + tf.nn.l2_loss(parameters['W2']) + loss = tf.reduce_mean(loss + lambd * reg_loss) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(forward_propagation(tf_valid_dataset, parameters)) + test_prediction = tf.nn.softmax(forward_propagation(tf_test_dataset, parameters)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_units = 1024 +lambd = 0.01 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_units])) + weights2 = tf.Variable(tf.truncated_normal([hidden_units, num_labels])) + biases1 = tf.Variable(tf.zeros([hidden_units])) + biases2 = tf.Variable(tf.zeros([num_labels])) + parameters = {'W1': weights1, 'b1': biases1, 'W2': weights2, 'b2': biases2} + + def forward_propagation(tf_train_dataset, parameters): + weights1 = parameters['W1'] + weights2 = parameters['W2'] + biases1 = parameters['b1'] + biases2 = parameters['b2'] + Z1 = tf.matmul(tf_train_dataset, weights1) + biases1 + A1 = tf.nn.relu(Z1) + loss = tf.nn.dropout(A1, keep_prob=0.5, noise_shape=None, seed=8) + logits = tf.matmul(A1, weights2) + biases2 + return logits + logits = forward_propagation(tf_train_dataset, parameters) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(forward_propagation(tf_valid_dataset, parameters)) + test_prediction = tf.nn.softmax(forward_propagation(tf_test_dataset, parameters)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +beta = 0.001 +hidden_nodes_1 = 1024 +hidden_nodes_2 = int(hidden_nodes_1 * 0.5) +hidden_nodes_3 = int(hidden_nodes_1 * np.power(0.5, 2)) +hidden_nodes_4 = int(hidden_nodes_1 * np.power(0.5, 3)) +hidden_nodes_5 = int(hidden_nodes_1 * np.power(0.5, 4)) +graph = tf.Graph() +with graph.as_default(): + """Input Data""" + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + """Variables""" + weights_1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes_1], stddev=math.sqrt(2.0 / (image_size * image_size)))) + biases_1 = tf.Variable(tf.zeros([hidden_nodes_1])) + weights_2 = tf.Variable(tf.truncated_normal([hidden_nodes_1, hidden_nodes_2], stddev=math.sqrt(2.0 / hidden_nodes_1))) + biases_2 = tf.Variable(tf.zeros([hidden_nodes_2])) + weights_3 = tf.Variable(tf.truncated_normal([hidden_nodes_2, hidden_nodes_3], stddev=math.sqrt(2.0 / hidden_nodes_2))) + biases_3 = tf.Variable(tf.zeros([hidden_nodes_3])) + weights_4 = tf.Variable(tf.truncated_normal([hidden_nodes_3, hidden_nodes_4], stddev=math.sqrt(2.0 / hidden_nodes_3))) + biases_4 = tf.Variable(tf.zeros([hidden_nodes_4])) + weights_5 = tf.Variable(tf.truncated_normal([hidden_nodes_4, hidden_nodes_5], stddev=math.sqrt(2.0 / hidden_nodes_4))) + biases_5 = tf.Variable(tf.zeros([hidden_nodes_5])) + weights_6 = tf.Variable(tf.truncated_normal([hidden_nodes_5, num_labels], stddev=math.sqrt(2.0 / hidden_nodes_5))) + biases_6 = tf.Variable(tf.zeros([num_labels])) + """Training computation""" + logits_1 = tf.matmul(tf_train_dataset, weights_1) + biases_1 + hidden_layer_1 = tf.nn.relu(logits_1) + keep_prob = tf.placeholder('float') + hidden_layer_1_dropout = tf.nn.dropout(hidden_layer_1, keep_prob) + logits_2 = tf.matmul(hidden_layer_1_dropout, weights_2) + biases_2 + hidden_layer_2 = tf.nn.relu(logits_2) + hidden_layer_2_dropout = tf.nn.dropout(hidden_layer_2, keep_prob) + logits_3 = tf.matmul(hidden_layer_2_dropout, weights_3) + biases_3 + hidden_layer_3 = tf.nn.relu(logits_3) + hidden_layer_3_dropout = tf.nn.dropout(hidden_layer_3, keep_prob) + logits_4 = tf.matmul(hidden_layer_3_dropout, weights_4) + biases_4 + hidden_layer_4 = tf.nn.relu(logits_4) + hidden_layer_4_dropout = tf.nn.dropout(hidden_layer_4, keep_prob) + logits_5 = tf.matmul(hidden_layer_4_dropout, weights_5) + biases_5 + hidden_layer_5 = tf.nn.relu(logits_5) + hidden_layer_5_dropout = tf.nn.dropout(hidden_layer_5, keep_prob) + logits_6 = tf.matmul(hidden_layer_5_dropout, weights_6) + biases_6 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_6, tf_train_labels)) + """Optimizer""" + optimizer = tf.train.AdamOptimizer(0.005).minimize(loss) + train_prediction = tf.nn.softmax(logits_6) + valid_logits_1 = tf.matmul(tf_valid_dataset, weights_1) + biases_1 + valid_relu_1 = tf.nn.relu(valid_logits_1) + valid_logits_2 = tf.matmul(valid_relu_1, weights_2) + biases_2 + valid_relu_2 = tf.nn.relu(valid_logits_2) + valid_logits_3 = tf.matmul(valid_relu_2, weights_3) + biases_3 + valid_relu_3 = tf.nn.relu(valid_logits_3) + valid_logits_4 = tf.matmul(valid_relu_3, weights_4) + biases_4 + valid_relu_4 = tf.nn.relu(valid_logits_4) + valid_logits_5 = tf.matmul(valid_relu_4, weights_5) + biases_5 + valid_relu_5 = tf.nn.relu(valid_logits_5) + valid_logits_6 = tf.matmul(valid_relu_5, weights_6) + biases_6 + valid_prediction = tf.nn.softmax(valid_logits_6) + test_logits_1 = tf.matmul(tf_test_dataset, weights_1) + biases_1 + test_relu_1 = tf.nn.relu(test_logits_1) + test_logits_2 = tf.matmul(test_relu_1, weights_2) + biases_2 + test_relu_2 = tf.nn.relu(test_logits_2) + test_logits_3 = tf.matmul(test_relu_2, weights_3) + biases_3 + test_relu_3 = tf.nn.relu(test_logits_3) + test_logits_4 = tf.matmul(test_relu_3, weights_4) + biases_4 + test_relu_4 = tf.nn.relu(test_logits_4) + test_logits_5 = tf.matmul(test_relu_4, weights_5) + biases_5 + test_relu_5 = tf.nn.relu(test_logits_5) + test_logits_6 = tf.matmul(test_relu_5, weights_6) + biases_6 + test_prediction = tf.nn.softmax(test_logits_6) +num_steps = 15000 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step {}: {}'.format(step, l)) + print('Minibatch accuracy: {:.1f}'.format(accuracy(predictions, batch_labels))) + print('Validation accuracy: {:.1f}'.format(accuracy(valid_prediction.eval(), valid_labels))) + print('Test accuracy: {:.1f}'.format(accuracy(test_prediction.eval(), test_labels))) diff --git a/dataset/preprocessed/630.txt b/dataset/preprocessed/630.txt new file mode 100644 index 0000000..d3f9fed --- /dev/null +++ b/dataset/preprocessed/630.txt @@ -0,0 +1,160 @@ +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import logz +import time +import math + + +class Config(object): + n_features = 11 + n_classes = 3 + dropout = 0.5 + hidden_size_1 = 128 + hidden_size_2 = 256 + hidden_size_3 = 64 + batch_size = 256 + lr = 0.0005 + itera = 20 + train_itera = 20 + envname = 'Hopper-v1' + max_steps = 1000 + + +class NN(object): + + def add_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_features), name='input') + self.labels_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_classes), name='label') + self.dropout_placeholder = tf.placeholder(tf.float32, name='drop') + self.is_training = tf.placeholder(tf.bool) + + def create_feed_dict(self, inputs_batch, labels_batch=None, dropout=1, is_training=False): + if labels_batch is None: + feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + else: + feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + return feed_dict + + def add_prediction_op(self): + self.global_step = tf.Variable(0) + with tf.name_scope('layer1'): + hidden1 = tf.contrib.layers.fully_connected(self.input_placeholder, num_outputs=Config.hidden_size_1, activation_fn=tf.nn.relu) + with tf.name_scope('layer2'): + hidden2 = tf.contrib.layers.fully_connected(hidden1, num_outputs=Config.hidden_size_2, activation_fn=tf.nn.relu) + with tf.name_scope('layer3'): + hidden3 = tf.contrib.layers.fully_connected(hidden2, num_outputs=Config.hidden_size_3, activation_fn=tf.nn.relu) + with tf.name_scope('output'): + pred = tf.contrib.layers.fully_connected(hidden3, num_outputs=Config.n_classes, activation_fn=None) + return pred + + def add_loss_op(self, pred): + loss = tf.losses.mean_squared_error(predictions=pred, labels=self.labels_placeholder) + tf.summary.scalar('loss', loss) + return loss + + def add_training_op(self, loss): + extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + with tf.control_dependencies(extra_update_ops): + learning_rate = tf.train.exponential_decay(Config.lr, self.global_step, 1000, 0.8, staircase=True) + train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=self.global_step) + return train_op + + def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + rs, _, loss = sess.run([merged, self.train_op, self.loss], feed_dict=feed) + train_writer.add_summary(rs, i) + return loss + + def __init__(self, config): + self.config = config + self.build() + + def fit(self, sess, train_x, train_y): + loss = self.train_on_batch(sess, train_x, train_y) + + def build(self): + with tf.name_scope('inputs'): + self.add_placeholders() + with tf.name_scope('predict'): + self.pred = self.add_prediction_op() + with tf.name_scope('loss'): + self.loss = self.add_loss_op(self.pred) + with tf.name_scope('train'): + self.train_op = self.add_training_op(self.loss) + + def get_pred(self, sess, inputs_batch): + feed = self.create_feed_dict(inputs_batch, dropout=1, is_training=False) + p = sess.run(self.pred, feed_dict=feed) + return p + + +def load(path): + all = np.load(path) + X = all['arr_0'] + y = all['arr_1'] + y1 = y.reshape(y.shape[0], y.shape[2]) + return X, y1 + + +def main(): + PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_BC_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + X_train, y_train = load(train_path) + print('train size :', X_train.shape, y_train.shape) + print('start training') + with tf.Graph().as_default(): + config = Config() + nn = NN(config) + init = tf.global_variables_initializer() + saver = tf.train.Saver(max_to_keep=10, keep_checkpoint_every_n_hours=0.5) + shuffle_batch_x, shuffle_batch_y = tf.train.shuffle_batch([X_train, y_train], batch_size=Config.batch_size, capacity=10000, min_after_dequeue=5000, enqueue_many=True) + with tf.Session() as session: + merged = tf.summary.merge_all() + train_writer = tf.summary.FileWriter(train_log_path, session.graph) + session.run(init) + coord = tf.train.Coordinator() + threads = tf.train.start_queue_runners(session, coord) + for j in tqdm.tqdm(range(Config.itera)): + i = 0 + try: + for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + batch_x, batch_y = session.run([shuffle_batch_x, shuffle_batch_y]) + loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + i += 1 + if i % 1000 == 0: + print('step:', i, 'loss:', loss) + saver.save(session, os.path.join(PROJECT_ROOT, 'model/model_ckpt'), global_step=i) + except tf.errors.OutOfRangeError: + print('') + finally: + coord.request_stop() + coord.join(threads) + env = gym.make(Config.envname) + rollouts = 20 + returns = [] + for _ in range(rollouts): + obs = env.reset() + done = False + totalr = 0.0 + steps = 0 + while not done: + action = nn.get_pred(session, obs[(None), :]) + obs, r, done, _ = env.step(action) + totalr += r + steps += 1 + if steps >= Config.max_steps: + break + returns.append(totalr) + logz.log_tabular('Iteration', j) + logz.log_tabular('AverageReturn', np.mean(returns)) + logz.log_tabular('StdReturn', np.std(returns)) + logz.dump_tabular() + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/631.txt b/dataset/preprocessed/631.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/631.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/632.txt b/dataset/preprocessed/632.txt new file mode 100644 index 0000000..253582f --- /dev/null +++ b/dataset/preprocessed/632.txt @@ -0,0 +1,127 @@ +import sys +import numpy as np +import tensorflow as tf +from sklearn import cross_validation +from sklearn.cross_validation import KFold +from sklearn import metrics + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +class TextCNN(object): + + def __init__(self, train_dataset, train_labels, valid_dataset, valid_labels, embeddings, vocabulary, l2_reg_lambda, num_steps, batch_size, num_filters, filter_sizes_1, filter_sizes_2, filter_sizes_3, dropout_keep_prob, lexical, shuffling): + vocab_size = len(vocabulary) + sequence_length = train_dataset.shape[1] + train_size = train_dataset.shape[0] + num_classes = 7 + filter_sizes = [filter_sizes_1, filter_sizes_2, filter_sizes_3] + num_filters_total = num_filters * len(filter_sizes) + embedding_size = embeddings.shape[1] + embeddings_number = embeddings.shape[0] + graph = tf.Graph() + with graph.as_default(): + tf.set_random_seed(10) + input_x = tf.placeholder(tf.int32, shape=[batch_size, sequence_length]) + input_y = tf.placeholder(tf.int32, shape=[batch_size, num_classes]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_argmax_dataset = tf.constant(valid_dataset) + reg_coef = tf.placeholder(tf.float32) + l2_loss = tf.constant(0.0) + weights_conv = [tf.Variable(tf.truncated_normal([filter_size, embedding_size, 1, num_filters], stddev=tf.sqrt(2.0 / (filter_size * embedding_size)), seed=filter_size + i * num_filters)) for i, filter_size in enumerate(filter_sizes)] + biases_conv = [tf.Variable(tf.constant(0.01, shape=[num_filters])) for filter_size in filter_sizes] + weight_output = tf.Variable(tf.truncated_normal([num_filters_total, num_classes], stddev=tf.sqrt(2.0 / (num_filters_total + num_classes)), seed=0)) + bias_output = tf.Variable(tf.constant(0.01, shape=[num_classes])) + embeddings_const = tf.placeholder(tf.float32, shape=[embeddings_number, embedding_size]) + embeddings_tuned = tf.Variable(embeddings_const) + embedded_chars = tf.nn.embedding_lookup(embeddings_tuned, input_x) + embedded_chars_expanded = tf.expand_dims(embedded_chars, -1) + embedded_chars_valid = tf.nn.embedding_lookup(embeddings_tuned, tf_valid_dataset) + embedded_chars_expanded_valid = tf.expand_dims(embedded_chars_valid, -1) + embedded_chars_argmax = tf.nn.embedding_lookup(embeddings_tuned, tf_argmax_dataset) + embedded_chars_expanded_argmax = tf.expand_dims(embedded_chars_argmax, -1) + + def model(data, dropout_prob): + pooled_outputs = [] + for i, filter_size in enumerate(filter_sizes): + conv = tf.nn.conv2d(data, weights_conv[i], strides=[1, 1, 1, 1], padding='VALID') + h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + pooled = tf.nn.max_pool(h, ksize=[1, sequence_length - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + pooled_outputs.append(pooled) + h_pool = tf.concat(3, pooled_outputs) + h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) + h_drop = tf.nn.dropout(h_pool_flat, dropout_prob) + scores = tf.nn.xw_plus_b(h_drop, weight_output, bias_output) + return scores + + def model_argmax(data, dropout_prob): + argmaxs = [] + maximums = [] + pooled_outputs = [] + for i, filter_size in enumerate(filter_sizes): + conv = tf.nn.conv2d(data, weights_conv[i], strides=[1, 1, 1, 1], padding='VALID') + h = tf.nn.relu(tf.nn.bias_add(conv, biases_conv[i])) + maximum = tf.reduce_max(h, tf.to_int32(1)) + maximums.append(maximum) + argmax = tf.argmax(h, tf.to_int32(1)) + argmaxs.append(argmax) + return argmaxs, maximums + scores = model(embedded_chars_expanded, dropout_keep_prob) + train_prediction = tf.nn.softmax(scores) + losses = tf.nn.softmax_cross_entropy_with_logits(scores, tf.cast(input_y, tf.float32)) + for i in range(len(weights_conv)): + l2_loss += tf.nn.l2_loss(weights_conv[i]) + l2_loss += tf.nn.l2_loss(weight_output) + loss = tf.reduce_mean(losses) + reg_coef * l2_loss + global_step = tf.Variable(0, trainable=False) + optimizer = tf.train.AdamOptimizer(0.0001).minimize(loss) + argmaxs, maximums = model_argmax(embedded_chars_expanded_argmax, 1.0) + maximum1 = maximums[0] + maximum2 = maximums[1] + maximum3 = maximums[2] + argmax1 = argmaxs[0] + argmax2 = argmaxs[1] + argmax3 = argmaxs[2] + valid_prediction = tf.nn.softmax(model(embedded_chars_expanded_valid, 1.0)) + with tf.Session(graph=graph) as session: + session.run(tf.initialize_all_variables(), feed_dict={embeddings_const: embeddings}) + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size] + batch_labels = train_labels[offset:offset + batch_size] + feed_dict = {input_x: batch_data, input_y: batch_labels, reg_coef: l2_reg_lambda, embeddings_const: embeddings} + _, l, predictions, embeddings = session.run([optimizer, loss, train_prediction, embeddings_tuned], feed_dict) + if not step % 100: + print('Minibatch loss at step', step, ':', l) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('\n') + maximum1 = session.run([maximum1], feed_dict={embeddings_const: embeddings}) + maximum1 = np.asarray(maximum1) + maximum2 = session.run([maximum2], feed_dict={embeddings_const: embeddings}) + maximum2 = np.asarray(maximum2) + maximum3 = session.run([maximum3], feed_dict={embeddings_const: embeddings}) + maximum3 = np.asarray(maximum3) + argmax1 = session.run([argmax1], feed_dict={embeddings_const: embeddings}) + argmax1 = np.asarray(argmax1) + argmax2 = session.run([argmax2], feed_dict={embeddings_const: embeddings}) + argmax2 = np.asarray(argmax2) + argmax3 = session.run([argmax3], feed_dict={embeddings_const: embeddings}) + argmax3 = np.asarray(argmax3) + np.save('argmax_filter_sizes_1_tuned.npy', argmax1) + np.save('argmax_filter_sizes_2_tuned.npy', argmax2) + np.save('argmax_filter_sizes_3_tuned.npy', argmax3) + np.save('maximum_filter_sizes_1_tuned.npy', maximum1) + np.save('maximum_filter_sizes_2_tuned.npy', maximum2) + np.save('maximum_filter_sizes_3_tuned.npy', maximum3) + self.valid_predictions = session.run([valid_prediction], feed_dict={embeddings_const: embeddings}) + self.valid_predictions = np.asarray(self.valid_predictions).reshape(valid_labels.shape) + predictions_label = np.argmax(self.valid_predictions, 1) + labels = ['coffeemachine', 'cutlery', 'microwave', 'toaster', 'trashcan', 'vacuum', 'washer'] + self.prediction_labels_char = [labels[i] for i in predictions_label] + self.prediction_labels_char = np.asarray(self.prediction_labels_char) + np.save('gold_labels_tuned.npy', predictions_label) + self.valid_accuracy = accuracy(self.valid_predictions, np.asarray(valid_labels)) + self.embeddings_final = embeddings diff --git a/dataset/preprocessed/633.txt b/dataset/preprocessed/633.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/633.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/635.txt b/dataset/preprocessed/635.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/635.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/636.txt b/dataset/preprocessed/636.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/636.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/637.txt b/dataset/preprocessed/637.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/637.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/638.txt b/dataset/preprocessed/638.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/638.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/639.txt b/dataset/preprocessed/639.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/639.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/64.txt b/dataset/preprocessed/64.txt new file mode 100644 index 0000000..12a2272 --- /dev/null +++ b/dataset/preprocessed/64.txt @@ -0,0 +1,116 @@ +""" import your model here """ +import joker as act_tf +import tensorflow as ans_tf +""" check the correctness of your model by comparing with tensorflow """ +import numpy as np +rrr = np.random.normal(size=(3 * 4 * 4 * 2,)).astype(np.float32) +sss = np.random.normal(size=(3 * 3 * 2 * 5,)).astype(np.float32) + + +def test_forward_conv(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.placeholder(tf.float32, shape=[3, 3, 2, 5]) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + res = sess.run(conv1, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2), w: np.arange(3 * 3 * 2 * 5).reshape(3, 3, 2, 5)}) + return res + + +def test_forward_conv2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.placeholder(tf.float32, shape=[3, 3, 2, 5]) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + res = sess.run(conv1, feed_dict={x: rrr.reshape(3, 4, 4, 2), w: sss.reshape(3, 3, 2, 5)}) + return res + + +def test_backward_conv(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(np.arange(3 * 3 * 2 * 5, dtype=np.float32).reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2)}) + res = w.eval() + return res + + +def test_backward_conv2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(sss.reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + sess.run(tf.global_variables_initializer()) + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + grad_W, = tf.gradients(loss, [w]) + res = grad_W.eval(feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_backward_conv3(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + w = tf.Variable(sss.reshape(3, 3, 2, 5)) + conv1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') + sess.run(tf.global_variables_initializer()) + loss = tf.reduce_sum(conv1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + grad_W, = tf.gradients(loss, [x]) + res = grad_W.eval(feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_forward_maxpool(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + res = sess.run(pool1, feed_dict={x: np.arange(3 * 4 * 4 * 2).reshape(3, 4, 4, 2)}) + return res + + +def test_forward_maxpool2(tf): + with tf.Session() as sess: + x = tf.placeholder(tf.float32, shape=[3, 4, 4, 2]) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + res = sess.run(pool1, feed_dict={x: rrr.reshape(3, 4, 4, 2)}) + return res + + +def test_backward_maxpool(tf): + with tf.Session() as sess: + x = tf.Variable(np.arange(3 * 4 * 4 * 2, dtype=np.float32).reshape(3, 4, 4, 2)) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + loss = tf.reduce_sum(pool1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op) + res = x.eval() + return res + + +def test_backward_maxpool2(tf): + with tf.Session() as sess: + x = tf.Variable(rrr.reshape(3, 4, 4, 2)) + pool1 = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + loss = tf.reduce_sum(pool1) + train_op = tf.train.GradientDescentOptimizer(1).minimize(loss) + sess.run(tf.global_variables_initializer()) + sess.run(train_op) + res = x.eval() + return res + + +if __name__ == '__main__': + testcases = [test_forward_conv, test_forward_conv2, test_backward_conv, test_backward_conv2, test_backward_conv3, test_forward_maxpool2, test_backward_maxpool2] + for item in testcases: + ans = item(ans_tf) + print() + print() + print(item) + res = item(act_tf) + np.testing.assert_allclose(res, ans, atol=0.01) + print('Pass') diff --git a/dataset/preprocessed/640.txt b/dataset/preprocessed/640.txt new file mode 100644 index 0000000..6f77697 --- /dev/null +++ b/dataset/preprocessed/640.txt @@ -0,0 +1,229 @@ +"""Tests for SparseSoftmaxCrossEntropyWithLogits op.""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import sys +import time +import numpy as np +import tensorflow as tf +from tensorflow.python.ops import gen_nn_ops +from tensorflow.python.ops import nn_ops +from tensorflow.python.ops import sparse_ops + + +class SparseXentTest(tf.test.TestCase): + + def _npXent(self, features, labels): + features = np.reshape(features, [-1, features.shape[-1]]) + labels = np.reshape(labels, [-1]) + batch_dim = 0 + class_dim = 1 + batch_size = features.shape[batch_dim] + e = np.exp(features - np.reshape(np.amax(features, axis=class_dim), [batch_size, 1])) + probs = e / np.reshape(np.sum(e, axis=class_dim), [batch_size, 1]) + labels_mat = np.zeros_like(probs).astype(probs.dtype) + labels_mat[np.arange(batch_size), labels] = 1.0 + bp = probs - labels_mat + l = -np.sum(labels_mat * np.log(probs + 1e-20), axis=1) + return l, bp + + def _testXent(self, np_features, np_labels): + np_loss, np_backprop = self._npXent(np_features, np_labels) + with self.test_session(use_gpu=True) as sess: + loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(np_features, np_labels) + tf_loss, tf_backprop = sess.run([loss, backprop]) + self.assertAllCloseAccordingToType(np_loss, tf_loss) + self.assertAllCloseAccordingToType(np_backprop, tf_backprop) + + def testSingleClass(self): + for label_dtype in (np.int32, np.int64): + with self.test_session(use_gpu=True) as sess: + loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(np.array([[1.0], [-1.0], [0.0]]).astype(np.float32), np.array([0, 0, 0]).astype(label_dtype)) + tf_loss, tf_backprop = sess.run([loss, backprop]) + self.assertAllClose([0.0, 0.0, 0.0], tf_loss) + self.assertAllClose([[0.0], [0.0], [0.0]], tf_backprop) + + def testInvalidLabel(self): + features = [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]] + labels = [4, 3, 0, -1] + if tf.test.is_built_with_cuda() and tf.test.is_gpu_available(): + with self.test_session(use_gpu=True) as sess: + loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(features, labels) + tf_loss, tf_backprop = sess.run([loss, backprop]) + self.assertAllClose([[np.nan] * 4, [0.25, 0.25, 0.25, -0.75], [-0.968, 0.087, 0.237, 0.6439], [np.nan] * 4], tf_backprop, rtol=0.001, atol=0.001) + self.assertAllClose([np.nan, 1.3862, 3.442, np.nan], tf_loss, rtol=0.001, atol=0.001) + with self.test_session(use_gpu=False) as sess: + loss, backprop = gen_nn_ops._sparse_softmax_cross_entropy_with_logits(features, labels) + with self.assertRaisesOpError('Received a label value of'): + sess.run([loss, backprop]) + + def testNpXent(self): + features = [[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]] + labels = [3, 0] + np_loss, np_backprop = self._npXent(np.array(features), np.array(labels)) + self.assertAllClose(np.array([[0.25, 0.25, 0.25, -0.75], [-0.968, 0.087, 0.237, 0.6439]]), np_backprop, rtol=0.001, atol=0.001) + self.assertAllClose(np.array([1.3862, 3.442]), np_loss, rtol=0.001, atol=0.001) + + def testShapeMismatch(self): + with self.test_session(use_gpu=True): + with self.assertRaisesRegexp(ValueError, '.*Rank mismatch:*'): + tf.nn.sparse_softmax_cross_entropy_with_logits([[0.0, 1.0], [2.0, 3.0], [2.0, 3.0]], [[0, 2]]) + + def testScalar(self): + with self.test_session(use_gpu=True): + with self.assertRaisesRegexp(ValueError, '.*Logits cannot be scalars*'): + tf.nn.sparse_softmax_cross_entropy_with_logits(tf.constant(1.0), tf.constant(0)) + + def testLabelsPlaceholderScalar(self): + with self.test_session(use_gpu=True): + labels = tf.placeholder(np.int32) + y = tf.nn.sparse_softmax_cross_entropy_with_logits([[7.0]], labels) + with self.assertRaisesOpError('labels must be 1-D'): + y.eval(feed_dict={labels: 0}) + + def testVector(self): + with self.test_session(use_gpu=True): + loss = tf.nn.sparse_softmax_cross_entropy_with_logits(tf.constant([1.0]), tf.constant(0)) + self.assertAllClose(0.0, loss.eval()) + + def testFloat(self): + for label_dtype in (np.int32, np.int64): + self._testXent(np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]).astype(np.float32), np.array([3, 0]).astype(label_dtype)) + + def testDouble(self): + for label_dtype in (np.int32, np.int64): + self._testXent(np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]).astype(np.float64), np.array([0, 3]).astype(label_dtype)) + + def testHalf(self): + for label_dtype in (np.int32, np.int64): + self._testXent(np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]).astype(np.float16), np.array([3, 0]).astype(label_dtype)) + + def testEmpty(self): + self._testXent(np.zeros((0, 3)), np.zeros((0,), dtype=np.int32)) + + def testGradient(self): + with self.test_session(use_gpu=True): + l = tf.constant([3, 0, 1], name='l') + f = tf.constant([0.1, 0.2, 0.3, 0.4, 0.1, 0.4, 0.9, 1.6, 0.1, 0.8, 2.7, 6.4], shape=[3, 4], dtype=tf.float64, name='f') + x = tf.nn.sparse_softmax_cross_entropy_with_logits(f, l, name='xent') + err = tf.test.compute_gradient_error(f, [3, 4], x, [3]) + print('cross entropy gradient err = ', err) + self.assertLess(err, 5e-08) + + def testSecondGradient(self): + images_placeholder = tf.placeholder(tf.float32, shape=(3, 2)) + labels_placeholder = tf.placeholder(tf.int32, shape=3) + weights = tf.Variable(tf.truncated_normal([2], stddev=1.0)) + weights_with_zeros = tf.pack([tf.zeros([2]), weights], axis=1) + logits = tf.matmul(images_placeholder, weights_with_zeros) + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels_placeholder) + loss = tf.reduce_mean(cross_entropy) + with self.assertRaisesRegexp(LookupError, '.*No gradient defined.*PreventGradient.*'): + _ = tf.hessians(loss, [weights]) + + def _testHighDim(self, features, labels): + np_loss, np_backprop = self._npXent(np.array(features), np.array(labels)) + np_loss = np.reshape(np_loss, np.array(labels).shape) + with self.test_session(use_gpu=True) as sess: + loss = tf.nn.sparse_softmax_cross_entropy_with_logits(features, labels) + backprop = loss.op.inputs[0].op.outputs[1] + tf_loss, tf_backprop = sess.run([loss, backprop]) + self.assertAllCloseAccordingToType(np_loss, tf_loss) + self.assertAllCloseAccordingToType(np_backprop, tf_backprop) + + def testHighDim(self): + features = [[[1.0, 1.0, 1.0, 1.0]], [[1.0, 2.0, 3.0, 4.0]]] + labels = [[3], [0]] + self._testHighDim(features, labels) + + def testHighDim2(self): + features = [[[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0]], [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]] + labels = [[3, 2], [0, 3]] + self._testHighDim(features, labels) + + def testScalarHandling(self): + with self.test_session(use_gpu=False) as sess: + with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, '.*labels must be 1-D.*'): + labels = tf.placeholder(tf.int32, shape=[None, 1]) + logits = tf.placeholder(tf.float32, shape=[None, 3]) + ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, tf.squeeze(labels)) + labels_v2 = np.zeros((1, 1), dtype=np.int32) + logits_v2 = np.random.randn(1, 3) + sess.run([ce], feed_dict={labels: labels_v2, logits: logits_v2}) + + +def _sparse_vs_dense_xent_benchmark_dense(labels, logits): + labels = tf.identity(labels) + logits = tf.identity(logits) + with tf.device('/cpu:0'): + batch_size = tf.shape(logits)[0] + num_entries = tf.shape(logits)[1] + length = batch_size * num_entries + labels += num_entries * tf.range(batch_size) + target = sparse_ops.sparse_to_dense(labels, tf.stack([length]), 1.0, 0.0) + target = tf.reshape(target, tf.stack([-1, num_entries])) + crossent = tf.nn.softmax_cross_entropy_with_logits(logits, target, name='SequenceLoss/CrossEntropy') + crossent_sum = tf.reduce_sum(crossent) + grads = tf.gradients([crossent_sum], [logits])[0] + return crossent_sum, grads + + +def _sparse_vs_dense_xent_benchmark_sparse(labels, logits): + labels = labels.astype(np.int64) + labels = tf.identity(labels) + logits = tf.identity(logits) + crossent = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name='SequenceLoss/CrossEntropy') + crossent_sum = tf.reduce_sum(crossent) + grads = tf.gradients([crossent_sum], [logits])[0] + return crossent_sum, grads + + +def sparse_vs_dense_xent_benchmark(batch_size, num_entries, use_gpu): + config = tf.ConfigProto() + config.allow_soft_placement = True + config.gpu_options.per_process_gpu_memory_fraction = 0.3 + labels = np.random.randint(num_entries, size=batch_size).astype(np.int32) + logits = np.random.randn(batch_size, num_entries).astype(np.float32) + + def _timer(sess, ops): + for _ in range(20): + sess.run(ops) + start = time.time() + for _ in range(20): + sess.run(ops) + end = time.time() + return (end - start) / 20.0 + with tf.Session(config=config) as sess: + if not use_gpu: + with tf.device('/cpu:0'): + ops = _sparse_vs_dense_xent_benchmark_dense(labels, logits) + else: + ops = _sparse_vs_dense_xent_benchmark_dense(labels, logits) + delta_dense = _timer(sess, ops) + with tf.Session(config=config) as sess: + if not use_gpu: + with tf.device('/cpu:0'): + ops = _sparse_vs_dense_xent_benchmark_sparse(labels, logits) + else: + ops = _sparse_vs_dense_xent_benchmark_sparse(labels, logits) + delta_sparse = _timer(sess, ops) + print('%d \t %d \t %s \t %f \t %f \t %f' % (batch_size, num_entries, use_gpu, delta_dense, delta_sparse, delta_sparse / delta_dense)) + + +def main(_): + print('Sparse Xent vs. SparseToDense + Xent') + print('batch \t depth \t gpu \t dt(dense) \t dt(sparse) \t dt(sparse)/dt(dense)') + for use_gpu in (False, True): + for batch_size in (32, 64, 128): + for num_entries in (100, 1000, 10000): + sparse_vs_dense_xent_benchmark(batch_size, num_entries, use_gpu) + sparse_vs_dense_xent_benchmark(32, 100000, use_gpu) + sparse_vs_dense_xent_benchmark(8, 1000000, use_gpu) + + +if __name__ == '__main__': + if '--benchmarks' in sys.argv: + sys.argv.remove('--benchmarks') + tf.app.run() + else: + tf.test.main() diff --git a/dataset/preprocessed/641.txt b/dataset/preprocessed/641.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/641.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/642.txt b/dataset/preprocessed/642.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/642.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/643.txt b/dataset/preprocessed/643.txt new file mode 100644 index 0000000..28eb8c3 --- /dev/null +++ b/dataset/preprocessed/643.txt @@ -0,0 +1,193 @@ +import tensorflow as tf +import os +import numpy as np +import tqdm +import gym +import load_policy +import math +import logz +import time + + +class Config(object): + n_features = 17 + n_classes = 6 + dropout = 0.5 + hidden_size_1 = 128 + hidden_size_2 = 256 + hidden_size_3 = 64 + batch_size = 256 + lr = 0.0005 + itera = 20 + train_itera = 20 + envname = 'Walker2d-v1' + max_steps = 1000 + + +class NN(object): + + def add_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_features), name='input') + self.labels_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_classes), name='label') + self.dropout_placeholder = tf.placeholder(tf.float32, name='drop') + self.is_training = tf.placeholder(tf.bool) + + def create_feed_dict(self, inputs_batch, labels_batch=None, dropout=1, is_training=False): + if labels_batch is None: + feed_dict = {self.input_placeholder: inputs_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + else: + feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + return feed_dict + + def add_prediction_op(self): + self.global_step = tf.Variable(0) + with tf.name_scope('layer1'): + hidden1 = tf.contrib.layers.fully_connected(self.input_placeholder, num_outputs=Config.hidden_size_1, activation_fn=tf.nn.relu) + with tf.name_scope('layer2'): + hidden2 = tf.contrib.layers.fully_connected(hidden1, num_outputs=Config.hidden_size_2, activation_fn=tf.nn.relu) + with tf.name_scope('layer3'): + hidden3 = tf.contrib.layers.fully_connected(hidden2, num_outputs=Config.hidden_size_3, activation_fn=tf.nn.relu) + with tf.name_scope('output'): + pred = tf.contrib.layers.fully_connected(hidden3, num_outputs=Config.n_classes, activation_fn=None) + return pred + + def add_loss_op(self, pred): + loss = tf.losses.mean_squared_error(predictions=pred, labels=self.labels_placeholder) + tf.summary.scalar('loss', loss) + return loss + + def add_training_op(self, loss): + extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + with tf.control_dependencies(extra_update_ops): + learning_rate = tf.train.exponential_decay(Config.lr, self.global_step, 1000, 0.8, staircase=True) + train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=self.global_step) + return train_op + + def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + rs, _, loss = sess.run([merged, self.train_op, self.loss], feed_dict=feed) + train_writer.add_summary(rs, i) + return loss + + def __init__(self, config): + self.config = config + self.build() + + def fit(self, sess, train_x, train_y): + loss = self.train_on_batch(sess, train_x, train_y) + + def build(self): + with tf.name_scope('inputs'): + self.add_placeholders() + with tf.name_scope('predict'): + self.pred = self.add_prediction_op() + with tf.name_scope('loss'): + self.loss = self.add_loss_op(self.pred) + with tf.name_scope('train'): + self.train_op = self.add_training_op(self.loss) + + def get_pred(self, sess, inputs_batch): + feed = self.create_feed_dict(inputs_batch, dropout=1, is_training=False) + p = sess.run(self.pred, feed_dict=feed) + return p + + +def load(path): + all = np.load(path) + X = all['arr_0'] + y = all['arr_1'] + y1 = y.reshape(y.shape[0], y.shape[2]) + return X, y1 + + +def run_env(env, nn, session): + obs = env.reset() + done = False + totalr = 0.0 + steps = 0 + observations = [] + while not done: + action = nn.get_pred(session, obs[(None), :]) + observations.append(obs) + obs, r, done, _ = env.step(action) + totalr += r + steps += 1 + if steps >= Config.max_steps: + break + return totalr, observations + + +def shuffle(X_train, y_train): + training_data = np.concatenate((X_train, y_train), axis=1) + np.random.shuffle(training_data) + X = training_data[:, :-Config.n_classes] + y = training_data[:, -Config.n_classes:] + return X, y + + +def main(): + PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + train_path = os.path.join(PROJECT_ROOT, 'data/' + Config.envname + '.train.npz') + policy_path = os.path.join(PROJECT_ROOT, 'experts/' + Config.envname + '.pkl') + train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + logz.configure_output_dir(os.path.join(PROJECT_ROOT, 'log/' + Config.envname + '_DA_' + time.strftime('%d-%m-%Y_%H-%M-%S'))) + X_train, y_train = load(train_path) + print('train size :', X_train.shape, y_train.shape) + print('start training') + with tf.Graph().as_default(): + config = Config() + nn = NN(config) + init = tf.global_variables_initializer() + saver = tf.train.Saver(max_to_keep=10, keep_checkpoint_every_n_hours=0.5) + print('loading and building expert policy') + policy_fn = load_policy.load_policy(policy_path) + print('loaded and built') + with tf.Session() as session: + merged = tf.summary.merge_all() + train_writer = tf.summary.FileWriter(train_log_path, session.graph) + session.run(init) + coord = tf.train.Coordinator() + threads = tf.train.start_queue_runners(session, coord) + for j in tqdm.tqdm(range(Config.itera)): + X_train, y_train = shuffle(X_train, y_train) + i = 0 + try: + for i in range(int(math.ceil(Config.train_itera * X_train.shape[0] / Config.batch_size))): + offset = i * Config.batch_size % X_train.shape[0] + batch_x = X_train[offset:offset + Config.batch_size, :] + batch_y = y_train[offset:offset + Config.batch_size] + loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + i += 1 + print('step:', i, 'loss:', loss) + except tf.errors.OutOfRangeError: + print('done') + finally: + coord.request_stop() + coord.join(threads) + observations = [] + actions = [] + env = gym.make(Config.envname) + for _ in range(10): + _, o = run_env(env, nn, session) + observations.extend(o) + action = policy_fn(o) + actions.extend(action) + new_x = np.array(observations) + new_y = np.array(actions) + X_train = np.concatenate((X_train, new_x)) + y_train = np.concatenate((y_train, new_y)) + print('train size :', X_train.shape, y_train.shape) + rollouts = 20 + returns = [] + for _ in range(rollouts): + totalr, _ = run_env(env, nn, session) + returns.append(totalr) + print() + logz.log_tabular('Iteration', j) + logz.log_tabular('AverageReturn', np.mean(returns)) + logz.log_tabular('StdReturn', np.std(returns)) + logz.dump_tabular() + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/644.txt b/dataset/preprocessed/644.txt new file mode 100644 index 0000000..9b93ed4 --- /dev/null +++ b/dataset/preprocessed/644.txt @@ -0,0 +1,130 @@ +import tensorflow as tf +import numpy as np +from print_ import print_ +from PIL import Image + + +class NN(object): + + def __init__(self, sizes=None, learning_rate=None, batch_size=None, n_epoches=None): + self._sizes = sizes + self._learning_rate = learning_rate + self._batch_size = batch_size + self._n_epoches = n_epoches + self.w_list = [] + self.b_list = [] + if self._sizes is None: + return + with tf.Session() as session: + for i, size in enumerate(self._sizes[1:]): + in_size = self._sizes[i] + out_size = size + self.w_list.append(session.run(tf.random_normal([in_size, out_size]))) + self.b_list.append(session.run(tf.random_normal([out_size]))) + + def load_from_dbn_to_reconstructNN(self, dbn): + assert len(self._sizes) == len(dbn._sizes) * 2 - 1 + for i in range(len(dbn._sizes)): + assert dbn._sizes[i] == self._sizes[i] + assert self._sizes[-i - 1] == dbn._sizes[i] + for i in range(len(dbn._sizes) - 1): + self.w_list[i], _, self.b_list[i] = dbn._rbm_list[i].get_param() + n_dbn_layers = len(dbn._sizes) + for i in range(n_dbn_layers - 1): + w, vb, _ = dbn._rbm_list[i].get_param() + self.w_list[-i - 1] = np.transpose(w) + self.b_list[-i - 1] = vb + + def load_layers_from_NN(self, nnet, start_layer, end_layer): + assert start_layer < end_layer + assert end_layer - start_layer + 1 < len(nnet._sizes) + self._sizes = nnet._sizes[start_layer:end_layer + 1] + self._learning_rate = nnet._learning_rate + self._batch_size = nnet._batch_size + self._n_epoches = nnet._n_epoches + self.w_list = nnet.w_list[start_layer:end_layer] + self.b_list = nnet.b_list[start_layer:end_layer] + + def _load_w_b_from_self(self): + w_list = [] + b_list = [] + for i, size in enumerate(self._sizes[1:]): + in_size = self._sizes[i] + out_size = size + w_list.append(tf.get_variable('nn_weight_' + str(i), [in_size, out_size], initializer=tf.constant_initializer(self.w_list[i]))) + b_list.append(tf.get_variable('nn_bias_' + str(i), [out_size], initializer=tf.constant_initializer(self.b_list[i]))) + return w_list, b_list + + def _save_w_b_to_self(self, session, w_list, b_list): + self.w_list = session.run(w_list) + self.b_list = session.run(b_list) + + def _MLP(self, x_in, w_list, b_list): + y_out = x_in + for i in range(len(self._sizes) - 2): + y_out = tf.nn.sigmoid(tf.add(tf.matmul(y_out, w_list[i]), b_list[i])) + y_out = tf.add(tf.matmul(y_out, w_list[len(self._sizes) - 2]), b_list[len(self._sizes) - 2]) + return y_out + + def train(self, X, Y): + batch_size = self._batch_size + n_epoches = self._n_epoches + display_epoches = 1 + x_input = tf.placeholder('float', [None, self._sizes[0]]) + y_target = tf.placeholder('float', [None, self._sizes[-1]]) + w_list, b_list = self._load_w_b_from_self() + y_output = self._MLP(x_input, w_list, b_list) + loss_cross_entropy = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_output, labels=y_target)) + train_op = tf.train.GradientDescentOptimizer(learning_rate=self._learning_rate).minimize(loss_cross_entropy) + init = tf.global_variables_initializer() + with tf.Session() as session_t: + session_t.run(init) + for epoch in range(n_epoches): + avg_lost = 0.0 + x_len = len(X) + total_batch = x_len // batch_size if x_len % batch_size == 0 else x_len // batch_size + 1 + for i in range(total_batch): + s_site = i * batch_size + if s_site + batch_size <= len(X): + e_site = s_site + batch_size + else: + e_site = len(X) + x_batch = X[s_site:e_site] + y_batch = Y[s_site:e_site] + lost_t, _ = session_t.run([loss_cross_entropy, train_op], feed_dict={x_input: x_batch, y_target: y_batch}) + self._save_w_b_to_self(session_t, w_list, b_list) + avg_lost += float(lost_t) / total_batch + if epoch % display_epoches == 0: + print_('NNET Training : Epoch' + ' %04d' % (epoch + 1) + ' Lost ' + str(avg_lost)) + print_('Optimizer Finished') + + def test_linear(self, X, Y): + x_in = tf.placeholder('float', [None, self._sizes[0]]) + y_in = tf.placeholder('float', [None, self._sizes[-1]]) + __predict = tf.nn.sigmoid(self._MLP(x_in, self.w_list, self.b_list)) + error_square = tf.square(tf.subtract(__predict, y_in)) + mean_error_square = tf.reduce_mean(tf.cast(error_square, tf.float32)) + init = tf.global_variables_initializer() + with tf.Session() as session_t: + session_t.run(init) + print_('Error: ' + str(session_t.run(mean_error_square, feed_dict={x_in: X, y_in: Y}))) + + def test_logical(self, X, Y): + x_in = tf.placeholder('float', [None, self._sizes[0]]) + y_in = tf.placeholder('float', [None, self._sizes[-1]]) + __predict = tf.nn.softmax(tf.nn.sigmoid(self._MLP(x_in, self.w_list, self.b_list))) + __correct = tf.equal(tf.argmax(__predict, 1), tf.argmax(y_in, 1)) + __accuracy_rate = tf.reduce_mean(tf.cast(__correct, tf.float32)) + init = tf.global_variables_initializer() + with tf.Session() as session_t: + session_t.run(init) + print_('Accuracy: ' + str(session_t.run(__accuracy_rate, feed_dict={x_in: X, y_in: Y}))) + + def predict(self, X): + X_t = tf.placeholder('float', [None, self._sizes[0]]) + __predict = tf.nn.sigmoid(self._MLP(X_t, self.w_list, self.b_list)) + init = tf.global_variables_initializer() + with tf.Session() as session_t: + session_t.run(init) + __predict = session_t.run([__predict], feed_dict={X_t: X}) + return __predict diff --git a/dataset/preprocessed/645.txt b/dataset/preprocessed/645.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/645.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/646.txt b/dataset/preprocessed/646.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/646.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/647.txt b/dataset/preprocessed/647.txt new file mode 100644 index 0000000..adcca37 --- /dev/null +++ b/dataset/preprocessed/647.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/648.txt b/dataset/preprocessed/648.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/648.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/649.txt b/dataset/preprocessed/649.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/649.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/65.txt b/dataset/preprocessed/65.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/65.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/650.txt b/dataset/preprocessed/650.txt new file mode 100644 index 0000000..d3c03b5 --- /dev/null +++ b/dataset/preprocessed/650.txt @@ -0,0 +1,156 @@ +import tensorflow as tf +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + self.saver = tf.train.Saver() + self.saved_path = './saved_model' + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + def variable_output(self, session): + form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + def save(self, session): + self.saved_path = self.saver.save(session, './saved_model') + + def restore(self, session): + self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer(learning_rate=1e-06).minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + session.run(self.update_op) diff --git a/dataset/preprocessed/652.txt b/dataset/preprocessed/652.txt new file mode 100644 index 0000000..0d6830e --- /dev/null +++ b/dataset/preprocessed/652.txt @@ -0,0 +1,126 @@ +import random +import tensorflow as tf +import numpy as np +import gym +policy_network_verbose = False +update_gradients_verbose = False +policy_learning_rate = 0.0001 +baseline_learning_rate = 0.0001 +n_episode = 1000 +gamma = 0.99 +observation_placeholder = tf.placeholder(tf.float32, shape=[None, 4]) +W1_policy = tf.get_variable('W1_policy', shape=[4, 64]) +b1_policy = tf.get_variable('b1_policy', shape=[64]) +W2_policy = tf.get_variable('W2_policy', shape=[64, 64]) +b2_policy = tf.get_variable('b2_policy', shape=[64]) +W3_policy = tf.get_variable('W3_policy', shape=[64, 2]) +b3_policy = tf.get_variable('b3_policy', shape=[2]) +hidden1_policy = tf.nn.relu(tf.matmul(observation_placeholder, W1_policy) + b1_policy) +hidden2_policy = tf.nn.relu(tf.matmul(hidden1_policy, W2_policy) + b2_policy) +scores = tf.nn.relu(tf.matmul(hidden2_policy, W3_policy) + b3_policy) +probs = tf.nn.softmax(scores) +discount_placeholder = tf.placeholder(tf.float32) +gain_placeholder = tf.placeholder(tf.float32) +action_placeholder = tf.placeholder(tf.float32, shape=[None, 2]) +log_probs = tf.log(probs) * action_placeholder +grads_policy = tf.gradients(log_probs, [W1_policy, b1_policy, W2_policy, b2_policy, W3_policy, b3_policy]) +update_W1_policy = W1_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[0]) +update_b1_policy = b1_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[1]) +update_W2_policy = W2_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[2]) +update_b2_policy = b2_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[3]) +update_W3_policy = W3_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[4]) +update_b3_policy = b3_policy.assign_add(policy_learning_rate * discount_placeholder * gain_placeholder * grads_policy[5]) +W1_baseline = tf.get_variable('W1_baseline', shape=[4, 64]) +b1_baseline = tf.get_variable('b1_baseline', shape=[64]) +W2_baseline = tf.get_variable('W2_baseline', shape=[64, 64]) +b2_baseline = tf.get_variable('b2_baseline', shape=[64]) +W3_baseline = tf.get_variable('W3_baseline', shape=[64, 1]) +b3_baseline = tf.get_variable('b3_baseline', shape=[1]) +hidden1_baseline = tf.nn.relu(tf.matmul(observation_placeholder, W1_baseline) + b1_baseline) +hidden2_baseline = tf.nn.relu(tf.matmul(hidden1_baseline, W2_baseline) + b2_baseline) +values = tf.matmul(hidden2_baseline, W3_baseline) + b3_baseline +grads_baseline = tf.gradients(values, [W1_baseline, b1_baseline, W2_baseline, b2_baseline, W3_baseline, b3_baseline]) +update_W1_baseline = W1_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[0]) +update_b1_baseline = b1_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[1]) +update_W2_baseline = W2_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[2]) +update_b2_baseline = b2_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[3]) +update_W3_baseline = W3_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[4]) +update_b3_baseline = b3_baseline.assign_add(baseline_learning_rate * gain_placeholder * grads_baseline[5]) + + +def policy_network(observation, session, verbose=False): + probabilities = session.run(probs, feed_dict={observation_placeholder: [observation]}) + if verbose: + print(probabilities) + if random.random() <= probabilities[0][0]: + return 0 + else: + return 1 + + +def update_policy_gradients(discount, gain, observation, action, session, verbose=False): + if action == 0: + mask = [[1, 0]] + else: + mask = [[0, 1]] + result = session.run([probs, log_probs, grads_policy, update_W1_policy, update_b1_policy, update_W2_policy, update_b2_policy, update_W3_policy, update_b3_policy], feed_dict={observation_placeholder: [observation], discount_placeholder: discount, gain_placeholder: gain, action_placeholder: mask}) + if verbose: + print(result[2]) + + +def update_baseline_gradients(gain, observation, session, verbose=False): + result = session.run([grads_baseline, update_W1_baseline, update_b1_baseline, update_W2_baseline, update_b2_baseline, update_W3_baseline, update_b3_baseline], feed_dict={observation_placeholder: [observation], gain_placeholder: gain}) + if verbose: + print(result[0]) + + +init = tf.global_variables_initializer() +with tf.Session() as sess: + sess.run(init) + env = gym.make('CartPole-v1') + print('Training...') + for episode in range(n_episode): + observation = env.reset() + reward = 0 + done = False + total_reward = 0.0 + memory = [] + while not done: + action = policy_network(observation, sess, verbose=policy_network_verbose) + next_observation, next_reward, next_done, _ = env.step(action) + if next_done: + next_reward = -100 + memory.append((observation, action, next_reward)) + total_reward += next_reward + observation, reward, done = next_observation, next_reward, next_done + print('Episode {0} Reward: {1}'.format(episode, total_reward)) + discount = 1.0 + gain = 0 + for observation, action, reward in memory: + gain += discount * reward + discount *= gamma + discount = 1.0 + for t in range(len(memory)): + observation, action, reward = memory[t] + gain_with_baseline = gain - sess.run(values, feed_dict={observation_placeholder: [observation]})[0][0] + update_policy_gradients(discount=discount, gain=gain_with_baseline, observation=observation, action=action, session=sess, verbose=update_gradients_verbose) + update_baseline_gradients(gain=gain_with_baseline, observation=observation, session=sess, verbose=update_gradients_verbose) + gain -= discount * reward + discount *= gamma + print('Training done!') + print() + input('Press Enter to start testing!') + print('Testing...') + for episode in range(5): + observation = env.reset() + reward = 0 + done = False + total_reward = 0.0 + while not done: + env.render() + action = policy_network(observation, sess) + next_observation, next_reward, next_done, _ = env.step(action) + total_reward += next_reward + observation, reward, done = next_observation, next_reward, next_done + print('Reward:', total_reward) + env.close() diff --git a/dataset/preprocessed/653.txt b/dataset/preprocessed/653.txt new file mode 100644 index 0000000..9edecbc --- /dev/null +++ b/dataset/preprocessed/653.txt @@ -0,0 +1,142 @@ +import tensorflow as tf + + +class DQAgent: + + def __init__(self): + self.sess = tf.Session() + self.state_placeholder = tf.placeholder(shape=(None, 84, 84, 4), dtype=tf.float32, name='State_placeholder') + self.reward_placeholder = tf.placeholder(shape=(None,), dtype=tf.float32, name='Reward_placeholder') + self.action_index = tf.placeholder(shape=(None, 2), dtype=tf.int32, name='Action_taken') + self.define_variables() + self.action = self.define_graph(self.state_placeholder) + print(self.action.get_shape()) + self.optimizer = tf.train.RMSPropOptimizer(learning_rate=2.5e-07) + self.q_val = tf.gather_nd(self.action, self.action_index) + print(self.q_val.get_shape()) + self.q_val = tf.reshape(self.q_val, shape=(-1,)) + with tf.name_scope('Loss'): + self.loss = tf.losses.mean_squared_error(self.reward_placeholder, self.q_val) + self.opt = self.optimizer.minimize(-1 * self.loss) + init = tf.global_variables_initializer() + self.sess.run(init) + self.train_writer = tf.summary.FileWriter('results/', self.sess.graph) + self.game_len_placeholder = tf.placeholder(shape=(), dtype=tf.int32, name='Game_length') + self.game_len_summary = tf.summary.scalar('Game_length', self.game_len_placeholder) + + def define_variables(self): + self.variables = [] + self.biases = [] + self.fully_connected = [] + with tf.name_scope('Convolutional_weights'): + self.variables.append(tf.Variable(tf.random_normal(shape=(8, 8, 4, 32), mean=0, stddev=0.02, dtype=tf.float32), name='conv_1')) + self.variables.append(tf.Variable(tf.random_normal(shape=(4, 4, 32, 64), mean=0, stddev=0.02, dtype=tf.float32), name='conv_2')) + self.biases.append(tf.Variable(tf.random_normal(shape=(20, 20, 32), mean=0.05, stddev=0.02), dtype=tf.float32)) + self.biases.append(tf.Variable(tf.random_normal(shape=(4, 4, 64), mean=0.05, stddev=0.02), dtype=tf.float32)) + with tf.name_scope('Fully_connected_weights'): + self.fully_connected.append(tf.Variable(tf.random_normal(shape=(256, 256), mean=0.05, stddev=0.02, dtype=tf.float32), name='fc_1')) + self.fully_connected.append(tf.Variable(tf.random_normal(shape=(256, 3), mean=0.05, stddev=0.02, dtype=tf.float32), name='fc_2')) + self.biases.append(tf.Variable(tf.random_normal(shape=(256,), mean=0.05, stddev=0.02), dtype=tf.float32)) + + def define_graph(self, state): + with tf.name_scope('Convolutional_layer'): + state = tf.nn.conv2d(state, self.variables[0], strides=[1, 4, 4, 1], padding='VALID') + state = tf.add(state, self.biases[0]) + state = tf.nn.max_pool(state, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') + state = tf.nn.relu(state, name='Activation_function') + print(state) + with tf.name_scope('Convolutional_layer'): + state = tf.nn.conv2d(state, self.variables[1], strides=[1, 2, 2, 1], padding='VALID') + state = tf.add(state, self.biases[1]) + state = tf.nn.max_pool(state, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') + state = tf.nn.relu(state, name='Activation_function') + print(state) + state = tf.reshape(state, shape=[-1, 256]) + with tf.name_scope('Fully_connected_layer'): + out = state + out = tf.matmul(out, self.fully_connected[0]) + out = tf.add(out, self.biases[2]) + out = tf.nn.relu(out, name='ReLU_activation') + out = tf.matmul(out, self.fully_connected[1]) + return out + + def get_action(self, observation): + return self.sess.run(self.action, feed_dict={self.state_placeholder: observation}) + + def get_actions_taken(self, observation, actions): + return self.sess.run(self.q_val, feed_dict={self.state_placeholder: observation, self.action_index: actions}) + + def train_step(self, inputs_batch, targets_batch, actions_batch): + self.sess.run(self.opt, feed_dict={self.state_placeholder: inputs_batch, self.reward_placeholder: targets_batch, self.action_index: actions_batch}) + + def restore_session(self, steps): + saver = tf.train.Saver() + saver.restore(self.sess, 'results/dqmodels/model_%d.cpkt' % steps) + + +class StaleAgent: + + def __init__(self): + self.sess = tf.Session() + self.state_placeholder = tf.placeholder(shape=(None, 84, 84, 4), dtype=tf.float32, name='State_placeholder') + self.reward_placeholder = tf.placeholder(shape=(None,), dtype=tf.float32, name='Reward_placeholder') + self.action_index = tf.placeholder(shape=(None, None), dtype=tf.int32, name='Action_taken') + self.define_variables() + self.action = self.define_graph(self.state_placeholder) + self.optimizer = tf.train.RMSPropOptimizer(learning_rate=1e-05) + self.q_val = tf.gather_nd(self.action, self.action_index) + self.q_val = tf.reshape(self.q_val, shape=(-1,)) + with tf.name_scope('Loss'): + self.loss = tf.losses.mean_squared_error(self.reward_placeholder, self.q_val) + self.opt = self.optimizer.minimize(-1 * self.loss) + + def transfer_values(self, dqlearner): + pass + + def define_variables(self): + self.variables = [] + self.biases = [] + self.fully_connected = [] + with tf.name_scope('Convolutional_weights'): + self.variables.append(tf.Variable(tf.random_normal(shape=(8, 8, 4, 32), mean=0, stddev=0.001, dtype=tf.float32), name='conv_1')) + self.variables.append(tf.Variable(tf.random_normal(shape=(4, 4, 32, 64), mean=0, stddev=0.001, dtype=tf.float32), name='conv_2')) + with tf.name_scope('Fully_connected_weights'): + self.fully_connected.append(tf.Variable(tf.random_normal(shape=(256, 256), mean=0, stddev=0.001, dtype=tf.float32), name='fc_1')) + self.fully_connected.append(tf.Variable(tf.random_normal(shape=(256, 3), mean=0, stddev=0.001, dtype=tf.float32), name='fc_2')) + for var in self.variables: + print(var) + for fc in self.fully_connected: + print(fc) + + def define_graph(self, state): + with tf.name_scope('Convolutional_layer'): + state = tf.nn.conv2d(state, self.variables[0], strides=[1, 4, 4, 1], padding='VALID') + state = tf.nn.max_pool(state, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') + state = tf.nn.relu(state, name='Activation_function') + print(state) + with tf.name_scope('Convolutional_layer'): + state = tf.nn.conv2d(state, self.variables[1], strides=[1, 2, 2, 1], padding='VALID') + state = tf.nn.max_pool(state, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') + state = tf.nn.relu(state, name='Activation_function') + print(state) + print('State after convolutional layers') + print(state.get_shape().as_list()) + state = tf.reshape(state, shape=[-1, 256]) + print(state.get_shape().as_list()) + print('State before fully convolutional layers') + with tf.name_scope('Fully_connected_layer'): + out = state + out = tf.matmul(out, self.fully_connected[0]) + out = tf.nn.relu(out, name='ReLU_activation') + out = tf.matmul(out, self.fully_connected[1]) + return out + + def get_action(self, observation): + return self.sess.run(self.action, feed_dict={self.state_placeholder: observation}) + + def train_step(self, inputs_batch, targets_batch, actions_batch): + self.sess.run(self.opt, feed_dict={self.state_placeholder: inputs_batch, self.reward_placeholder: targets_batch, self.action_index: actions_batch}) + + def restore_session(self, steps): + saver = tf.train.Saver() + saver.restore(self.sess, 'results/dqmodels/model_%d.cpkt' % steps) diff --git a/dataset/preprocessed/654.txt b/dataset/preprocessed/654.txt new file mode 100644 index 0000000..7b2e45f --- /dev/null +++ b/dataset/preprocessed/654.txt @@ -0,0 +1,160 @@ +def split_data(data, train_frac, val_frac, test_frac, seed): + """ + + Split a numpy array into three parts for training, validation, and testing. + + + + Args: + + - data: numpy array, to be split along the first axis + + - train_frac, fraction of data to be used for training + + - val_frac, fraction of data to be used for validation + + - test_frac, fraction of data to be used for testing + + - seed, random seed for reproducibility + Returns: + + - Training Set + + - Validation Set + + - Testing Set + + + """ + if train_frac + val_frac + test_frac != 1: + print('ERROR: Train, validation, and test fractions must sum to one.') + else: + np.random.seed(seed) + size = data.shape[0] + split_train = int(train_frac * size) + split_val = int(val_frac * size) + split_train + np.random.shuffle(data) + return data[:split_train], data[split_train:split_val], data[split_val:] + + +import numpy as np +import tensorflow as tf +import pandas as pd +from random import randint +images = np.load('/work/cse496dl/shared/homework/01/fmnist_train_data.npy') +labels = np.load('/work/cse496dl/shared/homework/01/fmnist_train_labels.npy') +train_images, val_images, test_images = split_data(images, 0.7, 0.1, 0.2, 123) +train_labels, val_labels, test_labels = split_data(labels, 0.7, 0.1, 0.2, 123) +filepath = '$WORK' + '/CSCE_896/HW1/' +hlayers_max = 1 +nodes_min = 10 +nodes_max = 784 +batch_size = 100 +results = pd.DataFrame(columns=['Layers', 'Layer_1_Nodes', 'Layer_2_Nodes', 'Layer_3_Nodes', 'Accuracy']) +for run in range(25): + num_hlayers = randint(1, hlayers_max) + if num_hlayers == 1: + h1_nodes = randint(nodes_min, nodes_max) + x = tf.placeholder(tf.float32, [None, 784], name='input_placeholder') + with tf.name_scope('linear_model') as scope: + hidden = tf.layers.dense(x, h1_nodes, activation=tf.nn.relu, name='hidden_layer') + output = tf.layers.dense(hidden, 10, name='output_layer') + tf.identity(output, name='output') + y = tf.placeholder(tf.float32, [None, 10], name='label') + accuracy = tf.metrics.accuracy(y, predictions, name='accuracy') + cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=output, name='ce_loss') + with tf.Session() as session: + session.run(tf.global_variables_initializer()) + for epoch in range(100): + for i in range(train_num_examples // batch_size): + batch_xs = train_images[i * batch_size:(i + 1) * batch_size, :] + batch_ys = train_labels[i * batch_size:(i + 1) * batch_size, :] + session.run([train_op, tf.reduce_mean(cross_entropy)], {x: batch_xs, y: batch_ys}) + val_acc = session.run(accuracy, {x: val_images, y: val_labels}) + if val_acc < best_val_acc: + best_val_acc = val_acc + counter = 0 + else: + counter = counter + 1 + if counter > 5: + results.iloc[run, 0] = num_hlayers + results.iloc[run, 1] = h1_nodes + results.iloc[run, 2] = 'NA' + results.iloc[run, 3] = 'NA' + results.iloc[run, 4] = best_val_acc + break + else: + continue + elif num_hlayers == 2: + h1_nodes = randint(nodes_min, nodes_max) + h2_nodes = randint(nodes_min, nodes_max) + x = tf.placeholder(tf.float32, [None, 784], name='input_placeholder') + with tf.name_scope('linear_model') as scope: + hidden1 = tf.layers.dense(x, h1_nodes, activation=tf.nn.relu, name='hidden_layer1') + hidden2 = tf.layers.dense(hidden1, h2_nodes, activation=tf.nn.relu, name='hidden_layer2') + output = tf.layers.dense(hidden2, 10, name='output_layer') + tf.identity(output, name='output') + y = tf.placeholder(tf.float32, [None, 10], name='label') + accuracy = tf.metrics.accuracy(y, predictions, name='accuracy') + cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=output, name='ce_loss') + with tf.Session() as session: + session.run(tf.global_variables_initializer()) + for epoch in range(100): + for i in range(train_num_examples // batch_size): + batch_xs = train_images[i * batch_size:(i + 1) * batch_size, :] + batch_ys = train_labels[i * batch_size:(i + 1) * batch_size, :] + session.run([train_op, tf.reduce_mean(cross_entropy)], {x: batch_xs, y: batch_ys}) + val_acc = session.run(accuracy, {x: val_images, y: val_labels}) + if val_acc < best_val_acc: + best_val_acc = val_acc + counter = 0 + else: + counter = counter + 1 + if counter > 5: + results.iloc[run, 0] = num_hlayers + results.iloc[run, 1] = h1_nodes + results.iloc[run, 2] = h2_nodes + results.iloc[run, 3] = 'NA' + results.iloc[run, 4] = best_val_acc + break + else: + continue + elif num_hlayers == 3: + h1_nodes = randint(nodes_min, nodes_max) + h2_nodes = randint(nodes_min, nodes_max) + h3_nodes = randint(nodes_min, nodes_max) + x = tf.placeholder(tf.float32, [None, 784], name='input_placeholder') + with tf.name_scope('linear_model') as scope: + hidden1 = tf.layers.dense(x, h1_nodes, activation=tf.nn.relu, name='hidden_layer1') + hidden2 = tf.layers.dense(hidden1, h2_nodes, activation=tf.nn.relu, name='hidden_layer2') + hidden3 = tf.layers.dense(hidden2, h2_nodes, activation=tf.nn.relu, name='hidden_layer2') + output = tf.layers.dense(hidden3, 10, name='output_layer') + tf.identity(output, name='output') + y = tf.placeholder(tf.float32, [None, 10], name='label') + accuracy = tf.metrics.accuracy(y, predictions, name='accuracy') + cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=output, name='ce_loss') + with tf.Session() as session: + session.run(tf.global_variables_initializer()) + for epoch in range(100): + for i in range(train_num_examples // batch_size): + batch_xs = train_images[i * batch_size:(i + 1) * batch_size, :] + batch_ys = train_labels[i * batch_size:(i + 1) * batch_size, :] + session.run([train_op, tf.reduce_mean(cross_entropy)], {x: batch_xs, y: batch_ys}) + val_acc = session.run(tf.argmax(val_labels, 1), tf.argmax(output, 1), {x: val_images, y: val_labels}) + if val_acc < best_val_acc: + best_val_acc = val_acc + counter = 0 + else: + counter = counter + 1 + if counter > 5: + results.iloc[run, 0] = num_hlayers + results.iloc[run, 1] = h1_nodes + results.iloc[run, 2] = h2_nodes + results.iloc[run, 3] = h3_nodes + results.iloc[run, 4] = best_val_acc + break + else: + continue + results.to_csv(filepath + 'arc_search_results.csv', index=False) + print('Run', run, 'Complete') + tf.reset_default_graph() diff --git a/dataset/preprocessed/655.txt b/dataset/preprocessed/655.txt new file mode 100644 index 0000000..4906ee6 --- /dev/null +++ b/dataset/preprocessed/655.txt @@ -0,0 +1,85 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) + image_size = 28 + num_labels = 10 + + def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + train_dataset, train_labels = reformat(train_dataset, train_labels) + valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) + test_dataset, test_labels = reformat(test_dataset, test_labels) + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) + + def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + batch_size = 128 + num_hidden_nodes1 = 1024 + num_hidden_nodes2 = 256 + num_hidden_nodes3 = 128 + keep_prob = 0.5 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_hidden_nodes3], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + weights4 = tf.Variable(tf.truncated_normal([num_hidden_nodes3, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes3))) + biases4 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + logits = tf.matmul(lay3_train, weights4) + biases4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 4000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) + num_steps = 18001 + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/656.txt b/dataset/preprocessed/656.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/656.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/657.txt b/dataset/preprocessed/657.txt new file mode 100644 index 0000000..ae0bcdb --- /dev/null +++ b/dataset/preprocessed/657.txt @@ -0,0 +1,71 @@ +import tensorflow as tf +import numpy as np + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.01) + return tf.Variable(initial) + + +def bias_variable(shape): + initial = tf.constant(0.01, shape=shape) + return tf.Variable(initial) + + +def conv2d(x, W, stride): + return tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +def createNetwork(action_count): + W_conv1 = weight_variable([8, 8, 4, 32]) + b_conv1 = bias_variable([32]) + W_conv2 = weight_variable([4, 4, 32, 64]) + b_conv2 = bias_variable([64]) + W_conv3 = weight_variable([3, 3, 64, 64]) + b_conv3 = bias_variable([64]) + W_fc1 = weight_variable([1600, 512]) + b_fc1 = bias_variable([512]) + W_fc2 = weight_variable([512, action_count]) + b_fc2 = bias_variable([action_count]) + s = tf.placeholder('float', [None, 80, 80, 4]) + h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2) + h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3) + h_conv3_flat = tf.reshape(h_conv3, [-1, 1600]) + h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1) + readout = tf.matmul(h_fc1, W_fc2) + b_fc2 + return s, readout, h_fc1 + + +class DQNAgent(object): + + def __init__(self, action_count): + self.action_count = action_count + self.state_placeholder, self.readout, self.h_fc1 = createNetwork(action_count) + self.action_placeholder = tf.placeholder('float', [None, action_count]) + self.reward_placeholder = tf.placeholder('float', [None]) + self.readout_action = tf.reduce_sum(tf.multiply(self.readout, self.action_placeholder), reduction_indices=1) + self.losses = tf.square(self.reward_placeholder - self.readout_action) + self.loss = tf.reduce_mean(self.losses) + self.train_step = tf.train.AdamOptimizer(1e-06).minimize(self.loss) + tf.summary.scalar('loss', self.loss), + tf.summary.histogram('loss_hist', self.losses), + tf.summary.histogram('q_values_hist', self.readout), + tf.summary.scalar('max_q_value', tf.reduce_max(self.readout)) + self.summaries = tf.summary.merge_all() + + def act(self, session, state): + scores = self.score_actions(session, [state])[0] + return np.argmax(scores) + + def score_actions(self, session, state_batch): + return session.run(self.readout, feed_dict={self.state_placeholder: state_batch}) + + def train(self, session, reward_batch, action_batch, state_batch): + summaries, _, loss = session.run([self.summaries, self.train_step, self.loss], feed_dict={self.reward_placeholder: reward_batch, self.action_placeholder: action_batch, self.state_placeholder: state_batch}) + return summaries, loss diff --git a/dataset/preprocessed/658.txt b/dataset/preprocessed/658.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/658.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/659.txt b/dataset/preprocessed/659.txt new file mode 100644 index 0000000..bcc26a1 --- /dev/null +++ b/dataset/preprocessed/659.txt @@ -0,0 +1,77 @@ +import numpy as np +import scipy +from scipy import io +import os +import matplotlib.pyplot as plt +import tensorflow as tf +from sklearn.metrics import confusion_matrix +MAT = scipy.io.loadmat('MAT_Py_TF_Data_test2') +train_dataset = MAT['Train_Paras'] +train_labels = MAT['Label'] +train_labels_1D = MAT['Label_1D'] +train_dataset.astype(np.float32) +train_labels.astype(np.float32) +print(train_dataset) +print('*----------------*') +print(train_labels) +print('*----------------*') +print(train_labels_1D) +train_labels_1D_argmax = np.array([label.argmax() for label in train_labels]) +train_labels_cls = np.array([label.argmax() for label in train_labels]) +train_labels_1D_argmax[0:18] +x = tf.placeholder(tf.float32, [None, 6]) +y_true = tf.placeholder(tf.float32, [None, 2]) +y_true_cls = tf.placeholder(tf.int64, [None]) +beta_regul = tf.placeholder(tf.float32) +num_hidden_nodes = 5 +weights1 = tf.Variable(tf.truncated_normal([6, num_hidden_nodes])) +biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) +weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, 2])) +biases2 = tf.Variable(tf.zeros([2])) +lay1_train = tf.nn.relu(tf.matmul(x, weights1) + biases1) +logits = tf.matmul(lay1_train, weights2) + biases2 +y_pred = tf.nn.softmax(logits) +y_pred_cls = tf.argmax(y_pred, dimension=1) +cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_true) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2))) +optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(cost) +correct_prediction = tf.equal(y_pred_cls, y_true_cls) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +saver = tf.train.Saver() +save_dir = 'FullyConnected_Reg/' +if not os.path.exists(save_dir): + os.makedirs(save_dir) +save_path = os.path.join(save_dir, 'best_validation') +session = tf.Session() +session.run(tf.global_variables_initializer()) + + +def optimize(num_iterations): + for i in range(num_iterations): + feed_dict_train = {x: train_dataset, y_true: train_labels, beta_regul: 0.001} + session.run(optimizer, feed_dict=feed_dict_train) + + +feed_dict_test = {x: train_dataset, y_true: train_labels, y_true_cls: train_labels_cls} + + +def print_accuracy(): + acc = session.run(accuracy, feed_dict=feed_dict_test) + print('Accuracy on test-set: {0:.1%}'.format(acc)) + + +print_accuracy() +session.run(tf.global_variables_initializer()) +print_accuracy() +w = session.run(weights1) +plt.imshow(w) +plt.show +optimize(num_iterations=1000) +print_accuracy() +w = session.run(weights1) +plt.imshow(w) +plt.show +saver.save(sess=session, save_path=save_path) +session.run(tf.global_variables_initializer()) +print_accuracy() +saver.restore(sess=session, save_path=save_path) +print_accuracy() diff --git a/dataset/preprocessed/66.txt b/dataset/preprocessed/66.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/66.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/660.txt b/dataset/preprocessed/660.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/660.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/661.txt b/dataset/preprocessed/661.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/661.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/662.txt b/dataset/preprocessed/662.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/662.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/663.txt b/dataset/preprocessed/663.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/663.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/664.txt b/dataset/preprocessed/664.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/664.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/665.txt b/dataset/preprocessed/665.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/665.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/666.txt b/dataset/preprocessed/666.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/666.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/667.txt b/dataset/preprocessed/667.txt new file mode 100644 index 0000000..6e937a2 --- /dev/null +++ b/dataset/preprocessed/667.txt @@ -0,0 +1,213 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import pdb +pickle_file = '../1_notmnist/notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('L2 regularization Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Overfitting with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +hidden_size = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_size])) + b1 = tf.Variable(tf.zeros([hidden_size])) + W2 = tf.Variable(tf.truncated_normal([hidden_size, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y1 = tf.nn.dropout(y1, 0.5) + logits = tf.matmul(y1, W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + valid_logits = tf.matmul(y1_valid, W2) + b2 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + test_logits = tf.matmul(y1_test, W2) + b2 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +few_batch_size = batch_size * 5 +small_train_dataset = train_dataset[:few_batch_size, :] +small_train_labels = train_labels[:few_batch_size, :] +print('Training set', small_train_dataset.shape, small_train_labels.shape) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (small_train_labels.shape[0] - batch_size) + batch_data = small_train_dataset[offset:offset + batch_size, :] + batch_labels = small_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Dropout with small dataset Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +fc1_size = 4096 +fc2_size = 2048 +fc3_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + tf_beta = tf.placeholder(tf.float32) + global_step = tf.Variable(0) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, fc1_size], stddev=np.sqrt(2.0 / (image_size * image_size)))) + b1 = tf.Variable(tf.zeros([fc1_size])) + W2 = tf.Variable(tf.truncated_normal([fc1_size, fc2_size], stddev=np.sqrt(2.0 / fc1_size))) + b2 = tf.Variable(tf.zeros([fc2_size])) + W3 = tf.Variable(tf.truncated_normal([fc2_size, fc3_size], stddev=np.sqrt(2.0 / fc2_size))) + b3 = tf.Variable(tf.zeros([fc3_size])) + W4 = tf.Variable(tf.truncated_normal([fc3_size, num_labels], stddev=np.sqrt(2.0 / fc3_size))) + b4 = tf.Variable(tf.zeros([num_labels])) + y1 = tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1) + y2 = tf.nn.relu(tf.matmul(y1, W2) + b2) + y3 = tf.nn.relu(tf.matmul(y2, W3) + b3) + logits = tf.matmul(y3, W4) + b4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = loss + tf_beta * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(b1) + tf.nn.l2_loss(W2) + tf.nn.l2_loss(b2) + tf.nn.l2_loss(W3) + tf.nn.l2_loss(b3) + tf.nn.l2_loss(W4) + tf.nn.l2_loss(b4)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.7, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + y1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1) + y2_valid = tf.nn.relu(tf.matmul(y1_valid, W2) + b2) + y3_valid = tf.nn.relu(tf.matmul(y2_valid, W3) + b3) + valid_logits = tf.matmul(y3_valid, W4) + b4 + valid_prediction = tf.nn.softmax(valid_logits) + y1_test = tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1) + y2_test = tf.nn.relu(tf.matmul(y1_test, W2) + b2) + y3_test = tf.nn.relu(tf.matmul(y2_test, W3) + b3) + test_logits = tf.matmul(y3_test, W4) + b4 + test_prediction = tf.nn.softmax(test_logits) +num_steps = 12001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, tf_beta: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Final Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/668.txt b/dataset/preprocessed/668.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/668.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/669.txt b/dataset/preprocessed/669.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/669.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/67.txt b/dataset/preprocessed/67.txt new file mode 100644 index 0000000..36a411a --- /dev/null +++ b/dataset/preprocessed/67.txt @@ -0,0 +1,63 @@ +import tensorflow as tf + + +class CNN_NonLinearFC: + + def __init__(self, model_name=None, session=None, learning_rate=None, optimizer=None, learning_decay_rate=None, filter_sizes=None, num_filters=None, max_sentence_length=None, num_classes=None, embeddings=None, new_embeddings=None, embedding_dim=None, vocabulary_size=None, static=None, max_l2_norm=None, regularization_lambda=None, dropout_keep_prob=None): + if model_name == None: + return + self.model_name = model_name + self.session = session + self.learning_rate = learning_rate + self.optimizer = optimizer + self.dropout_keep_prob_train = dropout_keep_prob + self.regularization_lambda = regularization_lambda + self.input_x = tf.placeholder(shape=(None, max_sentence_length), dtype=tf.int32, name='input_x') + self.input_y = tf.placeholder(shape=(None, num_classes), dtype=tf.float32, name='input_y') + self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, name='dropout_keep_prob') + self.embeddings_placeholder = tf.placeholder(tf.float32, shape=(vocabulary_size, embedding_dim)) + self.embeddings = tf.Variable(self.embeddings_placeholder, trainable=not static) + self.new_embeddings = tf.Variable(new_embeddings, trainable=True) + self.all_embeddings = tf.concat([self.embeddings, self.new_embeddings], axis=0) + self.embedded_words = tf.nn.embedding_lookup(self.all_embeddings, self.input_x) + self.input_x_expanded = tf.expand_dims(self.embedded_words, -1) + self.pool_results = [] + for i, filter_size in enumerate(filter_sizes): + filter = tf.get_variable('filter' + str(i), shape=(filter_size, embedding_dim, 1, num_filters), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + if max_l2_norm != 0: + filter = tf.clip_by_norm(filter, max_l2_norm) + bias = tf.Variable(tf.constant(0.0, shape=(num_filters,))) + conv = tf.nn.conv2d(input=self.input_x_expanded, filter=filter, strides=[1, 1, 1, 1], padding='VALID') + relu = tf.nn.relu(tf.nn.bias_add(conv, bias)) + conv_dim = max_sentence_length - filter_size + 1 + pooled = tf.nn.max_pool(relu, ksize=[1, conv_dim, 1, 1], strides=[1, 1, 1, 1], padding='VALID') + self.pool_results.append(pooled) + num_filters_total = num_filters * len(filter_sizes) + self.flat = tf.reshape(tf.concat(self.pool_results, 3), [-1, num_filters_total]) + self.dropout = tf.nn.dropout(self.flat, self.dropout_keep_prob) + hidden_layer_size = num_classes + W_hidden = tf.get_variable('W_hidden', shape=(num_filters_total, hidden_layer_size), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + b_hidden = tf.Variable(tf.constant(0.1, shape=(hidden_layer_size,))) + out_hidden = tf.nn.relu(tf.nn.xw_plus_b(self.dropout, W_hidden, b_hidden)) + W_output = tf.get_variable('W_output', shape=(hidden_layer_size, num_classes), dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) + b_output = tf.Variable(tf.constant(0.1, shape=(num_classes,))) + self.output = tf.nn.xw_plus_b(out_hidden, W_output, b_output, name='output') + self.predictions = tf.argmax(self.output, 1, name='predictions') + losses = tf.nn.softmax_cross_entropy_with_logits(labels=self.input_y, logits=self.output) + if regularization_lambda != 0: + l2_loss = tf.nn.l2_loss(W_hidden) + tf.nn.l2_loss(W_output) + self.loss = tf.reduce_mean(tf.add(losses, tf.multiply(self.regularization_lambda, l2_loss)), name='loss') + else: + self.loss = tf.reduce_mean(losses, name='loss') + global_step = tf.Variable(0, trainable=False) + self.optimizer = optimizer(learning_rate=tf.train.exponential_decay(learning_rate, global_step, 1000000, learning_decay_rate, staircase=True)) + self.train_op = self.optimizer.minimize(self.loss, global_step=global_step) + self.saver = tf.train.Saver() + self.session.run(tf.global_variables_initializer(), feed_dict={self.embeddings_placeholder: embeddings}) + + def train_step(self, input_x, input_y): + _, loss = self.session.run([self.train_op, self.loss], feed_dict={self.input_x: input_x, self.input_y: input_y, self.dropout_keep_prob: self.dropout_keep_prob_train}) + return loss + + def feed(self, input_x): + return self.session.run([self.output, self.predictions], feed_dict={self.input_x: input_x, self.dropout_keep_prob: 1}) diff --git a/dataset/preprocessed/670.txt b/dataset/preprocessed/670.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/670.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/671.txt b/dataset/preprocessed/671.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/671.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/672.txt b/dataset/preprocessed/672.txt new file mode 100644 index 0000000..bef0f3c --- /dev/null +++ b/dataset/preprocessed/672.txt @@ -0,0 +1,85 @@ +import tensorflow as tf +import numpy as np +import logging + + +class CNN: + """ + Convolutional Neural Network model. + """ + + def __init__(self, num_actions, observation_shape, verbose=False): + """ + Initialize the CNN model with a set of parameters. + Args: + params: a dictionary containing values of the models' parameters. + """ + self.verbose = verbose + self.num_actions = num_actions + self.observation_shape = observation_shape[0] + self.lr = 0.0001 + self.reg = 0.001 + self.hidden_size = 400 + self.session = self.create_model() + + def add_placeholders(self): + input_placeholder = tf.placeholder(tf.float32, shape=(None, self.observation_shape)) + labels_placeholder = tf.placeholder(tf.float32, shape=(None,)) + actions_placeholder = tf.placeholder(tf.float32, shape=(None, self.num_actions)) + return input_placeholder, labels_placeholder, actions_placeholder + + def nn(self, input_obs): + with tf.name_scope('Layer1') as scope: + W1shape = [self.observation_shape, self.hidden_size] + W1 = tf.get_variable('W1', shape=W1shape) + bshape = [1, self.hidden_size] + b1 = tf.get_variable('b1', shape=bshape, initializer=tf.constant_initializer(0.0)) + with tf.name_scope('Layer3') as scope: + W3shape = [self.hidden_size, 12] + W3 = tf.get_variable('W3', shape=W3shape) + bshape = [1, 12] + b3 = tf.get_variable('b3', shape=bshape, initializer=tf.constant_initializer(0.0)) + with tf.name_scope('OutputLayer') as scope: + Ushape = [12, self.num_actions] + U = tf.get_variable('U', shape=Ushape) + b3shape = [1, self.num_actions] + b4 = tf.get_variable('b4', shape=b3shape, initializer=tf.constant_initializer(0.0)) + xW = tf.matmul(input_obs, W1) + h = tf.nn.softsign(tf.add(xW, b1)) + xW = tf.matmul(h, W3) + h = tf.nn.softsign(tf.add(xW, b3)) + hU = tf.matmul(h, U) + out = tf.add(hU, b4) + reg = self.reg * (tf.reduce_sum(tf.square(W1)) + tf.reduce_sum(tf.square(W3)) + tf.reduce_sum(tf.square(U))) + return out, reg + + def create_model(self): + """ + The model definition. + """ + self.input_placeholder, self.labels_placeholder, self.actions_placeholder = self.add_placeholders() + outputs, reg = self.nn(self.input_placeholder) + self.predictions = outputs + self.q_vals = tf.reduce_sum(tf.mul(self.predictions, self.actions_placeholder), 1) + self.loss = tf.reduce_sum(tf.square(self.labels_placeholder - self.q_vals)) + reg + optimizer = tf.train.AdamOptimizer(learning_rate=self.lr, use_locking=True) + self.train_op = optimizer.minimize(self.loss) + init = tf.initialize_all_variables() + session = tf.Session() + session.run(init) + return session + + def train_step(self, Xs, ys, actions): + """ + Updates the CNN model with a mini batch of training examples. + """ + loss, _, prediction_probs, q_values = self.session.run([self.loss, self.train_op, self.predictions, self.q_vals], feed_dict={self.input_placeholder: Xs, self.labels_placeholder: ys, self.actions_placeholder: actions}) + + def predict(self, observation): + """ + Predicts the rewards for an input observation state. + Args: + observation: a numpy array of a single observation state + """ + loss, prediction_probs = self.session.run([self.loss, self.predictions], feed_dict={self.input_placeholder: observation, self.labels_placeholder: np.zeros(len(observation)), self.actions_placeholder: np.zeros((len(observation), self.num_actions))}) + return prediction_probs diff --git a/dataset/preprocessed/673.txt b/dataset/preprocessed/673.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/673.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/674.txt b/dataset/preprocessed/674.txt new file mode 100644 index 0000000..8540631 --- /dev/null +++ b/dataset/preprocessed/674.txt @@ -0,0 +1,156 @@ +import tensorflow as tf +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + self.saver = tf.train.Saver() + self.saved_path = './saved_model' + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + def variable_output(self, session): + form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + def save(self, session): + self.saved_path = self.saver.save(session, './saved_model') + + def restore(self, session): + self.saver.restore(session, self.saved_path) + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def initialize_from_sl(self, session, form_emb, pos_emb, deprel_emb, W0, b0, W1, b1): + session.run([self.form_emb.assign(form_emb), self.pos_emb.assign(pos_emb), self.deprel_emb.assign(deprel_emb), self.W0.assign(W0), self.b0.assign(b0), self.W1.assign(W1), self.b1.assign(b1)]) + session.run(self.update_op) diff --git a/dataset/preprocessed/675.txt b/dataset/preprocessed/675.txt new file mode 100644 index 0000000..d0a165d --- /dev/null +++ b/dataset/preprocessed/675.txt @@ -0,0 +1,226 @@ +from plot_functions import * +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt +import time + + +def make_model1(X, isTrain): + W1 = tf.get_variable('W1', shape=[108, 6], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 111))) + L1 = tf.matmul(X, W1) + return L1 + + +def make_model2(X, isTrain): + W1 = tf.get_variable('W1', shape=[111, 256], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 111))) + L1 = tf.matmul(X, W1) + L1 = tf.layers.batch_normalization(L1, training=isTrain) + L1 = tf.nn.relu(L1) + W2 = tf.get_variable('W2', shape=[256, 512], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 256))) + L2 = tf.matmul(L1, W2) + L2 = tf.layers.batch_normalization(L2, training=isTrain) + L2 = tf.nn.relu(L2) + W3 = tf.get_variable('W3', shape=[512, 1024], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 512))) + L3 = tf.matmul(L2, W3) + L3 = tf.layers.batch_normalization(L3, training=isTrain) + L3 = tf.nn.relu(L3) + W4 = tf.get_variable('W4', shape=[1024, 3], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 1024))) + L4 = tf.matmul(L3, W4) + return L4 + + +def make_model3(X, isTrain): + W1 = tf.get_variable('W1', shape=[111, 256], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 111))) + L1 = tf.matmul(X, W1) + L1 = tf.layers.batch_normalization(L1, training=isTrain) + L1 = tf.nn.relu(L1) + W2 = tf.get_variable('W2', shape=[256, 512], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 256))) + L2 = tf.matmul(L1, W2) + L2 = tf.layers.batch_normalization(L2, training=isTrain) + L2 = tf.nn.relu(L2) + W3 = tf.get_variable('W3', shape=[512, 512], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 512))) + L3 = tf.matmul(L2, W3) + L3 = tf.layers.batch_normalization(L3, training=isTrain) + L3 = tf.nn.relu(L3) + W4 = tf.get_variable('W4', shape=[512, 512], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 512))) + L4 = tf.matmul(L3, W4) + L4 = tf.layers.batch_normalization(L4, training=isTrain) + L4 = tf.nn.relu(L4) + W5 = tf.get_variable('W5', shape=[512, 1024], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 512))) + L5 = tf.matmul(L4, W5) + L5 = tf.layers.batch_normalization(L5, training=isTrain) + L5 = tf.nn.relu(L5) + W6 = tf.get_variable('W6', shape=[1024, 1024], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 1024))) + L6 = tf.matmul(L5, W6) + L6 = tf.layers.batch_normalization(L6, training=isTrain) + L6 = tf.nn.relu(L6) + W7 = tf.get_variable('W7', shape=[1024, 512], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 1024))) + L7 = tf.matmul(L6, W7) + L7 = tf.layers.batch_normalization(L7, training=isTrain) + L7 = tf.nn.relu(L7) + W8 = tf.get_variable('W8', shape=[512, 3], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 512))) + L8 = tf.matmul(L7, W8) + return L8 + + +def make_model4(X, isTrain): + W1 = tf.get_variable('W1', shape=[111, 256], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 111))) + B1 = tf.get_variable('B1', shape=[256], dtype=np.float32, initializer=tf.zeros_initializer()) + L1 = tf.matmul(X, W1) + L1 = tf.nn.bias_add(L1, B1) + L1 = tf.nn.relu(L1) + W2 = tf.get_variable('W2', shape=[256, 512], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 256))) + B2 = tf.get_variable('B2', shape=[512], dtype=np.float32, initializer=tf.zeros_initializer()) + L2 = tf.matmul(L1, W2) + L2 = tf.nn.bias_add(L2, B2) + L2 = tf.nn.relu(L2) + W3 = tf.get_variable('W3', shape=[512, 1024], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 512))) + B3 = tf.get_variable('B3', shape=[1024], dtype=np.float32, initializer=tf.zeros_initializer()) + L3 = tf.matmul(L2, W3) + L3 = tf.nn.bias_add(L3, B3) + L3 = tf.nn.relu(L3) + W4 = tf.get_variable('W4', shape=[1024, 3], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 1024))) + L4 = tf.matmul(L3, W4) + return L4 + + +def make_model5(X, isTrain): + W1 = tf.get_variable('W1', shape=[216, 256], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 111))) + B1 = tf.get_variable('B1', shape=[256], dtype=np.float32, initializer=tf.zeros_initializer()) + L1 = tf.matmul(X, W1) + L1 = tf.nn.bias_add(L1, B1) + L1 = tf.nn.relu(L1) + W2 = tf.get_variable('W2', shape=[256, 12], dtype=np.float32, initializer=tf.random_normal_initializer(0, tf.sqrt(2 / 256))) + L2 = tf.matmul(L1, W2) + return L2 + + +def make_train_graph(input, label, is_training, gpu_num, split_num): + input_list = tf.split(input, gpu_num * split_num) + label_list = tf.split(label, gpu_num * split_num) + logit_list = [] + sum_L2_list = [] + ED_list = [] + pck_list = [] + iter = -1 + for d in range(gpu_num): + with tf.device('/gpu:' + str(d)): + for i in range(split_num): + iter = iter + 1 + with tf.variable_scope(tf.get_variable_scope(), reuse=iter > 0): + logit = make_model5(input_list[iter], is_training) + loss_L2 = tf.pow(logit - label_list[iter], 2) + sum_L2 = tf.reduce_sum(loss_L2) + ED = tf.sqrt(tf.reduce_sum(loss_L2, axis=1)) + pck = tf.to_float(tf.greater(0.3, ED)) + logit_list.append(logit) + sum_L2_list.append(sum_L2) + ED_list.append(ED) + pck_list.append(pck) + total_logit = tf.concat(logit_list, axis=0) + total_sum_L2 = tf.reduce_sum(sum_L2_list) + total_ED = tf.concat(ED_list, axis=0) + total_pck = tf.concat(pck_list, axis=0) + with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): + train_op = tf.train.AdamOptimizer(0.0002).minimize(sum_L2, colocate_gradients_with_ops=True) + return train_op, total_sum_L2, total_logit, total_ED, total_pck + + +def train(input, label, max_epoch, gpu_num, split_num): + X = tf.placeholder(tf.float32, [None, 111]) + Y = tf.placeholder(tf.float32, [None, 3]) + is_training = tf.placeholder(tf.bool) + train_op, sum_L2, logit_, loss_ED, pck = make_train_graph(X, Y, is_training, gpu_num, split_num) + mean_ED = tf.reduce_mean(loss_ED) + sess = tf.Session() + saver = tf.train.Saver(tf.global_variables()) + ckpt = tf.train.get_checkpoint_state('./model') + if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): + saver.restore(sess, ckpt.model_checkpoint_path) + else: + sess.run(tf.global_variables_initializer()) + for epoch in range(max_epoch): + _, ED = sess.run([train_op, mean_ED], feed_dict={X: input, Y: label, is_training: True}) + if epoch + 1 % 100 == 0: + saver.save(sess, './model/model.ckpt') + print('saved') + print('[%d/%d] - mean ED: %.3f' % (epoch + 1, max_epoch, ED)) + + +def inference(gpu_num=2, split_num=1): + input_batch = np.load('test_input_batch2.npy') + label_batch = np.load('test_label_batch2.npy') + test_len = len(input_batch) + if test_len % (gpu_num * split_num) != 0: + input_batch = input_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + label_batch = label_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + log_txt = open('inference_loss.txt', 'a') + X = tf.placeholder(tf.float32, [None, 108]) + Y = tf.placeholder(tf.float32, [None, 6]) + is_training = tf.placeholder(tf.bool) + train_op, sum_L2, logit_, loss_ED, pck_ = make_train_graph(X, Y, is_training, gpu_num, split_num) + sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) + saver = tf.train.Saver(tf.global_variables()) + ckpt = tf.train.get_checkpoint_state('./model') + if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): + saver.restore(sess, ckpt.model_checkpoint_path) + else: + sess.run(tf.global_variables_initializer()) + ED, pck = sess.run([loss_ED, pck_], feed_dict={X: input_batch, Y: label_batch, is_training: False}) + for i in range(len(input_batch)): + log_txt.write('\n[%d] - %.3f / %.3f' % (i, ED[i], pck[i])) + log_txt.close() + print('mean ED: %.3f, mean PCK: %.3f' % (np.mean(ED), np.mean(pck))) + + +def cross_val(max_epoch, gpu_num=2, split_num=1, already_done_epoch=0): + input_batch = np.load('input_batch3.npy') + label_batch = np.load('label_batch3.npy') + test_input_batch = np.load('test_input_batch3.npy') + test_label_batch = np.load('test_label_batch3.npy') + train_len = len(input_batch) + test_len = len(test_input_batch) + if train_len % (gpu_num * split_num) != 0: + input_batch = input_batch[:train_len // (gpu_num * split_num) * (gpu_num * split_num)] + label_batch = label_batch[:train_len // (gpu_num * split_num) * (gpu_num * split_num)] + if test_len % (gpu_num * split_num) != 0: + test_input_batch = test_input_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + test_label_batch = test_label_batch[:test_len // (gpu_num * split_num) * (gpu_num * split_num)] + log_txt = open('loss.txt', 'a') + X = tf.placeholder(tf.float32, [None, 216]) + Y = tf.placeholder(tf.float32, [None, 12]) + is_training = tf.placeholder(tf.bool) + train_op, sum_L2, logit_, loss_ED, pck_ = make_train_graph(X, Y, is_training, gpu_num, split_num) + mean_ED = tf.reduce_mean(loss_ED) + mean_pck = tf.reduce_mean(pck_) + a = time.time() + sess = tf.Session() + b = time.time() + print(b - a) + saver = tf.train.Saver(tf.global_variables()) + ckpt = tf.train.get_checkpoint_state('./model') + if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): + saver.restore(sess, ckpt.model_checkpoint_path) + else: + sess.run(tf.global_variables_initializer()) + start_time = time.time() + for epoch in range(max_epoch - already_done_epoch): + _, ED, pck = sess.run([train_op, mean_ED, mean_pck], feed_dict={X: input_batch, Y: label_batch, is_training: True}) + info_txt = '\n[%d/%d] - mean of train ED: %.3f // train PCK: %.3f' % (epoch + 1 + already_done_epoch, max_epoch, ED, pck) + log_txt.write(info_txt) + print(info_txt) + ED, pck = sess.run([mean_ED, mean_pck], feed_dict={X: test_input_batch, Y: test_label_batch, is_training: False}) + info_txt = ' /// mean of test ED: %.3f //// test PCK: %.3f\n' % (ED, pck) + log_txt.write(info_txt) + print(info_txt) + if epoch % 100 == 0: + end_time = time.time() + print(end_time - start_time) + saver.save(sess, './model/model.ckpt') + start_time = end_time + log_txt.close() + saver.save(sess, './model/model.ckpt') + + +cross_val(100000, 2, 1, 0) +epoch_ED_plot_from_txt('loss.txt') diff --git a/dataset/preprocessed/676.txt b/dataset/preprocessed/676.txt new file mode 100644 index 0000000..615b3a0 --- /dev/null +++ b/dataset/preprocessed/676.txt @@ -0,0 +1,432 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + """ + + def __init__(self, hidden_size, dropout): + self.hidden_size = hidden_size + self.dropout = dropout + + def encode(self, inputs, masks, attention_inputs=None, model_type='gru', name='encoder', reuse=False): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(name, reuse=reuse): + if attention_inputs is None: + if model_type == 'gru': + cell_fw = tf.nn.rnn_cell.GRUCell(self.hidden_size) + cell_bw = tf.nn.rnn_cell.GRUCell(self.hidden_size) + elif model_type == 'lstm': + cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + else: + raise Exception('Must specify model type.') + else: + raise Exception('Attention not implemented.') + cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, output_keep_prob=self.dropout) + cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, output_keep_prob=self.dropout) + outputs, final_state = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length=masks, dtype=tf.float32) + return outputs, final_state + + +class Decoder(object): + + def __init__(self, hidden_size, output_size, dropout): + self.hidden_size = hidden_size + self.output_size = output_size + self.dropout = dropout + + def decode(self, knowledge_rep, masks, size, model_type='gru'): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + with vs.variable_scope('decoder'): + rep_reshaped = tf.reshape(knowledge_rep, [-1, size]) + with vs.variable_scope('answer_start'): + start_probs = tf.nn.rnn_cell._linear(rep_reshaped, output_size=1, bias=True) + start_probs = tf.reshape(start_probs, [-1, self.output_size]) + with vs.variable_scope('answer_end'): + end_probs = tf.nn.rnn_cell._linear(rep_reshaped, output_size=1, bias=True) + end_probs = tf.reshape(end_probs, [-1, self.output_size]) + bool_masks = tf.cast(tf.sequence_mask(masks, maxlen=self.output_size), tf.float32) + add_mask = -1e+30 * (1.0 - bool_masks) + start_probs = tf.add(start_probs, add_mask) + end_probs = tf.add(end_probs, add_mask) + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, pretrained_embeddings, flags): + """ + Initializes your System + + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.flags = flags + self.h_size = self.flags.state_size + self.p_size = self.flags.output_size + self.q_size = self.flags.question_size + self.embed_size = self.flags.embedding_size + self.dropout = 1.0 - self.flags.dropout + self.encoder = Encoder(hidden_size=self.h_size, dropout=1.0 - self.flags.dropout) + self.decoder = Decoder(hidden_size=self.h_size, output_size=self.p_size, dropout=1.0 - self.flags.dropout) + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.p_size), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.q_size), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + self.saver = tf.train.Saver() + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def filter(self, Q, P): + with vs.variable_scope('filter'): + Qn = tf.nn.l2_normalize(Q, dim=2) + Pn = tf.nn.l2_normalize(P, dim=2) + R = batch_matmul(Qn, tf.transpose(Pn, perm=[0, 2, 1])) + r = tf.reduce_max(R, axis=1) + r = tf.expand_dims(r, axis=2) + P_filtered = P * r + return P_filtered + + def matching_layer(self, Q_fw, Q_bw, P_fw, P_bw, num_perspectives): + with vs.variable_scope('matching'): + Q_fw_final = Q_fw[:, (-1), :] + Q_bw_final = Q_bw[:, (-1), :] + Q_fw_n = tf.nn.l2_normalize(Q_fw, dim=2) + Q_bw_n = tf.nn.l2_normalize(Q_bw, dim=2) + Q_fw_final_n = tf.nn.l2_normalize(Q_fw_final, dim=1) + Q_bw_final_n = tf.nn.l2_normalize(Q_bw_final, dim=1) + P_fw_n = tf.nn.l2_normalize(P_fw, dim=2) + P_bw_n = tf.nn.l2_normalize(P_bw, dim=2) + Q_fw_final_n = tf.expand_dims(Q_fw_final_n, 2) + M_full_fw = batch_matmul(P_fw_n, Q_fw_final_n) + Q_bw_final_n = tf.expand_dims(Q_bw_final_n, 2) + M_full_bw = batch_matmul(P_bw_n, Q_bw_final_n) + M_max_fw = tf.reduce_max(batch_matmul(P_fw_n, tf.transpose(Q_fw_n, perm=[0, 2, 1])), axis=2, keep_dims=True) + M_max_bw = tf.reduce_max(batch_matmul(P_bw_n, tf.transpose(Q_bw_n, perm=[0, 2, 1])), axis=2, keep_dims=True) + M_mean_fw = tf.reduce_mean(batch_matmul(P_fw_n, tf.transpose(Q_fw_n, perm=[0, 2, 1])), axis=2, keep_dims=True) + M_mean_bw = tf.reduce_mean(batch_matmul(P_bw_n, tf.transpose(Q_bw_n, perm=[0, 2, 1])), axis=2, keep_dims=True) + M = [] + M.append(M_full_fw) + M.append(M_full_bw) + M.append(M_max_fw) + M.append(M_max_bw) + M.append(M_mean_fw) + M.append(M_mean_bw) + M = tf.concat(2, M) + return P_fw_n + P_bw_n + + def aggregation_layer(self, M, masks, size): + with tf.variable_scope('aggregation'): + cell_fw = tf.nn.rnn_cell.BasicLSTMCell(size) + cell_bw = tf.nn.rnn_cell.BasicLSTMCell(size) + cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, output_keep_prob=self.dropout) + cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, output_keep_prob=self.dropout) + outputs, final_state = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, M, sequence_length=masks, dtype=tf.float32) + concat_states = tf.concat(2, outputs) + return concat_states + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + P_filtered = self.filter(Q=self.question_embeddings, P=self.context_embeddings) + question_states, final_question_state = self.encoder.encode(self.question_embeddings, self.mask_q_placeholder, attention_inputs=None, model_type=self.flags.model_type, reuse=False) + question_states_fw, question_states_bw = question_states + ctx_states, final_ctx_state = self.encoder.encode(P_filtered, self.mask_ctx_placeholder, attention_inputs=None, model_type=self.flags.model_type, reuse=True) + ctx_states_fw, ctx_states_bw = ctx_states + M = self.matching_layer(Q_fw=question_states_fw, Q_bw=question_states_bw, P_fw=ctx_states_fw, P_bw=ctx_states_bw, num_perspectives=self.flags.num_perspectives) + A = self.aggregation_layer(M=M, masks=self.mask_ctx_placeholder, size=self.h_size) + self.start_probs, self.end_probs = self.decoder.decode(knowledge_rep=A, masks=self.mask_ctx_placeholder, model_type=self.flags.model_type, size=2 * self.h_size) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + if self.flags.batch_norm == 0: + self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + else: + question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + q_mean, q_var = tf.nn.moments(question_embeddings, axes=[2], shift=None, keep_dims=True) + ctx_mean, ctx_var = tf.nn.moments(context_embeddings, axes=[2], shift=None, keep_dims=True) + q_scale = tf.get_variable('q_scale', shape=[self.q_size, self.embed_size], initializer=tf.ones_initializer(tf.float32)) + ctx_scale = tf.get_variable('ctx_scale', shape=[self.p_size, self.embed_size], initializer=tf.ones_initializer(tf.float32)) + q_offset = tf.get_variable('q_offset', shape=[self.q_size, self.embed_size], initializer=tf.ones_initializer(tf.float32)) + ctx_offset = tf.get_variable('ctx_offset', shape=[self.p_size, self.embed_size], initializer=tf.ones_initializer(tf.float32)) + self.question_embeddings = tf.nn.batch_normalization(question_embeddings, q_mean, q_var, q_offset, q_scale, variance_epsilon=1e-06) + self.context_embeddings = tf.nn.batch_normalization(context_embeddings, ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon=1e-06) + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs + + def answer(self, session, data): + yp_lst = [] + yp2_lst = [] + prog_train = Progbar(target=1 + int(len(data[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle=False)): + yp, yp2 = self.decode(session, *batch) + yp_lst.append(yp) + yp2_lst.append(yp2) + prog_train.update(i + 1, [('computing F1...', 1)]) + print('') + yp_all = np.concatenate(yp_lst, axis=0) + yp2_all = np.concatenate(yp2_lst, axis=0) + a_s = np.argmax(yp_all, axis=1) + a_e = np.argmax(yp2_all, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(session=sess, context_batch=context_batch, question_batch=question_batch, answer_span_batch=answer_span_batch, mask_ctx_batch=mask_ctx_batch, mask_q_batch=mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset[0]) + else: + inds = np.random.choice(len(dataset[0]), sample) + sampled = [elem[inds] for elem in dataset] + a_s, a_e = self.answer(session, sampled) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + f1 = [] + em = [] + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + cur_em = exact_match_score(pred_words, actual_words) + em.append(float(cur_em)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + return np.mean(f1), np.mean(em) + + def run_epoch(self, sess, train_set, val_set, train_context, val_context): + prog_train = Progbar(target=1 + int(len(train_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + print('') + prog_val = Progbar(target=1 + int(len(val_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + self.evaluate_answer(session=sess, dataset=train_set, context=train_context, sample=len(val_set[0]), log=True, eval_set='-Epoch TRAIN-') + self.evaluate_answer(session=sess, dataset=val_set, context=val_context, sample=None, log=True, eval_set='-Epoch VAL-') + + def train(self, session, dataset, val_dataset, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = [elem[:self.flags.batch_size] for elem in train_dataset] + val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + num_epochs = 100 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, train_context=train_context, val_context=val_context) + logging.info('Saving model in %s', train_dir) + self.saver.save(session, train_dir + '/qa.ckpt') + + def minibatches(self, data, batch_size, shuffle=True): + num_data = len(data[0]) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + indices = np.arange(num_data) + if shuffle: + np.random.shuffle(indices) + for minibatch_start in np.arange(0, num_data, batch_size): + minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] diff --git a/dataset/preprocessed/677.txt b/dataset/preprocessed/677.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/677.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/678.txt b/dataset/preprocessed/678.txt new file mode 100644 index 0000000..b76cc03 --- /dev/null +++ b/dataset/preprocessed/678.txt @@ -0,0 +1,161 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from neural.full_connect import load_reformat_not_mnist, accuracy + + +def tf_better_nn(offset_range=-1, regular=False, drop_out=False, lrd=False): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_node_count = 1024 + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count])) + biases1 = tf.Variable(tf.zeros([hidden_node_count])) + weights2 = tf.Variable(tf.truncated_normal([hidden_node_count, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + ys = tf.matmul(tf_train_dataset, weights1) + biases1 + hidden = tf.nn.relu(ys) + h_fc = hidden + valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + valid_hidden1 = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + test_hidden1 = tf.nn.relu(test_y0) + keep_prob = tf.placeholder(tf.float32) + if drop_out: + hidden_drop = tf.nn.dropout(hidden, keep_prob) + h_fc = hidden_drop + logits = tf.matmul(h_fc, weights2) + biases2 + logits_predict = tf.matmul(hidden, weights2) + biases2 + valid_predict = tf.matmul(valid_hidden1, weights2) + biases2 + test_predict = tf.matmul(test_hidden1, weights2) + biases2 + l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(biases1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(biases2) + if not regular: + l2_loss = 0 + beta = 0.002 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + beta * l2_loss + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + if lrd: + cur_step = tf.Variable(0) + starter_learning_rate = 0.1 + learning_rate = tf.train.exponential_decay(starter_learning_rate, cur_step, 10000, 0.96, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=cur_step) + train_prediction = tf.nn.softmax(logits_predict) + valid_prediction = tf.nn.softmax(valid_predict) + test_prediction = tf.nn.softmax(test_predict) + num_steps = 30001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + if offset_range == -1: + offset_range = train_labels.shape[0] - batch_size + offset = step * batch_size % offset_range + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +def tf_deep_nn(regular=False, drop_out=False, lrd=False, layer_cnt=2): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_node_count = 1024 + hidden_stddev = np.sqrt(2.0 / 784) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_node_count], stddev=hidden_stddev)) + biases1 = tf.Variable(tf.zeros([hidden_node_count])) + weights = [] + biases = [] + hidden_cur_cnt = hidden_node_count + for i in range(layer_cnt - 2): + if hidden_cur_cnt > 2: + hidden_next_cnt = int(hidden_cur_cnt / 2) + else: + hidden_next_cnt = 2 + hidden_stddev = np.sqrt(2.0 / hidden_cur_cnt) + weights.append(tf.Variable(tf.truncated_normal([hidden_cur_cnt, hidden_next_cnt], stddev=hidden_stddev))) + biases.append(tf.Variable(tf.zeros([hidden_next_cnt]))) + hidden_cur_cnt = hidden_next_cnt + y0 = tf.matmul(tf_train_dataset, weights1) + biases1 + hidden = tf.nn.relu(y0) + hidden_drop = hidden + keep_prob = 0.5 + if drop_out: + hidden_drop = tf.nn.dropout(hidden, keep_prob) + valid_y0 = tf.matmul(tf_valid_dataset, weights1) + biases1 + valid_hidden = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(tf_test_dataset, weights1) + biases1 + test_hidden = tf.nn.relu(test_y0) + for i in range(layer_cnt - 2): + y1 = tf.matmul(hidden_drop, weights[i]) + biases[i] + hidden_drop = tf.nn.relu(y1) + if drop_out: + keep_prob += 0.5 * i / (layer_cnt + 1) + hidden_drop = tf.nn.dropout(hidden_drop, keep_prob) + y0 = tf.matmul(hidden, weights[i]) + biases[i] + hidden = tf.nn.relu(y0) + valid_y0 = tf.matmul(valid_hidden, weights[i]) + biases[i] + valid_hidden = tf.nn.relu(valid_y0) + test_y0 = tf.matmul(test_hidden, weights[i]) + biases[i] + test_hidden = tf.nn.relu(test_y0) + weights2 = tf.Variable(tf.truncated_normal([hidden_cur_cnt, num_labels], stddev=hidden_stddev / 2)) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_drop, weights2) + biases2 + logits_predict = tf.matmul(hidden, weights2) + biases2 + valid_predict = tf.matmul(valid_hidden, weights2) + biases2 + test_predict = tf.matmul(test_hidden, weights2) + biases2 + l2_loss = 0 + if regular: + l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + for i in range(len(weights)): + l2_loss += tf.nn.l2_loss(weights[i]) + beta = 0.25 / batch_size + beta = 1e-05 + l2_loss *= beta + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + l2_loss + if lrd: + cur_step = tf.Variable(0, trainable=False) + starter_learning_rate = 0.4 + learning_rate = tf.train.exponential_decay(starter_learning_rate, cur_step, 100000, 0.96, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=cur_step) + else: + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits_predict) + valid_prediction = tf.nn.softmax(valid_predict) + test_prediction = tf.nn.softmax(test_predict) + num_steps = 20001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset_range = train_labels.shape[0] - batch_size + offset = step * batch_size % offset_range + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + + +if __name__ == '__main__': + image_size = 28 + num_labels = 10 + train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = load_reformat_not_mnist(image_size, num_labels) + tf_deep_nn(layer_cnt=6, lrd=True, drop_out=True, regular=True) diff --git a/dataset/preprocessed/679.txt b/dataset/preprocessed/679.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/679.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/68.txt b/dataset/preprocessed/68.txt new file mode 100644 index 0000000..1c59c39 --- /dev/null +++ b/dataset/preprocessed/68.txt @@ -0,0 +1,132 @@ +import tensorflow as tf +import dataset +import random +import sys +from tensorflow.python import debug as tf_debug +NUM_CLASSES = len(dataset.CLASSES) +IMAGE_SIZE = 100 + + +def placeholder_inputs(batch_size): + images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_SIZE, IMAGE_SIZE, 3), name='images') + labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size,), name='labels') + return images_placeholder, labels_placeholder + + +def get_weights(shape): + initial = tf.truncated_normal(shape, stddev=0.1) + return tf.Variable(initial, name='Weights') + + +def get_biases(shape): + initial = tf.zeros(shape) + return tf.Variable(initial, name='Biases') + + +def conv_layer(input, filter_size, name, strides=[1, 1, 1, 1], pool_size=2, padding='SAME'): + with tf.name_scope(name): + weights = get_weights(filter_size) + biases = get_biases([filter_size[3]]) + conv = tf.nn.leaky_relu(tf.nn.conv2d(input, weights, strides, padding)) + pool = tf.nn.max_pool(conv, [1, pool_size, pool_size, 1], [1, pool_size, pool_size, 1], padding) + return pool + + +def dense_layer(inputs, input_size, output_size, name): + with tf.name_scope(name): + weights = get_weights([input_size, output_size]) + biases = get_biases([output_size]) + h = tf.matmul(inputs, weights) + biases + return tf.nn.leaky_relu(h) + + +def define_model(images): + conv1 = conv_layer(images, [20, 20, 3, 10], 'Conv1', pool_size=4) + conv2 = conv_layer(conv1, [5, 5, 10, 20], 'Conv2') + conv2 = tf.reshape(conv2, [-1, 13 * 13 * 20]) + dense1 = dense_layer(conv2, 13 * 13 * 20, 1000, 'Dense1') + dense2 = dense_layer(dense1, 1000, 100, 'Dense2') + logits = dense_layer(dense2, 100, NUM_CLASSES, 'Softmax_Linear') + return logits + + +def define_loss(logits, labels): + labels = tf.to_int64(labels) + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits, name='xentropy') + loss = tf.reduce_mean(cross_entropy, name='xentropy_mean') + return loss + + +def training(loss, learning_rate, momentum=0.75, beta1=0.9, beta2=0.999): + tf.summary.scalar('loss', loss) + optimizer = tf.train.AdamOptimizer(learning_rate, beta1=beta1, beta2=beta2) + train_op = optimizer.minimize(loss) + return train_op + + +def evaluation(logits, labels): + correct = tf.nn.in_top_k(logits, labels, 1) + return tf.reduce_sum(tf.cast(correct, tf.int32)) + + +def do_evaluation(session, eval_correct, testDataset, batch_size, images_placeholder, labels_placeholder): + training_data, training_labels = testDataset + batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + totalCorrect = 0 + for batch in batches: + image_data = batch[0] + label_data = batch[1] + feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + totalCorrect += session.run(eval_correct, feed_dict=feed_dict) + precision = float(totalCorrect) / len(training_data) + print('Num examples: %d Num correct: %d Precision @ 1: %0.04f' % (len(training_data), totalCorrect, precision)) + sys.stdout.flush() + return precision > 0.95 + + +def run_training(batch_size, learning_rate, epochs, run_number): + with tf.Graph().as_default(): + images_placeholder, labels_placeholder = placeholder_inputs(batch_size) + logits = define_model(images_placeholder) + lossFunction = define_loss(logits, labels_placeholder) + train_op = training(lossFunction, learning_rate) + eval_correct = evaluation(logits, labels_placeholder) + saver = tf.train.Saver() + init = tf.global_variables_initializer() + with tf.Session() as session: + logdir = 'log/' + str(run_number) + session.run(init) + for step in range(epochs): + training_data, training_labels = dataset.get_training_data() + batches = [(training_data[i:i + batch_size], training_labels[i:i + batch_size]) for i in range(0, len(training_data), batch_size)] + epochLoss = 0 + for batch in batches: + image_data = batch[0] + label_data = batch[1] + feed_dict = {images_placeholder: image_data, labels_placeholder: label_data} + activations, loss_value = session.run([train_op, lossFunction], feed_dict=feed_dict) + epochLoss += loss_value + if step % 2 == 0: + print('Step %d: loss = %.2f' % (step, loss_value)) + sys.stdout.flush() + early_stop = False + if (step + 1) % 5 == 0 or step + 1 == epochs: + validation_data = dataset.get_validation_data(batch_size) + print('Doing evaluation on validation Set') + sys.stdout.flush() + early_stop = do_evaluation(session, eval_correct, validation_data, batch_size, images_placeholder, labels_placeholder) + if step + 1 == epochs or early_stop: + print('Doing evaluation on training set') + sys.stdout.flush() + do_evaluation(session, eval_correct, (training_data, training_labels), batch_size, images_placeholder, labels_placeholder) + print('Doing evaluation on the test set') + sys.stdout.flush() + test_data = dataset.get_test_data(batch_size) + do_evaluation(session, eval_correct, test_data, batch_size, images_placeholder, labels_placeholder) + saver.save(session, 'model.ckpt') + if early_stop: + print('Achieved desired precision at step %d' % step) + return + + +run_training(89, 0.001, 40, 13) diff --git a/dataset/preprocessed/680.txt b/dataset/preprocessed/680.txt new file mode 100644 index 0000000..ee4ee99 --- /dev/null +++ b/dataset/preprocessed/680.txt @@ -0,0 +1,369 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import matplotlib.pyplot as plt +get_ipython().magic('matplotlib inline') +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (logistic)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_steps = 3001 +regul_val = [pow(10, i) for i in np.arange(-4, -2, 0.1)] +accuracy_val = [] +for regul in regul_val: + with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: regul} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracy_val.append(accuracy(test_prediction.eval(), test_labels)) +plt.semilogx(regul_val, accuracy_val) +plt.grid(True) +plt.title('Test accuracy by regularization (1-layer net)') +plt.show() +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta_regul = tf.placeholder(tf.float32) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + logits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_bacthes = 3 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_bacthes + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta_regul: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + drop1 = tf.nn.dropout(lay1_train, 0.5) + logits = tf.matmul(drop1, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 101 +num_batches = 3 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_batches + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + logits = tf.matmul(lay2_train, weights3) + biases3 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_hidden_nodes3], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + weights4 = tf.Variable(tf.truncated_normal([num_hidden_nodes3, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes3))) + biases4 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + logits = tf.matmul(lay3_train, weights4) + biases4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 4000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 18001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 512 +num_hidden_nodes3 = 256 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_hidden_nodes3], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + weights4 = tf.Variable(tf.truncated_normal([num_hidden_nodes3, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes3))) + biases4 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + drop1 = tf.nn.dropout(lay1_train, 0.5) + lay2_train = tf.nn.relu(tf.matmul(drop1, weights2) + biases2) + drop2 = tf.nn.dropout(lay2_train, 0.5) + lay3_train = tf.nn.relu(tf.matmul(drop2, weights3) + biases3) + drop3 = tf.nn.dropout(lay3_train, 0.5) + logits = tf.matmul(drop3, weights4) + biases4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 5000, 0.8, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 20001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/681.txt b/dataset/preprocessed/681.txt new file mode 100644 index 0000000..bb0d40a --- /dev/null +++ b/dataset/preprocessed/681.txt @@ -0,0 +1,85 @@ +import tensorflow as tf +import math +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) + + +def weight_variable(shape, stddev): + x = tf.truncated_normal(shape, stddev=stddev) + return tf.Variable(x) + + +def bias_variable(shape): + x = tf.zeros(shape) + return tf.Variable(x) + + +print('Building Graph') +train_input = tf.placeholder(tf.float32, shape=[None, 784]) +train_output = tf.placeholder(tf.float32, shape=[None, 10]) +dropout = tf.placeholder(tf.float32) +weight1 = weight_variable([5, 5, 1, 8], 1.0) +bias1 = bias_variable([8]) +train_image = tf.reshape(train_input, [-1, 28, 28, 1]) +hidden1 = tf.nn.relu(tf.nn.conv2d(train_image, weight1, strides=[1, 1, 1, 1], padding='SAME') + bias1) +hiddenp1 = tf.nn.max_pool(hidden1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') +weight2 = weight_variable([5, 5, 8, 32], 0.5) +bias2 = bias_variable([32]) +hidden2 = tf.nn.relu(tf.nn.conv2d(hiddenp1, weight2, strides=[1, 1, 1, 1], padding='SAME') + bias2) +hiddenp2 = tf.nn.max_pool(hidden2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') +weight5 = weight_variable([2, 2, 32, 64], 0.5) +bias5 = bias_variable([64]) +hiddenp3 = tf.nn.relu(tf.nn.conv2d(hiddenp2, weight5, strides=[1, 1, 1, 1], padding='SAME') + bias5) +weight3 = weight_variable([7 * 7 * 64, 1024], 1.0) +bias3 = bias_variable([1024]) +hidden3 = tf.reshape(hiddenp3, [-1, 7 * 7 * 64]) +hidden4 = tf.nn.relu(tf.matmul(hidden3, weight3) + bias3) +hidden_drop = tf.nn.dropout(hidden4, dropout) +weight4 = weight_variable([1024, 120], 1.0) +weight6 = weight_variable([120, 10], 1.0) +bias4 = bias_variable([120]) +bias6 = bias_variable([10]) +hidden5 = tf.matmul(hidden_drop, weight4) + bias4 +hidden6 = tf.matmul(hidden5, weight6) + bias6 +output = tf.nn.softmax(hidden6) +learning_rate = tf.placeholder(tf.float32) +cross_entropy = -tf.reduce_sum(train_output * tf.log(output)) +train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) +correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(train_output, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) +saver = tf.train.Saver() +init = tf.initialize_all_variables() +num_steps = 20000 +max_accuracy = 0 +count = 0 +avg_accuracy = 0 +print('Running session:') +rate = 0.0001 +session = tf.InteractiveSession() +session.run(init) +iterations = 5 +for i in range(num_steps): + batch = mnist.train.next_batch(50) + feed_dict = {train_input: batch[0], train_output: batch[1], dropout: 0.8, learning_rate: rate} + for j in range(iterations): + train_step.run(feed_dict=feed_dict) + if i % 50 == 0 and i > 0: + feed_dict[dropout] = 1 + train_accuracy = accuracy.eval(feed_dict=feed_dict) + avg_accuracy += train_accuracy + if train_accuracy > max_accuracy: + max_accuracy = train_accuracy + saver.save(session, './mnist_best.ckpt') + if max_accuracy > 0.92: + break + print('Step ' + str(i) + ' and accuracy : ' + str(train_accuracy) + ' and count : ' + str(count) + '/50') + if i % 500 == 0: + avg_accuracy /= 10 + print('Average accuracy is : ' + str(avg_accuracy)) + avg_accuracy = 0 + if i % 1000 == 0 and i > 0: + rate *= 0.8 +feed_dict = {train_input: mnist.test.images, train_output: mnist.test.labels, dropout: 1} +print('Final accuracy : ' + str(accuracy.eval(feed_dict=feed_dict))) +path = saver.save(session, './mnist.ckpt') +print('Saved in file with name as : ' + str(path)) diff --git a/dataset/preprocessed/682.txt b/dataset/preprocessed/682.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/682.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/683.txt b/dataset/preprocessed/683.txt new file mode 100644 index 0000000..30f1112 --- /dev/null +++ b/dataset/preprocessed/683.txt @@ -0,0 +1,276 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +import pickle +import os +import matplotlib.pyplot as plt +dirname = os.path.dirname +dataPath = dirname(dirname(os.getcwd())) + '\\DataSets\\' +pickle_file = dataPath + 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +def best_accuracy_id(accuracies): + return accuracies.index(max(accuracies)) + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +accuracies = [] +beta_val = [i for i in np.arange(0, 0.01, 0.0005)] +for reg in beta_val: + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: reg} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracies.append(accuracy(test_prediction.eval(), test_labels)) +print('The max accuracy occurs at %f' % beta_val[best_accuracy_id(accuracies)]) +plt.semilogx(beta_val, accuracies) +plt.grid(True) +plt.title('Test Accuracy versus regularization Curve') +plt.show() +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: beta_val[best_accuracy_id(accuracies)]} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta = tf.placeholder(tf.float32) + weights_hidden = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases_hidden = tf.Variable(tf.zeros([num_hidden_nodes])) + weights = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + hidden_layer_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + logits = tf.matmul(hidden_layer_train, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_hidden)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, weights) + biases) + test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden, weights) + biases) +accuracies = [] +for reg in beta_val: + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: reg} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + accuracies.append(accuracy(test_prediction.eval(), test_labels)) +print('The max accuracy occurs at %f' % beta_val[best_accuracy_id(accuracies)]) +plt.semilogx(beta_val, accuracies) +plt.grid(True) +plt.title('Test Accuracy versus regularization Curve') +plt.show() +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialize') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: beta_val[best_accuracy_id(accuracies)]} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta = tf.placeholder(tf.float32) + weights_hidden = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases_hidden = tf.Variable(tf.zeros([num_hidden_nodes])) + weights = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + hidden_layer_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + logits = tf.matmul(hidden_layer_train, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, weights) + biases) + test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden, weights) + biases) +num_steps = 100 +num_batches = 3 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_batches + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: 0} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + beta = tf.placeholder(tf.float32) + weights_hidden = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases_hidden = tf.Variable(tf.zeros([num_hidden_nodes])) + weights = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + hidden_layer_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + drop1 = tf.nn.dropout(hidden_layer_train, 0.5) + logits = tf.matmul(drop1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + valid_prediction = tf.nn.softmax(tf.matmul(valid_hidden, weights) + biases) + test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + test_prediction = tf.nn.softmax(tf.matmul(test_hidden, weights) + biases) +num_steps = 100 +num_batches = 3 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step % num_batches + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, beta: 0} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 2 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 256 +num_hidden_nodes3 = 128 +keep_prob = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_hidden_nodes3], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_hidden_nodes3])) + weights4 = tf.Variable(tf.truncated_normal([num_hidden_nodes3, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes3))) + biases4 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + lay3_train = tf.nn.relu(tf.matmul(lay2_train, weights3) + biases3) + logits = tf.matmul(lay3_train, weights4) + biases4 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 4000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + lay3_valid = tf.nn.relu(tf.matmul(lay2_valid, weights3) + biases3) + valid_prediction = tf.nn.softmax(tf.matmul(lay3_valid, weights4) + biases4) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + lay3_test = tf.nn.relu(tf.matmul(lay2_test, weights3) + biases3) + test_prediction = tf.nn.softmax(tf.matmul(lay3_test, weights4) + biases4) +num_steps = 18001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/684.txt b/dataset/preprocessed/684.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/684.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/685.txt b/dataset/preprocessed/685.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/685.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/686.txt b/dataset/preprocessed/686.txt new file mode 100644 index 0000000..2de4e6f --- /dev/null +++ b/dataset/preprocessed/686.txt @@ -0,0 +1,149 @@ +import numpy as np +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +from time import time +from sys import stdout +import os +NUM_EPOCHS = 100000 +BATCH_SIZE = 128 +VALIDATION_SIZE = 4096 +LOG_FREQUENCY = 25 +INITIAL_LEARNING_RATE = 0.01 +DECAY_RATE = 0.1 +NUM_EPOCHS_PER_DECAY = NUM_EPOCHS // 4 +MODEL_CKPT_DIR = os.path.join('.', 'model') +train_data, validation_data, test_data = input_data.read_data_sets('MNIST_data', one_hot=True, reshape=False, validation_size=VALIDATION_SIZE) +log_device_placement = False +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0' if log_device_placement else '3' + + +def build_model(images, bIsTraining): + with tf.name_scope('Model'): + nn = tf.layers.batch_normalization(images, training=bIsTraining) + nn = tf.layers.conv2d(nn, filters=32, kernel_size=5, strides=(1, 1), padding='same') + nn = tf.layers.average_pooling2d(nn, pool_size=5, strides=(1, 1), padding='valid') + nn = tf.nn.relu(nn) + for i in range(3): + nn = tf.layers.batch_normalization(nn, training=bIsTraining) + nn = tf.layers.conv2d(nn, filters=64, kernel_size=5, strides=(1, 1), padding='same') + nn = tf.layers.average_pooling2d(nn, pool_size=5, strides=(1, 1), padding='valid') + nn = tf.nn.relu(nn) + nn = tf.layers.batch_normalization(nn, training=bIsTraining) + nn = tf.layers.conv2d(nn, filters=32, kernel_size=5, strides=(1, 1), padding='same') + nn = tf.layers.average_pooling2d(nn, pool_size=5, strides=(1, 1), padding='valid') + nn = tf.nn.relu(nn) + nn = tf.layers.flatten(nn) + nn = tf.layers.dropout(nn, rate=0.8, training=bIsTraining) + nn = tf.layers.dense(nn, units=nn.shape[-1]) + nn = tf.nn.relu(nn) + nn = tf.layers.dropout(nn, rate=0.8, training=bIsTraining) + nn = tf.layers.dense(nn, units=nn.shape[-1] // 2) + nn = tf.nn.relu(nn) + nn = tf.layers.dropout(nn, rate=0.3, training=bIsTraining) + nn = tf.layers.dense(nn, units=10) + logits = tf.nn.softmax(nn) + return tf.cond(bIsTraining, true_fn=lambda : nn, false_fn=lambda : logits) + + +def get_loss_op(labels, logits): + loss_op = tf.nn.softmax_cross_entropy_with_logits_v2(labels=labels, logits=logits) + return tf.reduce_mean(loss_op) + + +def get_accuracy_op(labels, logits): + prediction = tf.argmax(logits, 1) + correct_prediction = tf.equal(prediction, tf.argmax(labels, axis=1)) + accuray_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + return tf.multiply(accuray_op, tf.constant(100.0, dtype=tf.float32)) + + +def get_train_op(loss_op, global_step): + decay_steps = NUM_EPOCHS_PER_DECAY + lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE, global_step, decay_steps, DECAY_RATE, staircase=True) + update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + opt = tf.train.GradientDescentOptimizer(lr) + with tf.control_dependencies(update_ops): + grads = opt.compute_gradients(loss_op) + apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) + return apply_gradient_op + + +def train(): + images = tf.placeholder(tf.float32, shape=[None, 28, 28, 1]) + labels = tf.placeholder(tf.float32, shape=[None, 10]) + bIsTraining = tf.placeholder(tf.bool) + global_step = tf.train.get_or_create_global_step() + logits = build_model(images, bIsTraining) + loss_op = get_loss_op(labels, logits) + train_op = get_train_op(loss_op, global_step) + accuracy_op = get_accuracy_op(labels, logits) + scaffold = tf.train.Scaffold(init_op=tf.global_variables_initializer(), local_init_op=tf.local_variables_initializer()) + + + class _LoggerHook(tf.train.SessionRunHook): + """Logs loss and runtime.""" + + def __init__(self, *args, **kwargs): + super(*args, **kwargs) + self._start_time = time() + self._train_losses = np.array([], dtype=np.float32) + self._train_accuracies = np.array([], dtype=np.float32) + + def begin(self): + print('%8s | %9s | %11s | %10s | %5s%% | %10s%%' % ('Duration', 'Epochs', 'Samples', 'Loss', 'Train', 'Validation')) + + def before_run(self, run_context): + return tf.train.SessionRunArgs([loss_op, accuracy_op]) + + def after_run(self, run_context, run_values): + loss_value, accuracy_value = run_values.results + self._train_losses = np.append(self._train_losses, loss_value) + self._train_accuracies = np.append(self._train_accuracies, accuracy_value) + sess = run_context.session + step = sess.run(global_step) + if step % LOG_FREQUENCY == 0: + current_time = time() + duration = current_time - self._start_time + self._start_time = current_time + X, Y = validation_data.next_batch(BATCH_SIZE) + avg_validation_acc = sess.run(accuracy_op, feed_dict={images: X, labels: Y, bIsTraining: False}) + avg_train_loss = np.mean(self._train_losses, dtype=np.float32) + avg_train_acc = np.mean(self._train_accuracies, dtype=np.float32) + print('%3.2f sec | %9d | %11d | %10.6f | %5.2f%% | %5.2f%%' % (duration, step, step * BATCH_SIZE, avg_train_loss, avg_train_acc, avg_validation_acc)) + self._train_losses, self._train_accuracies = np.array([]), np.array([]) + with tf.train.MonitoredTrainingSession(checkpoint_dir=MODEL_CKPT_DIR, scaffold=scaffold, hooks=[tf.train.StopAtStepHook(last_step=NUM_EPOCHS), tf.train.NanTensorHook(loss_op), _LoggerHook()]) as mon_sess: + while not mon_sess.should_stop(): + X, Y = train_data.next_batch(BATCH_SIZE) + mon_sess.run(train_op, feed_dict={images: X, labels: Y, bIsTraining: True}) + + +def load_and_test(): + tf.reset_default_graph() + NUM_TEST_IMGS = test_data.num_examples + images = tf.placeholder(tf.float32, shape=[None, 28, 28, 1]) + labels = tf.placeholder(tf.float32, shape=[None, 10]) + bIsTraining = tf.placeholder(tf.bool) + logits = build_model(images, bIsTraining) + saver = tf.train.Saver() + with tf.Session() as sess: + saver.restore(sess, tf.train.latest_checkpoint(MODEL_CKPT_DIR)) + is_correct_prediction = tf.cast(tf.reshape(tf.equal(tf.argmax(logits, axis=1), tf.argmax(labels, axis=1)), []), dtype=tf.int16) + total_correct_predictions = tf.Variable(0, dtype=tf.int16, trainable=False) + predict_op = tf.assign_add(total_correct_predictions, is_correct_prediction) + sess.run(tf.variables_initializer([total_correct_predictions])) + for i in range(NUM_TEST_IMGS): + stdout.write('\rPredicting %d/%d' % (i + 1, NUM_TEST_IMGS)) + X, Y = test_data.next_batch(1) + sess.run(predict_op, feed_dict={images: X, labels: Y, bIsTraining: False}) + stdout.write('\n') + num_correct = sess.run(total_correct_predictions) + print('Accuracy: %3.3f%%' % (float(num_correct / NUM_TEST_IMGS) * 100)) + + +def main(): + train() + load_and_test() + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/687.txt b/dataset/preprocessed/687.txt new file mode 100644 index 0000000..0a9d08d --- /dev/null +++ b/dataset/preprocessed/687.txt @@ -0,0 +1,24 @@ +import numpy as np +import tensorflow as tf +X = tf.placeholder('float', [10, 10], name='X') +Y1 = tf.placeholder('float', [10, 20], name='Y1') +Y2 = tf.placeholder('float', [10, 20], name='Y2') +initial_shared_layer_weights = np.random.rand(10, 20) +initial_Y1_layer_weights = np.random.rand(20, 20) +initial_Y2_layer_weights = np.random.rand(20, 20) +shared_layer_weights = tf.Variable(initial_shared_layer_weights, name='share_W', dtype='float32') +Y1_layer_weights = tf.Variable(initial_Y1_layer_weights, name='share_Y1', dtype='float32') +Y2_layer_weights = tf.Variable(initial_Y2_layer_weights, name='share_Y2', dtype='float32') +shared_layer = tf.nn.relu(tf.matmul(X, shared_layer_weights)) +Y1_layer = tf.nn.relu(tf.matmul(shared_layer, Y1_layer_weights)) +Y2_layer = tf.nn.relu(tf.matmul(shared_layer, Y2_layer_weights)) +Y1_Loss = tf.nn.l2_loss(Y1 - Y1_layer) +Y2_Loss = tf.nn.l2_loss(Y2 - Y2_layer) +Joint_Loss = Y1_Loss + Y2_Loss +Optimiser = tf.train.AdamOptimizer().minimize(Joint_Loss) +Y1_op = tf.train.AdamOptimizer().minimize(Y1_Loss) +Y2_op = tf.train.AdamOptimizer().minimize(Y2_Loss) +with tf.Session() as session: + session.run(tf.initialize_all_variables()) + _, Joint_Loss = session.run([Optimiser, Joint_Loss], {X: np.random.rand(10, 10) * 10, Y1: np.random.rand(10, 20) * 10, Y2: np.random.rand(10, 20) * 10}) + print(Joint_Loss) diff --git a/dataset/preprocessed/688.txt b/dataset/preprocessed/688.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/688.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/689.txt b/dataset/preprocessed/689.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/689.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/69.txt b/dataset/preprocessed/69.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/69.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/690.txt b/dataset/preprocessed/690.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/690.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/691.txt b/dataset/preprocessed/691.txt new file mode 100644 index 0000000..b43dce8 --- /dev/null +++ b/dataset/preprocessed/691.txt @@ -0,0 +1,141 @@ +import tensorflow as tf +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + self.weights = tf.placeholder(tf.float32, name='weights') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.tgt_form_emb = tf.Variable(tf.zeros((self.form_size, self.form_dim)), name='tgt_form_emb') + self.tgt_pos_emb = tf.Variable(tf.zeros((self.pos_size, self.pos_dim)), name='tgt_pos_emb') + self.tgt_deprel_emb = tf.Variable(tf.zeros((self.deprel_size, self.deprel_dim)), name='tgt_deprel_emb') + tgt_inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.tgt_form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.tgt_deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + self.tgt_W0 = tf.Variable(tf.zeros((self.input_dim, self.hidden_dim)), name='tgt_W0') + self.tgt_b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='tgt_b0') + self.tgt_W1 = tf.Variable(tf.zeros((self.hidden_dim, self.output_dim)), name='tgt_W1') + self.tgt_b1 = tf.Variable(tf.zeros([self.output_dim]), name='tgt_b1') + self.update_op = [self.tgt_form_emb.assign(self.form_emb), self.tgt_pos_emb.assign(self.pos_emb), self.tgt_deprel_emb.assign(self.deprel_emb), self.tgt_W0.assign(self.W0), self.tgt_b0.assign(self.b0), self.tgt_W1.assign(self.W1), self.tgt_b1.assign(self.b1)] + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + tgt_hidden_layer = tf.nn.relu(tf.add(tf.matmul(tgt_inputs, self.tgt_W0), self.tgt_b0)) + self.tgt_q_function = tf.add(tf.matmul(tgt_hidden_layer, self.tgt_W1), self.tgt_b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1) * actions_one_hot, reduction_indices=1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + self.loss = tf.reduce_mean(tf.multiply(self.weights, tf.square(predicted_q - self.output))) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer().minimize(self.loss) + + def train(self, session, inputs, action, outputs, weights): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs, self.weights: weights}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def target_policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.tgt_q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def update_target(self, session): + session.run(self.update_op) + + def classify(self, session, inputs): + return self.policy(session, inputs) diff --git a/dataset/preprocessed/692.txt b/dataset/preprocessed/692.txt new file mode 100644 index 0000000..c407164 --- /dev/null +++ b/dataset/preprocessed/692.txt @@ -0,0 +1,96 @@ +import numpy as np +import random as random +import tensorflow as tf +import pickle +from collections import deque + + +class FNN: + """ + neural network model + """ + + def __init__(self, num_actions, STATES_SHAPE, params={}): + self.num_actions = num_actions + self.state_shape = STATES_SHAPE + self.LEARNING_RATE = params['LearningRate'] + self.reg = params['reg'] + self.num_hidden = params['HiddenLayerNum'] + self.hidden_size = params['HiddenLayerSize'] + self.log_file_path = params['log_file_path'] + self.session = self.create_model() + + def define_placeholders(self): + input_placeholder = tf.placeholder(tf.float32, shape=(None, self.state_shape)) + output_placeholder = tf.placeholder(tf.float32, shape=(None, self.num_actions)) + return input_placeholder, output_placeholder + + def variable_summaries(self, var, name): + with tf.name_scope('summaries'): + mean = tf.reduce_mean(var) + tf.scalar_summary('mean/' + name, mean) + with tf.name_scope('stddev'): + stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean))) + tf.scalar_summary('sttdev/' + name, stddev) + tf.scalar_summary('max/' + name, tf.reduce_max(var)) + tf.scalar_summary('min/' + name, tf.reduce_min(var)) + + def multilayer_perceptron(self, input_mat): + n_input = self.state_shape + n_hidden_1 = self.hidden_size + n_hidden_2 = self.hidden_size + n_classes = self.num_actions + weights = {'h1': tf.Variable(tf.truncated_normal([n_input, n_hidden_1], stddev=0.1)), 'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2], stddev=0.1)), 'out': tf.Variable(tf.truncated_normal([n_hidden_2, n_classes], stddev=0.1))} + biases = {'b1': tf.Variable(tf.random_normal([n_hidden_1], stddev=0.1)), 'b2': tf.Variable(tf.random_normal([n_hidden_2], stddev=0.1)), 'out': tf.Variable(tf.random_normal([n_classes], stddev=0.1))} + layer_1 = tf.add(tf.matmul(input_mat, weights['h1']), biases['b1']) + layer_1 = tf.nn.relu(layer_1) + layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) + layer_2 = tf.nn.relu(layer_2) + out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] + reg_term = self.reg * (tf.reduce_sum(tf.square(weights['h1'])) + tf.reduce_sum(tf.square(weights['h2'])) + tf.reduce_sum(tf.square(weights['out']))) + return out_layer, reg_term, weights, biases + + def create_model(self): + self.input_placeholder, self.output_placeholder = self.define_placeholders() + output, reg_term, weights, biases = self.multilayer_perceptron(self.input_placeholder) + self.variable_summaries(weights['h1'], 'layer1' + '/weights') + self.variable_summaries(weights['h2'], 'layer2' + '/weights') + self.variable_summaries(weights['out'], 'layer3' + '/weights') + self.NN_output = output + self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, self.output_placeholder)) + tf.scalar_summary('cost', self.cost) + optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.LEARNING_RATE) + self.train_op = optimizer.minimize(self.cost) + init = tf.initialize_all_variables() + """ + Default runs at cpu, + + For GPU runnning, it takes 33 percent of entire gpu memory + use this: + gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) + session = tf.Session(config=tf.ConfigProto(gpu_options = gpu_options)) + """ + session = tf.Session() + session.run(init) + self.saver_op = tf.train.Saver() + self.merged = tf.merge_all_summaries() + self.train_writer = tf.train.SummaryWriter('dqn_log_files/log1/data3', session.graph) + return session + + def init_t(self): + self.t = 0 + + def save_model(self, path): + save_path = self.saver_op.save(self.session, path) + print('Model saved in file:%s' % save_path) + + def restore_model(self, path): + self.saver_op.restore(self.session, path) + + def train_step(self, Input_mat, actions): + _, cost, prediction_probs, self.summary = self.session.run([self.train_op, self.cost, self.NN_output, self.merged], feed_dict={self.input_placeholder: Input_mat, self.output_placeholder: actions}) + return cost + + def predict(self, states, label): + cost, prediction_probs = self.session.run([self.cost, self.NN_output], feed_dict={self.input_placeholder: states, self.output_placeholder: label}) + return prediction_probs diff --git a/dataset/preprocessed/693.txt b/dataset/preprocessed/693.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/693.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/694.txt b/dataset/preprocessed/694.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/694.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/695.txt b/dataset/preprocessed/695.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/695.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/696.txt b/dataset/preprocessed/696.txt new file mode 100644 index 0000000..4768611 --- /dev/null +++ b/dataset/preprocessed/696.txt @@ -0,0 +1,134 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + print('test_shape', test_shape) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/697.txt b/dataset/preprocessed/697.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/697.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/698.txt b/dataset/preprocessed/698.txt new file mode 100644 index 0000000..f5c1f81 --- /dev/null +++ b/dataset/preprocessed/698.txt @@ -0,0 +1,51 @@ +from read_data import * +from time import time +import tensorflow as tf +beta = 0.002 + + +def train_two_layer(data, weights1, biases1, weights2, biases2): + data_d = tf.nn.dropout(data, keep_prob=0.9) + output1 = tf.matmul(data_d, weights1) + biases1 + relu1 = tf.nn.relu(output1) + logits = tf.matmul(relu1, weights2) + biases2 + return logits + + +batch_size = 128 +hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) + biases1 = tf.Variable(tf.zeros([hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = train_two_layer(tf_train_dataset, weights1, biases1, weights2, biases2) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + loss = np.add(loss, beta * tf.nn.l2_loss(weights1)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(train_two_layer(tf_valid_dataset, weights1, biases1, weights2, biases2)) + test_prediction = tf.nn.softmax(train_two_layer(tf_test_dataset, weights1, biases1, weights2, biases2)) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + t0 = time() + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('') + print('Session elapsed time for %d steps: %f seconds' % (num_steps, round(time() - t0, 3))) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/699.txt b/dataset/preprocessed/699.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/699.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/7.txt b/dataset/preprocessed/7.txt new file mode 100644 index 0000000..146c527 --- /dev/null +++ b/dataset/preprocessed/7.txt @@ -0,0 +1,132 @@ +import tensorflow as tf +import numpy as np +import layers_dihedral_equi as nn + + +def summary_images(x, name): + for i in range(min(4, x.get_shape().as_list()[3])): + tf.summary.image('{}-{}'.format(name, i), x[:, :, :, i:i + 1]) + + +class CNN: + + def __init__(self): + self.tfx = None + self.tfy = None + self.tfp = None + self.xent = None + self.tftrain_step = None + self.tfkp = None + self.tfacc = None + self.train_counter = 0 + self.test = None + self.embedding_input = None + + def NN(self, x): + assert x.get_shape().as_list()[:3] == [None, 101, 101] + summary_images(x, 'layer0') + x = nn.convolution(x, 8 * 4, w=4, input_repr='invariant') + x = nn.convolution(x) + summary_images(x, 'layer2') + x = nn.max_pool(x) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 48, 48, 8 * 4] + x = nn.convolution(x, 8 * 8) + x = nn.convolution(x) + summary_images(x, 'layer4') + x = nn.max_pool(x) + x = nn.batch_normalization(x, self.tfacc) + assert x.get_shape().as_list() == [None, 22, 22, 8 * 8] + x = nn.convolution(x, 8 * 16) + x = nn.convolution(x) + summary_images(x, 'layer6') + x = nn.max_pool(x) + x = nn.batch_normalization(x, self.tfacc) + x = tf.nn.dropout(x, self.tfkp) + assert x.get_shape().as_list() == [None, 9, 9, 8 * 16] + x = nn.convolution(x, 8 * 32) + summary_images(x, 'layer7') + x = tf.nn.dropout(x, self.tfkp) + x = nn.convolution(x) + x = nn.batch_normalization(x, self.tfacc) + x = tf.nn.dropout(x, self.tfkp) + assert x.get_shape().as_list() == [None, 5, 5, 8 * 32] + x = nn.convolution(x, 8 * 128, w=5) + assert x.get_shape().as_list() == [None, 1, 1, 8 * 128] + x = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) + self.embedding_input = x + x = tf.nn.dropout(x, self.tfkp) + x = nn.fullyconnected(x, 8 * 256) + x = tf.nn.dropout(x, self.tfkp) + x = nn.fullyconnected(x, 8 * 256) + x = nn.batch_normalization(x, self.tfacc) + self.test = x + x = nn.fullyconnected(x, 1, output_repr='invariant', activation=None) + return x + + def create_architecture(self, bands): + self.tfkp = tf.placeholder_with_default(tf.constant(1.0, tf.float32), [], name='kp') + self.tfacc = tf.placeholder_with_default(tf.constant(0.0, tf.float32), [], name='acc') + x = self.tfx = tf.placeholder(tf.float32, [None, 101, 101, bands], name='input') + with tf.name_scope('nn'): + x = self.NN(x) + assert x.get_shape().as_list() == [None, 1] + self.tfp = tf.nn.sigmoid(tf.reshape(x, [-1])) + with tf.name_scope('xent'): + self.tfy = tf.placeholder(tf.float32, [None]) + xent = tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=tf.reshape(self.tfy, [-1, 1])) + self.xent = tf.reduce_mean(xent) + with tf.name_scope('train'): + self.tftrain_step = tf.train.AdamOptimizer(0.0001).minimize(self.xent) + + @staticmethod + def split_test_train(path): + import os + files = ['{}/{}'.format(path, f) for f in sorted(os.listdir(path))] + return files[:3000], files[3000:] + + @staticmethod + def load(files): + xs = np.stack([np.load(f)['image'] for f in files]) + return CNN.prepare(xs) + + @staticmethod + def prepare(images): + images[images == 100] = 0.0 + if images.shape[-1] == 1: + images = (images - 4.337e-13) / 5.504e-12 + elif images.shape[-1] == 4: + images = (images - 1.685e-12) / 5.122e-11 + else: + print('No statistics to prepare this kind of data') + return images + + @staticmethod + def batch(files, labels): + id0 = np.where(labels == 0)[0] + id1 = np.where(labels == 1)[0] + k = 15 + idn = np.random.choice(id0, k, replace=False) + idp = np.random.choice(id1, k, replace=False) + xs = CNN.load([files[i] for i in idp] + [files[i] for i in idn]) + ys = np.concatenate((labels[idp], labels[idn])) + for i in range(len(xs)): + s = np.random.uniform(0.8, 1.2) + u = np.random.uniform(-0.1, 0.1) + xs[i] = xs[i] * s + u + return xs, ys + + def train(self, session, xs, ys, options=None, run_metadata=None, tensors=None): + if tensors is None: + tensors = [] + acc = 0.6 ** (self.train_counter / 1000.0) + kp = 0.5 + 0.5 * 0.5 ** (self.train_counter / 2000.0) + output = session.run([self.tftrain_step, self.xent] + tensors, feed_dict={self.tfx: xs, self.tfy: ys, self.tfkp: kp, self.tfacc: acc}, options=options, run_metadata=run_metadata) + self.train_counter += 1 + return output[1], output[2:] + + def predict(self, session, images): + return session.run(self.tfp, feed_dict={self.tfx: images}) + + def predict_xentropy(self, session, images, labels): + return session.run([self.tfp, self.xent], feed_dict={self.tfx: images, self.tfy: labels}) diff --git a/dataset/preprocessed/70.txt b/dataset/preprocessed/70.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/70.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/700.txt b/dataset/preprocessed/700.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/700.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/701.txt b/dataset/preprocessed/701.txt new file mode 100644 index 0000000..7ffc546 --- /dev/null +++ b/dataset/preprocessed/701.txt @@ -0,0 +1,209 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import os +if sys.platform == 'win32': + data_root = 'C:\\data\\' +elif sys.platform == 'linux': + data_root = '/home/jovarty/data' +else: + raise Exception('Unknown OS') +pickle_file = 'notMNIST.pickle' +dest_file_path = os.path.join(data_root, pickle_file) +with open(dest_file_path, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +def LogisticRegression(l2_weight): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + l2_weight * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + num_steps = 3001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + return accuracy(test_prediction.eval(), test_labels) + + +def OneHiddenLayer(l2_weight): + batch_size = 128 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer_size = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_size])) + hidden_biases = tf.Variable(tf.zeros([hidden_layer_size])) + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + weights = tf.Variable(tf.truncated_normal([hidden_layer_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + +l2_weight * tf.nn.l2_loss(weights) + +l2_weight * tf.nn.l2_loss(hidden_weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + valid_logits = tf.matmul(valid_hidden, weights) + biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + test_logits = tf.matmul(test_hidden, weights) + biases + test_prediction = tf.nn.softmax(test_logits) + num_steps = 3001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + return accuracy(test_prediction.eval(), test_labels) + + +def OneHiddenLayer_WithSmallBatchSize(l2_weight): + batch_size = 3 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer_size = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_size])) + hidden_biases = tf.Variable(tf.zeros([hidden_layer_size])) + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + weights = tf.Variable(tf.truncated_normal([hidden_layer_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + +l2_weight * tf.nn.l2_loss(weights) + +l2_weight * tf.nn.l2_loss(hidden_weights) + optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + valid_logits = tf.matmul(valid_hidden, weights) + biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + test_logits = tf.matmul(test_hidden, weights) + biases + test_prediction = tf.nn.softmax(test_logits) + num_steps = 3001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + return accuracy(test_prediction.eval(), test_labels) + + +result = OneHiddenLayer_WithSmallBatchSize(0.0) +print('0.000 ' + str(result) + '%') + + +def OneHiddenLayer_Dropout(l2_weight): + batch_size = 10 + graph = tf.Graph() + with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + hidden_layer_size = 1024 + hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_size])) + hidden_biases = tf.Variable(tf.zeros([hidden_layer_size])) + hidden_layer = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases) + keep_prob = tf.placeholder(tf.float32) + hidden_layer_drop = tf.nn.dropout(hidden_layer, keep_prob) + weights = tf.Variable(tf.truncated_normal([hidden_layer_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(hidden_layer_drop, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + +l2_weight * tf.nn.l2_loss(weights) + +l2_weight * tf.nn.l2_loss(hidden_weights) + optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_hidden = tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases) + valid_logits = tf.matmul(valid_hidden, weights) + biases + valid_prediction = tf.nn.softmax(valid_logits) + test_hidden = tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases) + test_logits = tf.matmul(test_hidden, weights) + biases + test_prediction = tf.nn.softmax(test_logits) + num_steps = 5001 + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run(feed_dict={keep_prob: 0.5}) + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 1.0} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + return accuracy(test_prediction.eval(), test_labels) diff --git a/dataset/preprocessed/702.txt b/dataset/preprocessed/702.txt new file mode 100644 index 0000000..c0142c1 --- /dev/null +++ b/dataset/preprocessed/702.txt @@ -0,0 +1,302 @@ +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +import load_cifer10 +import random +from PIL import Image +np.random.seed(20160612) +tf.set_random_seed(20160612) + + +class layer0: + + def __init__(self): + with tf.Graph().as_default(): + self.prepare_model() + self.prepare_session() + + def prepare_model(self): + with tf.name_scope('input_layer'): + with tf.name_scope('input_size'): + input_size = 3072 + with tf.name_scope('x'): + x = tf.placeholder(tf.float32, [None, input_size]) + with tf.name_scope('layer0_output_fully_connected'): + with tf.name_scope('w3'): + w3 = tf.Variable(tf.truncated_normal(shape=[input_size, 10], mean=0.0, stddev=1 / input_size)) + with tf.name_scope('b3'): + b3 = tf.Variable(tf.zeros([10])) + with tf.name_scope('p'): + p = tf.nn.softmax(tf.matmul(x, w3) + b3) + t = tf.placeholder(tf.float32, [None, 10]) + with tf.name_scope('optimizer'): + with tf.name_scope('loss'): + loss = -tf.reduce_sum(t * tf.log(tf.clip_by_value(p, 1e-10, 1.0))) + with tf.name_scope('train_step'): + train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + with tf.name_scope('correct_prediction'): + correct_prediction = tf.equal(tf.argmax(p, 1), tf.argmax(t, 1)) + with tf.name_scope('accuracy'): + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + tf.summary.scalar('loss', loss) + tf.summary.scalar('accuracy', accuracy) + tf.summary.scalar('w3', tf.reduce_sum(w3)) + self.x, self.t, self.p = x, t, p + self.train_step = train_step + self.loss = loss + self.accuracy = accuracy + self.w3 = w3 + + def prepare_session(self): + sess = tf.InteractiveSession() + sess.run(tf.initialize_all_variables()) + summary = tf.summary.merge_all() + writer = tf.summary.FileWriter('./log/cifar10_logs0', sess.graph) + self.sess = sess + self.summary = summary + self.writer = writer + + +class layer1: + + def __init__(self): + with tf.Graph().as_default(): + self.prepare_model() + self.prepare_session() + + def prepare_model(self): + with tf.name_scope('input_layer'): + with tf.name_scope('input_size'): + input_size = 3072 + with tf.name_scope('x'): + x = tf.placeholder(tf.float32, [None, input_size]) + with tf.name_scope('layer2_fully_connected'): + with tf.name_scope('num_units1'): + num_units2 = 1024 + with tf.name_scope('w2'): + w2 = tf.Variable(tf.truncated_normal(shape=[input_size, num_units2], mean=0.0, stddev=1 / input_size)) + with tf.name_scope('b2'): + b2 = tf.Variable(tf.constant(0.1, shape=[num_units2])) + with tf.name_scope('hidden2'): + hidden2 = tf.nn.relu(tf.matmul(x, w2) + b2) + with tf.name_scope('keep_prob'): + keep_prob = tf.placeholder(tf.float32) + with tf.name_scope('hidden2_drop'): + hidden2_drop = tf.nn.dropout(hidden2, keep_prob) + with tf.name_scope('layer3_output_fully_connected'): + with tf.name_scope('w3'): + w3 = tf.Variable(tf.truncated_normal(shape=[num_units2, 10], mean=0.0, stddev=1 / num_units2)) + with tf.name_scope('w3_0'): + w3_0 = tf.Variable(tf.truncated_normal(shape=[input_size, 10], mean=0.0, stddev=1 / input_size)) + with tf.name_scope('b3'): + b3 = tf.Variable(tf.zeros([10])) + with tf.name_scope('p'): + p = tf.nn.softmax(tf.matmul(hidden2_drop, w3) + tf.matmul(x, w3_0) + b3) + with tf.name_scope('input_placeholder'): + input_placeholder = tf.placeholder(tf.float32, shape=[input_size, 10]) + assign_op = w3_0.assign(input_placeholder) + t = tf.placeholder(tf.float32, [None, 10]) + with tf.name_scope('optimizer'): + with tf.name_scope('loss'): + loss = -tf.reduce_sum(t * tf.log(tf.clip_by_value(p, 1e-10, 1.0))) + with tf.name_scope('train_step'): + train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + with tf.name_scope('correct_prediction'): + correct_prediction = tf.equal(tf.argmax(p, 1), tf.argmax(t, 1)) + with tf.name_scope('accuracy'): + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + tf.summary.scalar('loss', loss) + tf.summary.scalar('accuracy', accuracy) + tf.summary.scalar('w3', tf.reduce_sum(w3)) + tf.summary.scalar('w2', tf.reduce_sum(w2)) + self.x, self.t, self.p = x, t, p + self.train_step = train_step + self.loss = loss + self.accuracy = accuracy + self.keep_prob = keep_prob + self.w3 = w3 + self.w3_0 = w3_0 + self.w2 = w2 + self.assign_op = assign_op + self.input_placeholder = input_placeholder + + def prepare_session(self): + sess = tf.InteractiveSession() + sess.run(tf.initialize_all_variables()) + summary = tf.summary.merge_all() + writer = tf.summary.FileWriter('./log/cifar10_logs1', sess.graph) + self.sess = sess + self.summary = summary + self.writer = writer + + +class layer2: + + def __init__(self): + with tf.Graph().as_default(): + self.prepare_model() + self.prepare_session() + + def prepare_model(self): + with tf.name_scope('input_layer'): + with tf.name_scope('input_size'): + input_size = 3072 + with tf.name_scope('x'): + x = tf.placeholder(tf.float32, [None, input_size]) + with tf.name_scope('layer1_fully_connected'): + with tf.name_scope('num_units1'): + num_units1 = 1024 + with tf.name_scope('w1'): + w1 = tf.Variable(tf.truncated_normal(shape=[input_size, num_units1], mean=0.0, stddev=1 / input_size)) + with tf.name_scope('b1'): + b1 = tf.Variable(tf.constant(0.1, shape=[num_units1])) + with tf.name_scope('hidden1'): + hidden1 = tf.nn.relu(tf.matmul(x, w1) + b1) + with tf.name_scope('keep_prob'): + keep_prob = tf.placeholder(tf.float32) + with tf.name_scope('hidden1_drop'): + hidden1_drop = tf.nn.dropout(hidden1, keep_prob) + with tf.name_scope('layer2_fully_connected'): + with tf.name_scope('num_units2'): + num_units2 = 1024 + with tf.name_scope('w2'): + w2 = tf.Variable(tf.truncated_normal(shape=[num_units1, num_units2], mean=0.0, stddev=1 / num_units1)) + with tf.name_scope('w2_0'): + w2_0 = tf.Variable(tf.truncated_normal(shape=[input_size, num_units2], mean=0.0, stddev=1 / input_size)) + with tf.name_scope('b2'): + b2 = tf.Variable(tf.constant(0.1, shape=[num_units2])) + with tf.name_scope('hidden2'): + hidden2 = tf.nn.relu(tf.matmul(hidden1_drop, w2) + tf.matmul(x, w2_0) + b2) + with tf.name_scope('hidden2_drop'): + hidden2_drop = tf.nn.dropout(hidden2, keep_prob) + with tf.name_scope('layer3_output_fully_connected'): + with tf.name_scope('w3'): + w3 = tf.Variable(tf.zeros([num_units2, 10])) + with tf.name_scope('w3_0'): + w3_0 = tf.Variable(tf.zeros([input_size, 10])) + with tf.name_scope('b3'): + b3 = tf.Variable(tf.zeros([10])) + with tf.name_scope('p'): + p = tf.nn.softmax(tf.matmul(hidden2_drop, w3) + tf.matmul(x, w3_0) + b3) + with tf.name_scope('input_placeholder'): + with tf.name_scope('input_placeholder_for_w3_0'): + input_placeholder_for_w3_0 = tf.placeholder(tf.float32, shape=[input_size, 10]) + assign_op_for_w3_0 = w3_0.assign(input_placeholder_for_w3_0) + with tf.name_scope('input_placeholder_for_w3'): + input_placeholder_for_w3 = tf.placeholder(tf.float32, shape=[num_units2, 10]) + assign_op_for_w3 = w3.assign(input_placeholder_for_w3) + with tf.name_scope('input_placeholder_for_w2_0'): + input_placeholder_for_w2_0 = tf.placeholder(tf.float32, shape=[input_size, num_units2]) + assign_op_for_w2_0 = w2_0.assign(input_placeholder_for_w2_0) + with tf.name_scope('true'): + t = tf.placeholder(tf.float32, [None, 10]) + with tf.name_scope('optimizer'): + with tf.name_scope('loss'): + loss = -tf.reduce_sum(t * tf.log(tf.clip_by_value(p, 1e-10, 1.0))) - 0.005 * tf.reduce_sum(w3_0) + with tf.name_scope('train_step'): + train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + with tf.name_scope('correct_prediction'): + correct_prediction = tf.equal(tf.argmax(p, 1), tf.argmax(t, 1)) + with tf.name_scope('accuracy'): + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + tf.summary.scalar('loss', loss) + tf.summary.scalar('accuracy', accuracy) + tf.summary.scalar('w1', tf.reduce_sum(w1)) + tf.summary.scalar('w2', tf.reduce_sum(w2)) + tf.summary.scalar('w3', tf.reduce_sum(w3)) + tf.summary.scalar('w2_0', tf.reduce_sum(w2_0)) + tf.summary.scalar('w3_0', tf.reduce_sum(w3_0)) + self.x, self.t, self.p = x, t, p + self.train_step = train_step + self.loss = loss + self.accuracy = accuracy + self.keep_prob = keep_prob + self.w1 = w1 + self.w2 = w2 + self.w3 = w3 + self.w2_0 = w2_0 + self.w3_0 = w3_0 + self.assign_op_for_w3_0 = assign_op_for_w3_0 + self.input_placeholder_for_w3_0 = input_placeholder_for_w3_0 + self.assign_op_for_w3 = assign_op_for_w3 + self.input_placeholder_for_w3 = input_placeholder_for_w3 + self.assign_op_for_w2_0 = assign_op_for_w2_0 + self.input_placeholder_for_w2_0 = input_placeholder_for_w2_0 + + def prepare_session(self): + sess = tf.InteractiveSession() + sess.run(tf.initialize_all_variables()) + summary = tf.summary.merge_all() + writer = tf.summary.FileWriter('./log/cifar10_logs2', sess.graph) + self.sess = sess + self.summary = summary + self.writer = writer + + +if __name__ == '__main__': + data, labels_non_onehot, test_data, test_labels_non_onehot, label_names = load_cifer10.load_dataset() + print(label_names) + labels = np.mat([[(0) for i in range(10)] for k in range(len(labels_non_onehot))]) + for i in range(len(labels)): + labels[i] = np.eye(10)[labels_non_onehot[i]] + test_labels = np.mat([[(0) for i in range(10)] for k in range(len(test_labels_non_onehot))]) + for i in range(len(test_labels)): + test_labels[i] = np.eye(10)[test_labels_non_onehot[i]] + nn = layer0() + batchsize = 100 + batch_xs = np.mat([[(0.0) for n in range(3072)] for k in range(batchsize)]) + batch_ts = np.mat([[(0.0) for n in range(10)] for k in range(batchsize)]) + print(test_data[0].shape) + print(test_data.shape) + i = 0 + for _ in range(10000): + i += 1 + for n in range(batchsize): + tmp = int(random.uniform(0, len(data))) + batch_xs[n] = data[tmp].reshape(1, 3072) + batch_xs[n] /= batch_xs[n].max() + batch_ts[n] = labels[tmp].reshape(1, 10) + nn.sess.run(nn.train_step, feed_dict={nn.x: batch_xs, nn.t: batch_ts}) + if i % 100 == 0: + summary, loss_val, acc_val = nn.sess.run([nn.summary, nn.loss, nn.accuracy], feed_dict={nn.x: test_data, nn.t: test_labels}) + print('Step: %d, Loss: %f, Accuracy: %f' % (i, loss_val, acc_val)) + nn.writer.add_summary(summary, i) + w3_0 = nn.sess.run(nn.w3) + nn = layer1() + nn.sess.run(nn.assign_op, feed_dict={nn.input_placeholder: w3_0}) + print(w3_0) + print(w3_0 - nn.sess.run(nn.w3_0)) + i = 0 + for _ in range(10000): + i += 1 + for n in range(batchsize): + tmp = int(random.uniform(0, len(data))) + batch_xs[n] = data[tmp].reshape(1, 3072) + batch_xs[n] /= batch_xs[n].max() + batch_ts[n] = labels[tmp].reshape(1, 10) + nn.sess.run(nn.train_step, feed_dict={nn.x: batch_xs, nn.t: batch_ts, nn.keep_prob: 0.5}) + if i % 100 == 0: + summary, loss_val, acc_val = nn.sess.run([nn.summary, nn.loss, nn.accuracy], feed_dict={nn.x: test_data, nn.t: test_labels, nn.keep_prob: 1.0}) + print('Step: %d, Loss: %f, Accuracy: %f' % (i, loss_val, acc_val)) + nn.writer.add_summary(summary, i) + w2_0 = nn.sess.run(nn.w2) + w3_0 = nn.sess.run(nn.w3_0) + w3 = nn.sess.run(nn.w3) + nn = layer2() + nn.sess.run(nn.assign_op_for_w3_0, feed_dict={nn.input_placeholder_for_w3_0: w3_0}) + nn.sess.run(nn.assign_op_for_w3, feed_dict={nn.input_placeholder_for_w3: w3}) + nn.sess.run(nn.assign_op_for_w2_0, feed_dict={nn.input_placeholder_for_w2_0: w2_0}) + i = 0 + for _ in range(100000): + i += 1 + for n in range(batchsize): + tmp = int(random.uniform(0, len(data))) + batch_xs[n] = data[tmp].reshape(1, 3072) + batch_xs[n] /= batch_xs[n].max() + batch_ts[n] = labels[tmp].reshape(1, 10) + nn.sess.run(nn.train_step, feed_dict={nn.x: batch_xs, nn.t: batch_ts, nn.keep_prob: 0.5}) + if i % 100 == 0: + summary, loss_val, acc_val = nn.sess.run([nn.summary, nn.loss, nn.accuracy], feed_dict={nn.x: test_data, nn.t: test_labels, nn.keep_prob: 1.0}) + print('Step: %d, Loss: %f, Accuracy: %f' % (i, loss_val, acc_val)) + nn.writer.add_summary(summary, i) diff --git a/dataset/preprocessed/703.txt b/dataset/preprocessed/703.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/703.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/704.txt b/dataset/preprocessed/704.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/704.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/705.txt b/dataset/preprocessed/705.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/705.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/706.txt b/dataset/preprocessed/706.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/706.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/707.txt b/dataset/preprocessed/707.txt new file mode 100644 index 0000000..672a3a0 --- /dev/null +++ b/dataset/preprocessed/707.txt @@ -0,0 +1,73 @@ +""" + Tensor: Multidimensional array + + Scalar: s = 1, 2, 3, ... + Vector: v = [1, 2, 3, ...] + Matrix: m = [[1, 2, 3], [4, 5, 6], [7, 8, 9] , ...] + Tensor: t = [[[[[...]]]]] +""" +import numpy as np +import tensorflow as tf +a = tf.constant([1.0, 2.0]) +print(a) +b = tf.constant([3.0, 4.0]) +result = a + b +""" + "add:0": node name + shape=(2,0): 1-dimensional vector, length 2 + dtype=float32: data type +""" +print(result) +x1 = tf.constant([[1.0, 2.0]]) +print(x1) +x2 = tf.constant([1.0, 2.0]) +print(x2) +w = tf.constant([[3.0], [4.0]]) +print(w) +y = tf.matmul(x, w) +print(y) +with tf.Session() as sess: + print(sess.run(y)) + + +def simpleNN(): + """ Simple two fully connect NN, no improvement""" + x = tf.constant([[0.7, 0.5]]) + w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1)) + w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1)) + h = tf.matmul(x, w1) + y = tf.matmul(h, w2) + with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + print(sess.run(y)) + + +simpleNN() + + +def FeedOneDataPointNN(): + x = tf.placeholder(tf.float32, shape=(1, 2)) + w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1)) + w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1)) + h = tf.matmul(x, w1) + y = tf.matmul(h, w2) + with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + print(sess.run(y, feed_dict={x: [[0.7, 0.5]]})) + + +FeedOneDataPointNN() + + +def FeedMultipleDataNN(): + x = tf.placeholder(tf.float32, shape=(None, 2)) + w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1)) + w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1)) + h = tf.matmul(x, w1) + y = tf.matmul(h, w2) + with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + print(sess.run(y, feed_dict={x: [[0.7, 0.5], [0.2, 0.3], [0.3, 0.4]]})) + + +FeedMultipleDataNN() diff --git a/dataset/preprocessed/708.txt b/dataset/preprocessed/708.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/708.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/709.txt b/dataset/preprocessed/709.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/709.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/71.txt b/dataset/preprocessed/71.txt new file mode 100644 index 0000000..23d73b3 --- /dev/null +++ b/dataset/preprocessed/71.txt @@ -0,0 +1,105 @@ +from __future__ import division, print_function, unicode_literals +import numpy as np +import os +import tensorflow as tf +reset_graph() +n_steps = 28 +n_inputs = 28 +n_neurons = 150 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) +outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) +logits = tf.layers.dense(states, n_outputs) +xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits) +loss = tf.reduce_mean(xentropy) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +reset_graph() +n_steps = 28 +n_inputs = 28 +n_outputs = 10 +learning_rate = 0.001 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.int32, [None]) +n_neurons = 100 +n_layers = 3 +layers = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu) for layer in range(n_layers)] +multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers) +outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32) +layers +states_concat = tf.concat(axis=1, values=states) +logits = tf.layers.dense(states_concat, n_outputs) +xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits) +loss = tf.reduce_mean(xentropy) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +training_op = optimizer.minimize(loss) +correct = tf.nn.in_top_k(logits, y, 1) +accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) +init = tf.global_variables_initializer() +n_epochs = 10 +batch_size = 150 +with tf.Session() as sess: + init.run() + for epoch in range(n_epochs): + for iteration in range(mnist.train.num_examples // batch_size): + X_batch, y_batch = mnist.train.next_batch(batch_size) + X_batch = X_batch.reshape((-1, n_steps, n_inputs)) + sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) + acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch}) + acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test}) + print(epoch, 'Train accuracy:', acc_train, 'Test curacy:', acc_test) +reset_graph() +sess = tf.Session() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu) +outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) +reset_graph() +n_steps = 20 +n_inputs = 1 +n_neurons = 100 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +cell = tf.contrib.rnn.OutputProjectionWrapper(tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu), output_size=n_outputs) +outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) +learning_rate = 0.001 +loss = tf.reduce_mean(tf.square(outputs - y)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) +training_op = optimizer.minimize(loss) +init = tf.global_variables_initializer() +saver = tf.train.Saver() +n_iterations = 1500 +batch_size = 50 +with tf.Session() as sess: + init.run() + for iteration in range(n_iterations): + X_batch, y_batch = next_batch(batch_size, n_steps) + sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) + if iteration % 100 == 0: + mse = loss.eval(feed_dict={X: X_batch, y: y_batch}) + print(iteration, '\tMSE:', mse) + saver.save(sess, './my_time_series_model') +reset_graph() +n_inputs = 1 +n_neurons = 100 +n_layers = 3 +n_steps = 20 +n_outputs = 1 +X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) +y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) +keep_prob = 0.5 +cells = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) for layer in range(n_layers)] +cells_drop = [tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) for cell in cells] +multi_layer_cell = tf.contrib.rnn.MultiRNNCell(cells_drop) +rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32) diff --git a/dataset/preprocessed/710.txt b/dataset/preprocessed/710.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/710.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/711.txt b/dataset/preprocessed/711.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/711.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/712.txt b/dataset/preprocessed/712.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/712.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/713.txt b/dataset/preprocessed/713.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/713.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/714.txt b/dataset/preprocessed/714.txt new file mode 100644 index 0000000..9fc0d4e --- /dev/null +++ b/dataset/preprocessed/714.txt @@ -0,0 +1,276 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +train_subset = 10000 +beta = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.constant(train_dataset[:train_subset, :]) + tf_train_labels = tf.constant(train_labels[:train_subset]) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +import time +batch_size = 128 +num_steps = 3001 +with tf.Session(graph=graph) as session: + t1 = time.time() + tf.initialize_all_variables().run() + print('Initialized with no RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 128 +num_nodes = 1024 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(relu1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +import time +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.initialize_all_variables().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 12 +num_nodes = 1024 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(relu1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + l2_regularizer = tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_relu1) + loss += 0.0005 * l2_regularizer + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +import time +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.initialize_all_variables().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 128 +num_nodes = 1024 +beta = 0.001 +prob_limit = 0.75 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + keep_prob = tf.placeholder('float') + hidden_layer_drop = tf.nn.dropout(relu1, keep_prob) + logits = tf.matmul(hidden_layer_drop, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +import time +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.initialize_all_variables().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: prob_limit} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 128 +h1_num_nodes = 1024 +h2_num_nodes = 768 +h3_num_nodes = 512 +h1_stddev = np.sqrt(2.0 / 784) +h2_stddev = np.sqrt(2.0 / h1_num_nodes) +h3_stddev = np.sqrt(2.0 / h2_num_nodes) +logits_stddev = np.sqrt(2.0 / h3_num_nodes) +beta = 1e-05 +print('graph best: l2 regularization, no dropout') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + h1_weights = tf.Variable(tf.truncated_normal([image_size * image_size, h1_num_nodes], stddev=h1_stddev)) + h1_biases = tf.Variable(tf.zeros([h1_num_nodes])) + h1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, h1_weights) + h1_biases) + print('TF hidden layer 1->', h1_layer) + h2_weights = tf.Variable(tf.truncated_normal([h1_num_nodes, h2_num_nodes], stddev=h2_stddev)) + h2_biases = tf.Variable(tf.zeros([h2_num_nodes])) + h2_layer = tf.nn.relu(tf.matmul(h1_layer, h2_weights) + h2_biases) + print('TF hidden layer 2->', h2_layer) + h3_weights = tf.Variable(tf.truncated_normal([h2_num_nodes, h3_num_nodes], stddev=h3_stddev)) + h3_biases = tf.Variable(tf.zeros([h3_num_nodes])) + h3_layer = tf.nn.relu(tf.matmul(h2_layer, h3_weights) + h3_biases) + print('TF hidden layer 3->', h3_layer) + weights = tf.Variable(tf.truncated_normal([h3_num_nodes, num_labels], stddev=logits_stddev)) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(h3_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels) + beta * tf.nn.l2_loss(h1_weights) + beta * tf.nn.l2_loss(h2_weights) + beta * tf.nn.l2_loss(h3_weights) + beta * tf.nn.l2_loss(weights)) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 100000, 0.95, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + h1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, h1_weights) + h1_biases) + h2_valid = tf.nn.relu(tf.matmul(h1_valid, h2_weights) + h2_biases) + h3_valid = tf.nn.relu(tf.matmul(h2_valid, h3_weights) + h3_biases) + valid_logits = tf.matmul(h3_valid, weights) + biases + valid_prediction = tf.nn.softmax(valid_logits) + print('valid_prediction(raw)->', valid_prediction) + h1_test = tf.nn.relu(tf.matmul(tf_test_dataset, h1_weights) + h1_biases) + h2_test = tf.nn.relu(tf.matmul(h1_test, h2_weights) + h2_biases) + h3_test = tf.nn.relu(tf.matmul(h2_test, h3_weights) + h3_biases) + test_logits = tf.matmul(h3_test, weights) + biases + test_prediction = tf.nn.softmax(test_logits) +import time +print('run with best: l2 regularization, no dropout') +num_steps = 10000 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.initialize_all_variables().run() + print('valid_prediction(eval)->', valid_prediction.eval()) + print('Initialized with best: l2 regularization, no dropout') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) diff --git a/dataset/preprocessed/715.txt b/dataset/preprocessed/715.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/715.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/716.txt b/dataset/preprocessed/716.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/716.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/717.txt b/dataset/preprocessed/717.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/717.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/718.txt b/dataset/preprocessed/718.txt new file mode 100644 index 0000000..43cc9cb --- /dev/null +++ b/dataset/preprocessed/718.txt @@ -0,0 +1,158 @@ +import tensorflow as tf +hello_constant = tf.constant('Hello World!') +with tf.Session() as sess: + output = sess.run(hello_constant) + print(output) +A = tf.constant(1234) +B = tf.constant([123, 456, 789]) +C = tf.constant([[123, 456, 789], [222, 333, 444]]) +with tf.Session() as sess2: + output1 = sess2.run(C) + print(output1) +x = tf.placeholder(tf.string) +y = tf.placeholder(tf.int32) +z = tf.placeholder(tf.float32) + + +def placeHolder(string): + output = None + x = tf.placeholder(tf.string) + with tf.Session() as sess: + output = sess.run(x, feed_dict={x: string}) + return print(output) + + +placeHolder('Good Morning World!') +x = tf.add(5, 2) +x = tf.subtract(10, 4) +y = tf.multiply(2, 5) +tf.subtract(tf.cast(tf.constant(2.0), tf.int32), tf.constant(1)) +x = tf.constant(10) +y = tf.constant(2) +z = tf.subtract(tf.divide(x, y), tf.cast(tf.constant(1), tf.float64)) +with tf.Session() as sess: + output = sess.run(z) + print(output) +matW = tf.constant([[-0.5, 0.2, 0.1], [0.7, -0.8, 0.2]]) +matX = tf.constant([[0.2], [0.5], [0.6]]) +b = tf.constant([[0.1], [0.2]]) +prod = tf.matmul(matW, matX) +sum1 = tf.add(prod, b) +with tf.Session() as sess: + output = sess.run(sum1) + print(output) +x = tf.Variable(5) +init = tf.global_variables_initializer() +with tf.Session() as sess: + sess.run(init) +n_features = 120 +n_labels = 5 +weights = tf.Variable(tf.truncated_normal((n_features, n_labels))) +n_labels = 5 +bias = tf.Variable(tf.zeros(n_labels)) + + +def get_weights(n_features, n_labels): + return tf.Variable(tf.truncated_normal((n_features, n_labels))) + + +def get_biases(n_labels): + return tf.Variable(tf.zeros(n_labels)) + + +def linear(input, w, b): + return tf.add(tf.matmul(input, w), b) + + +x = tf.nn.softmax([2.0, 1.0, 0.2]) + + +def run(): + output = None + logit_data = [2.0, 1.0, 0.1] + logits = tf.placeholder(tf.float32) + softmax = tf.nn.softmax(logit_data) + with tf.Session() as sess: + output = sess.run(softmax, feed_dict={logits: logit_data}) + return output + + +softmax_data = [0.7, 0.2, 0.1] +one_hot_data = [1.0, 0.0, 0.0] +softmax = tf.placeholder(tf.float32) +one_hot = tf.placeholder(tf.float32) +cross_entropy = -tf.reduce_sum(tf.multiply(one_hot, tf.log(softmax))) +with tf.Session() as sess: + print(sess.run(cross_entropy, feed_dict={softmax: softmax_data, one_hot: one_hot_data})) +from tensorflow.examples.tutorials.mnist import input_data +import tensorflow as tf +n_input = 784 +n_classes = 10 +mnist = input_data.read_data_sets('/Users/ChrisErnst/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/include/unsupported/Eigen/CXX11/src/datasets/ud730/mnist', one_hot=True) +train_features = mnist.train.images +test_features = mnist.test.images +train_labels = mnist.train.labels.astype(np.float32) +test_labels = mnist.test.labels.astype(np.float32) +weights = tf.Variable(tf.random_normal([n_input, n_classes])) +bias = tf.Variable(tf.random_normal([n_classes])) +features = tf.placeholder(tf.float32, [None, n_input]) +labels = tf.placeholder(tf.float32, [None, n_classes]) +example_features = [['F11', 'F12', 'F13', 'F14'], ['F21', 'F22', 'F23', 'F24'], ['F31', 'F32', 'F33', 'F34'], ['F41', 'F42', 'F43', 'F44']] +example_labels = [['L11', 'L12'], ['L21', 'L22'], ['L31', 'L32'], ['L41', 'L42']] +import math + + +def batches(batch_size, features, labels): + """ + Create batches of features and labels + :param batch_size: The batch size + :param features: List of features + :param labels: List of labels + :return: Batches of (Features, Labels) + """ + assert len(features) == len(labels) + output_batches = [] + sample_size = len(features) + for start_i in range(0, sample_size, batch_size): + end_i = start_i + batch_size + batch = [features[start_i:end_i], labels[start_i:end_i]] + output_batches.append(batch) + return output_batches + + +example_batches = batches(batch_size, example_features, example_labels) +hidden_layer = tf.add(tf.matmul(features, hidden_weights), hidden_biases) +hidden_layer = tf.nn.relu(hidden_layer) +output = tf.add(tf.matmul(hidden_layer, output_weights), output_biases) +import tensorflow as tf +output = None +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[1.0, 2.0, 3.0, 4.0], [-1.0, -2.0, -3.0, -4.0], [11.0, 12.0, 13.0, 14.0]]) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + print(sess.run(logits)) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +import tensorflow as tf +hidden_layer_weights = [[0.1, 0.2, 0.4], [0.4, 0.6, 0.6], [0.5, 0.9, 0.1], [0.8, 0.2, 0.8]] +out_weights = [[0.1, 0.6], [0.2, 0.1], [0.7, 0.9]] +weights = [tf.Variable(hidden_layer_weights), tf.Variable(out_weights)] +biases = [tf.Variable(tf.zeros(3)), tf.Variable(tf.zeros(2))] +features = tf.Variable([[0.0, 2.0, 3.0, 4.0], [0.1, 0.2, 0.3, 0.4], [11.0, 12.0, 13.0, 14.0]]) +keep_prob = tf.placeholder(tf.float32) +hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0]) +hidden_layer = tf.nn.relu(hidden_layer) +hidden_layer = tf.nn.dropout(hidden_layer, keep_prob) +logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1]) +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) +print(sess.run(logits, feed_dict={keep_prob: 0.5})) diff --git a/dataset/preprocessed/719.txt b/dataset/preprocessed/719.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/719.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/72.txt b/dataset/preprocessed/72.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/72.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/721.txt b/dataset/preprocessed/721.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/721.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/722.txt b/dataset/preprocessed/722.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/722.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/723.txt b/dataset/preprocessed/723.txt new file mode 100644 index 0000000..6b104ae --- /dev/null +++ b/dataset/preprocessed/723.txt @@ -0,0 +1,813 @@ +from unittest import TestCase +import pkg_resources +import logging, warnings +import tensorflow as tf +import numpy as np +from deepexplain.tensorflow import DeepExplain +from deepexplain.tensorflow.methods import original_grad +activations = {'Relu': tf.nn.relu, 'Sigmoid': tf.nn.sigmoid, 'Softplus': tf.nn.softplus, 'Tanh': tf.nn.tanh} + + +def simple_model(activation, session): + X = tf.placeholder('float', [None, 2]) + w1 = tf.Variable(initial_value=[[1.0, -1.0], [-1.0, 1.0]]) + b1 = tf.Variable(initial_value=[1.5, -1.0]) + w2 = tf.Variable(initial_value=[[1.1, 1.4], [-0.5, 1.0]]) + b2 = tf.Variable(initial_value=[0.0, 2.0]) + layer1 = activation(tf.matmul(X, w1) + b1) + out = tf.matmul(layer1, w2) + b2 + session.run(tf.global_variables_initializer()) + return X, out + + +def simpler_model(session): + """ + Implements ReLU( ReLU(x1 - 1) - ReLU(x2) ) + : + """ + X = tf.placeholder('float', [None, 2]) + w1 = tf.Variable(initial_value=[[1.0, 0.0], [0.0, 1.0]], trainable=False) + b1 = tf.Variable(initial_value=[-1.0, 0], trainable=False) + w2 = tf.Variable(initial_value=[[1.0], [-1.0]], trainable=False) + l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + out = tf.nn.relu(tf.matmul(l1, w2)) + session.run(tf.global_variables_initializer()) + return X, out + + +def min_model(session): + """ + Implements min(xi) + """ + X = tf.placeholder('float', [None, 2]) + out = tf.reduce_min(X, 1) + session.run(tf.global_variables_initializer()) + return X, out + + +def min_model_2d(session): + """ + Implements min(xi) + """ + X = tf.placeholder('float', [None, 2, 2]) + out = tf.reduce_min(tf.reshape(X, (-1, 4)), 1) + session.run(tf.global_variables_initializer()) + return X, out + + +def simple_multi_inputs_model(session): + """ + Implements Relu (3*x1|2*x2) | is a concat op + : + """ + X1 = tf.placeholder('float', [None, 2]) + X2 = tf.placeholder('float', [None, 2]) + w1 = tf.Variable(initial_value=[[3.0, 0.0], [0.0, 3.0]], trainable=False) + w2 = tf.Variable(initial_value=[[2.0, 0.0], [0.0, 2.0]], trainable=False) + out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + session.run(tf.global_variables_initializer()) + return X1, X2, out + + +def simple_multi_inputs_model2(session): + """ + Implements Relu (3*x1|2*x2) | is a concat op + : + """ + X1 = tf.placeholder('float', [None, 2]) + X2 = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[3.0], trainable=False) + w2 = tf.Variable(initial_value=[2.0], trainable=False) + out = tf.nn.relu(tf.concat([X1 * w1, X2 * w2], 1)) + session.run(tf.global_variables_initializer()) + return X1, X2, out + + +def train_xor(session): + X = tf.placeholder('float', [None, 2]) + Y = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[[0.10711301, -0.0987727], [-1.57625198, 1.34942603]]) + b1 = tf.Variable(initial_value=[-0.30955192, -0.14483099]) + w2 = tf.Variable(initial_value=[[0.69259691], [-0.16255915]]) + b2 = tf.Variable(initial_value=[1.53952825]) + l1 = tf.nn.relu(tf.matmul(X, w1) + b1) + out = tf.matmul(l1, w2) + b2 + session.run(tf.global_variables_initializer()) + loss = tf.reduce_mean(tf.losses.mean_squared_error(Y, out)) + train_step = tf.train.GradientDescentOptimizer(0.05).minimize(loss) + np.random.seed(10) + x = np.random.randint(0, 2, size=(10, 2)) + y = np.expand_dims(np.logical_or(x[:, (0)], x[:, (1)]), -1) + l = None + for _ in range(100): + l, _ = session.run([loss, train_step], feed_dict={X: x, Y: y}) + return np.abs(l - 0.1) < 0.01 + + +class TestDeepExplainGeneralTF(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_tf_available(self): + try: + pkg_resources.require('tensorflow>=1.0.0') + except Exception: + try: + pkg_resources.require('tensorflow-gpu>=1.0.0') + except Exception: + self.fail('Tensorflow requirement not met') + + def test_simple_model(self): + X, out = simple_model(tf.nn.relu, self.session) + xi = np.array([[1, 0]]) + r = self.session.run(out, {X: xi}) + self.assertEqual(r.shape, xi.shape) + np.testing.assert_equal(r[0], [2.75, 5.5]) + + def test_simpler_model(self): + X, out = simpler_model(self.session) + xi = np.array([[3.0, 1.0]]) + r = self.session.run(out, {X: xi}) + self.assertEqual(r.shape, (xi.shape[0], 1)) + np.testing.assert_equal(r[0], [1.0]) + + def test_training(self): + session = tf.Session() + r = train_xor(session) + self.assertTrue(r) + + def test_context(self): + """ + DeepExplain overrides nonlinearity gradient + """ + from deepexplain.tensorflow import DeepExplain + X = tf.placeholder('float', [None, 1]) + for name in activations: + x1 = activations[name](X) + x1_g = tf.gradients(x1, X)[0] + self.assertEqual(x1_g.op.type, '%sGrad' % name) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + for name in activations: + x2 = activations[name](X) + self.assertEqual(x2.op.get_attr('_gradient_op_type').decode('utf-8'), 'DeepExplainGrad') + + def test_mismatch_input_lens(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1 = tf.placeholder('float', [None, 1]) + X2 = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[[0.10711301]]) + w2 = tf.Variable(initial_value=[[0.69259691]]) + out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + self.session.run(tf.global_variables_initializer()) + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', out, [X1, X2], [[1], [2], [3]]) + self.assertIn('List of input tensors and input data have different lengths', str(cm.exception)) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1 = tf.placeholder('float', [None, 1]) + X2 = tf.placeholder('float', [None, 1]) + w1 = tf.Variable(initial_value=[[10.0]]) + w2 = tf.Variable(initial_value=[[10.0]]) + out = tf.matmul(X1, w1) + tf.matmul(X2, w2) + self.session.run(tf.global_variables_initializer()) + attributions = de.explain('grad*input', out, [X1, X2], [[[2]], [[3]]]) + self.assertEqual(len(attributions), 2) + self.assertEqual(attributions[0][0], 20.0) + self.assertEqual(attributions[1][0], 30.0) + + def test_supported_activations(self): + X = tf.placeholder('float', [None, 3]) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + xi = [[-1, 0, 1]] + Y = tf.nn.relu(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [0, 0, 1], 7) + Y = tf.nn.elu(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [-0.632120558, 0, 1], 7) + Y = tf.nn.sigmoid(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [0.268941421, 0.5, 0.731058578], 7) + Y = tf.nn.tanh(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [-0.761594155, 0, 0.761594155], 7) + Y = tf.nn.softplus(X) + r = self.session.run(Y, {X: xi}) + np.testing.assert_almost_equal(r[0], [0.313261687, 0.693147181, 1.31326168], 7) + + def test_original_grad(self): + X = tf.placeholder('float', [None, 3]) + for name in activations: + Y = activations[name](X) + grad = original_grad(Y.op, tf.ones_like(X)) + self.assertTrue('Tensor' in str(type(grad))) + + def test_warning_unsupported_activations(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X = tf.placeholder('float', [None, 3]) + Y = tf.nn.relu6(X) + xi = [[-1, 0, 1]] + de.explain('elrp', Y, X, xi) + assert any([('unsupported activation' in str(wi.message)) for wi in w]) + + def test_override_as_default(self): + """ + In DeepExplain context, nonlinearities behave as default, including training time + """ + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + r = train_xor(self.session) + self.assertTrue(r) + + def test_explain_not_in_context(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + pass + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', None, None, None) + self.assertEqual('Explain can be called only within a DeepExplain context.', str(cm.exception)) + + def test_invalid_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with self.assertRaises(RuntimeError) as cm: + de.explain('invalid', None, None, None) + self.assertIn('Method must be in', str(cm.exception)) + + def test_T_is_tensor(self): + X = tf.placeholder('float', [None, 3]) + Y = tf.nn.relu(X) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', [Y], X, [[0, 0, 0]]) + self.assertIn('T must be a Tensorflow Tensor object', str(cm.exception)) + + def test_X_is_tensor(self): + X = tf.placeholder('float', [None, 3]) + Y = tf.nn.relu(X) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', Y, np.eye(3), [[0, 0, 0]]) + self.assertIn('Tensorflow Tensor object', str(cm.exception)) + + def test_all_in_X_are_tensor(self): + X = tf.placeholder('float', [None, 3]) + Y = tf.nn.relu(X) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', Y, [X, np.eye(3)], [[0, 0, 0]]) + self.assertIn('Tensorflow Tensor object', str(cm.exception)) + + def test_X_has_compatible_batch_dim(self): + X = tf.placeholder('float', [10, 3]) + Y = tf.nn.relu(X) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with self.assertRaises(RuntimeError) as cm: + de.explain('grad*input', Y, X, [[0, 0, 0]], batch_size=2) + self.assertIn('the first dimension of the input tensor', str(cm.exception)) + + def test_T_has_compatible_batch_dim(self): + X, out = simpler_model(self.session) + xi = np.array([[-10, -5]]).repeat(50, 0) + Y = out * np.expand_dims(np.array(range(50)), -1) + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + with self.assertRaises(RuntimeError) as cm: + de.explain('saliency', Y, X, xi, batch_size=10) + self.assertIn('the first dimension of the target tensor', str(cm.exception)) + + def test_use_of_target_weights(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, T = simple_model(tf.identity, self.session) + xi = np.array([[1, 0]]) + yi1 = np.array([[1, 0]]) + yi2 = np.array([[0, 1]]) + yi3 = np.array([[1, 1]]) + yi4 = np.array([[0, 0]]) + a1 = de.explain('saliency', T, X, xi, ys=yi1) + a2 = de.explain('saliency', T, X, xi, ys=yi2) + a3 = de.explain('saliency', T, X, xi, ys=yi3) + a4 = de.explain('saliency', T, X, xi, ys=yi4) + np.testing.assert_almost_equal(a1 + a2, a3, 10) + np.testing.assert_almost_equal(a4, np.array([[0.0, 0.0]]), 10) + + def test_use_of_target_weights_batch(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, T = simple_model(tf.identity, self.session) + xi = np.array([[1, 0]]).repeat(20, 0) + yi1 = np.array([[1, 0]]).repeat(20, 0) + yi2 = np.array([[0, 1]]).repeat(20, 0) + yi3 = np.array([[1, 1]]).repeat(20, 0) + yi4 = np.array([[0, 0]]).repeat(20, 0) + a1 = de.explain('saliency', T, X, xi, ys=yi1, batch_size=5) + a2 = de.explain('saliency', T, X, xi, ys=yi2, batch_size=5) + a3 = de.explain('saliency', T, X, xi, ys=yi3, batch_size=5) + a4 = de.explain('saliency', T, X, xi, ys=yi4, batch_size=5) + np.testing.assert_almost_equal(a1 + a2, a3, 10) + np.testing.assert_almost_equal(a4, np.array([[0.0, 0.0]]).repeat(20, 0), 10) + + def test_wrong_weight_len(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, T = simple_model(tf.identity, self.session) + xi = np.array([[1, 0]]).repeat(20, 0) + yi1 = np.array([[1, 0]]) + with self.assertRaises(RuntimeError) as cm: + de.explain('saliency', T, X, xi, ys=yi1, batch_size=5) + self.assertIn('the number of elements in ys must equal ', str(cm.exception)) + + def test_explainer_api_memory(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, T = simple_model(tf.identity, self.session) + xi = np.array([[1, 0]]).repeat(20, 0) + prev_ops_count = None + explainer = de.get_explainer('saliency', T, X) + for i in range(10): + explainer.run(xi) + ops_count = len([n.name for n in tf.get_default_graph().as_graph_def().node]) + if prev_ops_count is None: + prev_ops_count = ops_count + else: + self.assertEquals(prev_ops_count, ops_count) + + +class TestDummyMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_dummy_zero(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.sigmoid, self.session) + xi = np.array([[10, -10]]) + attributions = de.explain('zero', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions[0], [0.0, 0.0], 10) + + def test_gradient_restored(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.sigmoid, self.session) + xi = np.array([[10, -10]]) + de.explain('zero', out, X, xi) + r = train_xor(self.session) + self.assertTrue(r) + + +class TestSaliencyMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_saliency_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('saliency', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, 1.0]], 10) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('saliency', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[2.0, 2.0]], 10) + + def test_multiple_inputs_explainer_api(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + explainer = de.get_explainer('saliency', out, [X1, X2]) + attributions = explainer.run(xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[2.0, 2.0]], 10) + + +class TestGradInputMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_saliency_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('grad*input', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 10) + + def test_saliency_method_explainer_api(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + explainer = de.get_explainer('grad*input', out, X) + attributions = explainer.run(xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 10) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('grad*input', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + +class TestIntegratedGradientsMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_int_grad(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('intgrad', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 10) + + def test_int_grad_higher_precision(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('intgrad', out, X, xi, steps=500) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.5, -0.5]], 10) + + def test_int_grad_baseline(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[2, 0]]) + attributions = de.explain('intgrad', out, X, xi, baseline=np.array([1, 0])) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[1.0, 0.0]], 10) + + def test_int_grad_baseline_explainer_api(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[2, 0]]) + explainer = de.get_explainer('intgrad', out, X, baseline=np.array([1, 0])) + attributions = explainer.run(xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[1.0, 0.0]], 10) + + def test_int_grad_baseline_2(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[2, 0], [3, 0]]) + attributions = de.explain('intgrad', out, X, xi, baseline=np.array([1, 0])) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[1.0, 0.0], [2.0, 0.0]], 10) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('intgrad', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 10) + + def test_multiple_inputs_different_sizes(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model2(self.session) + xi = [np.array([[-10, -5]]), np.array([[3]])] + attributions = de.explain('intgrad', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[6]], 10) + + def test_intgrad_targeting_equivalence(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.relu, self.session) + xi = np.array([[5, 3]]) + self.assertEqual(out.shape[1], 2) + a1 = de.explain('intgrad', out * np.array([[1, 0]]), X, xi) + b1 = de.explain('intgrad', out * np.array([[0, 1]]), X, xi) + a2 = de.explain('intgrad', out, X, xi, ys=np.array([[1, 0]])) + b2 = de.explain('intgrad', out, X, xi, ys=np.array([[0, 1]])) + np.testing.assert_almost_equal(a1, a2, 1) + np.testing.assert_almost_equal(b1, b2, 1) + + +class TestEpsilonLRPMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_elrp_method(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('elrp', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 3) + + def test_elrp_epsilon(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('elrp', out, X, xi, epsilon=1e-09) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 7) + + def test_elrp_epsilon_explainer_api(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + explainer = de.get_explainer('elrp', out, X, epsilon=1e-09) + attributions = explainer.run(xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [3.0, -1.0]], 7) + + def test_elrp_zero_epsilon(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + with self.assertRaises(AssertionError): + de.explain('elrp', out, X, xi, epsilon=0) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('elrp', out, [X1, X2], xi, epsilon=1e-09) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + def test_elrp_targeting_equivalence(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.relu, self.session) + xi = np.array([[5, 3]]) + self.assertEqual(out.shape[1], 2) + a1 = de.explain('elrp', out * np.array([[1, 0]]), X, xi) + b1 = de.explain('elrp', out * np.array([[0, 1]]), X, xi) + a2 = de.explain('elrp', out, X, xi, ys=np.array([[1, 0]])) + b2 = de.explain('elrp', out, X, xi, ys=np.array([[0, 1]])) + np.testing.assert_almost_equal(a1, a2, 1) + np.testing.assert_almost_equal(b1, b2, 1) + + +class TestDeepLIFTMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_deeplift(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('deeplift', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [2.0, -1.0]], 10) + + def test_deeplift_batches(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + xi = np.repeat(xi, 25, 0) + self.assertEqual(xi.shape[0], 50) + attributions = de.explain('deeplift', out, X, xi, batch_size=32) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, np.repeat([[0.0, 0.0], [2.0, -1.0]], 25, 0), 10) + + def test_deeplift_batches_explainer_api(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + xi = np.repeat(xi, 25, 0) + self.assertEqual(xi.shape[0], 50) + explaoiner = de.get_explainer('deeplift', out, X) + attributions = explaoiner.run(xi, batch_size=32) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, np.repeat([[0.0, 0.0], [2.0, -1.0]], 25, 0), 10) + + def test_deeplift_baseline(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[3, 1]]) + attributions = de.explain('deeplift', out, X, xi, baseline=xi[0]) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0]], 5) + + def test_multiple_inputs(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + attributions = de.explain('deeplift', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 7) + np.testing.assert_almost_equal(attributions[1], [[6.0, 2.0]], 7) + + def test_multiple_inputs_different_sizes(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model2(self.session) + xi = [np.array([[-10, -5]]), np.array([[3]])] + attributions = de.explain('deeplift', out, [X1, X2], xi) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], [[0.0, 0.0]], 10) + np.testing.assert_almost_equal(attributions[1], [[6]], 10) + + def test_multiple_inputs_different_sizes_batches(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model2(self.session) + xi = [np.array([[-10, -5]]).repeat(50, 0), np.array([[3]]).repeat(50, 0)] + attributions = de.explain('deeplift', out, [X1, X2], xi, batch_size=32) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], np.repeat([[0.0, 0.0]], 50, 0), 10) + np.testing.assert_almost_equal(attributions[1], np.repeat([[6]], 50, 0), 10) + + def test_multiple_inputs_different_sizes_batches_disable(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model2(self.session) + xi = [np.array([[-10, -5]]).repeat(50, 0), np.array([[3]]).repeat(50, 0)] + attributions = de.explain('deeplift', out, [X1, X2], xi, batch_size=None) + self.assertEqual(len(attributions), len(xi)) + np.testing.assert_almost_equal(attributions[0], np.repeat([[0.0, 0.0]], 50, 0), 10) + np.testing.assert_almost_equal(attributions[1], np.repeat([[6]], 50, 0), 10) + + def test_deeplift_targeting_equivalence(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.relu, self.session) + xi = np.array([[5, 3]]) + self.assertEqual(out.shape[1], 2) + a1 = de.explain('deeplift', out * np.array([[1, 0]]), X, xi) + b1 = de.explain('deeplift', out * np.array([[0, 1]]), X, xi) + a2 = de.explain('deeplift', out, X, xi, ys=np.array([[1, 0]])) + b2 = de.explain('deeplift', out, X, xi, ys=np.array([[0, 1]])) + np.testing.assert_almost_equal(a1, a2, 1) + np.testing.assert_almost_equal(b1, b2, 1) + + +class TestOcclusionMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_occlusion(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('occlusion', out, X, xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, -1.0]], 10) + + def test_occlusion_explainer_api(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + explainer = de.get_explainer('occlusion', out, X) + attributions = explainer.run(xi) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [1.0, -1.0]], 10) + + def test_occlusion_batches(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]).repeat(10, 0) + attributions = de.explain('occlusion', out, X, xi, batch_size=5) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, np.repeat([[0.0, 0.0], [1.0, -1.0]], 10, 0), 10) + + def test_window_shape(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('occlusion', out, X, xi, window_shape=(2,)) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, 0.0], [0.5, 0.5]], 10) + + def test_nan_warning(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simpler_model(self.session) + xi = np.array([[-10, -5], [3, 1]]) + attributions = de.explain('occlusion', out, X, xi, step=2) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, np.nan], [1.0, np.nan]], 10) + assert any([('nans' in str(wi.message)) for wi in w]) + + def test_multiple_inputs_error(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + with self.assertRaises(RuntimeError) as cm: + de.explain('occlusion', out, [X1, X2], xi) + self.assertIn('not yet supported', str(cm.exception)) + + def test_occlusion_targeting_equivalence(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.nn.relu, self.session) + xi = np.array([[5, 3]]) + self.assertEqual(out.shape[1], 2) + a1 = de.explain('occlusion', out * np.array([[1, 0]]), X, xi) + b1 = de.explain('occlusion', out * np.array([[0, 1]]), X, xi) + a2 = de.explain('occlusion', out, X, xi, ys=np.array([[1, 0]])) + b2 = de.explain('occlusion', out, X, xi, ys=np.array([[0, 1]])) + np.testing.assert_almost_equal(a1, a2, 10) + np.testing.assert_almost_equal(b1, b2, 10) + + +class TestShapleySamplingMethod(TestCase): + + def setUp(self): + self.session = tf.Session() + + def tearDown(self): + self.session.close() + tf.reset_default_graph() + + def test_shapley_sampling(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = min_model(self.session) + xi = np.array([[2, -2], [4, 2]]) + attributions = de.explain('shapley_sampling', out, X, xi, samples=300) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, [[0.0, -2.0], [1.0, 1.0]], 1) + + def test_shapley_sampling_batches(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = min_model(self.session) + xi = np.array([[2, -2], [4, 2]]).repeat(20, 0) + attributions = de.explain('shapley_sampling', out, X, xi, samples=300, batch_size=5) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, np.repeat([[0.0, -2.0], [1.0, 1.0]], 20, 0), 1) + + def test_shapley_sampling_batches_explainer_api(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = min_model(self.session) + xi = np.array([[2, -2], [4, 2]]).repeat(20, 0) + explainer = de.get_explainer('shapley_sampling', out, X, samples=300) + attributions = explainer.run(xi, batch_size=5) + self.assertEqual(attributions.shape, xi.shape) + np.testing.assert_almost_equal(attributions, np.repeat([[0.0, -2.0], [1.0, 1.0]], 20, 0), 1) + + def test_shapley_sampling_dims(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = min_model_2d(self.session) + xi = np.array([[[-1, 4], [-2, 1]]]) + attributions = de.explain('shapley_sampling', out, X, xi, samples=300, sampling_dims=[1]) + self.assertEqual(attributions.shape, (1, 2)) + np.testing.assert_almost_equal(attributions, [[-0.5, -1.5]], 1) + + def test_multiple_inputs_error(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X1, X2, out = simple_multi_inputs_model(self.session) + xi = [np.array([[-10, -5]]), np.array([[3, 1]])] + with self.assertRaises(RuntimeError) as cm: + de.explain('shapley_sampling', out, [X1, X2], xi) + self.assertIn('not yet supported', str(cm.exception)) + + def test_shapley_targeting_equivalence(self): + with DeepExplain(graph=tf.get_default_graph(), session=self.session) as de: + X, out = simple_model(tf.identity, self.session) + xi = np.array([[5, 3]]) + self.assertEqual(out.shape[1], 2) + np.random.seed(10) + a1 = de.explain('shapley_sampling', out * np.array([[1, 0]]), X, xi, samples=10) + np.random.seed(10) + b1 = de.explain('shapley_sampling', out * np.array([[0, 1]]), X, xi, samples=10) + np.random.seed(10) + a2 = de.explain('shapley_sampling', out, X, xi, ys=np.array([[1, 0]]), samples=10) + np.random.seed(10) + b2 = de.explain('shapley_sampling', out, X, xi, ys=np.array([[0, 1]]), samples=10) + np.testing.assert_almost_equal(a1, a2, 3) + np.testing.assert_almost_equal(b1, b2, 3) diff --git a/dataset/preprocessed/724.txt b/dataset/preprocessed/724.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/724.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/725.txt b/dataset/preprocessed/725.txt new file mode 100644 index 0000000..8110698 --- /dev/null +++ b/dataset/preprocessed/725.txt @@ -0,0 +1,62 @@ +import tensorflow as tf +m1 = tf.constant([[2, 2]]) +m2 = tf.constant([[3], [3]]) +dot_operation = tf.matmul(m1, m2) +init = tf.OptimizerOptions() +sess = tf.Session() +result = sess.run(dot_operation) +print(result) +sess.close() +with tf.Session() as sess: + result_ = sess.run(dot_operation) + print(result_) +x1 = tf.placeholder(tf.float32, shape=None) +y1 = tf.placeholder(tf.float32, shape=None) +z1 = x1 + y1 +x2 = tf.placeholder(tf.float32, shape=[2, 1]) +y2 = tf.placeholder(tf.float32, shape=[1, 2]) +z2 = tf.matmul(x2, y2) +with tf.Session() as sess: + z1_value = sess.run(z1, feed_dict={x1: 1, y1: 2}) + z2_value = sess.run(z2, feed_dict={x2: [[2], [2]], y2: [[3, 3]]}) + print(z1_value) + print(z2_value) + z1_value, z2_value = sess.run([z1, z2], feed_dict={x1: 1, y1: 2, x2: [[2], [2]], y2: [[3, 3]]}) + print(z1_value) + print(z2_value) +var = tf.Variable(0) +add_operation = tf.add(var, 1) +update_operation = tf.assign(var, add_operation) +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + for _ in range(3): + sess.run(update_operation) + print(sess.run(var)) +import numpy as np +import matplotlib.pyplot as plt +x = np.linspace(-5, 5, 200) +y_relu = tf.nn.relu(x) +y_sigmoid = tf.nn.sigmoid(x) +y_tanh = tf.nn.tanh(x) +y_softplus = tf.nn.softplus(x) +y_softmax = tf.nn.softmax(x) +sess = tf.Session() +y_relu, y_sigmoid, y_tanh, y_softplus, y_softmax = sess.run([y_relu, y_sigmoid, y_tanh, y_softplus, y_softmax]) +plt.figure(1, figsize=(8, 6)) +plt.subplot(221) +plt.plot(x, y_relu, c='red', label='relu') +plt.ylim((-1, 5)) +plt.legend(loc='best') +plt.subplot(222) +plt.plot(x, y_sigmoid, c='red', label='sigmoid') +plt.ylim((-0.2, 1.2)) +plt.legend(loc='best') +plt.subplot(223) +plt.plot(x, y_tanh, c='red', label='tanh') +plt.ylim((-1.2, 1.2)) +plt.legend(loc='best') +plt.subplot(224) +plt.plot(x, y_softplus, c='red', label='softplus') +plt.ylim((-0.2, 6)) +plt.legend(loc='best') +plt.show() diff --git a/dataset/preprocessed/726.txt b/dataset/preprocessed/726.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/726.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/727.txt b/dataset/preprocessed/727.txt new file mode 100644 index 0000000..2a37dd3 --- /dev/null +++ b/dataset/preprocessed/727.txt @@ -0,0 +1,237 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + reg = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss += reg * tf.nn.l2_loss(weights) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, reg: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + reg = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + lay1_train = tf.nn.relu(logits) + secLogits = tf.matmul(lay1_train, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=secLogits)) + loss += reg * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(secLogits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights) + biases) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +num_steps = 3001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, reg: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +num_steps = 3001 +n_batches = 5 +limited_train_dataset = train_dataset[0:batch_size * n_batches] +limited_train_labels = train_labels[0:batch_size * n_batches] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + reg = tf.placeholder(tf.float32) + W1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + b1 = tf.Variable(tf.zeros([num_hidden_nodes])) + W2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + b2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, W1) + b1), W2) + b2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss += reg * (tf.nn.l2_loss(W1) + tf.nn.l2_loss(W2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, W1) + b1), W2) + b2) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, W1) + b1), W2) + b2) +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (limited_train_labels.shape[0] - batch_size) + batch_data = limited_train_dataset[offset:offset + batch_size, :] + batch_labels = limited_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, reg: 0.001} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes = 1024 +num_steps = 3001 +reg = 0.001 +dropout = 0.5 +n_batches = 5 +limited_train_dataset = train_dataset[0:batch_size * n_batches] +limited_train_labels = train_labels[0:batch_size * n_batches] +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes])) + biases = tf.Variable(tf.zeros([num_hidden_nodes])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes, num_labels])) + biases2 = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + lay1_train = tf.nn.relu(logits) + drop1 = tf.nn.dropout(lay1_train, 0.5) + secLogits = tf.matmul(drop1, weights2) + biases2 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=secLogits)) + loss += reg * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights2)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(secLogits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights) + biases) + valid_prediction = tf.nn.softmax(tf.matmul(lay1_valid, weights2) + biases2) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(lay1_test, weights2) + biases2) +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (limited_train_labels.shape[0] - batch_size) + batch_data = limited_train_dataset[offset:offset + batch_size, :] + batch_labels = limited_train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_hidden_nodes1 = 1024 +num_hidden_nodes2 = 100 +beta_regul = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + global_step = tf.Variable(0) + weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_nodes1], stddev=np.sqrt(2.0 / (image_size * image_size)))) + biases1 = tf.Variable(tf.zeros([num_hidden_nodes1])) + weights2 = tf.Variable(tf.truncated_normal([num_hidden_nodes1, num_hidden_nodes2], stddev=np.sqrt(2.0 / num_hidden_nodes1))) + biases2 = tf.Variable(tf.zeros([num_hidden_nodes2])) + weights3 = tf.Variable(tf.truncated_normal([num_hidden_nodes2, num_labels], stddev=np.sqrt(2.0 / num_hidden_nodes2))) + biases3 = tf.Variable(tf.zeros([num_labels])) + lay1_train = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) + lay2_train = tf.nn.relu(tf.matmul(lay1_train, weights2) + biases2) + logits = tf.matmul(lay2_train, weights3) + biases3 + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + beta_regul * (tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) + learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.65, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + lay1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1) + lay2_valid = tf.nn.relu(tf.matmul(lay1_valid, weights2) + biases2) + valid_prediction = tf.nn.softmax(tf.matmul(lay2_valid, weights3) + biases3) + lay1_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1) + lay2_test = tf.nn.relu(tf.matmul(lay1_test, weights2) + biases2) + test_prediction = tf.nn.softmax(tf.matmul(lay2_test, weights3) + biases3) +num_steps = 9001 +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/728.txt b/dataset/preprocessed/728.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/728.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/729.txt b/dataset/preprocessed/729.txt new file mode 100644 index 0000000..e614c05 --- /dev/null +++ b/dataset/preprocessed/729.txt @@ -0,0 +1,466 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(LSTMAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.tanh(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + return out, lstm_state + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + """ + + def __init__(self, hidden_size, dropout): + self.hidden_size = hidden_size + self.dropout = dropout + + def encode(self, inputs, masks, attention_inputs=None, initial_state=(None, None), model_type='gru', name='encoder', reuse=False): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(name, reuse=reuse): + if attention_inputs is None: + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + else: + raise Exception('Must specify model type.') + else: + assert model_type == 'lstm' + cell = LSTMAttnCell(self.hidden_size, attention_inputs) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + outputs, final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs, sequence_length=masks, initial_state_fw=initial_state[0], initial_state_bw=initial_state[1], dtype=tf.float32) + final_outputs = outputs[0] + outputs[1] + return final_outputs, final_state + + +class Decoder(object): + + def __init__(self, hidden_size, output_size, dropout): + self.hidden_size = hidden_size + self.output_size = output_size + self.dropout = dropout + + def decode(self, knowledge_rep, masks, initial_state=(None, None)): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + with vs.variable_scope('decoder'): + with vs.variable_scope('answer_start'): + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + start_states, start_final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, knowledge_rep, sequence_length=masks, initial_state_fw=initial_state[0], initial_state_bw=initial_state[1], dtype=tf.float32) + start_states = start_states[0] + start_states[1] + start_states_reshaped = tf.reshape(start_states, [-1, self.hidden_size]) + start_probs = tf.nn.rnn_cell._linear(start_states_reshaped, output_size=1, bias=True) + start_probs = tf.reshape(start_probs, [-1, self.output_size]) + with vs.variable_scope('answer_end'): + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + end_states, end_final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, knowledge_rep, sequence_length=masks, initial_state_fw=start_final_state[0], initial_state_bw=start_final_state[1], dtype=tf.float32) + end_states = end_states[0] + end_states[1] + end_states_reshaped = tf.reshape(end_states, [-1, self.hidden_size]) + end_probs = tf.nn.rnn_cell._linear(end_states_reshaped, output_size=1, bias=True) + end_probs = tf.reshape(end_probs, [-1, self.output_size]) + bool_masks = tf.cast(tf.sequence_mask(masks, maxlen=self.output_size), tf.float32) + add_mask = -1e+30 * (1.0 - bool_masks) + start_probs = tf.add(start_probs, add_mask) + end_probs = tf.add(end_probs, add_mask) + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, pretrained_embeddings, flags): + """ + Initializes your System + + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.flags = flags + self.h_size = self.flags.state_size + self.p_size = self.flags.output_size + self.q_size = self.flags.question_size + self.embed_size = self.flags.embedding_size + self.dropout = self.flags.dropout + self.encoder = Encoder(hidden_size=self.h_size, dropout=1.0 - self.flags.dropout) + self.decoder = Decoder(hidden_size=self.h_size, output_size=self.p_size, dropout=1.0 - self.flags.dropout) + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.p_size), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.q_size), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + self.saver = tf.train.Saver() + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def mixer(self, q_states, ctx_states): + with vs.variable_scope('mixer'): + A = tf.nn.softmax(batch_matmul(ctx_states, tf.transpose(q_states, perm=[0, 2, 1]))) + C_P = batch_matmul(A, q_states) + C_P = tf.reshape(C_P, [-1, self.h_size]) + P = tf.reshape(ctx_states, [-1, self.h_size]) + P_final = tf.nn.rnn_cell._linear([C_P, P], output_size=self.h_size, bias=True) + P_final = tf.reshape(P_final, [-1, self.p_size, self.h_size]) + return P_final + + def filter(self, Q, P): + with vs.variable_scope('filter'): + Qn = tf.nn.l2_normalize(Q, dim=2) + Pn = tf.nn.l2_normalize(P, dim=2) + R = batch_matmul(Qn, tf.transpose(Pn, perm=[0, 2, 1])) + r = tf.reduce_max(R, axis=1) + r = tf.expand_dims(r, axis=2) + P_filtered = P * r + return P_filtered + + def coattention(self, P, Q, masks): + P_t = tf.transpose(P, perm=[0, 2, 1]) + Q_t = tf.transpose(Q, perm=[0, 2, 1]) + L = tf.batch_matmul(P, Q_t) + AQ = tf.nn.softmax(L, dim=1) + AD = tf.nn.softmax(tf.transpose(L, perm=[0, 2, 1]), dim=1) + CQ = tf.batch_matmul(P_t, AQ) + contexts = tf.concat(1, [Q_t, CQ]) + CD = tf.batch_matmul(contexts, AD) + lstm_inputs = tf.transpose(tf.concat(1, [P_t, CD]), perm=[0, 2, 1]) + cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, output_keep_prob=self.dropout) + cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, output_keep_prob=self.dropout) + all_states, _ = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, lstm_inputs, sequence_length=masks, dtype=tf.float32) + U = tf.concat(2, all_states) + U = U[:, :self.p_size, :] + return U + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + P_filtered = self.context_embeddings + P_filtered = self.filter(Q=self.question_embeddings, P=self.context_embeddings) + question_states, final_question_state = self.encoder.encode(self.question_embeddings, self.mask_q_placeholder, attention_inputs=None, model_type=self.flags.model_type, reuse=False, name='q_encoder') + ctx_states, final_ctx_state = self.encoder.encode(P_filtered, self.mask_ctx_placeholder, attention_inputs=None, initial_state=final_question_state, model_type=self.flags.model_type, reuse=False, name='ctx_encoder') + feed_states = self.mixer(q_states=question_states, ctx_states=ctx_states) + self.start_probs, self.end_probs = self.decoder.decode(knowledge_rep=feed_states, masks=self.mask_ctx_placeholder, initial_state=final_question_state) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + """ + q_mean,q_var = tf.nn.moments(question_embeddings, axes=[2], shift=None, name=None, keep_dims=True) + ctx_mean,ctx_var = tf.nn.moments(context_embeddings, axes=[2], shift=None, name=None, keep_dims=True) + + + + # q_scale = tf.Variable(tf.zeros,name='q_scale',dtype=tf.float32,trainable=True) + # ctx_scale = tf.Variable(1.0,name='ctx_scale',dtype=tf.float32,trainable=True) + # q_offset = tf.Variable(1.0,name='q_offset',dtype=tf.float32,trainable=True) + # ctx_offset = tf.Variable(1.0,name='ctx_offset',dtype=tf.float32,trainable=True) + + + q_scale = tf.get_variable("q_scale", shape=[self.q_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + ctx_scale = tf.get_variable("ctx_scale", shape=[self.p_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + q_offset = tf.get_variable("q_offset", shape=[self.q_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + ctx_offset = tf.get_variable("ctx_offset", shape=[self.p_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + + + self.question_embeddings = tf.nn.batch_normalization(question_embeddings, q_mean, q_var, q_offset,q_scale, variance_epsilon=0.0000001) + self.context_embeddings = tf.nn.batch_normalization(context_embeddings, ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon=0.0000001) + """ + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs + + def answer(self, session, data): + yp_lst = [] + yp2_lst = [] + prog_train = Progbar(target=1 + int(len(data[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle=False)): + yp, yp2 = self.decode(session, *batch) + yp_lst.append(yp) + yp2_lst.append(yp2) + prog_train.update(i + 1, [('Answering Questions....', 0.0)]) + print('') + yp_all = np.concatenate(yp_lst, axis=0) + yp2_all = np.concatenate(yp2_lst, axis=0) + a_s = np.argmax(yp_all, axis=1) + a_e = np.argmax(yp2_all, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(session=sess, context_batch=context_batch, question_batch=question_batch, answer_span_batch=answer_span_batch, mask_ctx_batch=mask_ctx_batch, mask_q_batch=mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset[0]) + else: + inds = np.random.choice(len(dataset[0]), sample) + sampled = [elem[inds] for elem in dataset] + context = [context[i] for i in inds] + a_s, a_e = self.answer(session, sampled) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + f1 = [] + em = [] + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + cur_em = exact_match_score(pred_words, actual_words) + em.append(float(cur_em)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + return np.mean(f1), np.mean(em) + + def run_epoch(self, sess, train_set, val_set, train_context, val_context): + prog_train = Progbar(target=1 + int(len(train_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + print('') + prog_val = Progbar(target=1 + int(len(val_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + self.evaluate_answer(session=sess, dataset=train_set, context=train_context, sample=len(val_set[0]), log=True, eval_set='-Epoch TRAIN-') + self.evaluate_answer(session=sess, dataset=val_set, context=val_context, sample=None, log=True, eval_set='-Epoch VAL-') + + def train(self, session, dataset, val_dataset, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = [elem[:self.flags.batch_size * 1] for elem in train_dataset] + val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + num_epochs = 100 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, train_context=train_context, val_context=val_context) + logging.info('Saving model in %s', train_dir) + self.saver.save(session, train_dir + '/' + self.flags.run_name + '.ckpt') + + def minibatches(self, data, batch_size, shuffle=True): + num_data = len(data[0]) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + indices = np.arange(num_data) + if shuffle: + np.random.shuffle(indices) + for minibatch_start in np.arange(0, num_data, batch_size): + minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] diff --git a/dataset/preprocessed/73.txt b/dataset/preprocessed/73.txt new file mode 100644 index 0000000..ee15997 --- /dev/null +++ b/dataset/preprocessed/73.txt @@ -0,0 +1,205 @@ +import tensorflow as tf +import load_data as ld +import os +import numpy as np + + +class Config(object): + n_features = 36 + n_height = 6 + n_classes = 3 + dropout = 0.5 + hidden_size_1 = 512 + hidden_size_2 = 256 + batch_size = 4096 + n_epochs = 100 + lr = 0.0001 + lamda = 0.01 + trainrate = 0.9 + + +class NN(object): + + def add_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=(None, Config.n_height, Config.n_height, 1), name='input') + self.labels_placeholder = tf.placeholder(tf.int32, shape=(None, Config.n_classes), name='label') + self.dropout_placeholder = tf.placeholder(tf.float32, name='drop') + self.is_training = tf.placeholder(tf.bool) + + def create_feed_dict(self, inputs_batch, labels_batch, dropout=1, is_training=False): + feed_dict = {self.input_placeholder: inputs_batch, self.labels_placeholder: labels_batch, self.dropout_placeholder: dropout, self.is_training: is_training} + return feed_dict + + def add_prediction_op(self): + xavier_initializer = tf.contrib.layers.xavier_initializer() + with tf.name_scope('w_variable'): + w1_init = xavier_initializer((3 * 3 * 32, Config.hidden_size_1)) + self.W1 = tf.Variable(w1_init, name='W1') + tf.summary.histogram('W1', self.W1) + w2_init = xavier_initializer((Config.hidden_size_1, Config.hidden_size_2)) + self.W2 = tf.Variable(w2_init, name='W2') + tf.summary.histogram('W2', self.W2) + w3_init = xavier_initializer((Config.hidden_size_2, Config.n_classes)) + self.W3 = tf.Variable(w3_init, name='W3') + tf.summary.histogram('W3', self.W3) + with tf.name_scope('b_variable'): + b1 = tf.Variable(tf.zeros(Config.hidden_size_1), name='b1') + tf.summary.histogram('b1', b1) + b2 = tf.Variable(tf.zeros(Config.hidden_size_2), name='b2') + tf.summary.histogram('b2', b2) + b3 = tf.Variable(tf.zeros(Config.n_classes), name='b3') + tf.summary.histogram('b3', b3) + self.global_step = tf.Variable(0) + with tf.name_scope('cnn'): + layer_cnn = tf.layers.conv2d(self.input_placeholder, 32, 3, strides=1, padding='SAME') + hidden = tf.nn.relu(layer_cnn) + pool = tf.nn.max_pool(hidden, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + shape = pool.get_shape().as_list() + reshape = tf.reshape(pool, [-1, shape[1] * shape[2] * shape[3]]) + with tf.name_scope('layer1'): + layer1 = tf.matmul(reshape, self.W1) + b1 + h_batch1 = tf.layers.batch_normalization(layer1, center=True, scale=True, training=self.is_training) + hidden1 = tf.nn.relu(h_batch1) + tf.summary.histogram('hidden1_out', hidden1) + h_drop1 = tf.nn.dropout(hidden1, self.dropout_placeholder) + with tf.name_scope('layer2'): + layer2 = tf.matmul(h_drop1, self.W2) + b2 + h_batch2 = tf.layers.batch_normalization(layer2, center=True, scale=True, training=self.is_training) + hidden2 = tf.nn.relu(h_batch2) + tf.summary.histogram('hidden2_out', hidden2) + h_drop2 = tf.nn.dropout(hidden2, self.dropout_placeholder) + with tf.name_scope('output'): + pred = tf.matmul(h_drop2, self.W3) + b3 + tf.summary.histogram('out', pred) + return pred + + def add_loss_op(self, pred): + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=self.labels_placeholder)) + loss += Config.lamda * (tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.W2) + tf.nn.l2_loss(self.W3)) + tf.summary.scalar('loss', loss) + return loss + + def add_training_op(self, loss): + extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) + with tf.control_dependencies(extra_update_ops): + learning_rate = tf.train.exponential_decay(Config.lr, self.global_step, 1000, 0.8, staircase=True) + train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=self.global_step) + return train_op + + def train_on_batch(self, sess, inputs_batch, labels_batch, merged, train_writer, i): + feed = self.create_feed_dict(inputs_batch, labels_batch, self.config.dropout, True) + rs, _, loss = sess.run([merged, self.train_op, self.loss], feed_dict=feed) + train_writer.add_summary(rs, i) + return loss + + def __init__(self, config): + self.config = config + self.build() + + def fit(self, sess, train_x, train_y): + loss = self.train_on_batch(sess, train_x, train_y) + + def build(self): + with tf.name_scope('inputs'): + self.add_placeholders() + with tf.name_scope('predict'): + self.pred = self.add_prediction_op() + with tf.name_scope('loss'): + self.loss = self.add_loss_op(self.pred) + with tf.name_scope('train'): + self.train_op = self.add_training_op(self.loss) + with tf.name_scope('accuracy'): + self.acc_op = self.add_acc_op(self.pred) + self.argmax_op = self.add_argmax_ops(self.pred) + + def add_acc_op(self, pred): + correct_prediction = tf.cast(tf.equal(tf.argmax(pred, 1), tf.argmax(self.labels_placeholder, 1)), tf.float32) + accuracy = tf.reduce_mean(correct_prediction) + tf.summary.scalar('accuracy', accuracy) + return accuracy + + def accuracy(self, sess, inputs_batch, labels_batch, name, merged, writer, i): + feed = self.create_feed_dict(inputs_batch, labels_batch, 1, False) + self.class_accuracy(sess, feed) + rs, acc = sess.run([merged, self.acc_op], feed_dict=feed) + writer.add_summary(rs, i) + return acc + + def add_argmax_ops(self, pred): + op = [] + pred_op = tf.argmax(pred, 1) + op.append(pred_op) + label_op = tf.argmax(self.labels_placeholder, 1) + op.append(label_op) + return op + + def class_accuracy(self, sess, feed): + y_pre_argmax, v_y_argmax = sess.run(self.argmax_op, feed_dict=feed) + for i in range(Config.n_classes): + y_pre_mask = y_pre_argmax != i + y_pre_class = np.ma.masked_array(y_pre_argmax, y_pre_mask) + v_y_class = np.ma.masked_array(v_y_argmax, y_pre_mask) + correct_prediction = np.equal(y_pre_class, v_y_class) + acc = np.mean(correct_prediction) + print('class:', i, 'acc:', acc, end=' ') + print('') + + +def main(): + PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) + train_path = os.path.join(PROJECT_ROOT, 'data/train2.data') + test_path = os.path.join(PROJECT_ROOT, 'data/test2.data') + train_log_path = os.path.join(PROJECT_ROOT, 'log/train/') + test_log_path = os.path.join(PROJECT_ROOT, 'log/test/') + dev_log_path = os.path.join(PROJECT_ROOT, 'log/dev/') + X, y = ld.load(train_path) + X = X.reshape([-1, 6, 6, 1]) + train_num = int(X.shape[0] * Config.trainrate) + X_train = X[:train_num] + y_train = y[:train_num] + X_dev = X[train_num:-1] + y_dev = y[train_num:-1] + X_test, y_test = ld.load(test_path) + X_test = X_test.reshape([-1, 6, 6, 1]) + print('train size :', X_train.shape, y_train.shape) + print('dev size :', X_dev.shape, y_dev.shape) + print('test size :', X_test.shape, y_test.shape) + print('start training') + with tf.Graph().as_default(): + config = Config() + nn = NN(config) + init = tf.global_variables_initializer() + saver = tf.train.Saver(max_to_keep=10, keep_checkpoint_every_n_hours=0.5) + y_train = tf.one_hot(y_train, depth=Config.n_classes) + y_test = tf.one_hot(y_test, depth=Config.n_classes) + y_dev = tf.one_hot(y_dev, depth=Config.n_classes) + shuffle_batch_x, shuffle_batch_y = tf.train.shuffle_batch([X_train, y_train], batch_size=Config.batch_size, capacity=10000, min_after_dequeue=5000, enqueue_many=True) + with tf.Session() as session: + merged = tf.summary.merge_all() + train_writer = tf.summary.FileWriter(train_log_path, session.graph) + test_writer = tf.summary.FileWriter(test_log_path) + dev_writer = tf.summary.FileWriter(dev_log_path) + session.run(init) + coord = tf.train.Coordinator() + threads = tf.train.start_queue_runners(session, coord) + y_test, y_dev = session.run([y_test, y_dev]) + i = 0 + try: + while not coord.should_stop(): + batch_x, batch_y = session.run([shuffle_batch_x, shuffle_batch_y]) + loss = nn.train_on_batch(session, batch_x, batch_y, merged, train_writer, i) + i += 1 + if i % 1000 == 0: + dev_acc = nn.accuracy(session, X_dev, y_dev, 'dev', merged, dev_writer, i) + test_acc = nn.accuracy(session, X_test, y_test, 'test', merged, test_writer, i) + print('step:', i, 'loss:', loss, 'dev_acc:', dev_acc, 'test_acc:', test_acc) + saver.save(session, os.path.join(PROJECT_ROOT, 'model/model_ckpt'), global_step=i) + except tf.errors.OutOfRangeError: + print('done') + finally: + coord.request_stop() + coord.join(threads) + + +if __name__ == '__main__': + main() diff --git a/dataset/preprocessed/730.txt b/dataset/preprocessed/730.txt new file mode 100644 index 0000000..df4e762 --- /dev/null +++ b/dataset/preprocessed/730.txt @@ -0,0 +1,103 @@ +import tensorflow as tf +import numpy as np +import Utility as util +import math + + +class SoftMax(object): + + def __init__(self, d, k): + """ + :param d: dimensionality + :param k: number of classes + """ + self.D = d + self.K = k + self.NR_VALIDATION_DATA = 50 + self.NR_ITERATION = 1000 + self.SHOW_ACC = 100 + self.BATCH_SIZE = 5000 + self.TRAIN_STEP = 0.01 + + def training(self, features, labels): + """ + Training Neural Network + :param features: training data [50000 x 3072] + :param labels: labels for X [50000 x 1] + :return: return a dictionary which contains all learned parameter + """ + features = self.__preprocessing(features) + train_features = features[self.NR_VALIDATION_DATA:] + train_labels = labels[self.NR_VALIDATION_DATA:] + validation_features = features[0:self.NR_VALIDATION_DATA] + validation_labels = labels[0:self.NR_VALIDATION_DATA] + sess = tf.InteractiveSession() + x = tf.placeholder(tf.float32, shape=[None, self.D]) + y_ = tf.placeholder(tf.int64, shape=[None]) + W = tf.Variable(np.random.randn(self.D, self.K) * math.sqrt(2.0 / self.D * self.K), dtype=tf.float32) + b = tf.Variable(tf.zeros([self.K]), dtype=tf.float32) + y = tf.nn.softmax(tf.matmul(x, W) + b) + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_) + cross_entropy_mean = tf.reduce_mean(cross_entropy) + train_step = tf.train.AdamOptimizer(self.TRAIN_STEP).minimize(cross_entropy_mean) + correct_prediction = tf.equal(tf.argmax(y, 1), y_) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + sess.run(tf.initialize_all_variables()) + for i in range(self.NR_ITERATION): + batch = util.generate_batch(train_features, train_labels, self.BATCH_SIZE) + train_step.run(feed_dict={x: batch[0], y_: batch[1]}) + if i % self.SHOW_ACC == 0: + value = accuracy.eval(feed_dict={x: validation_features, y_: validation_labels}) + print('Step - ', i, ' - Acc : ', value) + W_final = W.eval() + b_final = b.eval() + sess.close() + return {'W': W_final, 'b': b_final} + + def predict(self, test_features, test_labels, nn): + """ + Predict data + :param test_features: testing data + :param test_labels: labels for X + :param nn: it is a dictionary which contains a Neural Network + :return: return the predicted labels + """ + test_features = self.__preprocessing(test_features) + x = tf.placeholder(tf.float32, shape=[None, self.D]) + W = tf.placeholder(tf.float32, shape=[self.D, self.K]) + b = tf.placeholder(tf.float32, shape=[self.K]) + y = tf.nn.softmax(tf.matmul(x, W) + b) + sess = tf.InteractiveSession() + predicted_labels = sess.run(y, feed_dict={x: test_features, W: nn['W'], b: nn['b']}) + sess.close() + predicted_labels = np.argmax(predicted_labels, axis=1) + acc = np.mean(predicted_labels == test_labels) + print('The final accuracy is : ', acc) + return predicted_labels + + def __preprocessing(self, X): + """ + Preprocessing the X data by zero-centered and normalized them. + :param X: the data. + :return: return the new zero-centered and normalized data. + """ + X = X.astype(np.float64) + X = X.T - np.array(np.mean(X, axis=1, dtype=np.float64)) + X = X / np.std(X.T, axis=1, dtype=np.float64) + X = X.T + return X + + +if __name__ == '__main__': + learn_data = 'result/SoftMax2/cifar_10' + D = 3072 + K = 10 + softMax = SoftMax(D, K) + X, y, X_test, y_test = util.load_CIFAR10('data/') + if util.file_exist(learn_data): + nn_parameter = util.unpickle(learn_data) + else: + nn_parameter = softMax.training(X, y) + util.pickle_nn(learn_data, nn_parameter) + predicted_labels = softMax.predict(X_test, y_test, nn_parameter) + util.save_predicted_labels('result/SoftMax2/submission.csv', predicted_labels) diff --git a/dataset/preprocessed/732.txt b/dataset/preprocessed/732.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/732.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/733.txt b/dataset/preprocessed/733.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/733.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/734.txt b/dataset/preprocessed/734.txt new file mode 100644 index 0000000..810c3f4 --- /dev/null +++ b/dataset/preprocessed/734.txt @@ -0,0 +1,77 @@ +""" +采用tensorflow实现 policy value net +""" +import numpy as np +import tensorflow as tf + + +class PolicyValueNet: + + def __init__(self, board_width, board_height, model_file=None): + self.board_width = board_width + self.board_height = board_height + self.input_states = tf.placeholder(tf.float32, shape=[None, 4, self.board_width, self.board_height]) + self.input_state = tf.transpose(self.input_states, [0, 2, 3, 1]) + self.conv1 = tf.layers.conv2d(inputs=self.input_state, filters=32, kernel_size=[3, 3], padding='same', activation=tf.nn.relu) + self.conv2 = tf.layers.conv2d(inputs=self.conv1, filters=64, kernel_size=[3, 3], padding='same', activation=tf.nn.relu) + self.conv3 = tf.layers.conv2d(inputs=self.conv2, filters=128, kernel_size=[3, 3], padding='same', activation=tf.nn.relu) + self.action_conv = tf.layers.conv2d(inputs=self.conv3, filters=4, kernel_size=[1, 1], padding='same', activation=tf.nn.relu) + self.action_conv_flat = tf.reshape(self.action_conv, [-1, 4 * self.board_width * self.board_height]) + self.action_fc = tf.layers.dense(inputs=self.action_conv_flat, units=self.board_height * self.board_width, activation=tf.nn.log_softmax) + self.evaluation_conv = tf.layers.conv2d(inputs=self.conv3, filters=2, kernel_size=[1, 1], padding='same', activation=tf.nn.relu) + self.evaluation_conv_flat = tf.reshape(self.evaluation_conv, [-1, 2 * self.board_width * self.board_height]) + self.evaluation_fc1 = tf.layers.dense(inputs=self.evaluation_conv_flat, units=self.board_height * self.board_width, activation=tf.nn.relu) + self.evaluation_fc2 = tf.layers.dense(inputs=self.evaluation_fc1, units=1, activation=tf.nn.tanh) + self.labels = tf.placeholder(tf.float32, shape=[None, 1]) + self.value_loss = tf.losses.mean_squared_error(self.labels, self.evaluation_fc2) + self.mcts_probs = tf.placeholder(tf.float32, shape=[None, self.board_width * self.board_height]) + self.policy_loss = tf.losses.softmax_cross_entropy(self.mcts_probs, self.action_fc) + l2_penalty_beta = 0.0001 + vars = tf.trainable_variables() + self.l2_penalty = l2_penalty_beta * tf.add_n([tf.nn.l2_loss(v) for v in vars if 'bias' not in v.name.lower()]) + self.loss = self.value_loss + self.policy_loss + self.l2_penalty + self.learning_rate = tf.placeholder(tf.float32) + self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss) + self.session = tf.Session() + self.entropy = tf.losses.softmax_cross_entropy(tf.exp(self.action_fc), self.action_fc) + init = tf.global_variables_initializer() + self.session.run(init) + self.saver = tf.train.Saver() + if model_file is not None: + self.restore_model(model_file) + print('restore model_file:', model_file) + + def policy_value(self, state_batch): + """ + 计算动作概率和当前的value + 输入当前的状态 + 输出动作的softmax概率和value + :return: + """ + log_act_probs, value = self.session.run([self.action_fc, self.evaluation_fc2], feed_dict={self.input_states: state_batch}) + act_probs = np.exp(log_act_probs) + return act_probs, value + + def policy_value_fn(self, board): + """ + 输入当前的board + 输出 action-probability 对及当前board状态的value + :param board: + :return: + """ + legal_positions = board.availables + current_state = np.ascontiguousarray(board.current_state().reshape(-1, 4, self.board_width, self.board_height)) + act_probs, value = self.policy_value(current_state) + act_probs = zip(legal_positions, act_probs[0][legal_positions]) + return act_probs, value + + def train_step(self, state_batch, mcts_probs, winner_batch, learning_rate): + winner_batch = np.reshape(winner_batch, (-1, 1)) + loss, entropy, _ = self.session.run([self.loss, self.entropy, self.optimizer], feed_dict={self.input_states: state_batch, self.mcts_probs: mcts_probs, self.labels: winner_batch, self.learning_rate: learning_rate}) + return loss, entropy + + def save_model(self, model_path): + self.saver.save(self.session, model_path) + + def restore_model(self, model_path): + self.saver.restore(self.session, model_path) diff --git a/dataset/preprocessed/735.txt b/dataset/preprocessed/735.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/735.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/736.txt b/dataset/preprocessed/736.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/736.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/737.txt b/dataset/preprocessed/737.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/737.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/738.txt b/dataset/preprocessed/738.txt new file mode 100644 index 0000000..d0e2f2f --- /dev/null +++ b/dataset/preprocessed/738.txt @@ -0,0 +1,379 @@ +from helpers.custom_rnn_cells import PredictionLSTMStateTuple, GridPredictionLSTMCell, BasicPredictionLSTMCell, _linear, DiscountRNNCell +from helpers.layer_helpers import normalized_columns_initializer, linear, conv2d, flatten, categorical_sample +from helpers.rnn import old_dynamic_rnn +from tensorflow.python.ops.rnn_cell_impl import _RNNCell as RNNCell +from tensorflow.python.ops.math_ops import tanh, sigmoid +import tensorflow as tf +import numpy as np +from tensorflow.python.ops import variable_scope as vs + + +class GridQAnswerNet: + + def __init__(self, ob_space, ac_space, replay_size=2000, grid_size=20): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.bs = tf.placeholder(dtype=tf.int32) + self.replay_memory = [] + self.replay_size = replay_size + self.grid_size = grid_size + self.prob = 1.0 + self.final_prob = 0.1 + self.anneal_rate = 1.8e-07 + self.num_actions = ac_space + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + x = tf.concat(axis=1, values=[x, self.action, self.reward]) + x = tf.expand_dims(x, [0]) + size = 256 + lstm = GridPredictionLSTMCell(size, state_is_tuple=True, ac_space=ac_space, grid_size=20) + self.state_size = lstm.state_size + step_size = tf.shape(self.x)[:1] + c_init = np.zeros((1, lstm.state_size.c), np.float32) + h_init = np.zeros((1, lstm.state_size.h), np.float32) + pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + self.state_init = [c_init, h_init, pred_init] + c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + pred_in = tf.placeholder(tf.float32, [1, lstm.state_size.pred]) + self.state_in = [c_in, h_in, pred_in] + state_in = PredictionLSTMStateTuple(c_in, h_in, pred_in) + lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm, x, initial_state=state_in, sequence_length=step_size, time_major=False) + lstm_c, lstm_h, lstm_pred = lstm_state + x = tf.reshape(lstm_outputs, [-1, size]) + self.Q = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + self.vf = tf.reduce_max(self.Q, axis=[1]) + self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + self.predictions = tf.reshape(lstm_pred, shape=[-1, grid_size, grid_size, ac_space]) + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + self.target_weights = [] + + def get_initial_features(self): + return self.state_init + + def act(self, ob, prev_a, prev_r, c, h, pred): + sess = tf.get_default_session() + self.update_exploration() + fetched = sess.run([self.Q, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred}) + qvals = fetched[0] + if np.random.uniform > self.prob: + action = np.argmax(qvals) + else: + action = np.random.choice(range(self.num_actions)) + return action, fetched[1], fetched[2:] + + def value(self, ob, prev_a, prev_r, c, h, pred): + sess = tf.get_default_session() + return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred})[0] + + def update_replay_memory(self, tuple): + self.replay_memory.append(tuple) + if len(self.replay_memory) > self.replay_size: + self.replay_memory.pop(0) + + def update_exploration(self): + if self.prob > self.final_prob: + self.prob -= self.anneal_rate + + def update_target_weights(self): + sess = tf.get_default_session() + self.target_weights = sess.run(self.var_list) + + +class GridQQuestionNet: + + def __init__(self, ob_space, ac_space, grid_size=20): + self.pixel_changes = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.bs = tf.placeholder(tf.int32) + x = flatten(self.obs) + x = tf.concat(axis=1, values=[x, self.acs, self.rewards]) + rnn = tf.nn.rnn_cell.BasicRNNCell(num_units=100) + self.state_size = rnn.state_size + step_size = tf.shape(self.obs)[:1] + self.pred_init = np.zeros((1, rnn.state_size), np.float32) + pred_in = tf.placeholder(tf.float32, [1, rnn.state_size]) + outputs, state = tf.nn.dynamic_rnn(rnn, x, initial_state=pred_in, sequence_length=step_size, time_major=False) + self.pred_targets = tf.reshape(state, shape=[-1, grid_size * grid_size * ac_space]) + + def get_initial_features(self): + return self.pred_init + + +class GridQTDNet: + + def __init__(self, ob_space, ac_space, grid_size=20): + self.qnet = GridQQuestionNet(ob_space=ob_space, ac_space=ac_space, grid_size=grid_size) + self.anet = GridQAnswerNet(ob_space=ob_space, ac_space=ac_space, grid_size=grid_size) + pass + + +class BasicQAnswerNet: + + def __init__(self, ob_space, ac_space, replay_size=2000, num_predictions=100): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.bs = tf.placeholder(dtype=tf.int32) + self.replay_memory = [] + self.replay_size = replay_size + self.prob = 1.0 + self.final_prob = 0.1 + self.anneal_rate = 1.8e-07 + self.num_actions = ac_space + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + x = tf.concat(axis=1, values=[x, self.action, self.reward]) + x = tf.expand_dims(x, [0]) + size = 256 + lstm = BasicPredictionLSTMCell(size, state_is_tuple=True, num_pred=num_predictions) + self.state_size = lstm.state_size + step_size = tf.shape(self.x)[:1] + c_init = np.zeros((1, lstm.state_size.c), np.float32) + h_init = np.zeros((1, lstm.state_size.h), np.float32) + pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + self.state_init = [c_init, h_init, pred_init] + c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + pred_in = tf.placeholder(tf.float32, [1, lstm.state_size.pred]) + self.state_in = [c_in, h_in, pred_in] + state_in = PredictionLSTMStateTuple(c_in, h_in, pred_in) + lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm, x, initial_state=state_in, sequence_length=step_size, time_major=False) + lstm_c, lstm_h, lstm_pred = lstm_state + x = tf.reshape(lstm_outputs, [-1, size]) + self.Q = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + self.vf = tf.reduce_max(self.Q, axis=[1]) + self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + self.predictions = lstm_pred + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + self.target_weights = [] + + def get_initial_features(self): + return self.state_init + + def act(self, ob, prev_a, prev_r, c, h, pred): + sess = tf.get_default_session() + self.update_exploration() + fetched = sess.run([self.Q, self.vf] + self.state_out, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred}) + qvals = fetched[0] + if np.random.uniform > self.prob: + action = np.argmax(qvals) + else: + action = np.random.choice(range(self.num_actions)) + return action, fetched[1], fetched[2:] + + def value(self, ob, prev_a, prev_r, c, h, pred): + sess = tf.get_default_session() + return sess.run(self.vf, {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.state_in[0]: c, self.state_in[1]: h, self.state_in[2]: pred})[0] + + def update_replay_memory(self, tuple): + self.replay_memory.append(tuple) + if len(self.replay_memory) > self.replay_size: + self.replay_memory.pop(0) + + def update_exploration(self): + if self.prob > self.final_prob: + self.prob -= self.anneal_rate + + def update_target_weights(self): + sess = tf.get_default_session() + self.target_weights = sess.run(self.var_list) + + +class BasicQQuestionNet: + + def __init__(self, ob_space, ac_space, replay_size=2000, num_predictions=100): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + x = tf.reverse(x, axis=[0]) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.bs = tf.placeholder(dtype=tf.int32) + self.replay_memory = [] + self.replay_size = replay_size + self.num_actions = ac_space + x = flatten(x) + x = tf.concat(axis=1, values=[x, tf.reverse(self.action, axis=[0]), tf.reverse(self.reward, axis=[0])]) + x = tf.expand_dims(x, [0]) + size = num_predictions + rnn = BasicRNNCell(num_units=size, activation=sigmoid) + self.pred_init = np.zeros((1, size), np.float32) + self.pred_in = tf.placeholder(tf.float32, [1, size]) + step_size = tf.shape(self.x)[:1] + rnn_output, rnn_state = tf.nn.dynamic_rnn(rnn, x, initial_state=self.pred_in, sequence_length=step_size, time_major=False) + self.prediction_targs = tf.reshape(rnn_output, shape=[-1, size]) + self.final_prediction_targs = tf.slice(self.prediction_targs, begin=[-1, 0], size=[1, -1]) + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + + +class BasicQTDNet: + + def __init__(self, ob_space, ac_space, replay_size=2000, num_predictions=100): + self.num_predictions = num_predictions + with tf.variable_scope('Question'): + self.qnet = BasicQQuestionNet(ob_space=ob_space, ac_space=ac_space) + with tf.variable_scope('Answer'): + self.anet = BasicQAnswerNet(ob_space=ob_space, ac_space=ac_space) + with tf.variable_scope('Question', reuse=True): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.bs = tf.placeholder(dtype=tf.int32) + self.num_actions = ac_space + x = tf.reverse(x, axis=[0]) + rev_action = tf.reverse(self.action, axis=[0]) + rev_reward = tf.reverse(self.reward, axis=[0]) + x = flatten(x) + x = tf.concat(axis=1, values=[x, rev_action, rev_reward]) + x = tf.expand_dims(x, [0]) + size = 100 + rnn = BasicRNNCell(num_units=size) + self.pred_init = np.zeros((1, size), np.float32) + self.pred_in = tf.placeholder(tf.float32, [1, size]) + step_size = tf.shape(self.x)[:1] + rnn_output, rnn_state = tf.nn.dynamic_rnn(rnn, x, initial_state=self.pred_in, sequence_length=step_size, time_major=False) + self.prediction_targs = tf.reshape(rnn_output, shape=[-1, size]) + self.final_prediction_targs = tf.slice(self.prediction_targs, begin=[tf.shape(self.prediction_targs)[0] - 1, 0], size=[1, -1]) + with tf.variable_scope('Answer', reuse=True): + x = self.x + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + x = conv_features = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + x = tf.concat(axis=1, values=[x, self.action, self.reward]) + x = tf.expand_dims(x, [0]) + size = 256 + lstm = BasicPredictionLSTMCell(num_units=256, state_is_tuple=True, num_pred=100) + self.state_size = lstm.state_size + step_size = tf.shape(self.x)[:1] + c_init = np.zeros((1, lstm.state_size.c), np.float32) + h_init = np.zeros((1, lstm.state_size.h), np.float32) + pred_init = np.zeros((1, lstm.state_size.pred), np.float32) + self.state_init = [c_init, h_init, pred_init] + c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c]) + h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h]) + self.state_in = [c_in, h_in, self.final_prediction_targs] + state_in = PredictionLSTMStateTuple(c_in, h_in, self.final_prediction_targs) + lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm, x, initial_state=state_in, sequence_length=step_size, time_major=False) + lstm_c, lstm_h, lstm_pred = lstm_state + x = tf.reshape(lstm_outputs, [-1, size]) + self.Q = linear(x, ac_space, 'action', normalized_columns_initializer(0.01)) + self.vf = tf.reduce_max(self.Q, axis=[1]) + self.state_out = [lstm_c[:1, :], lstm_h[:1, :], lstm_pred[:1, :]] + self.predictions = lstm_pred + self.target_weights = [] + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + + +class BasicRNNCell(RNNCell): + """The most basic RNN cell.""" + + def __init__(self, num_units, input_size=None, activation=tanh): + self._num_units = num_units + self._activation = activation + + @property + def state_size(self): + return self._num_units + + @property + def output_size(self): + return self._num_units + + def __call__(self, inputs, state, scope=None): + """Most basic RNN: output = new_state = act(W * input + U * state + B).""" + with vs.variable_scope(scope or 'basic_rnn_cell'): + output = self._activation(_linear([inputs, state], self._num_units, True, scope=scope)) + return output, output + + +class ConvTDNet: + + def __init__(self, ob_space, ac_space, future_steps=5, pred_per_step=10, ckpt=None): + self.ckpt_file = ckpt + self.replay_memory = [] + self.future_steps = future_steps + self.bs = tf.placeholder(dtype=tf.int32) + self.num_actions = ac_space + self.pred_per_step = pred_per_step + self.num_pred = pred_per_step * future_steps + self.use_target = tf.placeholder(dtype=tf.bool) + self.default_last_prediction = tf.zeros(shape=[self.bs, self.num_pred]) + self.default_target_obs = tf.zeros(shape=[self.bs] + [self.future_steps] + list(ob_space)) + with tf.variable_scope('Question'): + self.target_obs = tf.placeholder(tf.float32, [None] + [self.future_steps] + list(ob_space)) + feature_list = [] + for i in range(self.future_steps): + obs = tf.squeeze(tf.slice(self.target_obs, begin=[0, i, 0, 0, 0], size=[-1, 1, -1, -1, -1]), axis=[1]) + z = tf.nn.relu(conv2d(obs, 16, 'ql1' + str(i), [8, 8], [4, 4])) + z = tf.nn.relu(conv2d(z, 32, 'ql2' + str(i), [4, 4], [2, 2])) + z = flatten(z) + z = tf.nn.relu(linear(z, self.pred_per_step, 'ql3' + str(i), normalized_columns_initializer(0.1))) + feature_list.append(z) + self.target_predictions = tf.concat(feature_list, axis=1) + with tf.variable_scope('Main'): + self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) + self.action = tf.placeholder(tf.float32, [None, ac_space]) + self.reward = tf.placeholder(tf.float32, [None, 1]) + self.last_prediction = tf.placeholder(tf.float32, [None, self.num_pred]) + x = tf.nn.relu(conv2d(x, 16, 'l1', [8, 8], [4, 4])) + x = tf.nn.relu(conv2d(x, 32, 'l2', [4, 4], [2, 2])) + x = flatten(x) + x = tf.nn.relu(linear(x, 256, 'l3', normalized_columns_initializer(0.1))) + p = tf.nn.l2_normalize(self.last_prediction, dim=1) + p = tf.nn.tanh(linear(p, 256, 'encode_pred', normalized_columns_initializer(0.1))) + xmain = tf.concat(axis=1, values=[x, self.action, self.reward]) + xaux = tf.concat(axis=1, values=[x, self.action, self.reward, p]) + xmain = tf.nn.relu(linear(xmain, 256, 'l4', normalized_columns_initializer(0.1))) + y = tf.nn.relu(linear(xaux, 256, 'auxbranch_l1', normalized_columns_initializer(0.1))) + self.approx_predictions = linear(y, self.num_pred, 'auxbranch_l2', normalized_columns_initializer(0.1)) + self.predictions = tf.where(self.use_target, self.target_predictions, self.approx_predictions) + x = tf.concat(axis=1, values=[xmain, self.predictions]) + val = linear(x, 1, 'value', normalized_columns_initializer(0.01)) + self.val = tf.reshape(val, shape=[-1]) + self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) + self.target_weights = [] + + def get_initial_features(self): + return np.zeros((1, self.num_pred), np.float32) + + def restore(self, sess, saver): + if self.ckpt_file is not None: + saver.restore(sess, self.ckpt_file) + self.target_weights = sess.run(self.var_list) + + def targ_value(self, ob, prev_a, prev_r, target_obs, current_session=None): + if current_session is not None: + sess = current_session + else: + sess = tf.get_default_session() + reward_value, prediction_values = sess.run((self.val, self.predictions), {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.bs: 1, self.target_obs: target_obs, self.use_target: True}) + return reward_value, prediction_values + + def approx_value(self, ob, prev_a, prev_r, last_pred, current_session=None): + if current_session is not None: + sess = current_session + else: + sess = tf.get_default_session() + reward_value, prediction_values = sess.run((self.val, self.predictions), {self.x: [ob], self.action: [prev_a], self.reward: [[prev_r]], self.bs: 1, self.last_prediction: last_pred, self.use_target: False}) + return reward_value, prediction_values + + def update_target_weights(self, current_session=None): + if current_session is not None: + sess = current_session + else: + sess = tf.get_default_session() + self.target_weights = sess.run(self.var_list) + + def value_with_target(self, ob, prev_a, prev_r, pred, current_session=None): + if current_session is not None: + sess = current_session + else: + sess = tf.get_default_session() + feed_dict = {self.x: ob, self.action: prev_a, self.reward: np.expand_dims(prev_r, axis=1), self.bs: np.shape(ob)[0], self.pred: pred} + feed_dict.update(zip(self.var_list, self.target_weights)) + reward_value, prediction_values = sess.run((self.val, self.predictions), feed_dict=feed_dict) + return reward_value, prediction_values diff --git a/dataset/preprocessed/739.txt b/dataset/preprocessed/739.txt new file mode 100644 index 0000000..de3f174 --- /dev/null +++ b/dataset/preprocessed/739.txt @@ -0,0 +1,95 @@ +import sys +import tensorflow as tf + + +class Model: + + def __init__(self, args, num_words, num_chars, num_tags): + graph = tf.Graph() + self.session = tf.Session(graph=graph, config=tf.ConfigProto(inter_op_parallelism_threads=args.threads, intra_op_parallelism_threads=args.threads)) + with self.session.graph.as_default(): + self.sentence_lens = tf.placeholder(tf.int32, [None], name='sentence_lens') + self.word_ids = tf.placeholder(tf.int32, [None, None], name='word_ids') + self.charseqs = tf.placeholder(tf.int32, [None, None], name='charseqs') + self.charseq_lens = tf.placeholder(tf.int32, [None], name='charseq_lens') + self.charseq_ids = tf.placeholder(tf.int32, [None, None], name='charseq_ids') + self.tags = tf.placeholder(tf.int32, [None, None], name='tags') + self.is_training = tf.placeholder(tf.bool, [], name='is_training') + self.learning_rate = tf.placeholder_with_default(0.01, None) + output_layer, self.predictions = self.build_model(args, num_words, num_chars, num_tags) + weights = tf.sequence_mask(self.sentence_lens, dtype=tf.float32) + loss = tf.losses.sparse_softmax_cross_entropy(self.tags, output_layer, weights) + global_step = tf.train.create_global_step() + self.training = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss, global_step=global_step, name='training') + self.current_accuracy, self.update_accuracy = tf.metrics.accuracy(self.tags, self.predictions, weights=weights) + self.current_loss, self.update_loss = tf.metrics.mean(loss, weights=tf.reduce_sum(weights)) + self.reset_metrics = tf.variables_initializer(tf.get_collection(tf.GraphKeys.METRIC_VARIABLES)) + summary_writer = tf.contrib.summary.create_file_writer(args.logdir, flush_millis=10 * 1000) + self.summaries = {} + with summary_writer.as_default(), tf.contrib.summary.record_summaries_every_n_global_steps(10): + self.summaries['train'] = [tf.contrib.summary.scalar('train/loss', self.update_loss), tf.contrib.summary.scalar('train/lr', self.learning_rate), tf.contrib.summary.scalar('train/accuracy', self.update_accuracy)] + with summary_writer.as_default(), tf.contrib.summary.always_record_summaries(): + for dataset in ['dev', 'test']: + self.summaries[dataset] = [tf.contrib.summary.scalar(dataset + '/loss', self.current_loss), tf.contrib.summary.scalar(dataset + '/accuracy', self.current_accuracy)] + self.session.run(tf.global_variables_initializer()) + with summary_writer.as_default(): + tf.contrib.summary.initialize(session=self.session, graph=self.session.graph) + self.saver = tf.train.Saver() + + def build_model(self, args, num_words, num_chars, num_tags): + raise NotImplemented + + def train_epoch(self, train, batch_size, lr): + while not train.epoch_finished(): + sentence_lens, word_ids, charseq_ids, charseqs, charseq_lens = train.next_batch(batch_size, including_charseqs=True) + self.session.run(self.reset_metrics) + self.session.run([self.training, self.summaries['train']], {self.sentence_lens: sentence_lens, self.charseqs: charseqs[train.FORMS], self.charseq_lens: charseq_lens[train.FORMS], self.word_ids: word_ids[train.FORMS], self.charseq_ids: charseq_ids[train.FORMS], self.tags: word_ids[train.TAGS], self.learning_rate: lr, self.is_training: True}) + + def evaluate(self, dataset_name, dataset, batch_size): + self.session.run(self.reset_metrics) + while not dataset.epoch_finished(): + sentence_lens, word_ids, charseq_ids, charseqs, charseq_lens = dataset.next_batch(batch_size, including_charseqs=True) + self.session.run([self.update_accuracy, self.update_loss], {self.sentence_lens: sentence_lens, self.charseqs: charseqs[dataset.FORMS], self.charseq_lens: charseq_lens[dataset.FORMS], self.word_ids: word_ids[dataset.FORMS], self.charseq_ids: charseq_ids[dataset.FORMS], self.tags: word_ids[dataset.TAGS], self.is_training: False}) + acc, loss, _ = self.session.run([self.current_accuracy, self.current_loss, self.summaries[dataset_name]]) + return acc, loss + + def predict(self, dataset, batch_size): + tags = [] + while not dataset.epoch_finished(): + sentence_lens, word_ids, charseq_ids, charseqs, charseq_lens = dataset.next_batch(batch_size, including_charseqs=True) + tags.extend(self.session.run(self.predictions, {self.sentence_lens: sentence_lens, self.charseqs: charseqs[dataset.FORMS], self.charseq_lens: charseq_lens[dataset.FORMS], self.word_ids: word_ids[dataset.FORMS], self.charseq_ids: charseq_ids[dataset.FORMS], self.is_training: False})) + return tags + + def save(self, path): + self.saver.save(self.session, path) + + def restore(self, path): + self.saver.restore(self.session, path) + + +class CLE(Model): + + def build_model(self, args, num_words, num_chars, num_tags): + rnn_cell_types = {'LSTM': tf.nn.rnn_cell.BasicLSTMCell, 'GRU': tf.nn.rnn_cell.GRUCell} + fw_rnn_cell = rnn_cell_types[args.rnn_cell](args.rnn_cell_dim) + bw_rnn_cell = rnn_cell_types[args.rnn_cell](args.rnn_cell_dim) + word_embeddings = tf.get_variable('word_embeddings', [num_words, args.we_dim], dtype=tf.float32) + word_embeddings = tf.layers.dropout(word_embeddings, rate=args.we_dropout, noise_shape=[num_words, 1], training=self.is_training) + word_embedded = tf.nn.embedding_lookup(word_embeddings, self.word_ids) + char_embeddings = tf.get_variable('char_embeddings', [num_chars, args.cle_dim], dtype=tf.float32) + char_embeddings = tf.layers.dropout(char_embeddings, rate=args.cle_dropout, noise_shape=[num_chars, 1], training=self.is_training) + char_embedded = tf.nn.embedding_lookup(char_embeddings, self.charseqs) + _, char_rnn_final_outputs = tf.nn.bidirectional_dynamic_rnn(tf.nn.rnn_cell.GRUCell(args.cle_dim), tf.nn.rnn_cell.GRUCell(args.cle_dim), char_embedded, sequence_length=self.charseq_lens, dtype=tf.float32, scope='char_rnn') + char_rnn = char_rnn_final_outputs[0] + char_rnn_final_outputs[1] + cle = tf.nn.embedding_lookup(char_rnn, self.charseq_ids) + embedded = tf.concat([word_embedded, cle], axis=2) + rnn_outputs, _ = tf.nn.bidirectional_dynamic_rnn(fw_rnn_cell, bw_rnn_cell, embedded, dtype=tf.float32, sequence_length=self.sentence_lens, scope='mixed_rnn') + rnn_output = tf.concat(rnn_outputs, axis=2) + rnn_output = tf.layers.dropout(rnn_output, rate=args.dropout, training=self.is_training) + output_layer = tf.layers.dense(rnn_output, num_tags) + predictions = tf.argmax(output_layer, axis=2) + return output_layer, predictions + + +def get_model(name): + return getattr(sys.modules[__name__], name) diff --git a/dataset/preprocessed/74.txt b/dataset/preprocessed/74.txt new file mode 100644 index 0000000..0427d99 --- /dev/null +++ b/dataset/preprocessed/74.txt @@ -0,0 +1,178 @@ +import numpy as np +import os +import matplotlib.pyplot as plt +import tqdm +import tensorflow as tf +from six.moves import cPickle as pickle +import random +import math +pickle_dir = '.' +with open(os.path.join(pickle_dir, 'notMNIST.pickle'), 'rb') as f: + letter_set = pickle.load(f) +train_dataset = letter_set['train_dataset'] +train_labels = letter_set['train_labels'] +valid_dataset = letter_set['valid_dataset'] +valid_labels = letter_set['valid_labels'] +test_dataset = letter_set['test_dataset'] +test_labels = letter_set['test_labels'] +valid_dataset_sani = letter_set['valid_dataset_sani'] +valid_labels_sani = letter_set['valid_labels_sani'] +test_dataset_sani = letter_set['test_dataset_sani'] +test_labels_sani = letter_set['test_labels_sani'] +samples, width, height = train_dataset.shape +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, width * height)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +n_hidden = 1024 +beta_values = [0.0005, 0.1, 1e-05, 1e-06, 0.01] +num_steps = 3001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, width * height)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + minibatch_dataset = tf.placeholder(tf.float32, shape=(batch_size, width * height)) + minibatch_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + l2_regularization_beta = tf.placeholder(tf.float32) + weights = tf.Variable(tf.truncated_normal([n_hidden, num_labels], stddev=math.sqrt(2.0 / (width * height)))) + biases = tf.Variable(tf.zeros([num_labels])) + weights_hidden = tf.Variable(tf.truncated_normal([width * height, n_hidden], stddev=math.sqrt(2.0 / n_hidden))) + biases_hidden = tf.Variable(tf.zeros([n_hidden])) + hidden = tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden) + logits = tf.matmul(hidden, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = loss + l2_regularization_beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_hidden)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + hidden_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + valid_prediction = tf.nn.softmax(tf.matmul(hidden_valid, weights) + biases) + hidden_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + test_prediction = tf.nn.softmax(tf.matmul(hidden_test, weights) + biases) +betaplot = [] +accuracyplot = [] +for beta in beta_values: + with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in tqdm.trange(num_steps): + offset = random.randint(0, int((train_labels.shape[0] - batch_size) / batch_size)) + offset = offset * batch_size + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, l2_regularization_beta: beta} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + betaplot.append(beta) + accuracyplot.append(accuracy(valid_prediction.eval(), valid_labels)) + print('Beta: {}'.format(beta)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +plt.figure(1) +plt.semilogx(betaplot, accuracyplot, '+') +plt.xlabel('Beta') +plt.ylabel('Valid Accuracy') +plt.show() +num_steps = 300 +num_batches = 25 +fixed_offset = random.sample(range(int((train_labels.shape[0] - batch_size) / batch_size)), num_batches) +fixed_offset = [(i * batch_size) for i in fixed_offset] +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in range(num_steps): + offset = random.choice(fixed_offset) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, l2_regularization_beta: 0} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step == num_steps - 1: + for idx, offset in enumerate(fixed_offset): + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + predictions = session.run([train_prediction], feed_dict=feed_dict) + print('Minibatch %i accuracy: %.1f%%' % (idx, accuracy(predictions[0], batch_labels))) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 * 3 +n_hidden = 1024 * 2 +dropout_keep_probablity = 1 +l2_regularization_beta = 0.0005 +num_steps = 9001 +initial_learning_rate = 0.5 +decay_steps = 3500 +decay_size = 0.6 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, width * height)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + minibatch_dataset = tf.placeholder(tf.float32, shape=(batch_size, width * height)) + minibatch_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(initial_learning_rate, global_step, decay_steps, decay_size, staircase=True) + weights = tf.Variable(tf.truncated_normal([n_hidden, num_labels], stddev=math.sqrt(2.0 / (width * height)))) + biases = tf.Variable(tf.zeros([num_labels])) + weights_hidden = tf.Variable(tf.truncated_normal([width * height, n_hidden], stddev=math.sqrt(2.0 / n_hidden))) + biases_hidden = tf.Variable(tf.zeros([n_hidden])) + hidden = tf.nn.dropout(tf.nn.relu(tf.matmul(tf_train_dataset, weights_hidden) + biases_hidden), dropout_keep_probablity) + logits = tf.matmul(hidden, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) + loss = loss + l2_regularization_beta * (tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_hidden)) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + hidden_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, weights_hidden) + biases_hidden) + valid_prediction = tf.nn.softmax(tf.matmul(hidden_valid, weights) + biases) + hidden_test = tf.nn.relu(tf.matmul(tf_test_dataset, weights_hidden) + biases_hidden) + test_prediction = tf.nn.softmax(tf.matmul(hidden_test, weights) + biases) +losses = [] +plotsteps = [] +acc = [] +learning_rate_steps = [] +with tf.Session(graph=graph) as session: + tf.global_variables_initializer().run() + print('Initialized') + for step in tqdm.trange(num_steps): + offset = random.randint(0, int((train_labels.shape[0] - batch_size) / batch_size)) + offset = offset * batch_size + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 100 == 0: + acc.append(accuracy(valid_prediction.eval(), valid_labels)) + losses.append(l) + plotsteps.append(step) + learning_rate_steps.append(session.run(learning_rate)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +plotoffset = 10 +f, axarr = plt.subplots(3, sharex=True) +axarr[0].plot(plotsteps[plotoffset:], acc[plotoffset:]) +axarr[0].set_title('Accuracy [%]') +axarr[1].plot(plotsteps[plotoffset:], losses[plotoffset:]) +axarr[1].set_title('Loss') +axarr[2].plot(plotsteps[plotoffset:], learning_rate_steps[plotoffset:]) +axarr[2].set_title('Learning Rate') +plt.show() diff --git a/dataset/preprocessed/740.txt b/dataset/preprocessed/740.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/740.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/741.txt b/dataset/preprocessed/741.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/741.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/742.txt b/dataset/preprocessed/742.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/742.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/743.txt b/dataset/preprocessed/743.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/743.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/745.txt b/dataset/preprocessed/745.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/745.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/746.txt b/dataset/preprocessed/746.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/746.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/747.txt b/dataset/preprocessed/747.txt new file mode 100644 index 0000000..04e55f7 --- /dev/null +++ b/dataset/preprocessed/747.txt @@ -0,0 +1,175 @@ +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from ModelSkeletons.SupervisedModel import SupervisedModel, define_scope +from LSTM_CTCConfig import LSTM_CTCModelConfig + + +class LSTM_CTCModel(SupervisedModel): + + def __init__(self, config: LSTM_CTCModelConfig): + self.config = config + self.graph = tf.Graph() + self.graph.as_default() + self.build_placeholders() + self.inference + self.loss + self.training + self.decoded_inference + self.evaluation + self.session = None + + def build_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=[None, self.config.input_max_time, self.config.input_frame_size], name='input_placeholder') + self.input_lengths_placeholder = tf.placeholder(tf.int32, shape=[None], name='input_lengths_placeholder') + self.output_placeholder = tf.sparse_placeholder(tf.int32, name='output_placeholder') + self.output_lengths_placeholder = tf.placeholder(tf.int32, shape=[None], name='output_placeholder') + + @define_scope + def inference(self): + with tf.name_scope('LSTM'): + cell = tf.nn.rnn_cell.LSTMCell(self.config.cell_size) + cell = tf.nn.rnn_cell.MultiRNNCell([cell] * self.config.cell_count) + cell_initial = cell.zero_state(tf.shape(self.input_placeholder)[0], tf.float32) + rnn, _ = tf.nn.dynamic_rnn(cell, self.input_placeholder, self.output_lengths_placeholder, cell_initial) + flatten_rnn = tf.reshape(rnn, shape=[-1, self.config.input_max_time * self.config.cell_size]) + layer = flatten_rnn + previous_layer_size = int(layer.get_shape()[1]) + for i in range(self.config.fully_connected_count): + with tf.name_scope('FullyConnected_n' + str(i)): + weights = tf.Variable(tf.random_normal(shape=[previous_layer_size, self.config.fully_connected_size], stddev=0.1), name='Weights_n' + str(i)) + biases = tf.Variable(tf.zeros(shape=[self.config.fully_connected_size]), name='Biases_n' + str(i)) + layer = tf.matmul(layer, weights) + biases + layer = tf.nn.relu(layer) + previous_layer_size = int(layer.get_shape()[1]) + with tf.name_scope('FullyConnected_Last'): + weights = tf.Variable(tf.random_normal(shape=[previous_layer_size, self.config.output_max_time * self.config.output_frame_size], stddev=0.1)) + biases = tf.Variable(tf.zeros(shape=[self.config.output_max_time * self.config.output_frame_size])) + layer = tf.matmul(layer, weights) + biases + logits = tf.reshape(layer, shape=[-1, self.config.output_max_time, self.config.output_frame_size]) + return logits + + @define_scope + def loss(self): + ctc_loss = tf.nn.ctc_loss(self.inference, self.output_placeholder, self.output_lengths_placeholder, time_major=False) + return tf.reduce_mean(ctc_loss, name='cost') + + @define_scope + def training(self): + tf.summary.scalar('loss', self.loss) + tf.summary.scalar('error', self.evaluation) + global_step = tf.Variable(0, name='global_step', trainable=False) + optimizer = tf.train.AdamOptimizer(self.config.learning_rate, epsilon=0.001) + return optimizer.minimize(self.loss, global_step=global_step) + + @define_scope + def decoded_inference(self): + inference_timemajor = tf.transpose(self.inference, [1, 0, 2]) + decoded, prob = tf.nn.ctc_greedy_decoder(inference_timemajor, self.output_lengths_placeholder) + return decoded + + @define_scope + def evaluation(self): + average_error = [] + for result in self.decoded_inference: + predictions = tf.cast(result, tf.int32) + error = tf.edit_distance(predictions, self.output_placeholder, normalize=True) + average_error.append(tf.reduce_mean(error, name='error')) + return tf.reduce_mean(average_error) + + @staticmethod + def tokens_for_sparse(sequences): + eos_value = 9632 + tmp = [] + for seq_idx in range(len(sequences)): + seq = sequences[seq_idx] + for i in range(len(seq)): + end_idx = i + if seq[i] == eos_value: + break + tmp.append(seq[:end_idx]) + sequences = tmp + indices = [] + values = [] + for n, seq in enumerate(sequences): + indices.extend(zip([n] * len(seq), range(len(seq)))) + values.extend(seq) + indices = np.asarray(indices, dtype=np.int64) + values = np.asarray(values, dtype=np.int32) + shape = np.asarray([len(sequences), np.asarray(indices).max(0)[1] + 1], dtype=np.int64) + return indices, values, shape + + def train(self, database, iteration_count: int, batch_size: int): + session_config = tf.ConfigProto() + session_config.gpu_options.allow_growth = True + self.session = tf.Session(config=session_config) + self.init_model() + train_data = database.train_dataset + for i in tqdm(range(iteration_count)): + feed_dict = self.train_step(train_data, batch_size) + if feed_dict is None: + continue + for key in feed_dict: + if feed_dict[key] is None: + continue + if len(feed_dict) == 0: + continue + if (i + 1) % 10 == 0: + summary_str = self.session.run(self.summary, feed_dict=feed_dict) + self.summary_writer.add_summary(summary_str, i) + self.summary_writer.flush() + if (i + 1) % 100 == 0: + print('######') + print('Loss :', self.session.run(self.loss, feed_dict=feed_dict)) + print('######') + if (i + 1) % 1000 == 0: + self.saver.save(self.session, self.config.checkpoints_path + '\\network', global_step=i) + self.test(database) + + def init_model(self): + self.summary = tf.summary.merge_all() + self.summary_writer = tf.summary.FileWriter(self.config.checkpoints_path + '/logs', self.session.graph) + self.saver = tf.train.Saver() + checkpoint = tf.train.get_checkpoint_state(self.config.checkpoints_path) + if checkpoint and checkpoint.model_checkpoint_path: + print('Found existing checkpoint, loading ...') + self.saver.restore(self.session, checkpoint.model_checkpoint_path) + else: + print("Didn't find existing checkpoint, creating one ...") + self.session.run(tf.global_variables_initializer()) + print('Model initialized !') + + def get_feed_dict(self, dataset, batch_size, one_hot=False): + batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = dataset.next_batch(batch_size, one_hot=one_hot) + tmp = [] + for j in range(len(batch_outputs)): + tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + batch_outputs_feed = np.array(tmp) + try: + batch_outputs_feed = LSTM_CTCModel.tokens_for_sparse(batch_outputs_feed) + except: + return None + tmp = [] + for j in range(len(batch_inputs)): + tmp.append(batch_inputs[j][:self.config.input_max_time]) + batch_inputs = np.array(tmp) + feed_dict = {self.input_placeholder: batch_inputs, self.input_lengths_placeholder: batch_input_lengths, self.output_placeholder: batch_outputs_feed, self.output_lengths_placeholder: batch_output_lengths} + return feed_dict + + def train_step(self, train_data, batch_size: int): + feed_dict = self.get_feed_dict(train_data, batch_size, one_hot=False) + self.session.run(self.training, feed_dict=feed_dict) + return feed_dict + + def test(self, database, batch_size=1000): + if self.session is None: + session_config = tf.ConfigProto() + session_config.gpu_options.allow_growth = True + self.session = tf.Session(config=session_config) + self.init_model() + test_data = database.test_dataset + feed_dict = self.get_feed_dict(test_data, batch_size, one_hot=False) + print('#-----#') + print('Evaluation :', (1 - self.session.run(self.evaluation, feed_dict=feed_dict)) * 100, '%') + print('#-----#') diff --git a/dataset/preprocessed/748.txt b/dataset/preprocessed/748.txt new file mode 100644 index 0000000..634cb1e --- /dev/null +++ b/dataset/preprocessed/748.txt @@ -0,0 +1,56 @@ +import tensorflow as tf +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) +x = tf.placeholder(tf.float32, [None, 784]) +y_ = tf.placeholder(tf.float32, [None, 10]) +x_image = tf.reshape(x, [-1, 28, 28, 1]) + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.01) + return tf.Variable(initial) + + +def bias_variable(shape): + initial = tf.constant(0.1, shape=shape) + return tf.Variable(initial) + + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + + +W_conv1 = weight_variable([5, 5, 1, 32]) +b_conv1 = bias_variable([32]) +h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) +h_pool1 = max_pool_2x2(h_conv1) +W_conv2 = weight_variable([5, 5, 32, 64]) +b_conv2 = bias_variable([64]) +h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) +h_pool2 = max_pool_2x2(h_conv2) +W_fc1 = weight_variable([7 * 7 * 64, 1024]) +b_fc1 = bias_variable([1024]) +h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) +h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) +keep_prob = tf.placeholder(tf.float32) +h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) +W_fc2 = weight_variable([1024, 10]) +b_fc2 = bias_variable([10]) +y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 +cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) +optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cross_entropy) +correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) +session = tf.InteractiveSession() +session.run(tf.global_variables_initializer()) +for i in range(1000): + batch = mnist.train.next_batch(50) + if i % 100 == 0: + train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) + print('step %d is %g accuracy' % (i, train_accuracy)) + optimizer.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) +print('test accuracy is %d' % accuracy.eval(feed_dict={x: mnist.train.images, y_: mnist.train.labels, keep_prob: 1.0})) diff --git a/dataset/preprocessed/749.txt b/dataset/preprocessed/749.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/749.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/75.txt b/dataset/preprocessed/75.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/75.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/750.txt b/dataset/preprocessed/750.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/750.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/751.txt b/dataset/preprocessed/751.txt new file mode 100644 index 0000000..e2bf824 --- /dev/null +++ b/dataset/preprocessed/751.txt @@ -0,0 +1,111 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import scipy.io as sio +import sys, os, os.path, inspect +filedir = inspect.getframeinfo(inspect.currentframe())[0] +filedir = os.path.dirname(os.path.abspath(filedir)) +mat_contents = sio.loadmat(filedir + '/sampleData_wNetChangeLabels.mat') +train_dataset = np.array(mat_contents['train_data']).astype(np.float32) +train_labels = np.array(mat_contents['train_label']).astype(np.float32) +valid_dataset = np.array(mat_contents['test_data']).astype(np.float32) +valid_labels = np.array(mat_contents['test_label']).astype(np.float32) +print('Training set - ', train_dataset.shape, train_labels.shape) +print('Test set - ', valid_dataset.shape, ' ', valid_labels.shape) +sample_size = train_dataset.shape[1] +num_labels = train_labels.shape[1] + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +batch_size = 128 +hidden_size = 512 +hidden_1_size = hidden_size +hidden_2_size = hidden_size +hidden_3_size = hidden_size +beta = 0.01 +SEED = None +dropoutPercent = 0.5 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, sample_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_predict_label = tf.placeholder(tf.float32, shape=(1, sample_size)) + weights_1 = tf.Variable(tf.truncated_normal([sample_size, hidden_1_size], stddev=0.1), name='weights_1') + biases_1 = tf.Variable(tf.zeros([hidden_1_size]), name='biases_1') + weights_2 = tf.Variable(tf.truncated_normal([hidden_1_size, hidden_2_size]), name='weights_2') + biases_2 = tf.Variable(tf.zeros([hidden_2_size]), name='biases_2d') + weights_3 = tf.Variable(tf.truncated_normal([hidden_2_size, hidden_3_size]), name='weights_3') + biases_3 = tf.Variable(tf.zeros([hidden_3_size]), name='biases_3') + weights_o = tf.Variable(tf.truncated_normal([hidden_3_size, num_labels], stddev=0.1), name='weights_o') + biases_o = tf.Variable(tf.zeros([num_labels]), name='biases_o') + + def model(data, train=False): + hidden = tf.nn.relu(tf.matmul(data, weights_1) + biases_1) + if train: + hidden = tf.nn.dropout(hidden, dropoutPercent, seed=SEED) + hidden = tf.nn.relu(tf.matmul(hidden, weights_2) + biases_2) + if train: + hidden = tf.nn.dropout(hidden, dropoutPercent, seed=SEED) + hidden = tf.nn.relu(tf.matmul(hidden, weights_3) + biases_3) + if train: + hidden = tf.nn.dropout(hidden, dropoutPercent, seed=SEED) + return tf.matmul(hidden, weights_o) + biases_o + logits = model(tf_train_dataset, True) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) + regularized_loss = loss + beta * (tf.nn.l2_loss(weights_1) + tf.nn.l2_loss(weights_2) + tf.nn.l2_loss(weights_3) + tf.nn.l2_loss(weights_o)) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.001, global_step, 250, 0.96) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(regularized_loss, global_step=global_step) + train_prediction = tf.nn.softmax(model(tf_train_dataset)) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset)) + data_prediction = tf.nn.softmax(model(tf_predict_label)) + init_op = tf.initialize_all_variables() + saver = tf.train.Saver() +session = tf.Session(graph=graph) + + +def train(num_steps=501, save=True, filename='model_directionDNN.ckpt'): + with session.as_default(): + session.run(init_op) + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f Learning_rate: %f' % (step, l, learning_rate.eval())) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + save_path = saver.save(session, filedir + '/' + filename, global_step=step) + if save: + save_path = saver.save(session, filedir + '/' + filename) + + +def init(): + with session.as_default(): + saver.restore(session, filedir + '/model_directionDNN.ckpt') + + +def predict(input_data): + with session.as_default(): + feed_dict = {tf_predict_label: input_data} + predictionArray = session.run([data_prediction], feed_dict=feed_dict) + prediction = np.argmax(predictionArray) + print('Direction-predictions: {}, np.argmax: {}'.format(predictionArray, prediction)) + return prediction + + +def close(): + session.close() + + +if __name__ == '__main__': + train(save=True) + close diff --git a/dataset/preprocessed/752.txt b/dataset/preprocessed/752.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/752.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/753.txt b/dataset/preprocessed/753.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/753.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/754.txt b/dataset/preprocessed/754.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/754.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/755.txt b/dataset/preprocessed/755.txt new file mode 100644 index 0000000..9c29a20 --- /dev/null +++ b/dataset/preprocessed/755.txt @@ -0,0 +1,129 @@ +import tensorflow as tf +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + +class DeepQNetwork(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2, lr): + super(DeepQNetwork, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.float32, shape=(None,), name='y_o') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.q_function = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.action = tf.placeholder(tf.int32, shape=(None,), name='action') + self.mle_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.action)) + l2 * regularizer + self.mle_optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.mle_loss) + actions_one_hot = tf.one_hot(self.action, self.output_dim, 1.0, 0.0, name='action_one_hot') + predicted_q = tf.reduce_sum((tf.nn.tanh(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + 1) / 2.0 * actions_one_hot, reduction_indices=1) + self.loss = tf.reduce_mean(tf.square(predicted_q - self.output)) + l2 * regularizer + self.optimization = tf.train.AdamOptimizer(learning_rate=lr).minimize(self.loss) + + def train(self, session, inputs, action, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: action, self.output: outputs}) + return cost + + def policy(self, session, inputs): + form, pos, deprel = inputs + return session.run(self.q_function, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + + def classify(self, session, inputs): + return self.policy(session, inputs) + + def oracle_train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.mle_optimization, self.mle_loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.action: outputs}) + return cost diff --git a/dataset/preprocessed/756.txt b/dataset/preprocessed/756.txt new file mode 100644 index 0000000..9113193 --- /dev/null +++ b/dataset/preprocessed/756.txt @@ -0,0 +1,54 @@ +import tensorflow as tf +from bilstm import biLSTM +from utils import feature2cos_sim, max_pooling, cal_loss_and_acc + + +class LSTM_QA(object): + + def __init__(self, batch_size, num_unroll_steps, embeddings, embedding_size, rnn_size, num_rnn_layers, max_grad_norm, l2_reg_lambda=0.0, adjust_weight=False, label_weight=[], is_training=True): + self.batch_size = batch_size + self.embeddings = embeddings + self.embedding_size = embedding_size + self.adjust_weight = adjust_weight + self.label_weight = label_weight + self.rnn_size = rnn_size + self.num_rnn_layers = num_rnn_layers + self.num_unroll_steps = num_unroll_steps + self.max_grad_norm = max_grad_norm + self.l2_reg_lambda = l2_reg_lambda + self.is_training = is_training + self.keep_prob = tf.placeholder(tf.float32, name='keep_drop') + self.lr = tf.Variable(0.0, trainable=False) + self.new_lr = tf.placeholder(tf.float32, shape=[], name='new_learning_rate') + self._lr_update = tf.assign(self.lr, self.new_lr) + self.ori_input_quests = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps]) + self.cand_input_quests = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps]) + self.neg_input_quests = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps]) + self.test_input_q = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps]) + self.test_input_a = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps]) + with tf.device('/cpu:0'), tf.name_scope('embedding_layer'): + W = tf.Variable(tf.to_float(self.embeddings), trainable=True, name='W') + ori_quests = tf.nn.embedding_lookup(W, self.ori_input_quests) + cand_quests = tf.nn.embedding_lookup(W, self.cand_input_quests) + neg_quests = tf.nn.embedding_lookup(W, self.neg_input_quests) + test_q = tf.nn.embedding_lookup(W, self.test_input_q) + test_a = tf.nn.embedding_lookup(W, self.test_input_a) + with tf.variable_scope('LSTM_scope', reuse=None): + ori_q = biLSTM(ori_quests, self.rnn_size) + ori_q_feat = tf.nn.tanh(max_pooling(ori_q)) + with tf.variable_scope('LSTM_scope', reuse=True): + cand_a = biLSTM(cand_quests, self.rnn_size) + neg_a = biLSTM(neg_quests, self.rnn_size) + cand_q_feat = tf.nn.tanh(max_pooling(cand_a)) + neg_q_feat = tf.nn.tanh(max_pooling(neg_a)) + test_q_out = biLSTM(test_q, self.rnn_size) + test_q_out = tf.nn.tanh(max_pooling(test_q_out)) + test_a_out = biLSTM(test_a, self.rnn_size) + test_a_out = tf.nn.tanh(max_pooling(test_a_out)) + self.ori_cand = feature2cos_sim(ori_q_feat, cand_q_feat) + self.ori_neg = feature2cos_sim(ori_q_feat, neg_q_feat) + self.loss, self.acc = cal_loss_and_acc(self.ori_cand, self.ori_neg) + self.test_q_a = feature2cos_sim(test_q_out, test_a_out) + + def assign_new_lr(self, session, lr_value): + session.run(self._lr_update, feed_dict={self.new_lr: lr_value}) diff --git a/dataset/preprocessed/757.txt b/dataset/preprocessed/757.txt new file mode 100644 index 0000000..fb68a97 --- /dev/null +++ b/dataset/preprocessed/757.txt @@ -0,0 +1,174 @@ +import random +import time +import math +import gc +import psutil +import tensorflow as tf +import numpy as np +from sklearn import preprocessing +from param import * +from test_funcs import eval_alignment, eval_alignment_mul, eval_alignment_multi_embed +g = 1000000000 + + +def embed_init(mat_x, mat_y, name, is_l2=False): + print('embed_init') + embeddings = tf.Variable(tf.truncated_normal([mat_x, mat_y], stddev=1.0 / math.sqrt(P.embed_size))) + return tf.nn.l2_normalize(embeddings, 1) if is_l2 else embeddings + + +def mul(tensor1, tensor2, session, num, sigmoid): + t = time.time() + if num < 20000: + sim_mat = tf.matmul(tensor1, tensor2, transpose_b=True) + if sigmoid: + res = tf.sigmoid(sim_mat).eval(session=session) + else: + res = sim_mat.eval(session=session) + else: + res = np.matmul(tensor1.eval(session=session), tensor2.eval(session=session).T) + print('mat mul costs: {:.3f}'.format(time.time() - t)) + return res + + +class KGE_Model: + + def __init__(self, ent_num, rel_num, seed_sup_ent1, seed_sup_ent2, ref_ent1, ref_ent2, kb1_ents, kb2_ents, dim, embed_size, lr): + self.ent_num = ent_num + self.rel_num = rel_num + self.seed_sup_ent1 = seed_sup_ent1 + self.seed_sup_ent2 = seed_sup_ent2 + self.ref_ent1 = ref_ent1 + self.ref_ent2 = ref_ent2 + self.kb1_ents = kb1_ents + self.kb2_ents = kb2_ents + self.embed_size = embed_size + self.lr = lr + self.dim = dim + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.session = tf.Session(config=config) + self._generate_variables() + self._generate_graph() + self._generate_alignment_graph() + self._generate_likelihood_graph() + tf.global_variables_initializer().run(session=self.session) + + def _generate_variables(self): + with tf.variable_scope('relation' + 'embedding'): + self.ent_embeddings = embed_init(self.ent_num, self.embed_size, 'ent_embeds') + self.rel_embeddings = embed_init(self.rel_num, self.embed_size, 'rel_embeds') + self.ent_embeddings = tf.nn.l2_normalize(self.ent_embeddings, 1) + self.rel_embeddings = tf.nn.l2_normalize(self.rel_embeddings, 1) + + def _generate_graph(self): + + def generate_loss(phs, prs, pts, nhs, nrs, nts): + pos_score = tf.reduce_sum(tf.pow(phs + prs - pts, 2), 1) + neg_score = tf.reduce_sum(tf.pow(nhs + nrs - nts, 2), 1) + pos_loss = tf.reduce_sum(tf.maximum(pos_score - tf.constant(P.lambda_1), 0)) + neg_loss = P.mu_1 * tf.reduce_sum(tf.maximum(tf.constant(P.lambda_2) - neg_score, 0)) + return pos_loss, neg_loss + + def generate_optimizer(loss): + opt_vars = [v for v in tf.trainable_variables() if v.name.startswith('relation')] + optimizer = tf.train.AdagradOptimizer(self.lr).minimize(loss, var_list=opt_vars) + return optimizer + self.pos_hs = tf.placeholder(tf.int32, shape=[None]) + self.pos_rs = tf.placeholder(tf.int32, shape=[None]) + self.pos_ts = tf.placeholder(tf.int32, shape=[None]) + self.neg_hs = tf.placeholder(tf.int32, shape=[None]) + self.neg_rs = tf.placeholder(tf.int32, shape=[None]) + self.neg_ts = tf.placeholder(tf.int32, shape=[None]) + phs = tf.nn.embedding_lookup(self.ent_embeddings, self.pos_hs) + prs = tf.nn.embedding_lookup(self.rel_embeddings, self.pos_rs) + pts = tf.nn.embedding_lookup(self.ent_embeddings, self.pos_ts) + nhs = tf.nn.embedding_lookup(self.ent_embeddings, self.neg_hs) + nrs = tf.nn.embedding_lookup(self.rel_embeddings, self.neg_rs) + nts = tf.nn.embedding_lookup(self.ent_embeddings, self.neg_ts) + self.pos_loss, self.neg_loss = generate_loss(phs, prs, pts, nhs, nrs, nts) + self.triple_loss = self.pos_loss + self.neg_loss + self.triple_optimizer = generate_optimizer(self.triple_loss) + + def _generate_alignment_graph(self): + + def generate_loss(phs, prs, pts): + pos_loss = tf.reduce_sum(tf.log(tf.sigmoid(-tf.reduce_sum(tf.pow(phs + prs - pts, 2), 1)))) + return -pos_loss + + def generate_optimizer(loss): + opt_vars = [v for v in tf.trainable_variables() if v.name.startswith('relation')] + optimizer = tf.train.AdagradOptimizer(self.lr).minimize(loss, var_list=opt_vars) + return optimizer + self.new_h = tf.placeholder(tf.int32, shape=[None]) + self.new_r = tf.placeholder(tf.int32, shape=[None]) + self.new_t = tf.placeholder(tf.int32, shape=[None]) + phs = tf.nn.embedding_lookup(self.ent_embeddings, self.new_h) + prs = tf.nn.embedding_lookup(self.rel_embeddings, self.new_r) + pts = tf.nn.embedding_lookup(self.ent_embeddings, self.new_t) + self.alignment_loss = generate_loss(phs, prs, pts) + self.alignment_optimizer = generate_optimizer(self.alignment_loss) + + def _generate_likelihood_graph(self): + self.ents1 = tf.placeholder(tf.int32, shape=[None]) + self.ents2 = tf.placeholder(tf.int32, shape=[None]) + self.likelihood_mat = tf.placeholder(tf.float32, shape=[self.dim, self.dim]) + ent1_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ents1) + ent2_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ents2) + mat = tf.log(tf.sigmoid(tf.matmul(ent1_embed, ent2_embed, transpose_b=True))) + self.likelihood_loss = -tf.reduce_sum(tf.multiply(mat, self.likelihood_mat)) + self.likelihood_optimizer = tf.train.AdagradOptimizer(self.lr).minimize(self.likelihood_loss) + + def test(self, selected_pairs=None): + t1 = time.time() + refs1_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent1) + refs2_embed = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent2) + refs1_embed = tf.nn.l2_normalize(refs1_embed, 1) + refs2_embed = tf.nn.l2_normalize(refs2_embed, 1) + refs1_embed = refs1_embed.eval(session=self.session) + refs2_embed = refs2_embed.eval(session=self.session) + prec_set = eval_alignment_multi_embed(refs1_embed, refs2_embed, P.ent_top_k, selected_pairs, mess='ent alignment') + t2 = time.time() + m1 = psutil.virtual_memory().used + del refs1_embed, refs2_embed + gc.collect() + print('testing ent alignment costs: {:.3f} s\n'.format(time.time() - t1)) + return prec_set + + def eval_ent_embeddings(self): + return self.ent_embeddings.eval(session=self.session) + + def eval_rel_embeddings(self): + return self.rel_embeddings.eval(session=self.session) + + def eval_ref_sim_mat(self, sigmoid=False): + refs1_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent1) + refs2_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.ref_ent2) + refs1_embeddings = tf.nn.l2_normalize(refs1_embeddings, 1) + refs2_embeddings = tf.nn.l2_normalize(refs2_embeddings, 1) + return mul(refs1_embeddings, refs2_embeddings, self.session, len(self.ref_ent1), sigmoid) + + def eval_kb1_mat(self, sigmoid=False): + ent1_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb1_ents) + ent2_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb1_ents) + return mul(ent1_embeddings, ent2_embeddings, self.session, len(self.kb1_ents), sigmoid) + + def eval_kb2_mat(self, sigmoid=False): + ent1_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb2_ents) + ent2_embeddings = tf.nn.embedding_lookup(self.ent_embeddings, self.kb2_ents) + return mul(ent1_embeddings, ent2_embeddings, self.session, len(self.kb2_ents), sigmoid) + + def eval_sim_mat(self, ent1, ent2, sigmoid=False): + embeddings1 = tf.nn.embedding_lookup(self.ent_embeddings, ent1) + embeddings2 = tf.nn.embedding_lookup(self.ent_embeddings, ent2) + return mul(embeddings1, embeddings2, self.session, len(ent1), sigmoid) + + def eval_kb1_embed(self): + return tf.nn.embedding_lookup(self.ent_embeddings, self.kb1_ents).eval(session=self.session) + + def eval_kb2_embed(self): + return tf.nn.embedding_lookup(self.ent_embeddings, self.kb2_ents).eval(session=self.session) + + def save(self, folder, suffix): + np.save(folder + 'ent1_embeds_' + suffix + '.npy', self.eval_kb1_embed()) + np.save(folder + 'ent2_embeds_' + suffix + '.npy', self.eval_kb2_embed()) diff --git a/dataset/preprocessed/758.txt b/dataset/preprocessed/758.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/758.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/76.txt b/dataset/preprocessed/76.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/76.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/760.txt b/dataset/preprocessed/760.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/760.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/761.txt b/dataset/preprocessed/761.txt new file mode 100644 index 0000000..bd6dca6 --- /dev/null +++ b/dataset/preprocessed/761.txt @@ -0,0 +1,135 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + test_phase = tf.placeholder(tf.bool) + logits_out = conv_net(test_x, test_k, test_phase) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network, x, y, keep_prob): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_phase = tf.placeholder(tf.bool) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y, x, y, keep_prob, train_phase=test_phase) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/762.txt b/dataset/preprocessed/762.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/762.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/763.txt b/dataset/preprocessed/763.txt new file mode 100644 index 0000000..886f5f3 --- /dev/null +++ b/dataset/preprocessed/763.txt @@ -0,0 +1,113 @@ +import tensorflow as tf +import numpy as np +try: + import bequick +except ImportError: + import sys + import os + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) +from bequick.tf_utils import random_uniform_matrix +try: + from .tb_parser import Parser +except (ValueError, SystemError) as e: + from tb_parser import Parser +tf.set_random_seed(1234) + + +def initialize_word_embeddings(session, form_emb, indices, matrix): + _indices = [tf.to_int32(i) for i in indices] + session.run(tf.scatter_update(form_emb, _indices, matrix)) + + +def unpack_inputs(inputs): + form = [_[0] for _ in inputs] + pos = [_[1] for _ in inputs] + deprel = [_[2] for _ in inputs] + return form, pos, deprel + + +class Network(object): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2): + self.form_size = form_size + self.form_dim = form_dim + self.pos_size = pos_size + self.pos_dim = pos_dim + self.deprel_size = deprel_size + self.deprel_dim = deprel_dim + self.hidden_dim = hidden_dim + self.output_dim = output_dim + self.form_features_dim = len(Parser.FORM_NAMES) * self.form_dim + self.pos_features_dim = len(Parser.POS_NAMES) * self.pos_dim + self.deprel_features_dim = len(Parser.DEPREL_NAMES) * self.deprel_dim + self.input_dim = self.form_features_dim + self.pos_features_dim + self.deprel_features_dim + self.dropout = dropout + self.l2 = l2 + self.form = tf.placeholder(tf.int32, shape=(None, len(Parser.FORM_NAMES)), name='form_i') + self.pos = tf.placeholder(tf.int32, shape=(None, len(Parser.POS_NAMES)), name='pos_i') + self.deprel = tf.placeholder(tf.int32, shape=(None, len(Parser.DEPREL_NAMES)), name='deprel_i') + + +class Classifier(Network): + + def __init__(self, form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2, learning_rate_rl): + super(Classifier, self).__init__(form_size, form_dim, pos_size, pos_dim, deprel_size, deprel_dim, hidden_dim, output_dim, dropout, l2) + self.output = tf.placeholder(tf.int32, shape=(None,), name='y_o') + self.rewards = tf.placeholder(tf.float32, shape=(None,), name='reward') + self.indices = tf.placeholder(tf.int32, shape=(None, 2), name='indices') + with tf.device('/cpu:0'), tf.name_scope('embedding'): + self.form_emb = tf.Variable(random_uniform_matrix(self.form_size, self.form_dim), name='form_emb') + self.pos_emb = tf.Variable(random_uniform_matrix(self.pos_size, self.pos_dim), name='pos_emb') + self.deprel_emb = tf.Variable(random_uniform_matrix(self.deprel_size, self.deprel_dim), name='deprel_emb') + inputs = tf.concat(1, [tf.reshape(tf.nn.embedding_lookup(self.form_emb, self.form), [-1, self.form_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.pos_emb, self.pos), [-1, self.pos_features_dim]), tf.reshape(tf.nn.embedding_lookup(self.deprel_emb, self.deprel), [-1, self.deprel_features_dim])]) + self.W0 = tf.Variable(random_uniform_matrix(self.input_dim, self.hidden_dim), name='W0') + self.b0 = tf.Variable(tf.zeros([self.hidden_dim]), name='b0') + self.W1 = tf.Variable(random_uniform_matrix(self.hidden_dim, self.output_dim), name='W1') + self.b1 = tf.Variable(tf.zeros([self.output_dim]), name='b1') + hidden_layer = tf.nn.relu(tf.add(tf.matmul(inputs, self.W0), self.b0)) + self.prediction = tf.add(tf.matmul(hidden_layer, self.W1), self.b1) + regularizer = tf.nn.l2_loss(self.W0) + tf.nn.l2_loss(self.b0) + tf.nn.l2_loss(self.W1) + tf.nn.l2_loss(self.b1) + if self.dropout > 0: + hidden_layer2 = tf.nn.dropout(hidden_layer, self.dropout) + else: + hidden_layer2 = hidden_layer + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1), self.output)) + l2 * regularizer + self.prob = tf.nn.log_softmax(tf.add(tf.matmul(hidden_layer2, self.W1), self.b1)) + self.select_prob = tf.gather_nd(self.prob, self.indices) + self.result = tf.multiply(self.select_prob, self.rewards) + self.pg_loss = tf.reduce_mean(self.result) + l2 * regularizer + self.grad = tf.train.AdagradOptimizer(learning_rate=0.1).compute_gradients(self.pg_loss) + self.pg_optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.pg_loss) + self.optimization = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(self.loss) + self.saver = tf.train.Saver() + self.saved_path = './saved_model' + self.count = 0 + + def train(self, session, inputs, outputs): + form, pos, deprel = inputs + _, cost = session.run([self.optimization, self.loss], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.output: outputs}) + return cost + + def policy_train(self, session, inputs, outputs, rewards): + form, pos, deprel = inputs + indices = [] + for aid in range(len(outputs)): + indices.append([aid, outputs[aid]]) + _, cost, indices, prob, res = session.run([self.pg_optimization, self.pg_loss, self.indices, self.prob, self.result], feed_dict={self.form: form, self.pos: pos, self.deprel: deprel, self.rewards: -rewards, self.indices: indices}) + self.count += 1 + return cost + + def classify(self, session, inputs): + form, pos, deprel = inputs + prediction = session.run(self.prediction, feed_dict={self.form: form, self.pos: pos, self.deprel: deprel}) + return prediction + + def variable_output(self, session): + form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 = session.run([self.form_emb, self.pos_emb, self.deprel_emb, self.W0, self.b0, self.W1, self.b1]) + return form_emb, pos_emb, deprel_emb, W0, b0, W1, b1 + + def save(self, session): + self.saved_path = self.saver.save(session, './saved_model') + + def restore(self, session): + self.saver.restore(session, self.saved_path) diff --git a/dataset/preprocessed/764.txt b/dataset/preprocessed/764.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/764.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/765.txt b/dataset/preprocessed/765.txt new file mode 100644 index 0000000..e69de29 diff --git a/dataset/preprocessed/766.txt b/dataset/preprocessed/766.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/766.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/767.txt b/dataset/preprocessed/767.txt new file mode 100644 index 0000000..7d23aa1 --- /dev/null +++ b/dataset/preprocessed/767.txt @@ -0,0 +1,92 @@ +import numpy as np +import tensorflow as tf +from tensorflow.contrib import rnn +from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file + + +class LSTMModel: + + def __init__(self, embedding_dim, vocab_size, ref_size, learning_rate, dropout_prob): + self.embedding_dim = embedding_dim + self.vocab_size = vocab_size + 1 + self.ref_size = ref_size + 1 + self.learning_rate = learning_rate + self.dropout_prob = dropout_prob + self.stddev = 0.001 + self.loss_array = [] + + def create_Graph(self, embedding_matrix, refs_matrix): + tf.reset_default_graph() + self.left_context = tf.placeholder(tf.int32, shape=[None, None]) + self.right_context = tf.placeholder(tf.int32, shape=[None, None]) + self.left_len = tf.placeholder(tf.int32, shape=[None]) + self.right_len = tf.placeholder(tf.int32, shape=[None]) + self.refs = tf.placeholder(tf.int32, shape=[None]) + self.labels = tf.placeholder(tf.int32, shape=[None]) + self.size_placeholder = tf.placeholder(name='train_sizes', dtype=tf.int32) + if embedding_matrix is not None: + self.word_embeddings = tf.get_variable(name='word_embeddings', shape=[self.vocab_size, self.embedding_dim], initializer=tf.constant_initializer(embedding_matrix)) + else: + self.word_embeddings = tf.get_variable(name='word_embeddings', shape=[self.vocab_size, self.embedding_dim], initializer=tf.random_normal_initializer(stddev=self.stddev)) + if refs_matrix is not None: + self.refs_embeddings = tf.get_variable(name='refs_embeddings', shape=[self.ref_size, self.embedding_dim], initializer=tf.constant_initializer(refs_matrix)) + else: + self.refs_embeddings = tf.get_variable(name='refs_embeddings', shape=[self.ref_size, self.embedding_dim], initializer=tf.random_normal_initializer(stddev=self.stddev)) + self.relu_matrix = tf.get_variable(name='relu_matrix', shape=[2 * self.embedding_dim + self.embedding_dim, 2], initializer=tf.random_normal_initializer(stddev=self.stddev)) + self.relu_bias = tf.get_variable(name='relu_bias', shape=[2], initializer=tf.zeros_initializer()) + self.left_embedded_context = tf.nn.embedding_lookup(params=self.word_embeddings, ids=self.left_context) + self.right_embedded_context = tf.nn.embedding_lookup(params=self.word_embeddings, ids=self.right_context) + self.refs_embedded = tf.nn.embedding_lookup(params=self.refs_embeddings, ids=self.refs) + with tf.variable_scope('left'): + self.lstm_cell_left = rnn.LSTMCell(self.embedding_dim) + self.outputs_left, self.states_left = tf.nn.dynamic_rnn(cell=self.lstm_cell_left, inputs=self.left_embedded_context, dtype=tf.float32, sequence_length=self.left_len) + self.last_rnn_output_left = self.states_left.h + with tf.variable_scope('right'): + self.lstm_cell_right = rnn.LSTMCell(self.embedding_dim) + self.outputs_right, self.states_right = tf.nn.dynamic_rnn(cell=self.lstm_cell_right, inputs=self.right_embedded_context, dtype=tf.float32, sequence_length=self.right_len) + self.last_rnn_output_right = self.states_right.h + self.concat_vecs = tf.concat([self.last_rnn_output_left, self.last_rnn_output_right, self.refs_embedded], 1) + self.vecs_dp = tf.nn.dropout(self.concat_vecs, keep_prob=self.dropout_prob) + self.relu = tf.nn.leaky_relu(tf.matmul(self.vecs_dp, self.relu_matrix) + self.relu_bias) + self.loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.relu, labels=self.labels)) + self.optimizer = tf.train.AdagradOptimizer(learning_rate=self.learning_rate) + self.train_op = self.optimizer.minimize(self.loss_op) + self.stacked_vecs_left = tf.tile(self.last_rnn_output_left, [self.size_placeholder, 1]) + self.stacked_vecs_right = tf.tile(self.last_rnn_output_right, [self.size_placeholder, 1]) + self.train_concat_vecs = tf.concat([self.stacked_vecs_left, self.stacked_vecs_right, self.refs_embedded], 1) + self.train_relu = tf.nn.leaky_relu(tf.matmul(self.train_concat_vecs, self.relu_matrix) + self.relu_bias) + self.prediction = tf.nn.softmax(self.train_relu) + self.session = tf.Session() + self.session.run(tf.global_variables_initializer()) + self.saver = tf.train.Saver() + + def train_forward(self, left_context_batch, right_context_batch, left_len_batch, right_len_batch, refs_batch, labels_batch): + _, loss = self.session.run([self.train_op, self.loss_op], feed_dict={self.left_context: left_context_batch, self.right_context: right_context_batch, self.refs: refs_batch, self.left_len: left_len_batch, self.right_len: right_len_batch, self.labels: labels_batch}) + self.loss_array.append(loss) + return loss + + def eval_avg_loss(self): + avg_loss = sum(self.loss_array) / len(self.loss_array) + return avg_loss + + def reset_avg_loss(self): + self.loss_array = [] + + def save_model(self, path, step): + if step is not None: + self.saver.save(self.session, path, global_step=step) + else: + self.saver.save(self.session, path) + + def restore_model(self, path): + self.saver.restore(self.session, path) + + def test_batch(self, left_context_batch, right_context_batch, left_len_batch, right_len_batch, refs_batch): + """compute true-probability of this refs""" + p_vec = self.session.run([self.prediction], feed_dict={self.left_context: left_context_batch, self.right_context: right_context_batch, self.refs: refs_batch, self.left_len: left_len_batch, self.right_len: right_len_batch, self.size_placeholder: len(refs_batch)}) + p_vec_true = p_vec[0][:, (1)] + return p_vec_true + + def delete_model(self): + self.session.close() + tf.reset_default_graph() diff --git a/dataset/preprocessed/768.txt b/dataset/preprocessed/768.txt new file mode 100644 index 0000000..a6ba249 --- /dev/null +++ b/dataset/preprocessed/768.txt @@ -0,0 +1,79 @@ +import tensorflow as tf +import numpy as np + + +class Neural_Net: + + def __init__(self, session): + self.session = session + self.error = 0 + self.learning_rate = 0.01 + self.neural_net() + + def neural_net(self): + self.tf_x = tf.placeholder(tf.float32, shape=[1, 84, 84, 4], name='tf_x') + self.tf_x2 = tf.placeholder(tf.float32, shape=[1, 1], name='tf_x2') + self.tf_y = tf.placeholder(tf.float32, shape=[1, 1], name='tf_y') + with tf.variable_scope('conv_1'): + w1 = tf.get_variable(name='_weights', shape=[3, 3, 4, 32]) + b1 = tf.get_variable(name='_biases', initializer=tf.zeros(shape=[32])) + conv1 = tf.nn.conv2d(input=self.tf_x, filter=w1, strides=[1, 1, 1, 1], padding='VALID') + conv1 = tf.nn.bias_add(conv1, b1, name='net_preactivation') + conv1 = tf.nn.relu(conv1, name='activation') + """conv1 shape : [1, 82, 82, 32]""" + print('conv1 :\n', conv1) + conv1_pool = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='conv1_pool') + """conv1_pool shape : [1, 41, 41, 32]""" + print('conv1_pool :\n', conv1_pool) + with tf.variable_scope('conv_2'): + w2 = tf.get_variable(name='_weights', shape=[3, 3, 32, 64]) + b2 = tf.get_variable(name='_biases', initializer=tf.zeros(shape=[64])) + conv2 = tf.nn.conv2d(input=conv1_pool, filter=w2, strides=[1, 1, 1, 1], padding='VALID') + conv2 = tf.nn.bias_add(conv2, b2, name='net_preactivation') + conv2 = tf.nn.relu(conv2, name='activation') + """conv2 shape : [1, 39, 39, 64]""" + print('conv2 :\n', conv2) + conv2_pool = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='conv2_pool') + """conv2_pool shape : [1, 20, 20, 64]""" + print('conv2_pool :\n', conv2_pool) + with tf.variable_scope('conv_3'): + w3 = tf.get_variable(name='_weights', shape=[5, 5, 64, 128]) + b3 = tf.get_variable(name='_biases', initializer=tf.zeros(shape=[128])) + conv3 = tf.nn.conv2d(input=conv2_pool, filter=w3, strides=[1, 1, 1, 1], padding='VALID') + conv3 = tf.nn.bias_add(conv3, b3, name='net_preactivation') + conv3 = tf.nn.relu(conv3, name='activation') + """conv3 shape : [1, 16, 16, 128]""" + print('conv3 :\n', conv3) + conv3_pool = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='conv3_pool') + """conv3_pool shape : [1, 8, 8, 128]""" + print('conv3_pool :\n', conv3_pool) + conv3_pool_units = np.prod(conv3_pool.get_shape().as_list()[1:]) + print('conv3_pool_units :\n', conv3_pool_units) + conv3_pool_reshape = tf.reshape(conv3_pool, shape=(-1, conv3_pool_units)) + print('conv3_pool_reshape :\n', conv3_pool_reshape) + """conv3_pool_reshape shape : [1, 8192]""" + Conv3WithTf_x2 = tf.concat([conv3_pool_reshape, self.tf_x2], 1) + with tf.variable_scope('fcl_1'): + w4 = tf.get_variable(name='_weights', shape=[8193, 8193]) + b4 = tf.get_variable(name='_biases', initializer=tf.zeros(shape=[8193])) + fcl1 = tf.matmul(Conv3WithTf_x2, w4) + fcl1 = tf.nn.bias_add(fcl1, b4, name='net_pre-activation') + fcl1 = tf.nn.relu(fcl1, name='activation') + with tf.variable_scope('fcl_2'): + w5 = tf.get_variable(name='_weights', shape=[8193, 4096]) + b5 = tf.get_variable(name='_biases', initializer=tf.zeros(shape=[4096])) + fcl2 = tf.matmul(fcl1, w5) + fcl2 = tf.nn.bias_add(fcl2, b5, name='net_pre-activation') + with tf.variable_scope('fcl_3'): + w6 = tf.get_variable(name='_weights', shape=[4096, 1]) + b6 = tf.get_variable(name='_biases', initializer=tf.zeros(shape=[1])) + fcl3 = tf.matmul(fcl2, w6) + self.Q_value = tf.nn.bias_add(fcl3, b6, name='net_pre-activation') + self.error = tf.reduce_mean(tf.square(self.tf_y - self.Q_value)) + self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.error) + + def train(self, state, action, Q_value): + return self.session.run(self.optimizer, feed_dict={self.tf_x: state, self.tf_x2: action, self.tf_y: Q_value}) + + def predict(self, state, action): + return self.session.run(self.Q_value, feed_dict={self.tf_x: state, self.tf_x2: action}) diff --git a/dataset/preprocessed/769.txt b/dataset/preprocessed/769.txt new file mode 100644 index 0000000..62a4de3 --- /dev/null +++ b/dataset/preprocessed/769.txt @@ -0,0 +1,144 @@ +import tensorflow as tf +import numpy as np +import datetime +import matplotlib.pyplot as plt +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data/') + + +def discriminator(image, reuse=False): + if reuse: + tf.get_variable_scope().reuse_variables() + weights1 = tf.get_variable('discriminator_weights1', [5, 5, 1, 32], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais1 = tf.get_variable('discriminator_bais1', [32], initializer=tf.constant_initializer(0)) + layer1 = tf.nn.conv2d(input=image, filter=weights1, strides=[1, 1, 1, 1], padding='SAME') + layer1 = layer1 + bais1 + layer1 = tf.nn.relu(layer1) + layer1 = tf.nn.avg_pool(layer1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + weights2 = tf.get_variable('discriminator_weights2', [5, 5, 32, 64], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais2 = tf.get_variable('discriminator_bais2', [64], initializer=tf.constant_initializer(0)) + layer2 = tf.nn.conv2d(input=layer1, filter=weights2, strides=[1, 1, 1, 1], padding='SAME') + layer2 = layer2 + bais2 + layer2 = tf.nn.relu(layer2) + layer2 = tf.nn.avg_pool(layer2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + weights3 = tf.get_variable('discriminator_weights3', [7 * 7 * 64, 1024], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais3 = tf.get_variable('discriminator_bais3', [1024], initializer=tf.constant_initializer(0)) + layer3 = tf.reshape(layer2, [-1, 7 * 7 * 64]) + layer3 = tf.matmul(layer3, weights3) + layer3 = layer3 + bais3 + layer3 = tf.nn.relu(layer3) + weights4 = tf.get_variable('discriminator_weights4', [1024, 1], initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais4 = tf.get_variable('discriminator_bais4', [1], initializer=tf.constant_initializer(0)) + layer4 = tf.matmul(layer3, weights4) + bais4 + return layer4 + + +def generator(batch_size, dimension): + z = tf.truncated_normal([batch_size, dimension], mean=0, stddev=1, name='z') + weights1 = tf.get_variable('generator_weights1', [dimension, 3136], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais1 = tf.get_variable('generator_bais1', [3136], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer1 = tf.matmul(z, weights1) + bais1 + layer1 = tf.reshape(layer1, [-1, 56, 56, 1]) + layer1 = tf.contrib.layers.batch_norm(layer1, epsilon=1e-05, scope='bn1') + layer1 = tf.nn.relu(layer1) + weights2 = tf.get_variable('generator_weights2', [3, 3, 1, dimension / 2], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais2 = tf.get_variable('generator_bais2', [dimension / 2], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer2 = tf.nn.conv2d(layer1, weights2, strides=[1, 2, 2, 1], padding='SAME') + layer2 = layer2 + bais2 + layer2 = tf.contrib.layers.batch_norm(layer2, epsilon=1e-05, scope='bn2') + layer2 = tf.nn.relu(layer2) + layer2 = tf.image.resize_images(layer2, [56, 56]) + weights3 = tf.get_variable('generator_weights3', [3, 3, dimension / 2, dimension / 4], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais3 = tf.get_variable('generator_bais3', [dimension / 4], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer3 = tf.nn.conv2d(layer2, weights3, strides=[1, 2, 2, 1], padding='SAME') + layer3 = layer3 + bais3 + layer3 = tf.contrib.layers.batch_norm(layer3, epsilon=1e-05, scope='bn3') + layer3 = tf.nn.relu(layer3) + layer3 = tf.image.resize_images(layer3, [56, 56]) + weights4 = tf.get_variable('generator_weights4', [1, 1, dimension / 4, 1], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) + bais4 = tf.get_variable('generator_bais4', [1], initializer=tf.truncated_normal_initializer(stddev=0.02)) + layer4 = tf.nn.conv2d(layer3, weights4, strides=[1, 2, 2, 1], padding='SAME') + layer4 = layer4 + bais4 + layer4 = tf.sigmoid(layer4) + return layer4 + + +session = tf.Session() +batch_size = 50 +dimensions = 100 +x_placeholder = tf.placeholder('float', shape=[None, 28, 28, 1], name='x_placeholder') +generator_z = generator(batch_size, dimensions) +discriminator_x = discriminator(x_placeholder) +discriminator_g = discriminator(generator_z, reuse=True) +generator_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=discriminator_g, labels=tf.ones_like(discriminator_g))) +discriminator_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=discriminator_x, labels=tf.fill([batch_size, 1], 0.9))) +discriminator_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=discriminator_g, labels=tf.zeros_like(discriminator_g))) +discriminator_loss = discriminator_loss_real + discriminator_loss_fake +tvars = tf.trainable_variables() +discriminator_vars = [var for var in tvars if 'discriminator_' in var.name] +generator_vars = [var for var in tvars if 'generator_' in var.name] +with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE) as scope: + discriminator_trainer_fake = tf.train.AdamOptimizer(0.0001).minimize(discriminator_loss_fake, var_list=discriminator_vars) + discriminator_trainer_real = tf.train.AdamOptimizer(0.0001).minimize(discriminator_loss_real, var_list=discriminator_vars) + generator_trainer = tf.train.AdamOptimizer(0.0001).minimize(generator_loss, var_list=generator_vars) +tf.summary.scalar('Generator_loss', generator_loss) +tf.summary.scalar('Discriminator_loss_real', discriminator_loss_real) +tf.summary.scalar('Discriminator_loss_fake', discriminator_loss_fake) +discriminator_real_count_ph = tf.placeholder(tf.float32) +discriminator_fake_count_ph = tf.placeholder(tf.float32) +generator_count_ph = tf.placeholder(tf.float32) +tf.summary.scalar('d_real_count', discriminator_real_count_ph) +tf.summary.scalar('d_fake_count', discriminator_fake_count_ph) +tf.summary.scalar('g_count', generator_count_ph) +discriminator_on_generated = tf.reduce_mean(discriminator(generator(batch_size, dimensions))) +discriminator_on_real = tf.reduce_mean(discriminator(x_placeholder)) +tf.summary.scalar('d_on_generated_eval', discriminator_on_generated) +tf.summary.scalar('d_on_real_eval', discriminator_on_real) +images_for_tensorboard = generator(batch_size, dimensions) +tf.summary.image('Generated_images', images_for_tensorboard, 10) +merged = tf.summary.merge_all() +logdir = 'tensorboard/gan/' +writer = tf.summary.FileWriter(logdir, session.graph) +print(logdir) +saver = tf.train.Saver() +session.run(tf.global_variables_initializer()) +generatorLoss = 0 +discriminatorLossReal, discriminatorLossFake = 1, 1 +discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 +for i in range(100000): + real_image_batch = mnist.train.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + if discriminatorLossFake > 0.6: + _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_fake, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + discriminator_fake_count += 1 + if generatorLoss > 0.5: + _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([generator_trainer, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + generator_count += 1 + if discriminatorLossReal > 0.45: + _, discriminatorLossReal, discriminatorLossFake, generatorLoss = session.run([discriminator_trainer_real, discriminator_loss_real, discriminator_loss_fake, generator_loss], {x_placeholder: real_image_batch}) + discriminator_real_count += 1 + if i % 10 == 0: + real_image_batch = mnist.validation.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1]) + summary = session.run(merged, {x_placeholder: real_image_batch, discriminator_real_count_ph: discriminator_real_count, discriminator_fake_count_ph: discriminator_fake_count, generator_count_ph: generator_count}) + writer.add_summary(summary, i) + discriminator_real_count, discriminator_fake_count, generator_count = 0, 0, 0 + if i % 1000 == 0: + images = session.run(generator(3, dimensions)) + discriminator_result = session.run(discriminator(x_placeholder), {x_placeholder: images}) + print('TRAINING STEP', i, 'AT', datetime.datetime.now()) + for j in range(3): + print('Discriminator classification', discriminator_result[j]) + im = images[(j), :, :, (0)] + plt.imsave('imageAt' + str(i) + '.png', im.reshape([28, 28]), cmap='Greys') + if i % 5000 == 0: + save_path = saver.save(session, 'models/pretrained_gan.ckpt', global_step=i) + print('saved to %s' % save_path) +test_images = session.run(generator(10, 100)) +test_eval = session.run(discriminator(x_placeholder), {x_placeholder: test_images}) +real_images = mnist.validation.next_batch(10)[0].reshape([10, 28, 28, 1]) +real_eval = session.run(discriminator(x_placeholder), {x_placeholder: real_images}) +for i in range(10): + print(test_eval[i]) + plt.imsave('generated_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap='Greys') +for i in range(10): + print(real_eval[i]) + plt.imsave('real_images' + str(i) + '.png', test_images[(i), :, :, (0)], cmap='Greys') diff --git a/dataset/preprocessed/77.txt b/dataset/preprocessed/77.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/77.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/770.txt b/dataset/preprocessed/770.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/770.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/772.txt b/dataset/preprocessed/772.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/772.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/774.txt b/dataset/preprocessed/774.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/774.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/775.txt b/dataset/preprocessed/775.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/775.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/776.txt b/dataset/preprocessed/776.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/776.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/777.txt b/dataset/preprocessed/777.txt new file mode 100644 index 0000000..8065929 --- /dev/null +++ b/dataset/preprocessed/777.txt @@ -0,0 +1,128 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import argparse +import tensorflow as tf +import numpy as np +import time +import os +from tensorflow.examples.tutorials.mnist import input_data +from tensorflow.examples.tutorials.mnist import mnist +FLAGS = None +IMAGE_WIDTH = 28 +IMAGE_HEIGHT = 28 +LABEL_NUM = 10 + + +def load_data(): + mnist_data = input_data.read_data_sets(FLAGS.data_dir) + return mnist_data + + +def weight_variable(shape): + return tf.Variable(tf.truncated_normal(shape, stddev=0.01), name='W') + + +def bias_variable(shape): + return tf.Variable(tf.constant(0.1, shape=shape), name='b') + + +class CNN: + + def __init__(self, session, X_placeholder, Y_placeholder, y_pred, loss): + self.session = session + self.X_placeholder = X_placeholder + self.Y_placeholder = Y_placeholder + self.Y_pred = y_pred + self.loss = loss + self.train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) + correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(Y_placeholder, 1)) + self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + + def init(self): + self.session.run(tf.global_variables_initializer()) + + def train(self, X, Y): + self.train_step.run(feed_dict={self.X_placeholder: X, self.Y_placeholder: Y}) + + def test(self, X, Y): + return self.accuracy.eval(feed_dict={self.X_placeholder: X, self.Y_placeholder: Y}) + + +def generate_lenet_graph(inputX): + with tf.device('/cpu:0'): + num_fm_conv1 = 32 + with tf.name_scope('conv1'): + W_conv1 = weight_variable([5, 5, 1, num_fm_conv1]) + b_conv1 = bias_variable([num_fm_conv1]) + conv1 = tf.nn.conv2d(inputX, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + h1 = tf.nn.relu(conv1 + b_conv1) + pool1 = tf.nn.max_pool(h1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + with tf.name_scope('conv2'): + num_fm_conv2 = 64 + W_conv2 = weight_variable([5, 5, num_fm_conv1, num_fm_conv2]) + b_conv2 = bias_variable([num_fm_conv2]) + conv2 = tf.nn.conv2d(pool1, W_conv2, strides=[1, 1, 1, 1], padding='SAME') + h2 = tf.nn.relu(conv2 + b_conv2) + pool2 = tf.nn.max_pool(h2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + with tf.name_scope('fc'): + output_size = 7 * 7 * num_fm_conv2 + fc_output_size = 1024 + pool2_reshape = tf.reshape(pool2, [-1, output_size]) + W_fc = weight_variable([output_size, fc_output_size]) + b_fc = weight_variable([fc_output_size]) + h_fc = tf.nn.relu(tf.matmul(pool2_reshape, W_fc) + b_fc) + with tf.name_scope('pred'): + W_pred = weight_variable([fc_output_size, LABEL_NUM]) + b_pred = bias_variable([LABEL_NUM]) + Y_pred_activation = tf.matmul(h_fc, W_pred) + b_pred + return Y_pred_activation + + +def build_train_net(session, X_placeholder, Y_placeholder): + image = tf.reshape(X_placeholder, [-1, IMAGE_WIDTH, IMAGE_HEIGHT, 1]) + Y_pred_activation = generate_lenet_graph(image) + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(Y_pred_activation, Y_placeholder)) + return CNN(session, X_placeholder, Y_placeholder, Y_pred_activation, loss) + + +def simple_lr(X_placeholder, Y_placeholder): + N = IMAGE_WIDTH * IMAGE_HEIGHT + W = weight_variable([N, LABEL_NUM]) + b = bias_variable([LABEL_NUM]) + h = tf.matmul(X_placeholder, W) + b + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(h, Y_placeholder)) + return CNN(session, X_placeholder, Y_placeholder, h, loss) + + +def main(): + mnist_data = load_data() + batch_size = 32 + X_placeholder = tf.placeholder(tf.float32, shape=[None, IMAGE_WIDTH * IMAGE_HEIGHT], name='X') + Y_placeholder = tf.placeholder(tf.int32, shape=[None, LABEL_NUM], name='Y') + session = tf.InteractiveSession() + net = build_train_net(session, X_placeholder, Y_placeholder) + net.init() + saver = tf.train.Saver() + meta_graph_def = tf.train.export_meta_graph(filename=os.path.join(FLAGS.save_dir, 'model.meta'), collection_list=['variables']) + for i in range(10000): + start_time = time.time() + X, Y = mnist_data.train.next_batch(batch_size) + Y = np.eye(LABEL_NUM)[Y] + net.train(X, Y) + duration = time.time() - start_time + if i % 1000 == 999: + train_accuracy = net.test(X, Y) + print('train_accuracy: %f (walltime: %.3f)' % (train_accuracy, duration)) + saver.save(session, os.path.join(FLAGS.save_dir, 'model.ckpt'), global_step=i) + test_Y = np.eye(LABEL_NUM)[mnist_data.test.labels] + test_accuracy = net.test(mnist_data.test.images, test_Y) + print('test_accuracy: %f' % test_accuracy) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--data_dir', type=str, default='mnist_data', help='data path') + parser.add_argument('--save_dir', type=str, default='mnist_model', help='save path') + FLAGS, _ = parser.parse_known_args() + main() diff --git a/dataset/preprocessed/778.txt b/dataset/preprocessed/778.txt new file mode 100644 index 0000000..04e55f7 --- /dev/null +++ b/dataset/preprocessed/778.txt @@ -0,0 +1,175 @@ +import tensorflow as tf +import numpy as np +from tqdm import tqdm +from Timit_utils.TimitDatabase import TimitDatabase +from ModelSkeletons.SupervisedModel import SupervisedModel, define_scope +from LSTM_CTCConfig import LSTM_CTCModelConfig + + +class LSTM_CTCModel(SupervisedModel): + + def __init__(self, config: LSTM_CTCModelConfig): + self.config = config + self.graph = tf.Graph() + self.graph.as_default() + self.build_placeholders() + self.inference + self.loss + self.training + self.decoded_inference + self.evaluation + self.session = None + + def build_placeholders(self): + self.input_placeholder = tf.placeholder(tf.float32, shape=[None, self.config.input_max_time, self.config.input_frame_size], name='input_placeholder') + self.input_lengths_placeholder = tf.placeholder(tf.int32, shape=[None], name='input_lengths_placeholder') + self.output_placeholder = tf.sparse_placeholder(tf.int32, name='output_placeholder') + self.output_lengths_placeholder = tf.placeholder(tf.int32, shape=[None], name='output_placeholder') + + @define_scope + def inference(self): + with tf.name_scope('LSTM'): + cell = tf.nn.rnn_cell.LSTMCell(self.config.cell_size) + cell = tf.nn.rnn_cell.MultiRNNCell([cell] * self.config.cell_count) + cell_initial = cell.zero_state(tf.shape(self.input_placeholder)[0], tf.float32) + rnn, _ = tf.nn.dynamic_rnn(cell, self.input_placeholder, self.output_lengths_placeholder, cell_initial) + flatten_rnn = tf.reshape(rnn, shape=[-1, self.config.input_max_time * self.config.cell_size]) + layer = flatten_rnn + previous_layer_size = int(layer.get_shape()[1]) + for i in range(self.config.fully_connected_count): + with tf.name_scope('FullyConnected_n' + str(i)): + weights = tf.Variable(tf.random_normal(shape=[previous_layer_size, self.config.fully_connected_size], stddev=0.1), name='Weights_n' + str(i)) + biases = tf.Variable(tf.zeros(shape=[self.config.fully_connected_size]), name='Biases_n' + str(i)) + layer = tf.matmul(layer, weights) + biases + layer = tf.nn.relu(layer) + previous_layer_size = int(layer.get_shape()[1]) + with tf.name_scope('FullyConnected_Last'): + weights = tf.Variable(tf.random_normal(shape=[previous_layer_size, self.config.output_max_time * self.config.output_frame_size], stddev=0.1)) + biases = tf.Variable(tf.zeros(shape=[self.config.output_max_time * self.config.output_frame_size])) + layer = tf.matmul(layer, weights) + biases + logits = tf.reshape(layer, shape=[-1, self.config.output_max_time, self.config.output_frame_size]) + return logits + + @define_scope + def loss(self): + ctc_loss = tf.nn.ctc_loss(self.inference, self.output_placeholder, self.output_lengths_placeholder, time_major=False) + return tf.reduce_mean(ctc_loss, name='cost') + + @define_scope + def training(self): + tf.summary.scalar('loss', self.loss) + tf.summary.scalar('error', self.evaluation) + global_step = tf.Variable(0, name='global_step', trainable=False) + optimizer = tf.train.AdamOptimizer(self.config.learning_rate, epsilon=0.001) + return optimizer.minimize(self.loss, global_step=global_step) + + @define_scope + def decoded_inference(self): + inference_timemajor = tf.transpose(self.inference, [1, 0, 2]) + decoded, prob = tf.nn.ctc_greedy_decoder(inference_timemajor, self.output_lengths_placeholder) + return decoded + + @define_scope + def evaluation(self): + average_error = [] + for result in self.decoded_inference: + predictions = tf.cast(result, tf.int32) + error = tf.edit_distance(predictions, self.output_placeholder, normalize=True) + average_error.append(tf.reduce_mean(error, name='error')) + return tf.reduce_mean(average_error) + + @staticmethod + def tokens_for_sparse(sequences): + eos_value = 9632 + tmp = [] + for seq_idx in range(len(sequences)): + seq = sequences[seq_idx] + for i in range(len(seq)): + end_idx = i + if seq[i] == eos_value: + break + tmp.append(seq[:end_idx]) + sequences = tmp + indices = [] + values = [] + for n, seq in enumerate(sequences): + indices.extend(zip([n] * len(seq), range(len(seq)))) + values.extend(seq) + indices = np.asarray(indices, dtype=np.int64) + values = np.asarray(values, dtype=np.int32) + shape = np.asarray([len(sequences), np.asarray(indices).max(0)[1] + 1], dtype=np.int64) + return indices, values, shape + + def train(self, database, iteration_count: int, batch_size: int): + session_config = tf.ConfigProto() + session_config.gpu_options.allow_growth = True + self.session = tf.Session(config=session_config) + self.init_model() + train_data = database.train_dataset + for i in tqdm(range(iteration_count)): + feed_dict = self.train_step(train_data, batch_size) + if feed_dict is None: + continue + for key in feed_dict: + if feed_dict[key] is None: + continue + if len(feed_dict) == 0: + continue + if (i + 1) % 10 == 0: + summary_str = self.session.run(self.summary, feed_dict=feed_dict) + self.summary_writer.add_summary(summary_str, i) + self.summary_writer.flush() + if (i + 1) % 100 == 0: + print('######') + print('Loss :', self.session.run(self.loss, feed_dict=feed_dict)) + print('######') + if (i + 1) % 1000 == 0: + self.saver.save(self.session, self.config.checkpoints_path + '\\network', global_step=i) + self.test(database) + + def init_model(self): + self.summary = tf.summary.merge_all() + self.summary_writer = tf.summary.FileWriter(self.config.checkpoints_path + '/logs', self.session.graph) + self.saver = tf.train.Saver() + checkpoint = tf.train.get_checkpoint_state(self.config.checkpoints_path) + if checkpoint and checkpoint.model_checkpoint_path: + print('Found existing checkpoint, loading ...') + self.saver.restore(self.session, checkpoint.model_checkpoint_path) + else: + print("Didn't find existing checkpoint, creating one ...") + self.session.run(tf.global_variables_initializer()) + print('Model initialized !') + + def get_feed_dict(self, dataset, batch_size, one_hot=False): + batch_inputs, batch_input_lengths, batch_outputs, batch_output_lengths = dataset.next_batch(batch_size, one_hot=one_hot) + tmp = [] + for j in range(len(batch_outputs)): + tmp.append(batch_outputs[j][:batch_output_lengths[j]]) + batch_outputs_feed = np.array(tmp) + try: + batch_outputs_feed = LSTM_CTCModel.tokens_for_sparse(batch_outputs_feed) + except: + return None + tmp = [] + for j in range(len(batch_inputs)): + tmp.append(batch_inputs[j][:self.config.input_max_time]) + batch_inputs = np.array(tmp) + feed_dict = {self.input_placeholder: batch_inputs, self.input_lengths_placeholder: batch_input_lengths, self.output_placeholder: batch_outputs_feed, self.output_lengths_placeholder: batch_output_lengths} + return feed_dict + + def train_step(self, train_data, batch_size: int): + feed_dict = self.get_feed_dict(train_data, batch_size, one_hot=False) + self.session.run(self.training, feed_dict=feed_dict) + return feed_dict + + def test(self, database, batch_size=1000): + if self.session is None: + session_config = tf.ConfigProto() + session_config.gpu_options.allow_growth = True + self.session = tf.Session(config=session_config) + self.init_model() + test_data = database.test_dataset + feed_dict = self.get_feed_dict(test_data, batch_size, one_hot=False) + print('#-----#') + print('Evaluation :', (1 - self.session.run(self.evaluation, feed_dict=feed_dict)) * 100, '%') + print('#-----#') diff --git a/dataset/preprocessed/779.txt b/dataset/preprocessed/779.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/779.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/78.txt b/dataset/preprocessed/78.txt new file mode 100644 index 0000000..5883ab3 --- /dev/null +++ b/dataset/preprocessed/78.txt @@ -0,0 +1,111 @@ +import pandas as pd +import numpy as np +import pickle +import matplotlib.pyplot as plt +from scipy import stats +import tensorflow as tf +import seaborn as sns +from pylab import rcParams +from sklearn import metrics +from sklearn.model_selection import train_test_split +import tensorflow as tf +import pandas as pd +import numpy as np +from tensorflow.contrib import rnn +from sklearn.model_selection import train_test_split +from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score +import os +vocab_size = 10 + + +def reset_graph(seed=42): + tf.reset_default_graph() + tf.set_random_seed(seed) + np.random.seed(seed) + + +reset_graph() + + +def build_modeld(hidden_units=100, num_classes=vocab_size, batch_size=1, num_layers=1, learning_rate=0.001): + x = tf.placeholder(tf.int32, [batch_size, None], name='input_placeholder') + y = tf.placeholder(tf.int64, [batch_size, None], name='labels_placeholder') + seq_lens = tf.placeholder(tf.int32, [batch_size]) + embeddings = tf.get_variable('embedding_matrix', [num_classes, hidden_units]) + rnn_inputs = tf.nn.embedding_lookup(embeddings, x) + lstm_cell = tf.nn.rnn_cell.LSTMCell(hidden_units, state_is_tuple=True) + cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * num_layers, state_is_tuple=True) + init_state = cells.zero_state(batch_size, tf.float32) + rnn_outputs, final_state = tf.nn.dynamic_rnn(cells, rnn_inputs, initial_state=init_state, sequence_length=seq_lens) + with tf.variable_scope('softmax'): + W = tf.get_variable('W', [hidden_units, num_classes]) + b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0)) + outputs = tf.reshape(rnn_outputs, [-1, hidden_units]) + logits = tf.matmul(outputs, W) + b + error = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tf.reshape(y, [-1]))) + optimizer = tf.train.AdamOptimizer(learning_rate).minimize(error) + correct_predictions = tf.equal(tf.argmax(logits, 1), y) + accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32)) + return dict(x=x, y=y, init_state=init_state, final_state=final_state, error=error, accuracy=accuracy, optimizer=optimizer, sequence_lengths=seq_lens) + + +def run_epoch(session, model, data): + err = 0.0 + acc = 0.0 + iters = 0 + training_state = None + for _sequence_length, _input, _target in data: + feed_dict = {model['x']: np.array([_input], dtype=np.int32), model['y']: np.array([_target], dtype=np.int64), model['sequence_lengths']: np.array(_sequence_length, dtype=np.int32)} + if training_state is not None: + feed_dict[model['init_state']] = training_state + err, training_state, acc, _ = session.run([model['error'], model['final_state'], model['accuracy'], model['optimizer']], feed_dict) + iters += _sequence_length[0] + return err / iters, acc / iters + + +def train_model(train_data, epochs=10): + with tf.Graph().as_default(), tf.Session() as session: + initializer = tf.random_uniform_initializer(-0.1, 0.1) + with tf.variable_scope('model', reuse=None, initializer=initializer): + model = build_modeld() + session.run(tf.global_variables_initializer()) + for i in range(epochs): + error, accuracy = run_epoch(session, model, train_data) + print('Epoch: %d -- Train Error: %.4f -- Accuracy: %.4f ' % (i + 1, error, accuracy)) + + +data = pd.read_csv('TesterHRY.csv') +features = data.iloc[:, 0:7] +labels = data.iloc[:, (-1)] +X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, shuffle=False, random_state=42) +i = 0 +batch_size = 1 +for i in range(int(len(X_train) / batch_size)): + start = i + end = i + batch_size + batch_x = np.array(X_train[start:end]) + batch_y = np.array(y_train[start:end]) + print(batch_x) + print(batch_x.shape) + print(batch_y) + print(batch_y.shape) +print('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB') +_input = data.iloc[1:2, 0:7] +_target = data.iloc[1:2, (-1)] +x = _input.values +print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') +print(x) +print(x.shape) +print(type(x)) +y = np.zeros(len(x[0]), dtype=float) +print('yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy') +print(y) +print(y.shape) +print(type(y)) +_sequence_length, _input, _target = len(x[0]), x, y +x[0] = list(map(lambda x: x if x > 0 else x * -1, x[0])) +print(x[0]) +print('000000000000000000000000000000') +train_data = [([len(x[0])], x[0], y)] +train_data = [([5.8], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6])] +train_model(train_data) diff --git a/dataset/preprocessed/780.txt b/dataset/preprocessed/780.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/780.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/781.txt b/dataset/preprocessed/781.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/781.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/79.txt b/dataset/preprocessed/79.txt new file mode 100644 index 0000000..0e4b4df --- /dev/null +++ b/dataset/preprocessed/79.txt @@ -0,0 +1,119 @@ +import tensorflow as tf +import numpy as np +import gym + + +class Actor(object): + + def __init__(self, session, state_space, action_bounds, **options): + self.session = session + self.state_space = state_space + self.action_bounds = action_bounds + try: + self.learning_rate = options['learning_rate'] + except KeyError: + self.learning_rate = 0.0001 + self.global_steps = tf.Variable(0, trainable=False) + self._init_input() + self._init_nn() + self._init_op() + + def _init_input(self): + self.state = tf.placeholder(tf.float32, [1, self.state_space]) + self.action = tf.placeholder(tf.float32, None) + self.td_error = tf.placeholder(tf.float32, None) + + def _init_nn(self): + w_init, b_init = tf.random_normal_initializer(0.0, 0.3), tf.constant_initializer(0.1) + phi_state = tf.layers.dense(self.state, 20, tf.nn.relu, kernel_initializer=w_init, bias_initializer=b_init) + mu = tf.layers.dense(phi_state, 1, tf.nn.tanh, kernel_initializer=w_init, bias_initializer=b_init) + sigma = tf.layers.dense(phi_state, 1, tf.nn.softplus, kernel_initializer=w_init, bias_initializer=b_init) + self.mu, self.sigma = tf.squeeze(mu * 2), tf.squeeze(sigma + 0.1) + self.gaussian_func = tf.distributions.Normal(self.mu, self.sigma) + gaussian_sample = self.gaussian_func.sample(sample_shape=1) + self.action = tf.clip_by_value(gaussian_sample, self.action_bounds[0], self.action_bounds[1]) + + def _init_op(self): + self.loss = self.gaussian_func.log_prob(self.action) * self.td_error + 0.01 * self.gaussian_func.entropy() + self.train_op = tf.train.AdamOptimizer(self.learning_rate).minimize(-self.loss, self.global_steps) + + def train(self, state, action, td_error): + _, loss = self.session.run([self.train_op, self.loss], feed_dict={self.state: state[(np.newaxis), :], self.action: action, self.td_error: td_error}) + return loss + + def get_next_action(self, state): + return self.session.run(self.action, feed_dict={self.state: state[(np.newaxis), :]}) + + +class Critic(object): + + def __init__(self, session, state_space, **options): + self.session = session + self.state_space = state_space + try: + self.learning_rate = options['learning_rate'] + except KeyError: + self.learning_rate = 0.0001 + try: + self.gamma = options['gamma'] + except KeyError: + self.gamma = 0.9 + self._init_input() + self._init_nn() + self._init_op() + + def _init_input(self): + self.state = tf.placeholder(tf.float32, [1, self.state_space]) + self.reward = tf.placeholder(tf.float32) + self.value_next = tf.placeholder(tf.float32, [1, 1]) + + def _init_nn(self): + w_init, b_init = tf.random_normal_initializer(0.0, 0.3), tf.constant_initializer(0.1) + phi_state = tf.layers.dense(self.state, 30, tf.nn.relu, kernel_initializer=w_init, bias_initializer=b_init) + self.value = tf.layers.dense(phi_state, 1, kernel_initializer=w_init, bias_initializer=b_init) + + def _init_op(self): + self.td_error = tf.reduce_mean(self.reward + self.gamma * self.value_next - self.value) + self.loss = tf.square(self.td_error) + self.train_op = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss) + + def train(self, state, reward, state_next): + state, state_next = state[(np.newaxis), :], state_next[(np.newaxis), :] + value_next = self.session.run(self.value, feed_dict={self.state: state}) + td_error, _ = self.session.run([self.td_error, self.train_op], feed_dict={self.state: state, self.value_next: value_next, self.reward: reward}) + return td_error + + +def main(_): + env = gym.make('Pendulum-v0') + env.seed(1) + env = env.unwrapped + session = tf.Session() + actor = Actor(session, env.observation_space.shape[0], [-env.action_space.high, env.action_space.high]) + critic = Critic(session, env.observation_space.shape[0]) + session.run(tf.global_variables_initializer()) + running_reward = None + for episode in range(1000): + state, steps, reward_history = env.reset(), 0, [] + while True: + if episode > 500: + env.render() + action = actor.get_next_action(state) + state_next, reward, done, info = env.step(action) + reward /= 10 + td_error = critic.train(state, reward, state_next) + actor.train(state, action, td_error) + state = state_next + steps += 1 + reward_history.append(reward) + if steps > 200: + if running_reward is None: + running_reward = sum(reward_history) + else: + running_reward = running_reward * 0.9 + sum(reward_history) * 0.1 + print('Episode: {} | Reward: {}'.format(episode, running_reward)) + break + + +if __name__ == '__main__': + tf.app.run() diff --git a/dataset/preprocessed/8.txt b/dataset/preprocessed/8.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/8.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/80.txt b/dataset/preprocessed/80.txt new file mode 100644 index 0000000..28553cb --- /dev/null +++ b/dataset/preprocessed/80.txt @@ -0,0 +1,100 @@ +import tensorflow as tf +""" PART I """ + + +def add_consts(): + c1 = tf.constant(5.1) + c2 = tf.constant(1.0) + c3 = tf.constant(5.9) + a1 = tf.add(c1, c2) + af = tf.add(a1, c3) + return af + + +def add_consts_with_placeholder(): + c1 = tf.constant(5.1) + c2 = tf.constant(1.0) + c3 = tf.placeholder(tf.float32) + a1 = tf.add(c1, c2) + af = tf.add(a1, c3) + return af, c3 + + +def my_relu(in_value): + out_value = tf.maximum(0.0, in_value) + return out_value + + +def my_perceptron(x): + pf = tf.placeholder(tf.float32, shape=[x]) + my_int_variable = tf.get_variable('my_int_variable', [x], dtype=tf.float32, initializer=tf.ones_initializer) + mul = pf * my_int_variable + add = tf.reduce_sum(mul) + out_value = tf.maximum(0.0, add) + i = pf + out = out_value + return i, out + + +""" PART II """ +fc_count = 0 + + +def input_placeholder(): + return tf.placeholder(dtype=tf.float32, shape=[None, 784], name='image_input') + + +def target_placeholder(): + return tf.placeholder(dtype=tf.float32, shape=[None, 10], name='image_target_onehot') + + +def onelayer(X, Y, layersize=10): + w = tf.Variable(tf.zeros([784, layersize])) + b = tf.Variable(tf.zeros([layersize])) + logits1 = tf.matmul(X, w) + logits = logits1 + b + preds = tf.nn.softmax(logits) + batch_xentropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=logits) + batch_loss = tf.reduce_mean(batch_xentropy) + return w, b, logits, preds, batch_xentropy, batch_loss + + +def twolayer(X, Y, hiddensize=30, outputsize=10): + w1 = tf.Variable(tf.truncated_normal([784, hiddensize])) + w2 = tf.Variable(tf.zeros([hiddensize, outputsize])) + b1 = tf.Variable(tf.truncated_normal([hiddensize])) + b2 = tf.Variable(tf.zeros([outputsize])) + logitsmul1 = tf.matmul(X, w1) + logits1 = logitsmul1 + b1 + preds1 = tf.nn.relu(logits1) + logits12 = tf.matmul(preds1, w2) + logits = logits12 + b2 + preds = tf.nn.softmax(logits) + batch_xentropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=logits) + batch_loss = tf.reduce_mean(batch_xentropy) + return w1, b1, w2, b2, logits, preds, batch_xentropy, batch_loss + + +def convnet(X, Y, convlayer_sizes=[10, 10], filter_shape=[3, 3], outputsize=10, padding='same'): + my_conv = [filter_shape[0], filter_shape[1], 1, convlayer_sizes[0]] + weight_conv1 = tf.Variable(tf.truncated_normal(my_conv, stddev=0.1)) + biased_conv1 = tf.Variable(tf.constant(0.1, shape=[convlayer_sizes[0]])) + conv1 = tf.nn.relu(tf.nn.conv2d(X, weight_conv1, strides=[1, 1, 1, 1], padding='SAME') + biased_conv1) + my_conv2 = [filter_shape[0], filter_shape[1], convlayer_sizes[0], convlayer_sizes[1]] + weight_conv2 = tf.Variable(tf.truncated_normal(my_conv2, stddev=0.1)) + biased_conv2 = tf.Variable(tf.constant(0.1, shape=[convlayer_sizes[1]])) + conv2 = tf.nn.relu(tf.nn.conv2d(conv1, weight_conv2, strides=[1, 1, 1, 1], padding='SAME') + biased_conv2) + X_layer = tf.reshape(conv2, [-1, 28 * 28 * convlayer_sizes[1]]) + w = tf.Variable(tf.zeros([28 * 28 * convlayer_sizes[1], outputsize])) + b = tf.Variable(tf.zeros([outputsize])) + logits1 = tf.matmul(X_layer, w) + logits = logits1 + b + preds = tf.nn.softmax(logits) + batch_xentropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=logits) + batch_loss = tf.reduce_mean(batch_xentropy) + return conv1, conv2, w, b, logits, preds, batch_xentropy, batch_loss + + +def train_step(sess, batch, X, Y, train_op, loss_op, summaries_op): + train_result, loss, summary = sess.run([train_op, loss_op, summaries_op], feed_dict={X: batch[0], Y: batch[1]}) + return train_result, loss, summary diff --git a/dataset/preprocessed/81.txt b/dataset/preprocessed/81.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/81.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/82.txt b/dataset/preprocessed/82.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/82.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/83.txt b/dataset/preprocessed/83.txt new file mode 100644 index 0000000..27b3886 --- /dev/null +++ b/dataset/preprocessed/83.txt @@ -0,0 +1,120 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) +batch_size = 128 +num_steps = 3001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + isL2 = tf.placeholder(tf.bool) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + + def prediction(mInput): + logits = tf.matmul(mInput, weights) + biases + return logits + if isL2: + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset), tf_train_labels)) + else: + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset), tf_train_labels) + 0.05 * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(prediction(tf_train_dataset)) + valid_prediction = tf.nn.softmax(prediction(tf_valid_dataset)) + test_prediction = tf.nn.softmax(prediction(tf_test_dataset)) +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, isL2: False} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) +batch_size = 128 +num_steps = 3001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights_h = tf.Variable(tf.truncated_normal([image_size * image_size, 1024])) + biases_h = tf.Variable(tf.zeros([1024])) + weights = tf.Variable(tf.truncated_normal([1024, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + keep_prob = tf.placeholder(tf.float32) + isDropout = tf.placeholder(tf.bool) + isL2 = tf.placeholder(tf.bool) + + def prediction(mInput, dropout=False): + layer1 = tf.nn.relu(tf.matmul(mInput, weights_h) + biases_h) + if dropout: + layer1 = tf.nn.dropout(layer1, keep_prob) + logits = tf.matmul(layer1, weights) + biases + return logits + if isL2: + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset, isDropout), tf_train_labels)) + else: + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction(tf_train_dataset, isDropout), tf_train_labels) + 0.01 * tf.nn.l2_loss(weights) + 0.01 * tf.nn.l2_loss(weights_h)) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 100000, 0.96) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(prediction(tf_train_dataset)) + valid_prediction = tf.nn.softmax(prediction(tf_valid_dataset)) + test_prediction = tf.nn.softmax(prediction(tf_test_dataset)) +with tf.Session(graph=graph) as session: + tf.initialize_all_variables().run() + print('Initialized') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: 0.5, isL2: False, isDropout: False} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) diff --git a/dataset/preprocessed/84.txt b/dataset/preprocessed/84.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/84.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/85.txt b/dataset/preprocessed/85.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/85.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/86.txt b/dataset/preprocessed/86.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/86.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/87.txt b/dataset/preprocessed/87.txt new file mode 100644 index 0000000..f34ce5d --- /dev/null +++ b/dataset/preprocessed/87.txt @@ -0,0 +1,276 @@ +from __future__ import print_function +import numpy as np +import tensorflow as tf +from six.moves import cPickle as pickle +import time +pickle_file = 'notMNIST.pickle' +with open(pickle_file, 'rb') as f: + save = pickle.load(f) + train_dataset = save['train_dataset'] + train_labels = save['train_labels'] + valid_dataset = save['valid_dataset'] + valid_labels = save['valid_labels'] + test_dataset = save['test_dataset'] + test_labels = save['test_labels'] + del save + print('Training set', train_dataset.shape, train_labels.shape) + print('Validation set', valid_dataset.shape, valid_labels.shape) + print('Test set', test_dataset.shape, test_labels.shape) +image_size = 28 +num_labels = 10 + + +def reformat(dataset, labels): + dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) + labels = (np.arange(num_labels) == labels[:, (None)]).astype(np.float32) + return dataset, labels + + +train_dataset, train_labels = reformat(train_dataset, train_labels) +valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) +test_dataset, test_labels = reformat(test_dataset, test_labels) +print('Training set', train_dataset.shape, train_labels.shape) +print('Validation set', valid_dataset.shape, valid_labels.shape) +print('Test set', test_dataset.shape, test_labels.shape) + + +def accuracy(predictions, labels): + return 100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0] + + +train_subset = 10000 +batch_size = 128 +beta = 0.001 +graph = tf.Graph() +with graph.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(tf_train_dataset, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases) + print('train_prediction shape: ', train_prediction) + print('valid_prediction shape: ', valid_prediction) + print('test_prediction shape: ', test_prediction) +batch_size = 128 +num_steps = 3001 +with tf.Session(graph=graph) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with no RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 128 +num_nodes = 1024 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(relu1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 12 +num_nodes = 1024 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(relu1, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + l2_regularizer = tf.nn.l2_loss(weights) + tf.nn.l2_loss(weights_relu1) + loss += 0.0005 * l2_regularizer + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 12 +num_nodes = 1024 +beta = 0.001 +prob_limit = 0.75 +print('graph with RELU') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + weights_relu1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_nodes])) + biases_relu1 = tf.Variable(tf.zeros([num_nodes])) + relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights_relu1) + biases_relu1) + print('TF hidden set', relu1) + weights = tf.Variable(tf.truncated_normal([num_nodes, num_labels])) + biases = tf.Variable(tf.zeros([num_labels])) + keep_prob = tf.placeholder('float') + hidden_layer_drop = tf.nn.dropout(relu1, keep_prob) + logits = tf.matmul(hidden_layer_drop, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(weights_relu1) + beta * tf.nn.l2_loss(weights)) + optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights_relu1) + biases_relu1), weights) + biases) + test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights_relu1) + biases_relu1), weights) + biases) +print('run with RELU') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('Initialized with RELU') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels, keep_prob: prob_limit} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) +batch_size = 128 +h1_num_nodes = 1024 +h2_num_nodes = 768 +h3_num_nodes = 512 +h1_stddev = np.sqrt(2.0 / 784) +h2_stddev = np.sqrt(2.0 / h1_num_nodes) +h3_stddev = np.sqrt(2.0 / h2_num_nodes) +logits_stddev = np.sqrt(2.0 / h3_num_nodes) +beta = 0.001 +print('graph best: l2 regularization, no dropout') +graph_relu = tf.Graph() +with graph_relu.as_default(): + tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) + tf_valid_dataset = tf.constant(valid_dataset) + tf_test_dataset = tf.constant(test_dataset) + print('TF Training set', tf_train_dataset) + h1_weights = tf.Variable(tf.truncated_normal([image_size * image_size, h1_num_nodes], stddev=h1_stddev)) + h1_biases = tf.Variable(tf.zeros([h1_num_nodes])) + h1_layer = tf.nn.relu(tf.matmul(tf_train_dataset, h1_weights) + h1_biases) + print('TF hidden layer 1->', h1_layer) + h2_weights = tf.Variable(tf.truncated_normal([h1_num_nodes, h2_num_nodes], stddev=h2_stddev)) + h2_biases = tf.Variable(tf.zeros([h2_num_nodes])) + h2_layer = tf.nn.relu(tf.matmul(h1_layer, h2_weights) + h2_biases) + print('TF hidden layer 2->', h2_layer) + h3_weights = tf.Variable(tf.truncated_normal([h2_num_nodes, h3_num_nodes], stddev=h3_stddev)) + h3_biases = tf.Variable(tf.zeros([h3_num_nodes])) + h3_layer = tf.nn.relu(tf.matmul(h2_layer, h3_weights) + h3_biases) + print('TF hidden layer 3->', h3_layer) + weights = tf.Variable(tf.truncated_normal([h3_num_nodes, num_labels], stddev=logits_stddev)) + biases = tf.Variable(tf.zeros([num_labels])) + logits = tf.matmul(h3_layer, weights) + biases + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels) + beta * tf.nn.l2_loss(h1_weights) + beta * tf.nn.l2_loss(h2_weights) + beta * tf.nn.l2_loss(h3_weights) + beta * tf.nn.l2_loss(weights)) + global_step = tf.Variable(0) + learning_rate = tf.train.exponential_decay(0.5, global_step, 100000, 0.95, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + train_prediction = tf.nn.softmax(logits) + h1_valid = tf.nn.relu(tf.matmul(tf_valid_dataset, h1_weights) + h1_biases) + h2_valid = tf.nn.relu(tf.matmul(h1_valid, h2_weights) + h2_biases) + h3_valid = tf.nn.relu(tf.matmul(h2_valid, h3_weights) + h3_biases) + valid_logits = tf.matmul(h3_valid, weights) + biases + valid_prediction = tf.nn.softmax(valid_logits) + print('valid_prediction(raw)->', valid_prediction) + h1_test = tf.nn.relu(tf.matmul(tf_test_dataset, h1_weights) + h1_biases) + h2_test = tf.nn.relu(tf.matmul(h1_test, h2_weights) + h2_biases) + h3_test = tf.nn.relu(tf.matmul(h2_test, h3_weights) + h3_biases) + test_logits = tf.matmul(h3_test, weights) + biases + test_prediction = tf.nn.softmax(test_logits) +print('run with best: l2 regularization, no dropout') +num_steps = 3001 +with tf.Session(graph=graph_relu) as session: + t1 = time.time() + tf.global_variables_initializer().run() + print('valid_prediction(eval)->', valid_prediction.eval()) + print('Initialized with best: l2 regularization, no dropout') + for step in range(num_steps): + offset = step * batch_size % (train_labels.shape[0] - batch_size) + batch_data = train_dataset[offset:offset + batch_size, :] + batch_labels = train_labels[offset:offset + batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % 500 == 0: + print('Minibatch loss at step %d: %f' % (step, l)) + print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)) + print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)) + print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)) + t2 = time.time() + print('Elapsed Time: %0.2fs' % (t2 - t1)) diff --git a/dataset/preprocessed/88.txt b/dataset/preprocessed/88.txt new file mode 100644 index 0000000..6b9863f --- /dev/null +++ b/dataset/preprocessed/88.txt @@ -0,0 +1,188 @@ +import numpy as np +import tensorflow as tf +import logging +import sys +import os +import math +sys.path.insert(0, os.path.abspath('..')) +from Utility.DataToBinaryVec import DataLoader +from Utility.Configs import fullyNNConfig +import logging +logFormatter = logging.Formatter('%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s') +fileHandler = logging.FileHandler('{0}/{1}.log'.format('./', 'Fullyconnected_NN')) +fileHandler.setFormatter(logFormatter) +consoleHandler = logging.StreamHandler() +consoleHandler.setFormatter(logFormatter) +logger = logging.getLogger() +logger.setLevel(logging.INFO) +logger.addHandler(fileHandler) +logger.addHandler(consoleHandler) + + +class NN(object): + + def __init__(self): + self.d_loader = DataLoader() + self.input_vector_size = self.d_loader.d_handler.get_vocab_size() + self.output_vector_size = self.d_loader.d_handler.get_vocab_size() + self.train_dataset, self.train_labels, self.valid_dataset, self.valid_labels, self.test_dataset, self.test_labels = self.d_loader.get_ttv() + + def simple_NN(self): + logger.info('creating the computational graph...') + graph = tf.Graph() + with graph.as_default(): + with tf.device('/cpu:0'): + tf_train_dataset = tf.placeholder(tf.float32, shape=(fullyNNConfig.batch_size, self.input_vector_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(fullyNNConfig.batch_size, self.output_vector_size)) + tf_valid_dataset = tf.constant(self.valid_dataset) + tf_test_dataset = tf.constant(self.test_dataset) + if fullyNNConfig.regularization: + beta_regul = tf.placeholder(tf.float32) + if fullyNNConfig.learning_rate_decay: + global_step = tf.Variable(0) + + def init_weights(shape): + return tf.Variable(tf.truncated_normal(shape)) + + def init_biases(shape): + return tf.Variable(tf.zeros(shape)) + w_h = init_weights([self.input_vector_size, fullyNNConfig.num_hidden_nodes]) + b_h = init_biases([fullyNNConfig.num_hidden_nodes]) + w_o = init_weights([fullyNNConfig.num_hidden_nodes, self.output_vector_size]) + b_o = init_biases([self.output_vector_size]) + + def model(dataset, w_h, b_h, w_o, b_o, train): + if fullyNNConfig.dropout and train: + drop_i = tf.nn.dropout(dataset, fullyNNConfig.dropout_keep_prob_input) + h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + drop_h = tf.nn.dropout(h_lay_train, fullyNNConfig.dropout_keep_prob_hidden) + return tf.matmul(drop_h, w_o) + b_o + else: + h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + return tf.matmul(h_lay_train, w_o) + b_o + logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits, tf_train_labels)) + if fullyNNConfig.regularization: + loss += beta_regul * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + if fullyNNConfig.learning_rate_decay: + learning_rate = tf.train.exponential_decay(fullyNNConfig.learning_rate, global_step, fullyNNConfig.decay_steps, fullyNNConfig.decay_rate, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + else: + optimizer = tf.train.GradientDescentOptimizer(fullyNNConfig.learning_rate).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset, w_h, b_h, w_o, b_o, False)) + test_prediction = tf.nn.softmax(model(tf_test_dataset, w_h, b_h, w_o, b_o, False)) + with tf.name_scope('accuracy'): + pre = tf.placeholder('float', shape=[None, self.output_vector_size]) + lbl = tf.placeholder('float', shape=[None, self.output_vector_size]) + accuracy = tf.reduce_mean(tf.cast(tf.nn.sigmoid_cross_entropy_with_logits(pre, lbl), 'float')) + logger.info('running the session...') + with tf.Session(graph=graph, config=tf.ConfigProto(log_device_placement=True)) as session: + tf.initialize_all_variables().run() + logger.info('Initialized') + for step in range(fullyNNConfig.num_steps): + offset = step * fullyNNConfig.batch_size % (self.train_labels.shape[0] - fullyNNConfig.batch_size) + batch_data = self.train_dataset[offset:offset + fullyNNConfig.batch_size, :] + batch_labels = self.train_labels[offset:offset + fullyNNConfig.batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + if fullyNNConfig.regularization: + feed_dict[beta_regul] = fullyNNConfig.beta_regul + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % fullyNNConfig.summary_steps == 0: + logger.info('Minibatch loss at step %d: %f' % (step, l)) + logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: predictions, lbl: batch_labels})) + logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: valid_prediction.eval(), lbl: self.valid_labels})) + logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: test_prediction.eval(), lbl: self.test_labels})) + self.print_words(test_prediction.eval(), self.test_labels) + + def simple_NN_w_CandidateSampling(self): + print('creating the computational graph...') + graph = tf.Graph() + with graph.as_default(): + with tf.device('/cpu:0'): + tf_train_dataset = tf.placeholder(tf.float32, shape=(fullyNNConfig.batch_size, self.input_vector_size)) + tf_train_labels = tf.placeholder(tf.float32, shape=(fullyNNConfig.batch_size, self.output_vector_size)) + tf_valid_dataset = tf.constant(self.valid_dataset) + tf_test_dataset = tf.constant(self.test_dataset) + if fullyNNConfig.regularization: + beta_regul = tf.placeholder(tf.float32) + if fullyNNConfig.learning_rate_decay: + global_step = tf.Variable(0) + + def init_biases(shape): + return tf.Variable(tf.zeros(shape)) + w_h = tf.Variable(tf.truncated_normal([self.input_vector_size, fullyNNConfig.num_hidden_nodese], stddev=1.0 / math.sqrt(fullyNNConfig.num_hidden_node))) + b_h = init_biases([fullyNNConfig.num_hidden_nodes]) + w_o = tf.Variable(tf.truncated_normal([fullyNNConfig.num_hidden_nodes, self.output_vector_size], stddev=1.0 / math.sqrt(self.output_vector_size))) + b_o = init_biases([self.output_vector_size]) + + def model(dataset, w_h, b_h, w_o, b_o, train): + if fullyNNConfig.dropout and train: + drop_i = tf.nn.dropout(dataset, fullyNNConfig.dropout_keep_prob_input) + h_lay_train = tf.nn.relu(tf.matmul(drop_i, w_h) + b_h) + drop_h = tf.nn.dropout(h_lay_train, fullyNNConfig.dropout_keep_prob_hidden) + return tf.matmul(drop_h, w_o) + b_o + else: + h_lay_train = tf.nn.relu(tf.matmul(dataset, w_h) + b_h) + return tf.matmul(h_lay_train, w_o) + b_o + logits = model(tf_train_dataset, w_h, b_h, w_o, b_o, True) + if fullyNNConfig.candidate_sampling == 'nce_loss': + instances_loss = tf.nn.nce_loss(w_o, b_o, tf_train_dataset, tf_train_labels, fullyNNConfig.num_sampled, self.output_vector_siz, num_true=10) + elif fullyNNConfig.candidate_sampling == 'softmax_loss': + instances_loss = tf.nn.sampled_softmax_loss(w_o, b_o, tf_train_dataset, tf_train_labels, fullyNNConfig.num_sampled, self.output_vector_siz, num_true=10) + else: + instances_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits, tf_train_labels) + print('no candidate sampling....') + loss = tf.reduce_mean(instances_loss) + if fullyNNConfig.regularization: + loss += beta_regul * (tf.nn.l2_loss(w_h) + tf.nn.l2_loss(w_o)) + if fullyNNConfig.learning_rate_decay: + learning_rate = tf.train.exponential_decay(fullyNNConfig.learning_rate, global_step, fullyNNConfig.decay_steps, fullyNNConfig.decay_rate, staircase=True) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) + else: + optimizer = tf.train.GradientDescentOptimizer(fullyNNConfig.learning_rate).minimize(loss) + train_prediction = tf.nn.softmax(logits) + valid_prediction = tf.nn.softmax(model(tf_valid_dataset, w_h, b_h, w_o, b_o, False)) + test_prediction = tf.nn.softmax(model(tf_test_dataset, w_h, b_h, w_o, b_o, False)) + with tf.name_scope('accuracy'): + pre = tf.placeholder('float', shape=[None, self.output_vector_size]) + lbl = tf.placeholder('float', shape=[None, self.output_vector_size]) + accuracy = tf.reduce_mean(tf.cast(tf.nn.sigmoid_cross_entropy_with_logits(pre, lbl), 'float')) + logger.info('running the session...') + with tf.Session(graph=graph, config=tf.ConfigProto(log_device_placement=True)) as session: + tf.initialize_all_variables().run() + logger.info('Initialized') + for step in range(fullyNNConfig.num_steps): + offset = step * fullyNNConfig.batch_size % (self.train_labels.shape[0] - fullyNNConfig.batch_size) + batch_data = self.train_dataset[offset:offset + fullyNNConfig.batch_size, :] + batch_labels = self.train_labels[offset:offset + fullyNNConfig.batch_size, :] + feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} + if fullyNNConfig.regularization: + feed_dict[beta_regul] = fullyNNConfig.beta_regul + _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict) + if step % fullyNNConfig.summary_steps == 0: + logger.info('Minibatch loss at step %d: %f' % (step, l)) + logger.info('Minibatch accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: predictions, lbl: batch_labels})) + logger.info('Validation accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: valid_prediction.eval(), lbl: self.valid_labels})) + logger.info('Test accuracy: %.3f%%' % session.run(accuracy, feed_dict={pre: test_prediction.eval(), lbl: self.test_labels})) + self.print_words(test_prediction.eval(), self.test_labels) + + def print_words(self, preds, labels): + for pred, label in zip(preds, labels): + label_ids = self.d_loader.d_handler.get_ids_from_binary_vector(label)[0] + pred_ids = np.argsort(np.negative(pred))[:label_ids.size] + print(self.d_loader.d_handler.id_list_to_word_list(label_ids), '-->', self.d_loader.d_handler.id_list_to_word_list(pred_ids)) + + def get_words(self, vect): + ids = self.d_loader.d_handler.get_ids_from_binary_vector(vect)[0] + return self.d_loader.d_handler.id_list_to_word_list(ids) + + +if __name__ == '__main__': + try: + nn = NN() + nn.simple_NN() + logger.info('done...') + except Exception as e: + logger.exception(e) + raise diff --git a/dataset/preprocessed/89.txt b/dataset/preprocessed/89.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/89.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/9.txt b/dataset/preprocessed/9.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/9.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/90.txt b/dataset/preprocessed/90.txt new file mode 100644 index 0000000..e614c05 --- /dev/null +++ b/dataset/preprocessed/90.txt @@ -0,0 +1,466 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import time +import logging +import numpy as np +from six.moves import xrange +import tensorflow as tf +from tensorflow.python.ops import variable_scope as vs +from util import Progbar, minibatches +from evaluate import exact_match_score, f1_score +from IPython import embed +from tensorflow.python.ops.gen_math_ops import _batch_mat_mul as batch_matmul +logging.basicConfig(level=logging.INFO) + + +def get_optimizer(opt): + if opt == 'adam': + optfn = tf.train.AdamOptimizer + elif opt == 'sgd': + optfn = tf.train.GradientDescentOptimizer + else: + assert False + return optfn + + +class LSTMAttnCell(tf.nn.rnn_cell.BasicLSTMCell): + """ + Arguments: + -num_units: hidden state dimensions + -encoder_output: hidden states to compute attention over + -scope: lol who knows + """ + + def __init__(self, num_units, encoder_output, scope=None): + self.attn_states = encoder_output + super(LSTMAttnCell, self).__init__(num_units) + + def __call__(self, inputs, state, scope=None): + lstm_out, lstm_state = super(LSTMAttnCell, self).__call__(inputs, state, scope) + with vs.variable_scope(scope or type(self).__name__): + with vs.variable_scope('Attn'): + ht = tf.nn.rnn_cell._linear(lstm_out, self._num_units, True, 1.0) + ht = tf.expand_dims(ht, axis=1) + scores = tf.reduce_sum(self.attn_states * ht, reduction_indices=2, keep_dims=True) + scores = tf.exp(scores - tf.reduce_max(scores, reduction_indices=1, keep_dims=True)) + scores = scores / (1e-06 + tf.reduce_sum(scores, reduction_indices=1, keep_dims=True)) + context = tf.reduce_sum(self.attn_states * scores, reduction_indices=1) + with vs.variable_scope('AttnConcat'): + out = tf.nn.tanh(tf.nn.rnn_cell._linear([context, lstm_out], self._num_units, True, 1.0)) + return out, lstm_state + + +class Encoder(object): + """ + Arguments: + -size: dimension of the hidden states + """ + + def __init__(self, hidden_size, dropout): + self.hidden_size = hidden_size + self.dropout = dropout + + def encode(self, inputs, masks, attention_inputs=None, initial_state=(None, None), model_type='gru', name='encoder', reuse=False): + """ + In a generalized encode function, you pass in your inputs, + masks, and an initial + hidden state input into this function. + + :param inputs: Symbolic representations of your input + :param masks: this is to make sure tf.nn.dynamic_rnn doesn't iterate + through masked steps + :param encoder_state_input: (Optional) pass this as initial hidden state + to tf.nn.dynamic_rnn to build conditional representations + :param attention_inputs: (Optional) pass this to compute attention and context + over these encodings + :return: an encoded representation of your input. + It can be context-level representation, word-level representation, + or both. + """ + with tf.variable_scope(name, reuse=reuse): + if attention_inputs is None: + if model_type == 'gru': + cell = tf.nn.rnn_cell.GRUCell(self.hidden_size) + elif model_type == 'lstm': + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + else: + raise Exception('Must specify model type.') + else: + assert model_type == 'lstm' + cell = LSTMAttnCell(self.hidden_size, attention_inputs) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + outputs, final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs, sequence_length=masks, initial_state_fw=initial_state[0], initial_state_bw=initial_state[1], dtype=tf.float32) + final_outputs = outputs[0] + outputs[1] + return final_outputs, final_state + + +class Decoder(object): + + def __init__(self, hidden_size, output_size, dropout): + self.hidden_size = hidden_size + self.output_size = output_size + self.dropout = dropout + + def decode(self, knowledge_rep, masks, initial_state=(None, None)): + """ + takes in a knowledge representation + and output a probability estimation over + all paragraph tokens on which token should be + the start of the answer span, and which should be + the end of the answer span. + + :param knowledge_rep: it is a representation of the paragraph and question, + decided by how you choose to implement the encoder + :return: + """ + with vs.variable_scope('decoder'): + with vs.variable_scope('answer_start'): + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + start_states, start_final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, knowledge_rep, sequence_length=masks, initial_state_fw=initial_state[0], initial_state_bw=initial_state[1], dtype=tf.float32) + start_states = start_states[0] + start_states[1] + start_states_reshaped = tf.reshape(start_states, [-1, self.hidden_size]) + start_probs = tf.nn.rnn_cell._linear(start_states_reshaped, output_size=1, bias=True) + start_probs = tf.reshape(start_probs, [-1, self.output_size]) + with vs.variable_scope('answer_end'): + cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_size) + cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self.dropout) + end_states, end_final_state = tf.nn.bidirectional_dynamic_rnn(cell, cell, knowledge_rep, sequence_length=masks, initial_state_fw=start_final_state[0], initial_state_bw=start_final_state[1], dtype=tf.float32) + end_states = end_states[0] + end_states[1] + end_states_reshaped = tf.reshape(end_states, [-1, self.hidden_size]) + end_probs = tf.nn.rnn_cell._linear(end_states_reshaped, output_size=1, bias=True) + end_probs = tf.reshape(end_probs, [-1, self.output_size]) + bool_masks = tf.cast(tf.sequence_mask(masks, maxlen=self.output_size), tf.float32) + add_mask = -1e+30 * (1.0 - bool_masks) + start_probs = tf.add(start_probs, add_mask) + end_probs = tf.add(end_probs, add_mask) + return start_probs, end_probs + + +class QASystem(object): + + def __init__(self, pretrained_embeddings, flags): + """ + Initializes your System + + :param args: pass in more arguments as needed + """ + self.pretrained_embeddings = pretrained_embeddings + self.flags = flags + self.h_size = self.flags.state_size + self.p_size = self.flags.output_size + self.q_size = self.flags.question_size + self.embed_size = self.flags.embedding_size + self.dropout = self.flags.dropout + self.encoder = Encoder(hidden_size=self.h_size, dropout=1.0 - self.flags.dropout) + self.decoder = Decoder(hidden_size=self.h_size, output_size=self.p_size, dropout=1.0 - self.flags.dropout) + self.context_placeholder = tf.placeholder(tf.int32, shape=(None, self.p_size), name='context_placeholder') + self.question_placeholder = tf.placeholder(tf.int32, shape=(None, self.q_size), name='question_placeholder') + self.answer_span_placeholder = tf.placeholder(tf.int32, shape=(None, 2), name='answer_span_placeholder') + self.mask_q_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_q_placeholder') + self.mask_ctx_placeholder = tf.placeholder(tf.int32, shape=(None,), name='mask_ctx_placeholder') + self.dropout_placeholder = tf.placeholder(tf.float32, shape=(), name='dropout_placeholder') + with tf.variable_scope('qa', initializer=tf.uniform_unit_scaling_initializer(1.0)): + self.setup_embeddings() + self.setup_system() + self.setup_loss() + self.global_step = tf.Variable(0, trainable=False) + self.starter_learning_rate = self.flags.learning_rate + self.learning_rate = self.starter_learning_rate + self.optimizer = get_optimizer('adam') + if self.flags.grad_clip: + self.optimizer = self.optimizer(self.learning_rate) + grads = self.optimizer.compute_gradients(self.loss) + for i, (grad, var) in enumerate(grads): + if grad is not None: + grads[i] = tf.clip_by_norm(grad, self.flags.max_gradient_norm), var + self.train_op = self.optimizer.apply_gradients(grads, global_step=self.global_step) + else: + self.train_op = self.optimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) + self.saver = tf.train.Saver() + + def pad(self, sequence, max_length): + from qa_data import PAD_ID + padded_sequence = [] + mask = [] + for sentence in sequence: + mask.append(len(sentence)) + sentence.extend([PAD_ID] * (max_length - len(sentence))) + padded_sequence.append(sentence) + return padded_sequence, mask + + def mixer(self, q_states, ctx_states): + with vs.variable_scope('mixer'): + A = tf.nn.softmax(batch_matmul(ctx_states, tf.transpose(q_states, perm=[0, 2, 1]))) + C_P = batch_matmul(A, q_states) + C_P = tf.reshape(C_P, [-1, self.h_size]) + P = tf.reshape(ctx_states, [-1, self.h_size]) + P_final = tf.nn.rnn_cell._linear([C_P, P], output_size=self.h_size, bias=True) + P_final = tf.reshape(P_final, [-1, self.p_size, self.h_size]) + return P_final + + def filter(self, Q, P): + with vs.variable_scope('filter'): + Qn = tf.nn.l2_normalize(Q, dim=2) + Pn = tf.nn.l2_normalize(P, dim=2) + R = batch_matmul(Qn, tf.transpose(Pn, perm=[0, 2, 1])) + r = tf.reduce_max(R, axis=1) + r = tf.expand_dims(r, axis=2) + P_filtered = P * r + return P_filtered + + def coattention(self, P, Q, masks): + P_t = tf.transpose(P, perm=[0, 2, 1]) + Q_t = tf.transpose(Q, perm=[0, 2, 1]) + L = tf.batch_matmul(P, Q_t) + AQ = tf.nn.softmax(L, dim=1) + AD = tf.nn.softmax(tf.transpose(L, perm=[0, 2, 1]), dim=1) + CQ = tf.batch_matmul(P_t, AQ) + contexts = tf.concat(1, [Q_t, CQ]) + CD = tf.batch_matmul(contexts, AD) + lstm_inputs = tf.transpose(tf.concat(1, [P_t, CD]), perm=[0, 2, 1]) + cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.h_size) + cell_fw = tf.nn.rnn_cell.DropoutWrapper(cell_fw, output_keep_prob=self.dropout) + cell_bw = tf.nn.rnn_cell.DropoutWrapper(cell_bw, output_keep_prob=self.dropout) + all_states, _ = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, lstm_inputs, sequence_length=masks, dtype=tf.float32) + U = tf.concat(2, all_states) + U = U[:, :self.p_size, :] + return U + + def setup_system(self): + """ + After your modularized implementation of encoder and decoder + you should call various functions inside encoder, decoder here + to assemble your reading comprehension system! + :return: + """ + P_filtered = self.context_embeddings + P_filtered = self.filter(Q=self.question_embeddings, P=self.context_embeddings) + question_states, final_question_state = self.encoder.encode(self.question_embeddings, self.mask_q_placeholder, attention_inputs=None, model_type=self.flags.model_type, reuse=False, name='q_encoder') + ctx_states, final_ctx_state = self.encoder.encode(P_filtered, self.mask_ctx_placeholder, attention_inputs=None, initial_state=final_question_state, model_type=self.flags.model_type, reuse=False, name='ctx_encoder') + feed_states = self.mixer(q_states=question_states, ctx_states=ctx_states) + self.start_probs, self.end_probs = self.decoder.decode(knowledge_rep=feed_states, masks=self.mask_ctx_placeholder, initial_state=final_question_state) + + def setup_loss(self): + """ + Set up your loss computation here + :return: + """ + with vs.variable_scope('loss'): + self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.start_probs, self.answer_span_placeholder[:, (0)])) + tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.end_probs, self.answer_span_placeholder[:, (1)])) + + def setup_embeddings(self): + """ + Loads distributed word representations based on placeholder tokens + :return: + """ + with vs.variable_scope('embeddings'): + embeddings = tf.Variable(self.pretrained_embeddings, name='embedding', dtype=tf.float32, trainable=False) + self.question_embeddings = tf.nn.embedding_lookup(embeddings, self.question_placeholder) + self.context_embeddings = tf.nn.embedding_lookup(embeddings, self.context_placeholder) + """ + q_mean,q_var = tf.nn.moments(question_embeddings, axes=[2], shift=None, name=None, keep_dims=True) + ctx_mean,ctx_var = tf.nn.moments(context_embeddings, axes=[2], shift=None, name=None, keep_dims=True) + + + + # q_scale = tf.Variable(tf.zeros,name='q_scale',dtype=tf.float32,trainable=True) + # ctx_scale = tf.Variable(1.0,name='ctx_scale',dtype=tf.float32,trainable=True) + # q_offset = tf.Variable(1.0,name='q_offset',dtype=tf.float32,trainable=True) + # ctx_offset = tf.Variable(1.0,name='ctx_offset',dtype=tf.float32,trainable=True) + + + q_scale = tf.get_variable("q_scale", shape=[self.q_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + ctx_scale = tf.get_variable("ctx_scale", shape=[self.p_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + q_offset = tf.get_variable("q_offset", shape=[self.q_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + ctx_offset = tf.get_variable("ctx_offset", shape=[self.p_size,self.embed_size],initializer=tf.ones_initializer(tf.float32)) + + + self.question_embeddings = tf.nn.batch_normalization(question_embeddings, q_mean, q_var, q_offset,q_scale, variance_epsilon=0.0000001) + self.context_embeddings = tf.nn.batch_normalization(context_embeddings, ctx_mean, ctx_var, ctx_offset, ctx_scale, variance_epsilon=0.0000001) + """ + + def optimize(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Takes in actual data to optimize your model + This method is equivalent to a step() function + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.train_op, self.loss] + _, loss = session.run(output_feed, input_feed) + return loss + + def test(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + in here you should compute a cost for your validation set + and tune your hyperparameters according to the validation set performance + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + input_feed[self.answer_span_placeholder] = answer_span_batch + output_feed = [self.loss] + outputs = session.run(output_feed, input_feed) + return outputs[0] + + def decode(self, session, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Returns the probability distribution over different positions in the paragraph + so that other methods like self.answer() will be able to work properly + :return: + """ + input_feed = {} + input_feed[self.context_placeholder] = context_batch + input_feed[self.question_placeholder] = question_batch + input_feed[self.mask_ctx_placeholder] = mask_ctx_batch + input_feed[self.mask_q_placeholder] = mask_q_batch + input_feed[self.dropout_placeholder] = self.flags.dropout + output_feed = [self.start_probs, self.end_probs] + outputs = session.run(output_feed, input_feed) + return outputs + + def answer(self, session, data): + yp_lst = [] + yp2_lst = [] + prog_train = Progbar(target=1 + int(len(data[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(data, self.flags.batch_size, shuffle=False)): + yp, yp2 = self.decode(session, *batch) + yp_lst.append(yp) + yp2_lst.append(yp2) + prog_train.update(i + 1, [('Answering Questions....', 0.0)]) + print('') + yp_all = np.concatenate(yp_lst, axis=0) + yp2_all = np.concatenate(yp2_lst, axis=0) + a_s = np.argmax(yp_all, axis=1) + a_e = np.argmax(yp2_all, axis=1) + return a_s, a_e + + def validate(self, sess, context_batch, question_batch, answer_span_batch, mask_ctx_batch, mask_q_batch): + """ + Iterate through the validation dataset and determine what + the validation cost is. + + This method calls self.test() which explicitly calculates validation cost. + + How you implement this function is dependent on how you design + your data iteration function + + :return: + """ + return self.test(session=sess, context_batch=context_batch, question_batch=question_batch, answer_span_batch=answer_span_batch, mask_ctx_batch=mask_ctx_batch, mask_q_batch=mask_q_batch) + + def evaluate_answer(self, session, dataset, context, sample=100, log=False, eval_set='train'): + """ + Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM) + with the set of true answer labels + + This step actually takes quite some time. So we can only sample 100 examples + from either training or testing set. + + :param session: session should always be centrally managed in train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param sample: how many examples in dataset we look at + :param log: whether we print to std out stream + :return: + """ + if sample is None: + sampled = dataset + sample = len(dataset[0]) + else: + inds = np.random.choice(len(dataset[0]), sample) + sampled = [elem[inds] for elem in dataset] + context = [context[i] for i in inds] + a_s, a_e = self.answer(session, sampled) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = sampled + f1 = [] + em = [] + for i in range(len(sampled[0])): + pred_words = ' '.join(context[i][a_s[i]:a_e[i] + 1]) + actual_words = ' '.join(context[i][answer_spans[i][0]:answer_spans[i][1] + 1]) + f1.append(f1_score(pred_words, actual_words)) + cur_em = exact_match_score(pred_words, actual_words) + em.append(float(cur_em)) + if log: + logging.info('{},F1: {}, EM: {}, for {} samples'.format(eval_set, np.mean(f1), np.mean(em), sample)) + return np.mean(f1), np.mean(em) + + def run_epoch(self, sess, train_set, val_set, train_context, val_context): + prog_train = Progbar(target=1 + int(len(train_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(train_set, self.flags.batch_size)): + loss = self.optimize(sess, *batch) + prog_train.update(i + 1, [('train loss', loss)]) + print('') + prog_val = Progbar(target=1 + int(len(val_set[0]) / self.flags.batch_size)) + for i, batch in enumerate(self.minibatches(val_set, self.flags.batch_size)): + val_loss = self.validate(sess, *batch) + prog_val.update(i + 1, [('val loss', val_loss)]) + print('') + self.evaluate_answer(session=sess, dataset=train_set, context=train_context, sample=len(val_set[0]), log=True, eval_set='-Epoch TRAIN-') + self.evaluate_answer(session=sess, dataset=val_set, context=val_context, sample=None, log=True, eval_set='-Epoch VAL-') + + def train(self, session, dataset, val_dataset, train_dir): + """ + Implement main training loop + + TIPS: + You should also implement learning rate annealing (look into tf.train.exponential_decay) + Considering the long time to train, you should save your model per epoch. + + More ambitious approach can include implement early stopping, or reload + previous models if they have higher performance than the current one + + As suggested in the document, you should evaluate your training progress by + printing out information every fixed number of iterations. + + We recommend you evaluate your model performance on F1 and EM instead of just + looking at the cost. + + :param session: it should be passed in from train.py + :param dataset: a representation of our data, in some implementations, you can + pass in multiple components (arguments) of one dataset to this function + :param train_dir: path to the directory where you should save the model checkpoint + :return: + """ + tic = time.time() + params = tf.trainable_variables() + num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params)) + toc = time.time() + logging.info('Number of params: %d (retreival took %f secs)' % (num_params, toc - tic)) + context_ids, question_ids, answer_spans, ctx_mask, q_mask, train_context = dataset + train_dataset = [context_ids, question_ids, answer_spans, ctx_mask, q_mask] + val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask, val_context = val_dataset + val_dataset = [val_context_ids, val_question_ids, val_answer_spans, val_ctx_mask, val_q_mask] + num_epochs = self.flags.epochs + if self.flags.debug: + train_dataset = [elem[:self.flags.batch_size * 1] for elem in train_dataset] + val_dataset = [elem[:self.flags.batch_size] for elem in val_dataset] + num_epochs = 100 + for epoch in range(num_epochs): + logging.info('Epoch %d out of %d', epoch + 1, self.flags.epochs) + self.run_epoch(sess=session, train_set=train_dataset, val_set=val_dataset, train_context=train_context, val_context=val_context) + logging.info('Saving model in %s', train_dir) + self.saver.save(session, train_dir + '/' + self.flags.run_name + '.ckpt') + + def minibatches(self, data, batch_size, shuffle=True): + num_data = len(data[0]) + context_ids, question_ids, answer_spans, ctx_mask, q_mask = data + indices = np.arange(num_data) + if shuffle: + np.random.shuffle(indices) + for minibatch_start in np.arange(0, num_data, batch_size): + minibatch_indices = indices[minibatch_start:minibatch_start + batch_size] + yield [context_ids[minibatch_indices], question_ids[minibatch_indices], answer_spans[minibatch_indices], ctx_mask[minibatch_indices], q_mask[minibatch_indices]] diff --git a/dataset/preprocessed/91.txt b/dataset/preprocessed/91.txt new file mode 100644 index 0000000..8d2ed19 --- /dev/null +++ b/dataset/preprocessed/91.txt @@ -0,0 +1,140 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + for one_hot in new_enc_labels: + assert (one_hot == 1).sum() == 1, """Each one-hot-encoded value should include the number 1 exactly once. +Found {} +""".format(one_hot) + assert (one_hot == 0).sum() == len(one_hot) - 1, """Each one-hot-encoded value should include zeros in all but one position. +Found {} +""".format(one_hot) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/92.txt b/dataset/preprocessed/92.txt new file mode 100644 index 0000000..27698c3 --- /dev/null +++ b/dataset/preprocessed/92.txt @@ -0,0 +1,376 @@ +from __future__ import print_function +from __future__ import absolute_import +from __future__ import division +import sys +if sys.version[0] == '2': + import cPickle as pkl +else: + import pickle as pkl +import numpy as np +import tensorflow as tf +from python import utils +dtype = utils.DTYPE + + +class Model: + + def __init__(self): + self.sess = None + self.X = None + self.y = None + self.layer_keeps = None + self.vars = None + self.keep_prob_train = None + self.keep_prob_test = None + + def run(self, fetches, X=None, y=None, mode='train'): + feed_dict = {} + if type(self.X) is list: + for i in range(len(X)): + feed_dict[self.X[i]] = X[i] + else: + feed_dict[self.X] = X + if y is not None: + feed_dict[self.y] = y + if self.layer_keeps is not None: + if mode == 'train': + feed_dict[self.layer_keeps] = self.keep_prob_train + elif mode == 'test': + feed_dict[self.layer_keeps] = self.keep_prob_test + return self.sess.run(fetches, feed_dict) + + def dump(self, model_path): + var_map = {} + for name, var in self.vars.iteritems(): + var_map[name] = self.run(var) + pkl.dump(var_map, open(model_path, 'wb')) + print('model dumped at', model_path) + + +class LR(Model): + + def __init__(self, input_dim=None, output_dim=1, init_path=None, opt_algo='gd', learning_rate=0.01, l2_weight=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + b = self.vars['b'] + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=logits)) + l2_weight * tf.nn.l2_loss(xw) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FM(Model): + + def __init__(self, input_dim=None, output_dim=1, factor_order=10, init_path=None, opt_algo='gd', learning_rate=0.01, l2_w=0, l2_v=0, random_seed=None): + Model.__init__(self) + init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = tf.sparse_placeholder(dtype) + self.y = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = self.vars['w'] + v = self.vars['v'] + b = self.vars['b'] + X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) + xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) + p = 0.5 * tf.reshape(tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) + xw = tf.sparse_tensor_dense_matmul(self.X, w) + logits = tf.reshape(xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + l2_w * tf.nn.l2_loss(xw) + l2_v * tf.nn.l2_loss(xv) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class FNN(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + node_in = num_inputs * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + print(l.shape, wi.shape, bi.shape) + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class DeepFM(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + init_vars.append(('weight_%d' % i, [field_sizes[i], 1], 'xavier', dtype)) + init_vars.append(('bias', [1], 'zero', dtype)) + node_in = num_inputs * embed_size + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w = [self.vars['weight_%d' % i] for i in range(num_inputs)] + v = [self.vars['embed_%d' % i] for i in range(num_inputs)] + b = self.vars['bias'] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w[i]) for i in range(num_inputs)], 1) + xv = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], v[i]) for i in range(num_inputs)], 1) + l = xv + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + print(l.shape, wi.shape, bi.shape) + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + xv = tf.reshape(xv, [-1, num_inputs, embed_size]) + p = 0.5 * tf.reduce_sum(tf.square(tf.reduce_sum(xv, 1)) - tf.reduce_sum(tf.square(xv), 1), 1) + xw = tf.reduce_sum(xw, 1) + logits = tf.reshape(l + xw + b + p, [-1]) + self.y_prob = tf.sigmoid(logits) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class CCPM(Model): + + def __init__(self, field_sizes=None, embed_size=10, filter_sizes=None, layer_acts=None, drop_out=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) + init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) + init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) + init_vars.append(('b1', [1], 'zero', dtype)) + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + l = xw + l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) + f1 = self.vars['f1'] + l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) + f2 = self.vars['f2'] + l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') + l = tf.transpose(utils.max_pool_4d(tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) + l = tf.nn.dropout(utils.activate(tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) + w1 = self.vars['w1'] + b1 = self.vars['b1'] + l = tf.matmul(l, w1) + b1 + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN1(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + ip = tf.reshape(tf.reduce_sum(p * q, [-1]), [-1, num_pairs]) + l = tf.concat([xw, ip], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) + + +class PNN2(Model): + + def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=0.01, random_seed=None, layer_norm=True, kernel_type='mat'): + Model.__init__(self) + init_vars = [] + num_inputs = len(field_sizes) + for i in range(num_inputs): + init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) + num_pairs = int(num_inputs * (num_inputs - 1) / 2) + node_in = num_inputs * embed_size + num_pairs + if kernel_type == 'mat': + init_vars.append(('kernel', [embed_size, num_pairs, embed_size], 'xavier', dtype)) + elif kernel_type == 'vec': + init_vars.append(('kernel', [num_pairs, embed_size], 'xavier', dtype)) + elif kernel_type == 'num': + init_vars.append(('kernel', [num_pairs, 1], 'xavier', dtype)) + for i in range(len(layer_sizes)): + init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) + init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) + node_in = layer_sizes[i] + self.graph = tf.Graph() + with self.graph.as_default(): + if random_seed is not None: + tf.set_random_seed(random_seed) + self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] + self.y = tf.placeholder(dtype) + self.keep_prob_train = 1 - np.array(drop_out) + self.keep_prob_test = np.ones_like(drop_out) + self.layer_keeps = tf.placeholder(dtype) + self.vars = utils.init_var_map(init_vars, init_path) + w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] + xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) + xw3d = tf.reshape(xw, [-1, num_inputs, embed_size]) + row = [] + col = [] + for i in range(num_inputs - 1): + for j in range(i + 1, num_inputs): + row.append(i) + col.append(j) + p = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), row), [1, 0, 2]) + q = tf.transpose(tf.gather(tf.transpose(xw3d, [1, 0, 2]), col), [1, 0, 2]) + p = tf.reshape(p, [-1, num_pairs, embed_size]) + q = tf.reshape(q, [-1, num_pairs, embed_size]) + k = self.vars['kernel'] + if kernel_type == 'mat': + p = tf.expand_dims(p, 1) + kp = tf.reduce_sum(tf.multiply(tf.transpose(tf.reduce_sum(tf.multiply(p, k), -1), [0, 2, 1]), q), -1) + else: + k = tf.expand_dims(k, 0) + kp = tf.reduce_sum(p * q * k, -1) + l = tf.concat([xw, kp], 1) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + bi = self.vars['b%d' % i] + l = tf.nn.dropout(utils.activate(tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) + l = tf.squeeze(l) + self.y_prob = tf.sigmoid(l) + self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) + if layer_l2 is not None: + self.loss += embed_l2 * tf.nn.l2_loss(xw) + for i in range(len(layer_sizes)): + wi = self.vars['w%d' % i] + self.loss += layer_l2[i] * tf.nn.l2_loss(wi) + self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) + config = tf.ConfigProto() + config.gpu_options.allow_growth = True + self.sess = tf.Session(config=config) + tf.global_variables_initializer().run(session=self.sess) diff --git a/dataset/preprocessed/93.txt b/dataset/preprocessed/93.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/93.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/94.txt b/dataset/preprocessed/94.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/94.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/95.txt b/dataset/preprocessed/95.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/95.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/96.txt b/dataset/preprocessed/96.txt new file mode 100644 index 0000000..a2b0c39 --- /dev/null +++ b/dataset/preprocessed/96.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/97.txt b/dataset/preprocessed/97.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/97.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/98.txt b/dataset/preprocessed/98.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/98.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/dataset/preprocessed/99.txt b/dataset/preprocessed/99.txt new file mode 100644 index 0000000..3c50c73 --- /dev/null +++ b/dataset/preprocessed/99.txt @@ -0,0 +1,133 @@ +import os +import numpy as np +import tensorflow as tf +import random +from unittest.mock import MagicMock + + +def _print_success_message(): + return print('Tests Passed') + + +def test_folder_path(cifar10_dataset_folder_path): + assert cifar10_dataset_folder_path is not None, 'Cifar-10 data folder not set.' + assert cifar10_dataset_folder_path[-1] != '/', 'The "/" shouldn\'t be added to the end of the path.' + assert os.path.exists(cifar10_dataset_folder_path), 'Path not found.' + assert os.path.isdir(cifar10_dataset_folder_path), '{} is not a folder.'.format(os.path.basename(cifar10_dataset_folder_path)) + train_files = [(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id)) for batch_id in range(1, 6)] + other_files = [cifar10_dataset_folder_path + '/batches.meta', cifar10_dataset_folder_path + '/test_batch'] + missing_files = [path for path in train_files + other_files if not os.path.exists(path)] + assert not missing_files, 'Missing files in directory: {}'.format(missing_files) + print('All files found!') + + +def test_normalize(normalize): + test_shape = np.random.choice(range(1000)), 32, 32, 3 + test_numbers = np.random.choice(range(256), test_shape) + normalize_out = normalize(test_numbers) + assert type(normalize_out).__module__ == np.__name__, 'Not Numpy Object' + assert normalize_out.shape == test_shape, 'Incorrect Shape. {} shape found'.format(normalize_out.shape) + assert normalize_out.max() <= 1 and normalize_out.min() >= 0, 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) + _print_success_message() + + +def test_one_hot_encode(one_hot_encode): + test_shape = np.random.choice(range(1000)) + test_numbers = np.random.choice(range(10), test_shape) + one_hot_out = one_hot_encode(test_numbers) + assert type(one_hot_out).__module__ == np.__name__, 'Not Numpy Object' + assert one_hot_out.shape == (test_shape, 10), 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) + n_encode_tests = 5 + test_pairs = list(zip(test_numbers, one_hot_out)) + test_indices = np.random.choice(len(test_numbers), n_encode_tests) + labels = [test_pairs[test_i][0] for test_i in test_indices] + enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) + new_enc_labels = one_hot_encode(labels) + assert np.array_equal(enc_labels, new_enc_labels), """Encodings returned different results for the same numbers. +For the first call it returned: +{} +For the second call it returned +{} +Make sure you save the map of labels to encodings outside of the function.""".format(enc_labels, new_enc_labels) + _print_success_message() + + +def test_nn_image_inputs(neural_net_image_input): + image_shape = 32, 32, 3 + nn_inputs_out_x = neural_net_image_input(image_shape) + assert nn_inputs_out_x.get_shape().as_list() == [None, image_shape[0], image_shape[1], image_shape[2]], 'Incorrect Image Shape. Found {} shape'.format(nn_inputs_out_x.get_shape().as_list()) + assert nn_inputs_out_x.op.type == 'Placeholder', 'Incorrect Image Type. Found {} type'.format(nn_inputs_out_x.op.type) + assert nn_inputs_out_x.name == 'x:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_x.name) + print('Image Input Tests Passed.') + + +def test_nn_label_inputs(neural_net_label_input): + n_classes = 10 + nn_inputs_out_y = neural_net_label_input(n_classes) + assert nn_inputs_out_y.get_shape().as_list() == [None, n_classes], 'Incorrect Label Shape. Found {} shape'.format(nn_inputs_out_y.get_shape().as_list()) + assert nn_inputs_out_y.op.type == 'Placeholder', 'Incorrect Label Type. Found {} type'.format(nn_inputs_out_y.op.type) + assert nn_inputs_out_y.name == 'y:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_y.name) + print('Label Input Tests Passed.') + + +def test_nn_keep_prob_inputs(neural_net_keep_prob_input): + nn_inputs_out_k = neural_net_keep_prob_input() + assert nn_inputs_out_k.get_shape().ndims is None, 'Too many dimensions found for keep prob. Found {} dimensions. It should be a scalar (0-Dimension Tensor).'.format(nn_inputs_out_k.get_shape().ndims) + assert nn_inputs_out_k.op.type == 'Placeholder', 'Incorrect keep prob Type. Found {} type'.format(nn_inputs_out_k.op.type) + assert nn_inputs_out_k.name == 'keep_prob:0', 'Incorrect Name. Found {}'.format(nn_inputs_out_k.name) + print('Keep Prob Tests Passed.') + + +def test_con_pool(conv2d_maxpool): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 5]) + test_num_outputs = 10 + test_con_k = 2, 2 + test_con_s = 4, 4 + test_pool_k = 2, 2 + test_pool_s = 2, 2 + conv2d_maxpool_out = conv2d_maxpool(test_x, test_num_outputs, test_con_k, test_con_s, test_pool_k, test_pool_s) + assert conv2d_maxpool_out.get_shape().as_list() == [None, 4, 4, 10], 'Incorrect Shape. Found {} shape'.format(conv2d_maxpool_out.get_shape().as_list()) + _print_success_message() + + +def test_flatten(flatten): + test_x = tf.placeholder(tf.float32, [None, 10, 30, 6]) + flat_out = flatten(test_x) + assert flat_out.get_shape().as_list() == [None, 10 * 30 * 6], 'Incorrect Shape. Found {} shape'.format(flat_out.get_shape().as_list()) + _print_success_message() + + +def test_fully_conn(fully_conn): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + fc_out = fully_conn(test_x, test_num_outputs) + assert fc_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(fc_out.get_shape().as_list()) + _print_success_message() + + +def test_output(output): + test_x = tf.placeholder(tf.float32, [None, 128]) + test_num_outputs = 40 + output_out = output(test_x, test_num_outputs) + assert output_out.get_shape().as_list() == [None, 40], 'Incorrect Shape. Found {} shape'.format(output_out.get_shape().as_list()) + _print_success_message() + + +def test_conv_net(conv_net): + test_x = tf.placeholder(tf.float32, [None, 32, 32, 3]) + test_k = tf.placeholder(tf.float32) + logits_out = conv_net(test_x, test_k) + assert logits_out.get_shape().as_list() == [None, 10], 'Incorrect Model Output. Found {}'.format(logits_out.get_shape().as_list()) + print('Neural Network Built!') + + +def test_train_nn(train_neural_network): + mock_session = tf.Session() + test_x = np.random.rand(128, 32, 32, 3) + test_y = np.random.rand(128, 10) + test_k = np.random.rand(1) + test_optimizer = tf.train.AdamOptimizer() + mock_session.run = MagicMock() + train_neural_network(mock_session, test_optimizer, test_k, test_x, test_y) + assert mock_session.run.called, 'Session not used' + _print_success_message() diff --git a/model_2/calBleu.py b/model_2/calBleu.py new file mode 100644 index 0000000..5a3e0c8 --- /dev/null +++ b/model_2/calBleu.py @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +# @Time : 2019/5/21 17:51 +# @Author : zhangyx +# @FileName: calBleu.py +# @Software: PyCharm + +import sys +import codecs +import os +import math +import operator +import json +from functools import reduce + + +def fetch_data(cand, ref): + """ Store each reference and candidate sentences as a list """ + references = [] + if '.txt' in ref: + reference_file = codecs.open(ref, 'r', 'utf-8') + references.append(reference_file.readlines()) + else: + for root, dirs, files in os.walk(ref): + for f in files: + reference_file = codecs.open(os.path.join(root, f), 'r', 'utf-8') + references.append(reference_file.readlines()) + candidate_file = codecs.open(cand, 'r', 'utf-8') + candidate = candidate_file.readlines() + return candidate, references + + +def count_ngram(candidate, references, n): + clipped_count = 0 + count = 0 + r = 0 + c = 0 + for si in range(len(candidate)): + # Calculate precision for each sentence + ref_counts = [] + ref_lengths = [] + # Build dictionary of ngram counts + for reference in references: + ref_sentence = reference[si] + ngram_d = {} + words = ref_sentence.strip().split() + ref_lengths.append(len(words)) + limits = len(words) - n + 1 + # loop through the sentance consider the ngram length + for i in range(limits): + ngram = ' '.join(words[i:i + n]).lower() + if ngram in ngram_d.keys(): + ngram_d[ngram] += 1 + else: + ngram_d[ngram] = 1 + ref_counts.append(ngram_d) + # candidate + cand_sentence = candidate[si] + cand_dict = {} + words = cand_sentence.strip().split() + limits = len(words) - n + 1 + for i in range(0, limits): + ngram = ' '.join(words[i:i + n]).lower() + if ngram in cand_dict: + cand_dict[ngram] += 1 + else: + cand_dict[ngram] = 1 + clipped_count += clip_count(cand_dict, ref_counts) + count += limits + r += best_length_match(ref_lengths, len(words)) + c += len(words) + if clipped_count == 0: + pr = 0 + else: + pr = float(clipped_count) / count + bp = brevity_penalty(c, r) + return pr, bp + + +def clip_count(cand_d, ref_ds): + """Count the clip count for each ngram considering all references""" + count = 0 + for m in cand_d.keys(): + m_w = cand_d[m] + m_max = 0 + for ref in ref_ds: + if m in ref: + m_max = max(m_max, ref[m]) + m_w = min(m_w, m_max) + count += m_w + return count + + +def best_length_match(ref_l, cand_l): + """Find the closest length of reference to that of candidate""" + least_diff = abs(cand_l - ref_l[0]) + best = ref_l[0] + for ref in ref_l: + if abs(cand_l - ref) < least_diff: + least_diff = abs(cand_l - ref) + best = ref + return best + + +def brevity_penalty(c, r): + if c > r: + bp = 1 + else: + bp = math.exp(1 - (float(r) / c)) + return bp + + +def geometric_mean(precisions): + return (reduce(operator.mul, precisions)) ** (1.0 / len(precisions)) + + +def BLEU(candidate, references): + precisions = [] + for i in range(4): + pr, bp = count_ngram(candidate, references, i + 1) + precisions.append(pr) + bleu = geometric_mean(precisions) * bp + return bleu + + +def calBLEU(valfile): + testset = "testset\\" + candidate, references = fetch_data(valfile, testset) + bleu = BLEU(candidate, references) + print(bleu) + return bleu + + + +if __name__ == "__main__": + valfile = 'candidate.txt' + testset = "testset\\" + candidate, references = fetch_data(valfile, testset) + bleu = BLEU(candidate, references) + print(bleu) + out = open('bleu_out.txt', 'w') + out.write(str(bleu)) + out.close() + diff --git a/model_2/examineCode.py b/model_2/examineCode.py new file mode 100644 index 0000000..0136bdf --- /dev/null +++ b/model_2/examineCode.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# @Time : 2019/5/21 17:43 +# @Author : zhangyx +# @FileName: examineCode.py +# @Software: PyCharm + +import calBleu +import astor +import ast +def examineCode(code): + try: + node = ast.parse(code) + print(astor.to_source(node)) + return True + except SyntaxError as e: + print(e) + return False + +def examineCodeFile(code_file): + try: + node = astor.parse_file(code_file) + print(astor.to_source(node)) + return True + except SyntaxError as e: + print(e) + return False + +def BLEU(data_file): + pass + if not examineCodeFile(data_file): + print("不符合语法规范") + return 0 + return calBleu.calBLEU(data_file) \ No newline at end of file